Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_hdd():
"""Docstring.
Returns:
TYPE: Description
"""
try:
hdd = psutil.disk_partitions()
data = []
for each in hdd:
device = each.device
path = each.mountpoint
fstype = each.fstype
drive = psutil.disk_usage(path)
total = drive.total
total = total / 1000000000
used = drive.used
used = used / 1000000000
free = drive.free
free = free / 1000000000
percent = drive.percent
drives = {
"device": device,
"path": path,
"fstype": fstype,
"total": float("{0: .2f}".format(total)),
"used": float("{0: .2f}".format(used)),
"free": float("{0: .2f}".format(free)),
"percent": percent
}
system['cpu']['times'] = psutil.cpu_times(percpu=False)
system['cpu']['percent'] = psutil.cpu_times_percent(percpu=False)
system['cpu']['stats'] = psutil.cpu_stats()
system['cpu']['load_avg'] = psutil.getloadavg()
system['os']['architecture'] = platform.architecture()
system['os']['processor'] = platform.processor()
system['os']['release'] = platform.release()
system['os']['version'] = platform.version()
system['os']['libc'] = platform.libc_ver()
system['memory']['virtual'] = psutil.virtual_memory()
system['memory']['swap'] = psutil.swap_memory()
system['memory']['mounts'] = psutil.disk_partitions()
system['disk']['usage'] = psutil.disk_usage('/')
system['disk']['io'] = psutil.disk_io_counters(perdisk=False, nowrap=True)
process = psutil.Process()
system['process']['cpu_times'] = process.cpu_times()
system['process']['cpu_percent'] = process.cpu_percent()
system['process']['memory'] = process.memory_info()
system['process']['memory_percent'] = process.memory_percent()
# files to be uploaded and saved somewhere
'''
with open('/proc/cpuinfo', 'r') as f:
system['cpuinfo'] = f.read()
with open('/proc/meminfo', 'r') as f:
system['meminfo'] = f.read()
def on_chat_message(self, msg):
content_type, chat_type, chat_id = telepot.glance(msg)
# Do your stuff according to `content_type` ...
print("Your chat_id:" + str(chat_id)) # this will tell you your chat_id
if chat_id in adminchatid: # Store adminchatid variable in tokens.py
if content_type == 'text':
if msg['text'] == '/stats' and chat_id not in shellexecution:
bot.sendChatAction(chat_id, 'typing')
memory = psutil.virtual_memory()
disk = psutil.disk_usage('/')
boottime = datetime.fromtimestamp(psutil.boot_time())
now = datetime.now()
timedif = "Online for: %.1f Hours" % (((now - boottime).total_seconds()) / 3600)
memtotal = "Total memory: %.2f GB " % (memory.total / 1000000000)
memavail = "Available memory: %.2f GB" % (memory.available / 1000000000)
memuseperc = "Used memory: " + str(memory.percent) + " %"
diskused = "Disk used: " + str(disk.percent) + " %"
pids = psutil.pids()
pidsreply = ''
procs = {}
for pid in pids:
p = psutil.Process(pid)
try:
pmem = p.memory_percent()
if pmem > 0.5:
if p.name() in procs:
def main():
table = prettytable.PrettyTable(border=False, header=True, left_padding_width=2, padding_width=1)
table.field_names = ["Device", "Total", "Used", "Free", "Use%", "Type", "Mount"]
for part in psutil.disk_partitions(all=False):
if os.name == 'nt':
if 'cdrom' in part.opts or part.fstype == '':
# skip cd-rom drives with no disk in it; they may raise
# ENOENT, pop-up a Windows GUI error for a non-ready
# partition or just hang.
continue
if 'docker' in part.mountpoint and 'aufs' in part.mountpoint:
continue
usage = psutil.disk_usage(part.mountpoint)
table.add_row([part.device,
bytes2human(usage.total),
bytes2human(usage.used),
bytes2human(usage.free),
str(int(usage.percent)) + '%',
part.fstype,
part.mountpoint])
for field in table.field_names:
table.align[field] = "l"
print table
def get_performance_metrics(cls, application_uptime_s, queue_sizes={}):
"""Get sensor hardware and os performance statistics."""
retval = {}
retval["queue_sizes"] = queue_sizes
cpu_times = psutil.cpu_times()
retval["scan_program"] = "health_check"
retval["timestamp"] = Utility.get_now_string()
retval["cpu_percent"] = psutil.cpu_percent(percpu=True)
retval["cpu_times"] = {"user": cpu_times.user,
"system": cpu_times.system,
"idle": cpu_times.idle,
"iowait": cpu_times.iowait}
retval["mem"] = {"free": psutil.virtual_memory().free,
"swap_percent_used": psutil.swap_memory().percent}
retval["root_vol"] = psutil.disk_usage('/').percent
retval["data_vol"] = psutil.disk_usage('/data/').percent
retval["application_uptime_seconds"] = application_uptime_s
return retval
def disk_usage_dict(mount):
""" Return a dict for reported usage on a particular mount.
"""
disk_usage = {}
# We are not reporting any platform specific fields, such as
# buffers / cahced / shared on Linux/BSD
try:
if cadvisor_active:
disk_usage =\
cadvisor_disk_average(host_stats, mount, asbytes)
else:
disk = psutil.disk_usage(mount)
disk_usage['total'] =\
disk.total if asbytes else to_gb(disk.total)
disk_usage['percent'] = disk.percent
disk_usage['used'] =\
disk.used if asbytes else to_gb(disk.used)
disk_usage['free'] =\
disk.free if asbytes else to_gb(disk.free)
disk_usage['status'] = 'OK'
except OSError:
disk_usage['status'] =\
"Provided mount path does not exist ..."
return disk_usage
if (cpu_temp > 99):
cpu_temp = 99
buf[27] = str(cpu_temp / 10)
buf[28] = str(cpu_temp % 10)
#GPU Temperature:
gpuHandle = pynvml.nvmlDeviceGetHandleByIndex(0) #If you have multiple GPUs, iterate over them here and average the results
gpu_temp = pynvml.nvmlDeviceGetTemperature(gpuHandle, pynvml.NVML_TEMPERATURE_GPU)
if (gpu_temp > 99):
gpu_temp = 99
buf[30] = str(gpu_temp / 10)
buf[31] = str(gpu_temp % 10)
#Rotate through mount points and show percentage
mp = mountpoints[mp_index]
disk_percent = int(psutil.disk_usage(mp["mountpoint"]).percent)
buf[17] = mp["label"][0]
buf[18] = mp["label"][1]
buf[19] = mp["label"][2]
buf[20] = mp["label"][3]
buf[21] = mp["label"][4]
buf[22] = mp["label"][5]
buf[23] = str(disk_percent / 10)
buf[24] = str(disk_percent % 10)
mp_timer = mp_timer + 1
if (mp_timer >= 5):
mp_timer = 0;
mp_index = mp_index + 1
if (mp_index >= len(mountpoints)):
mp_index = 0;
def Stats():
s = {}
s["cpu"] = round(psutil.cpu_percent()) # Used CPU
s["memory"] = round(psutil.virtual_memory().percent) # Used memory
s["disk"] = round(psutil.disk_usage('/').percent) # Used disk
return json.dumps(s)