def getDisk(self): mountPoints, diskTotal = [], 0 #diskTotal累加 parts = psutil.disk_partitions() #分区信息 for p in parts: mp = str(p).split(",")[1].split("=")[1] mountPoints.append(mp) for x in mountPoints: pTotal = psutil.disk_usage(x.strip("'")).total #单个分区容量 diskTotal += pTotal diskTotal = changeUnit(int(diskTotal)) rootSize, rootRate = psutil.disk_usage('/').free, psutil.disk_usage('/').percent #获取/分区的剩余空间和使用率 writeToFile("totalDisk", diskTotal) writeToFile("rootSize", changeUnit(int(rootSize))) writeToFile("rootRate", rootRate) if float(rootRate) > float(85): """ON开关,防止多次重复报警;报警一次之后设为关闭""" if self.sDb.read(self.telIP, "rootAlarm") == "ON": ## 状态为"NO"且开关为"ON"时,报故障 sub = "DBTX ROOT partition Alarm:%s" % self.ps1 msg = "DateTime: %s\n根分区已使用%s%%, 超过85%%, 请警惕!!!" % (getTimeNow(), rootRate) saveLog.warning(msg) sendMail(sub, msg) self.sDb.update(self.telIP, "rootAlarm", "OFF") ## 报警后,将开关置为"OFF" else: if self.sDb.read(self.telIP, "rootAlarm") == "OFF": ## 状态OK且开关为"OFF"时,报恢复 sub = "DBTX ROOT partition is OK:%s" % self.ps1 msg = "DateTime: %s\n根分区可用空间充足%s." % (getTimeNow(), changeUnit(int(rootSize))) saveLog.info(msg) sendMail(sub, msg) self.sDb.update(self.telIP, "rootAlarm", "ON") ## 使用率不足85%时,将开关置为"ON"
def test_disk_usage(self): usage = psutil.disk_usage(os.getcwd()) assert usage.total > 0, usage assert usage.used > 0, usage assert usage.free > 0, usage assert usage.total > usage.used, usage assert usage.total > usage.free, usage assert 0 <= usage.percent <= 100, usage.percent if hasattr(shutil, 'disk_usage'): # py >= 3.3, see: http://bugs.python.org/issue12442 shutil_usage = shutil.disk_usage(os.getcwd()) tolerance = 5 * 1024 * 1024 # 5MB self.assertEqual(usage.total, shutil_usage.total) self.assertAlmostEqual(usage.free, shutil_usage.free, delta=tolerance) self.assertAlmostEqual(usage.used, shutil_usage.used, delta=tolerance) # if path does not exist OSError ENOENT is expected across # all platforms fname = tempfile.mktemp() try: psutil.disk_usage(fname) except OSError as err: if err.args[0] != errno.ENOENT: raise else: self.fail("OSError not raised")
def r_engine_status(): cpu_used = int(round((psutil.cpu_times().user * 100) + \ (psutil.cpu_times().system * 100), 0)) cpu_free = int(round(psutil.cpu_times().idle * 100, 0)) s = { "cpu": { "used": cpu_used, "free": cpu_free }, "memory": { "physical": { "used": psutil.phymem_usage().used, "free": psutil.phymem_usage().free }, "virtual": { "used": psutil.virtmem_usage().used, "free": psutil.virtmem_usage().free } }, "disk": { "used": psutil.disk_usage('/').used, "free": psutil.disk_usage('/').free } } r = Response("success", "status", s).get() return r
def test_disk_usage(self): usage = psutil.disk_usage(os.getcwd()) self.assertEqual(usage._fields, ('total', 'used', 'free', 'percent')) assert usage.total > 0, usage assert usage.used > 0, usage assert usage.free > 0, usage assert usage.total > usage.used, usage assert usage.total > usage.free, usage assert 0 <= usage.percent <= 100, usage.percent if hasattr(shutil, 'disk_usage'): # py >= 3.3, see: http://bugs.python.org/issue12442 shutil_usage = shutil.disk_usage(os.getcwd()) tolerance = 5 * 1024 * 1024 # 5MB self.assertEqual(usage.total, shutil_usage.total) self.assertAlmostEqual(usage.free, shutil_usage.free, delta=tolerance) self.assertAlmostEqual(usage.used, shutil_usage.used, delta=tolerance) # if path does not exist OSError ENOENT is expected across # all platforms fname = tempfile.mktemp() with self.assertRaises(OSError) as exc: psutil.disk_usage(fname) self.assertEqual(exc.exception.errno, errno.ENOENT)
def make_mountpoint_nodes(partition_name): mountpoint = partition_name.mountpoint total_size = RunnableNode('total_size', method=lambda: (ps.disk_usage(mountpoint).total, 'B')) used = RunnableNode('used', method=lambda: (ps.disk_usage(mountpoint).used, 'B')) free = RunnableNode('free', method=lambda: (ps.disk_usage(mountpoint).free, 'B')) used_percent = RunnableNode('used_percent', method=lambda: (ps.disk_usage(mountpoint).percent, '%')) device_name = RunnableNode('device_name', method=lambda: ([partition_name.device], '')) fstype = RunnableNode('fstype', method=lambda: (partition_name.fstype, '')) opts = RunnableNode('opts', method=lambda: (partition_name.opts, '')) safe_mountpoint = re.sub(r'[\\/]+', '|', mountpoint) node_children = [total_size, used, free, used_percent, device_name, fstype, opts] # Unix specific inode counter ~ sorry Windows! :'( if environment.SYSTEM != 'Windows': st = os.statvfs(mountpoint) iu = st.f_files - st.f_ffree inodes = RunnableNode('inodes', method=lambda: (st.f_files, 'inodes')) inodes_used = RunnableNode('inodes_used', method=lambda: (iu, 'inodes')) inodes_free = RunnableNode('inodes_free', method=lambda: (st.f_ffree, 'inodes')) node_children.append(inodes) node_children.append(inodes_used) node_children.append(inodes_free) # Make and return the full parent node return RunnableParentNode(safe_mountpoint, children=node_children, primary='used_percent', custom_output='Used disk space was', include=('total_size', 'used', 'free', 'used_percent'))
def diskInfo(self): disk={"total":'', "used":'', "threshhold":False} disk['total'] = psutil.disk_usage(conf.path).total disk['used'] = psutil.disk_usage(conf.path).used if (disk['total'] - disk['used']) <= conf.diskLimit: disk["threshhold"]=True return disk
def get_disk_stats(self): disk_stat = { "used": psutil.disk_usage('/').used, "free": psutil.disk_usage('/').free } return disk_stat
def test_disk_partitions(self): # all = False ls = psutil.disk_partitions(all=False) # on travis we get: # self.assertEqual(p.cpu_affinity(), [n]) # AssertionError: Lists differ: [0, 1, 2, 3, 4, 5, 6, 7,... != [0] self.assertTrue(ls, msg=ls) for disk in ls: if WINDOWS and 'cdrom' in disk.opts: continue if not POSIX: assert os.path.exists(disk.device), disk else: # we cannot make any assumption about this, see: # http://goo.gl/p9c43 disk.device if SUNOS: # on solaris apparently mount points can also be files assert os.path.exists(disk.mountpoint), disk else: assert os.path.isdir(disk.mountpoint), disk assert disk.fstype, disk self.assertIsInstance(disk.opts, str) # all = True ls = psutil.disk_partitions(all=True) self.assertTrue(ls, msg=ls) for disk in psutil.disk_partitions(all=True): if not WINDOWS: try: os.stat(disk.mountpoint) except OSError as err: if TRAVIS and OSX and err.errno == errno.EIO: continue # http://mail.python.org/pipermail/python-dev/ # 2012-June/120787.html if err.errno not in (errno.EPERM, errno.EACCES): raise else: if SUNOS: # on solaris apparently mount points can also be files assert os.path.exists(disk.mountpoint), disk else: assert os.path.isdir(disk.mountpoint), disk self.assertIsInstance(disk.fstype, str) self.assertIsInstance(disk.opts, str) def find_mount_point(path): path = os.path.abspath(path) while not os.path.ismount(path): path = os.path.dirname(path) return path.lower() mount = find_mount_point(__file__) mounts = [x.mountpoint.lower() for x in psutil.disk_partitions(all=True)] self.assertIn(mount, mounts) psutil.disk_usage(mount)
def update(self): """Get the latest system information.""" import psutil if self.type == 'disk_use_percent': self._state = psutil.disk_usage(self.argument).percent elif self.type == 'disk_use': self._state = round(psutil.disk_usage(self.argument).used / 1024**3, 1) elif self.type == 'disk_free': self._state = round(psutil.disk_usage(self.argument).free / 1024**3, 1) elif self.type == 'memory_use_percent': self._state = psutil.virtual_memory().percent elif self.type == 'memory_use': self._state = round((psutil.virtual_memory().total - psutil.virtual_memory().available) / 1024**2, 1) elif self.type == 'memory_free': self._state = round(psutil.virtual_memory().available / 1024**2, 1) elif self.type == 'swap_use_percent': self._state = psutil.swap_memory().percent elif self.type == 'swap_use': self._state = round(psutil.swap_memory().used / 1024**3, 1) elif self.type == 'swap_free': self._state = round(psutil.swap_memory().free / 1024**3, 1) elif self.type == 'processor_use': self._state = round(psutil.cpu_percent(interval=None)) elif self.type == 'process': if any(self.argument in l.name() for l in psutil.process_iter()): self._state = STATE_ON else: self._state = STATE_OFF elif self.type == 'network_out' or self.type == 'network_in': counters = psutil.net_io_counters(pernic=True) if self.argument in counters: counter = counters[self.argument][IO_COUNTER[self.type]] self._state = round(counter / 1024**2, 1) else: self._state = STATE_UNKNOWN elif self.type == 'packets_out' or self.type == 'packets_in': counters = psutil.net_io_counters(pernic=True) if self.argument in counters: self._state = counters[self.argument][IO_COUNTER[self.type]] else: self._state = STATE_UNKNOWN elif self.type == 'ipv4_address' or self.type == 'ipv6_address': addresses = psutil.net_if_addrs() if self.argument in addresses: self._state = addresses[self.argument][IF_ADDRS[self.type]][1] else: self._state = STATE_UNKNOWN elif self.type == 'last_boot': self._state = dt_util.as_local( dt_util.utc_from_timestamp(psutil.boot_time()) ).date().isoformat() elif self.type == 'since_last_boot': self._state = dt_util.utcnow() - dt_util.utc_from_timestamp( psutil.boot_time())
def _computeSlowStatus(process, status, db): status["diskPartitions"] = [_objectToDict(part) for part in psutil.disk_partitions()] try: # This fails in travis's environment, so guard it status["diskIO"] = _objectToDict(psutil.disk_io_counters()) except Exception: pass # Report on the disk usage where the script is located if hasattr(girder, "__file__"): status["girderPath"] = os.path.abspath(girder.__file__) status["girderDiskUsage"] = _objectToDict(psutil.disk_usage(status["girderPath"])) # Report where our logs are and how much space is available for them status["logs"] = [] for handler in logger.handlers: try: logInfo = {"path": handler.baseFilename} logInfo["diskUsage"] = _objectToDict(psutil.disk_usage(logInfo["path"])) status["logs"].append(logInfo) except Exception: # If we can't read information about the log, don't throw an # exception pass status["mongoDbStats"] = db.command("dbStats") try: # I don't know if this will work with a sharded database, so guard # it and don't throw an exception status["mongoDbPath"] = getDbConnection().admin.command("getCmdLineOpts")["parsed"]["storage"]["dbPath"] status["mongoDbDiskUsage"] = _objectToDict(psutil.disk_usage(status["mongoDbPath"])) except Exception: pass status["processDirectChildrenCount"] = len(process.children()) status["processAllChildrenCount"] = len(process.children(True)) status["openFiles"] = [_objectToDict(file) for file in process.open_files()] # I'd rather see textual names for the family and type of connections, # so make a lookup table for them connFamily = {getattr(socket, key): key for key in dir(socket) if key.startswith("AF_")} connType = {getattr(socket, key): key for key in dir(socket) if key.startswith("SOCK_")} connections = [] for conn in process.connections(): connDict = _objectToDict(conn) connDict.pop("raddr", None) connDict.pop("laddr", None) connDict["family"] = connFamily.get(connDict["family"], connDict["family"]) connDict["type"] = connType.get(connDict["type"], connDict["type"]) connections.append(connDict) status["connections"] = connections if hasattr(process, "io_counters"): status["ioCounters"] = _objectToDict(process.io_counters()) status["cherrypyThreads"] = {} for threadId in cherrypy.tools.status.seenThreads: info = cherrypy.tools.status.seenThreads[threadId].copy() if "end" in info: info["duration"] = info["end"] - info["start"] info["idle"] = time.time() - info["end"] status["cherrypyThreads"][threadId] = info
def make_mountpoint_nodes(partition_name): mountpoint = partition_name.mountpoint total_size = Node(u'total_size', method=lambda: (ps.disk_usage(mountpoint).total, u'b')) used = Node(u'used', method=lambda: (ps.disk_usage(mountpoint).used, u'b')) free = Node(u'free', method=lambda: (ps.disk_usage(mountpoint).free, u'b')) used_percent = Node(u'used_percent', method=lambda: (ps.disk_usage(mountpoint).percent, u'%')) device_name = Node(u'device_name', method=lambda: partition_name.device) safe_mountpoint = re.sub(ur'[\\/]+', u'|', mountpoint) return Node(safe_mountpoint, children=(total_size, used, free, used_percent, device_name))
def system_info(mounted_point='/'): return dict( hostname=socket.gethostname().split('.')[0], psutil_version='.'.join(map(str, ps.version_info)), mem_total=ps.virtual_memory().total/M, mem_used=ps.virtual_memory().used/M, disk_total=ps.disk_usage(mounted_point).total/M, disk_used=ps.disk_usage(mounted_point).used/M )
def make_mountpoint_nodes(partition_name): mountpoint = partition_name.mountpoint total_size = RunnableNode('total_size', method=lambda: (ps.disk_usage(mountpoint).total, 'b')) used = RunnableNode('used', method=lambda: (ps.disk_usage(mountpoint).used, 'b')) free = RunnableNode('free', method=lambda: (ps.disk_usage(mountpoint).free, 'b')) used_percent = RunnableNode('used_percent', method=lambda: (ps.disk_usage(mountpoint).percent, '%')) device_name = RunnableNode('device_name', method=lambda: ([partition_name.device], 'name')) safe_mountpoint = re.sub(r'[\\/]+', '|', mountpoint) return ParentNode(safe_mountpoint, children=[total_size, used, free, used_percent, device_name])
def get_disk_partitions(): count = 0 for i in p: disk_info[p[count][0]] = {'name': p[count][0], 'mount': p[count][1], 'type': p[count][2], 'total': psutil.disk_usage(p[count][count])[0], 'used': psutil.disk_usage(p[count][count])[1]} count += 1 return disk_info
def get_disk_stat(): disk_stat = [] for device in (psutil.disk_partitions()): partition = device[0] mounted = device[1] # convert bytes to Gb total = round(int(psutil.disk_usage(device[1])[0])/10**9, 1) used = round(int(psutil.disk_usage(device[1])[1])/10**9, 1) free = round(total - used, 1) disk_stat.append([partition, mounted, total, used, free]) return sorted(disk_stat)
def getDiskstate(disklist=None): diskstate = {} if not disklist: disklist = __getdisklist() for disk in disklist: diskstate[disk] = psutil.disk_usage(disk).percent else: for disk in disklist: disk = disk.strip("'") diskstate[disk] = psutil.disk_usage(disk).percent return diskstate
def test_disk_usage_unicode(self): # see: https://github.com/giampaolo/psutil/issues/416 # XXX this test is not really reliable as it always fails on # Python 3.X (2.X is fine) try: safe_rmdir(TESTFN_UNICODE) os.mkdir(TESTFN_UNICODE) psutil.disk_usage(TESTFN_UNICODE) safe_rmdir(TESTFN_UNICODE) except UnicodeEncodeError: pass
def __init__(self): temp = os.popen("vcgencmd measure_temp").readline() self.cpuTemp = float(temp.replace("temp=", "").replace("'C\n", "")) self.totalRAM = self.b2k(p.virtual_memory().total) self.usedRAM = self.b2k(p.virtual_memory().used) self.availableRAM = self.b2k(p.virtual_memory().available) self.totalDiskSpace = self.b2k(p.disk_usage("/").total) self.usedDiskSpace = self.b2k(p.disk_usage("/").used) self.freeDiskSpace = self.b2k(p.disk_usage("/").free) self.cpuUsage = p.cpu_percent(interval=1) self.createJSON()
def getInfo(): #print("Process list: ") #print psutil.get_process_list() print "cpu usage: " print psutil.cpu_percent(1) print "Memory Usage: " print psutil.virtual_memory() print "Disk Usage: " print psutil.disk_usage('/') print "Network info: " print psutil.network_io_counters(True)
def diskinfo(): disk_par=psutil.disk_partitions() info={} for u in disk_par: info1=[] disk_part=u.mountpoint info1.append(psutil.disk_usage(disk_part).total) info1.append(psutil.disk_usage(disk_part).used) info1.append(psutil.disk_usage(disk_part).free) info[disk_part]=info1 return info
def get_value(self): if self.variant == 'total': u, f, n = 0, 0, 0 for x in psutil.disk_partitions(): d = psutil.disk_usage(x.mountpoint) u += d.used f += d.free n += 1 return ((float(u)/float(f))*100)/n else: return psutil.disk_usage(next(x.mountpoint for x in psutil.disk_partitions() if x.device == self.variant)).percent
def __init__(self): self.devices = [disk.device for disk in psutil.disk_partitions()] self.mountpoint = [disk.mountpoint for disk in psutil.disk_partitions()] self.fstype = [disk.fstype for disk in psutil.disk_partitions()] self.device_type = [self.GetDriveType(path) for path in self.devices] self.os_name = self.get_os_name() for path in self.mountpoint: self.total.append(bytes2mega(psutil.disk_usage(path).total)) self.used.append(bytes2human(psutil.disk_usage(path).used)) self.free.append(bytes2human(psutil.disk_usage(path).free)) self.percent.append(str(psutil.disk_usage(path).percent) + "%")
def main(): server_name = socket.gethostname() doc = { 'server': server_name, 'date' : datetime.now(), 'cpu' : psutil.cpu_percent(interval=1), 'disk_app' : psutil.disk_usage('/Apps').free, 'disk_root' : psutil.disk_usage('/').free, 'memory' : psutil.virtual_memory().free, 'myapp': is_up(server_name) } db.monitor.insert(doc)
def index(request): data = {} context = RequestContext(request) try: summary = client.command('summary') except: data['offline'] = True summary = {} else: summary = summary['SUMMARY'][0] data['offline'] = False #summary_lower = {} new_summary = {} for k, v in summary.iteritems(): lower = k.lower().replace(' ', '_').replace('%','_') if lower =='mhs_av': new_summary['ghs_av'] = {'value': round(v/1000, 3), 'label': 'Gh/s av'} else: new_summary[lower] = {'value': v, 'label': k} data['summary'] = new_summary try: temp = get_cpu_temperature() except: temp = 0 system = { 'cpu_percent': psutil.cpu_percent(), 'cpu_temp': temp, 'mem_percent': psutil.virtual_memory().percent, 'mem_total': sizeof_fmt(psutil.virtual_memory().total), 'mem_used': sizeof_fmt(psutil.virtual_memory().used), 'disk_percent': psutil.disk_usage('/').percent, 'disk_total': sizeof_fmt(psutil.disk_usage('/').total), 'disk_used': sizeof_fmt(psutil.disk_usage('/').used), 'server_uptime': uptime(), } net_io = psutil.net_io_counters(pernic=True) for i in ('eth0', 'wlan0'): if i in net_io: system[i + '_sent'] = sizeof_fmt(net_io[i].bytes_sent) system[i + '_recv'] = sizeof_fmt(net_io[i].bytes_recv) data['system'] = system if request.is_ajax(): return HttpResponse(json.dumps(data), mimetype="application/json") context.update(data) return render_to_response('status/index.html', context,)
def _ensure_free_space_in_temp_dir(self, tempdir, space, minimum_age=None): """ Ensures that at least space bytes of data can be stored on the volume on which tempdir is located, deleting files from tempdir if necessary. .. warning:: This will delete files in tempdir to reclaim storage space. :raises InsufficientSpaceError: Raised if enough space cannot be claimed. """ assert isinstance(tempdir, STRING_TYPES), "Expected string for tempdir" try: space = int(space) except (ValueError, TypeError): raise TypeError( "Could not convert value %r for `space` in " "_ensure_free_space_in_temp_dir() to an integer." % space ) try: os.makedirs(tempdir) except OSError as e: # pragma: no cover if e.errno != EEXIST: raise if disk_usage(tempdir).free >= space: return tempfiles = [] # followlinks=False is the default for os.walk. I am specifying it # explicitly here to make it more obvious. Setting this to True # instead might make us delete files outside of tempdir, if there is # a symlink in there somewhere. for root, dirs, files in os.walk(tempdir, followlinks=False): for filename in files: fullpath = join(root, filename) atime = os.stat(fullpath).st_atime tempfiles.append({"filepath": fullpath, "atime": atime}) tempfiles.sort(key=lambda x: x["atime"]) while disk_usage(tempdir).free < space: if not tempfiles: raise InsufficientSpaceError("Cannot free enough space in temp " "directory %s" % tempdir) element = tempfiles.pop(0) if not minimum_age or os.stat(element["filepath"]).st_mtime + minimum_age < time.time(): logger.debug("Deleting tempfile %s", element["filepath"]) remove_file(element["filepath"], retry_on_exit=False, raise_=False) else: # pragma: no cover logger.debug( "Not deleting tempfile %s, it is newer than %s " "seconds", element["filepath"], minimum_age )
def update(self): """ Get the latest system informations. """ import psutil if self.type == 'disk_use_percent': self._state = psutil.disk_usage(self.argument).percent elif self.type == 'disk_use': self._state = round(psutil.disk_usage(self.argument).used / 1024**3, 1) elif self.type == 'disk_free': self._state = round(psutil.disk_usage(self.argument).free / 1024**3, 1) elif self.type == 'memory_use_percent': self._state = psutil.virtual_memory().percent elif self.type == 'memory_use': self._state = round((psutil.virtual_memory().total - psutil.virtual_memory().available) / 1024**2, 1) elif self.type == 'memory_free': self._state = round(psutil.virtual_memory().available / 1024**2, 1) elif self.type == 'swap_use_percent': self._state = psutil.swap_memory().percent elif self.type == 'swap_use': self._state = round(psutil.swap_memory().used / 1024**3, 1) elif self.type == 'swap_free': self._state = round(psutil.swap_memory().free / 1024**3, 1) elif self.type == 'processor_use': self._state = round(psutil.cpu_percent(interval=None)) elif self.type == 'process': if any(self.argument in l.name() for l in psutil.process_iter()): self._state = STATE_ON else: self._state = STATE_OFF elif self.type == 'network_out': self._state = round(psutil.net_io_counters(pernic=True) [self.argument][0] / 1024**2, 1) elif self.type == 'network_in': self._state = round(psutil.net_io_counters(pernic=True) [self.argument][1] / 1024**2, 1) elif self.type == 'packets_out': self._state = psutil.net_io_counters(pernic=True)[self.argument][2] elif self.type == 'packets_in': self._state = psutil.net_io_counters(pernic=True)[self.argument][3] elif self.type == 'ipv4_address': self._state = psutil.net_if_addrs()[self.argument][0][1] elif self.type == 'ipv6_address': self._state = psutil.net_if_addrs()[self.argument][1][1] elif self.type == 'last_boot': self._state = dt_util.datetime_to_date_str( dt_util.as_local( dt_util.utc_from_timestamp(psutil.boot_time()))) elif self.type == 'since_last_boot': self._state = dt_util.utcnow() - dt_util.utc_from_timestamp( psutil.boot_time())
def dsk(): '''This method returns the current DISK usage''' a=psutil.disk_partitions() b=[] x=0 y=0 for i in range (0,len(a)): b.append(a[i][1]) x+=psutil.disk_usage(b[i])[0] y+=psutil.disk_usage(b[i])[1] z=float(y)/float(x)*100 return "%2.2f" %z
def run_disk(): sys.path.append(os.path.split(os.path.dirname(__file__))[0]+'/lib') push = __import__('pushdata') value_rate= __import__('record_rate') jsondata=push.JonSon() jsondata.create_data() rate=value_rate.ValueRate() timestamp = int(datetime.datetime.now().strftime("%s")) try: partitions = psutil.disk_partitions() for index in range(0, len(partitions)): disk=tuple(partitions[index])[1] if disk == '/': diskname='_root' roottuple=tuple(psutil.disk_usage(disk)) jsondata.gen_data('drive'+diskname+'_used_bytes', timestamp, roottuple[1], push.hostname, check_type, cluster_name) jsondata.gen_data('drive'+diskname+'_free_bytes', timestamp, roottuple[2], push.hostname, check_type, cluster_name) jsondata.gen_data('drive'+diskname+'_pecent', timestamp, roottuple[3], push.hostname, check_type, cluster_name) else: diskname=disk.replace("/", "_") disktuple=tuple(psutil.disk_usage(disk)) jsondata.gen_data('drive'+diskname+'_used_bytes', timestamp, disktuple[1], push.hostname, check_type, cluster_name) jsondata.gen_data('drive'+diskname+'_free_bytes', timestamp, disktuple[2], push.hostname, check_type, cluster_name) jsondata.gen_data('drive'+diskname+'_pecent', timestamp, disktuple[3], push.hostname, check_type, cluster_name) proc_stats=open('/proc/diskstats') for line in proc_stats: if "loop" not in line: fields = line.strip().split() name='drive_'+(fields)[2]+'_percent' value=(fields)[12] reqrate=rate.record_value_rate(name, value, timestamp) if isinstance( reqrate, int ): diskrate=reqrate/10 jsondata.gen_data(name, timestamp, diskrate, push.hostname, check_type, cluster_name) disks=psutil.disk_io_counters(perdisk=True) for key, value in disks.iteritems(): tvalue=tuple(value) read_name='drive_'+key+'_read_bytes' write_name='drive_'+key+'_write_bytes' readrate=rate.record_value_rate(read_name, tvalue[2], timestamp) writerate=rate.record_value_rate(write_name, tvalue[3], timestamp) jsondata.gen_data(read_name, timestamp, readrate, push.hostname, check_type, cluster_name) jsondata.gen_data(write_name, timestamp, writerate, push.hostname, check_type, cluster_name) jsondata.put_json() jsondata.truncate_data() except Exception as e: push = __import__('pushdata') push.print_error(__name__ , (e)) pass
def get_stats(name, nic): global prev_recv global prev_sent global prev_read global prev_write with open('/proc/uptime', 'r') as f: uptime_seconds = float(f.readline().split()[0]) stats = {'date': datetime.datetime.now().isoformat(), 'name': name, 'cpu': psutil.cpu_percent(interval=None, percpu=True), 'cpu_count': psutil.cpu_count(), 'cpu_ctx_switches': psutil.cpu_stats().ctx_switches, 'cpu_interrupts': psutil.cpu_stats().interrupts, 'ram': psutil.virtual_memory().percent, 'ram-available': psutil.virtual_memory().available, 'ram-used': psutil.virtual_memory().used, 'swap': psutil.swap_memory().percent, 'swap-total': psutil.swap_memory().total, 'swap-used': psutil.swap_memory().used, 'disk_io_read': psutil.disk_io_counters().read_bytes, 'disk_io_write': psutil.disk_io_counters().write_bytes, 'disk_total': psutil.disk_usage('/').total, 'disk_used': psutil.disk_usage('/').used, 'uptime': uptime_seconds} nic_list = psutil.net_io_counters(pernic=True) nic = nic_list[nic] stats['packets_sent'] = [nic.packets_sent, nic.errout] stats['packets_recv'] = [nic.packets_recv, nic.errin] stats['bytes_recv'] = nic.bytes_recv stats['bytes_sent'] = nic.bytes_sent if prev_recv != -1: stats['dl_rate'] = stats['bytes_recv'] - prev_recv else: stats['dl_rate'] = 0 prev_recv = stats['bytes_recv'] if prev_sent != -1: stats['ul_rate'] = stats['bytes_sent'] - prev_sent else: stats['ul_rate'] = 0 prev_sent = stats['bytes_sent'] if prev_read != -1: stats['disk_read_rate'] = stats['disk_io_read'] - prev_read else: stats['disk_read_rate'] = 0 prev_read = stats['disk_io_read'] if prev_read != -1: stats['disk_write_rate'] = stats['disk_io_write'] - prev_write else: stats['disk_write_rate'] = 0 prev_write = stats['disk_io_write'] return stats
def getDetails(): # return these details deets = {"cpu_usage":None,"memory_usage":None,"disk_usage":None,"os_version":platform.platform()} deets['cpu_usage'] = psutil.cpu_percent(interval=None) deets['memory_usage'] = psutil.virtual_memory().percent deets['disk_usage'] = psutil.disk_usage('/').percent #### # Bonus data: #### # CPU deets['cpu_details'] = {'details':[]} cpu_details = psutil.cpu_percent(interval=None, percpu=True) deets['cpu_details']['count'] = len(cpu_details) for i in range(0,len(cpu_details)): deets['cpu_details']['details'].append({'name':i,'cpu_usage':cpu_details[i]}) # Memory deets['memory_details'] = {} deets['memory_details']['virtual'] = { 'percent':psutil.virtual_memory().percent, 'total':psutil.virtual_memory().total } deets['memory_details']['swap'] = { 'percent':psutil.swap_memory().percent, 'total':psutil.swap_memory().total } # Disk deets['disk_details'] = {} deets['disk_details']['partition_count'] = len(psutil.disk_partitions()) deets['disk_details']['partition_count'] = len(psutil.disk_partitions()) deets['disk_details']['partitions'] = [] for partition in psutil.disk_partitions(): deets['disk_details']['partitions'].append({ 'device':partition.device, 'mountpoint':partition.mountpoint, 'percent':psutil.disk_usage(partition.mountpoint).percent, 'total':psutil.disk_usage(partition.mountpoint).total, }) deets['disk_details']['iostats'] = {} sdiskio = psutil.disk_io_counters() for k in ['read_count','write_count','read_bytes','write_bytes','read_time','write_time']: deets['disk_details']['iostats'][k] = getattr(sdiskio,k) deets['hostname'] = platform.node() deets['boottime'] = datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S") return deets
def disk(): disk_list=[psutil.disk_usage('/').total,psutil.disk_usage('/').used,psutil.disk_usage('/').free,psutil.disk_usage('/').percent] return disk_list
def ps(): if not clients: raise Return("NoClient") timestamp = time.time() cpu_percent = yield thread_pool.submit(psutil.cpu_percent, 0.1) memory = psutil.virtual_memory() disk = psutil.disk_usage('/') net = psutil.net_io_counters() if SysInfo: interval = timestamp - SysInfo[-1]["timestamp"] net_sent_speed = (net.bytes_sent - SysInfo[-1]["net_sent"]) / interval net_recv_speed = (net.bytes_recv - SysInfo[-1]["net_recv"]) / interval else: net_sent_speed = 0 net_recv_speed = 0 cpu_process = yield thread_pool.submit(process.cpu_percent, 0.1) data = { # "hostname": hostname, # "hostname_ex": hostname_ex, # "ip": ip, "timestamp": timestamp, "cpu_percent": cpu_percent, "cpu_count": psutil.cpu_count(), "mem_percent": memory.percent, "mem_used": memory.used, "mem_total": memory.total, "disk_total": disk.total, "disk_used": disk.used, "disk_percent": disk.percent, "net_sent": net.bytes_sent, "net_recv": net.bytes_recv, "net_sent_speed": net_sent_speed, "net_recv_speed": net_recv_speed, "cpu_process": cpu_process, "mem_process": process.memory_info().rss, # "cmdline": process.cmdline(), # "pid": process.pid, "thread": process.num_threads(), "run_time": timestamp - process.create_time(), } # net = psutil.net_io_counters(pernic=True) # interval = timestamp - SysInfo[-1]["timestamp"] if SysInfo else 1 # for name, now in net.items(): # if now.bytes_sent + now.bytes_recv < 1024: # continue # name = name.decode("gbk" if sys.platform.startswith("win") else "utf8") # if SysInfo: # last = SysInfo[-1]["net"].get(name, [0, 0]) # else: # last = [now.bytes_sent, now.bytes_recv] # data["net"][name] = [now.bytes_sent, # now.bytes_recv, # (now.bytes_sent - last[0]) / interval, # (now.bytes_recv - last[1]) / interval, # ] SysInfo.append(data) if len(SysInfo) > 10: SysInfo.pop(0) WSHandler.sendSysInfo(data) # raise Return(data) raise Return("success")
async def g_system(client, data): """.system -- Sends notice with system information.""" # sensors_temperatures, sensors_fans, boot_time, pids hostname = platform.node() ops = platform.platform() architecture = '-'.join(platform.architecture()) cpu = platform.machine() cpus = psutil.cpu_count() cpu_per = psutil.cpu_percent() general = (f'Hostname: \x02{hostname}\x02, Operating System: ' f'\x02{ops}\x02, Architecture: \x02{architecture}\x02, ' f'CPU: \x02{cpu}\x02, Cores: \x02{cpus}\x02, CPU Percent: ' f'\x02{cpu_per}\x02') asyncio.create_task(client.notice(data.nickname, general)) mem = psutil.virtual_memory() mem_total = await _conv_bytes(mem.total, gb=True) mem_used = await _conv_bytes(mem.used, gb=True) mem_available = await _conv_bytes(mem.available, gb=True) mem_free = await _conv_bytes(mem.free, gb=True) mem_per = mem.percent memory = ( f'Total Memory: \x02{mem_total}\x02, Memory Used: \x02' f'{mem_used}\x02, Memory Avaiable: \x02{mem_available}\x02, ' f'Memory Free: \x02{mem_free}\x02, Memory Percent: \x02{mem_per}\x02') asyncio.create_task(client.notice(data.nickname, memory)) swap = psutil.swap_memory() swap_total = await _conv_bytes(swap.total, gb=True) swap_used = await _conv_bytes(swap.used, gb=True) swap_free = await _conv_bytes(swap.free, gb=True) swap_per = swap.percent p = psutil.Process() with p.oneshot(): cwd = p.cwd() duseage = psutil.disk_usage(cwd) disk_total = await _conv_bytes(duseage.total, gb=True) disk_used = await _conv_bytes(duseage.used, gb=True) disk_free = await _conv_bytes(duseage.free, gb=True) disk_per = duseage.percent disk = ( f'Total Swap: \x02{swap_total}\x02, Swap Used: \x02{swap_used}\x02, ' f'Swap Free: \x02{swap_free}\x02, Swap Percent: \x02{swap_per}\x02, ' f'CWD Disk Total: \x02{disk_total}\x02, CWD Disk Used: \x02' f'{disk_used}\x02, CWD Disk Free: \x02{disk_free}\x02, ' f'CWD Disk Percent: \x02{disk_per}\x02') asyncio.create_task(client.notice(data.nickname, disk)) net = psutil.net_io_counters() net_sent = await _conv_bytes(net.bytes_sent, gb=True) net_recv = await _conv_bytes(net.bytes_recv, gb=True) connections = len(psutil.net_connections()) last_boot = datetime.fromtimestamp( psutil.boot_time()).strftime('%Y-%m-%d %H:%M:%S') total_procs = len(psutil.pids()) netboot = ( f'Network Data Sent: \x02{net_sent}\x02, Network Data Recieved:' f' \x02{net_recv}\x02, Total Connections: \x02{connections}\x02, ' f'Booted: \x02{last_boot}\x02, Total Processes: \x02{total_procs}\x02') asyncio.create_task(client.notice(data.nickname, netboot))
def otherHDD(): otherHDD = psutil.disk_usage('/home/pi/other') # Divide from Bytes -> KB -> MB -> GB free = round(otherHDD.free / 1024.0 / 1024.0 / 1024.0, 1) total = round(otherHDD.total / 1024.0 / 1024.0 / 1024.0, 1) return str(free) + 'GB free'
def _disk_within_threshold(path, threshold): usage = psutil.disk_usage(path) return (1.0 - (usage.percent / 100.0)) >= threshold
def stats(self): stats = {} for i in range(0, self.gpu_count): handle = pynvml.nvmlDeviceGetHandleByIndex(i) try: util = pynvml.nvmlDeviceGetUtilizationRates(handle) memory = pynvml.nvmlDeviceGetMemoryInfo(handle) temp = pynvml.nvmlDeviceGetTemperature( handle, pynvml.NVML_TEMPERATURE_GPU) in_use_by_us = gpu_in_use_by_this_process(handle) stats["gpu.{}.{}".format(i, "gpu")] = util.gpu stats["gpu.{}.{}".format(i, "memory")] = util.memory stats["gpu.{}.{}".format( i, "memoryAllocated")] = (memory.used / float(memory.total)) * 100 stats["gpu.{}.{}".format(i, "temp")] = temp if in_use_by_us: stats["gpu.process.{}.{}".format(i, "gpu")] = util.gpu stats["gpu.process.{}.{}".format(i, "memory")] = util.memory stats["gpu.process.{}.{}".format( i, "memoryAllocated")] = (memory.used / float(memory.total)) * 100 stats["gpu.process.{}.{}".format(i, "temp")] = temp # Some GPUs don't provide information about power usage try: power_watts = pynvml.nvmlDeviceGetPowerUsage( handle) / 1000.0 power_capacity_watts = ( pynvml.nvmlDeviceGetEnforcedPowerLimit(handle) / 1000.0) power_usage = (power_watts / power_capacity_watts) * 100 stats["gpu.{}.{}".format(i, "powerWatts")] = power_watts stats["gpu.{}.{}".format(i, "powerPercent")] = power_usage if in_use_by_us: stats["gpu.process.{}.{}".format( i, "powerWatts")] = power_watts stats["gpu.process.{}.{}".format( i, "powerPercent")] = power_usage except pynvml.NVMLError: pass except pynvml.NVMLError: pass if psutil: net = psutil.net_io_counters() sysmem = psutil.virtual_memory() stats["cpu"] = psutil.cpu_percent() stats["memory"] = sysmem.percent stats["network"] = { "sent": net.bytes_sent - self.network_init["sent"], "recv": net.bytes_recv - self.network_init["recv"], } stats["disk"] = psutil.disk_usage("/").percent stats["proc.memory.availableMB"] = sysmem.available / 1048576.0 try: stats["proc.memory.rssMB"] = self.proc.memory_info( ).rss / 1048576.0 stats["proc.memory.percent"] = self.proc.memory_percent() stats["proc.cpu.threads"] = self.proc.num_threads() except psutil.NoSuchProcess: pass return stats
#!/usr/bin/env python3 import psutil from influxdb import InfluxDBClient import time client = InfluxDBClient(host='localhost', port=8086) client.switch_database('mdt') while True: currentCPU = psutil.cpu_percent() currentMemory = psutil.virtual_memory() currentDisk = psutil.disk_usage('/') currentDiskIO = psutil.disk_io_counters() currentNetworkIO = psutil.net_io_counters() payload = [{ "measurement": "stats", "tags": { "host": "NUC", "metric": "CPU" }, "fields": { "CPUpercent": currentCPU, } }, { "measurement": "stats", "tags": { "host": "NUC", "metric": "Memory" },
#All rights reserved © 2019 Abhimanyu Singh import socket, psutil, os from flask import Flask, redirect, request, url_for, render_template app = Flask(__name__) #Variable to store CPU usage Data of Raspberry pi cpu_usage = psutil.cpu_percent(interval=1, percpu=True) cpu_count = psutil.cpu_count() cpu_freq = psutil.cpu_freq() virtual_mem = psutil.virtual_memory() swap_mem = psutil.swap_memory() disk_part = psutil.disk_partitions() disk_usage = psutil.disk_usage('/') cpu_temp = psutil.sensors_temperatures(fahrenheit=False) @app.route('/') def dashboard(): return render_template('dash.html') @app.route('/rpi') def raspberry(): return render_template('rpi.html', cpu_usage=cpu_usage, cpu_count=cpu_count, cpu_freq=cpu_freq, virtual_mem=virtual_mem,
uuid2 = '{0}_{1}'.format(strftime("%Y%m%d%H%M%S", gmtime()), uuid.uuid4()) result = sgp30.get_air_quality() end = time.time() row = {} row['uuid'] = uniqueid row['ipaddress'] = ipaddress row['runtime'] = str(round(end - start)) row['host'] = os.uname()[1] row['host_name'] = host_name row['macaddress'] = psutil_iface('wlan0') row['end'] = '{0}'.format(str(end)) row['te'] = '{0}'.format(str(end - start)) row['systemtime'] = datetime.datetime.now().strftime('%m/%d/%Y %H:%M:%S') row['cpu'] = psutil.cpu_percent(interval=1) usage = psutil.disk_usage("/") row['diskusage'] = "{:.1f} MB".format(float(usage.free) / 1024 / 1024) row['memory'] = psutil.virtual_memory().percent # Equivalent C02: {:5d} (ppm) # Total VOC: {:5d} (ppb) row['equivalentco2ppm'] = '{:5d}'.format((result.equivalent_co2)) row['totalvocppb'] = '{:5d}'.format((result.total_voc)) row['id'] = str(uuid2) json_string = json.dumps(row) # need this for sensor run that at startup with cron # @reboot sleep 300 && /home/wwwjobs/clean-static-cache.sh fa = open("/opt/demo/logs/sgp30.log", "a+") fa.write(json_string + "\n") fa.close()
def _get_disk_usage(): dirs = [ os.environ["USERPROFILE"] if sys.platform == "win32" else os.sep, ray.utils.get_user_temp_dir(), ] return {x: psutil.disk_usage(x) for x in dirs}
def check_space(self): disk_space = psutil.disk_usage('/')[2] return self.file_size < disk_space
def test_disk_usage_bytes(self): psutil.disk_usage(b'.')
def test_disk_usage_unicode(self): # See: https://github.com/giampaolo/psutil/issues/416 with self.assertRaises(UnicodeEncodeError): psutil.disk_usage(UNICODE_SUFFIX)
def main(): # Catch CNTRL-C signel global shutdown signal.signal(signal.SIGINT, signal_cntrl_c) no_datapoints = 60 args = parse_commandline_arguments() init_logging(args.logfilename, args.verbose) logging.info("Connecting to server: %s", args.server) logging.info(" Connection ID: %s", args.connection) logging.info(" Control topic: %s/%s/%s/control", args.username, args.connection, args.device_id) logging.info(" Data topic: %s/%s/%s/data", args.username, args.connection, args.device_id) device = dashio.dashDevice(args.connection, args.device_id, args.device_name) dash_conn = dashio.dashConnection(args.username, args.password) dash_conn.add_device(device) monitor_page = dashio.Page("monpg", "Dash Server Moniter") gph_network = dashio.TimeGraph("NETWORKGRAPH") gph_network.title = "Server Network Traffic: {}".format(args.connection) gph_network.y_axis_label = "Kbytes" gph_network.y_axis_min = 0.0 gph_network.y_axis_max = 1000.0 gph_network.y_axis_num_bars = 11 Network_Rx = dashio.TimeGraphLine("RX", dashio.TimeGraphLineType.LINE, color=dashio.Color.FUSCIA, max_data_points=no_datapoints) Network_Tx = dashio.TimeGraphLine("TX", dashio.TimeGraphLineType.LINE, color=dashio.Color.AQUA, max_data_points=no_datapoints) gph_network.add_line("NET_RX", Network_Rx) gph_network.add_line("NET_TX", Network_Tx) last_Tx, last_Rx = get_network_rx_tx() gph_cpu = dashio.TimeGraph("CPULOAD") gph_cpu.title = "CPU load: {}".format(args.connection) gph_cpu.y_axis_label = "Percent" gph_cpu.y_axis_max = 100 gph_cpu.y_axis_min = 0 gph_cpu.y_axis_num_bars = 8 monitor_page.add_control(gph_network) monitor_page.add_control(gph_cpu) device.add_control(gph_network) device.add_control(gph_cpu) number_of_cores = psutil.cpu_count() cpu_core_line_array = [] cpu_data = psutil.cpu_percent(percpu=True) for cpu in range(0, number_of_cores): line = dashio.TimeGraphLine( name="CPU:{}".format(cpu), line_type=dashio.TimeGraphLineType.LINE, color=dashio.Color(cpu + 1), transparency=1.0, max_data_points=no_datapoints, ) cpu_core_line_array.append(line) gph_cpu.add_line("CPU:{}".format(cpu), line) hd_dial = dashio.Dial("HD_USAGE") hd_dial.title = "Disk Usage" hd_dial.dial_value = psutil.disk_usage("/").percent hd_dial.min = 0.0 hd_dial.max = 100.0 hd_dial.red_value = 95.0 hd_dial.show_min_max = True device.add_control(hd_dial) monitor_page.add_control(hd_dial) device.add_control(monitor_page) while not shutdown: time.sleep(10) Tx, Rx = get_network_rx_tx() Network_Rx.add_data_point(Tx - last_Tx) Network_Tx.add_data_point(Rx - last_Rx) last_Tx = Tx last_Rx = Rx gph_network.send_data() cpu_data = psutil.cpu_percent(percpu=True) i = 0 for line in cpu_core_line_array: line.add_data_point(cpu_data[i]) i += 1 gph_cpu.send_data() hd_dial.dial_value = psutil.disk_usage("/").percent device.close()
async def get_resource(self): cpus = psutil.cpu_percent(interval=.3, percpu=True) ms = self.machine_specification cpu_prop = dict( used=round(sum(cpus) / len(cpus)), used_per_cpu=cpus, ) mem = psutil.virtual_memory() mem_prop = dict(total=mem.total, used=mem.used, free=mem.free) disk = psutil.disk_usage( self.settings.get('NOKKHUM_PROCESSOR_RECORDER_PATH')) disk_prop = dict( total=disk.total, used=disk.used, free=disk.free, percent=disk.percent, ) processor_manager = self.processor_manager processor_list = [] pcpu = 0 pmem = 0 # for pid, processor_id in processor_manager.get_pids(): for processor_id, processor in processor_manager.pool.items(): if not processor.is_running(): continue pid = processor.process.pid try: process = psutil.Process(pid) process_status = dict( pid=pid, processor_id=processor_id, num_threads=process.num_threads(), cpu=process.cpu_percent(interval=0.2), memory=process.memory_info().rss, processors=processor.get_status(), # messages=compute.processor_manager.read_process_output(processor_id) ) pcpu += process_status['cpu'] pmem += process_status['memory'] processor_list.append(process_status) except Exception as e: logger.exception(e) system_load = dict( cpu=sum(cpus) - pcpu if sum(cpus) - pcpu >= 0 else 0, memory=mem.used - pmem if mem.used - pmem >= 0 else 0) resource = dict(name=platform.node(), cpu=cpu_prop, memory=mem_prop, disk=disk_prop, processors=processor_list, system_load=system_load, ip=ms['ip'], mac=ms['mac'], date=datetime.datetime.now().isoformat()) # logging.debug('update resource: %s' % messages) return resource
def get_info(request): """ 用于提供数据 :param request: HttpRequest :return: Json """ if request.method == "GET": # 如果是要获取分类列表则加上这个参数 需要提供的内容为: 1ds2ppJu2I9dl1 cateory_list = request.GET.get("cateory") user_list = request.GET.get("users") get_access_num = request.GET.get("num") get_blog_list = request.GET.get("blog_list") get_mp3 = request.GET.get("mp3") get_status = request.GET.get("status") # 获取分类列表 if cateory_list is not None and cateory_list == "1ds2ppJu2I9dl1": return JsonResponse({ "status_code": 0, "data": [ i[0] for i in list(Cateory.objects.values_list("cateory_name")) ] }) if user_list is not None and user_list == "2ds2ppJu2I9dl1": return JsonResponse({ "status_code": 0, "data": [i[0] for i in list(User.objects.values_list("username"))] }) if get_access_num is not None and get_access_num == "true": db = GetNum.objects.get(date=time.strftime("%Y-%m-%d")) return JsonResponse({"status_code": 0, "num": int(db.number)}) if get_blog_list is not None and get_blog_list == "true": db = Content.objects.all() data = [{ "title": i.title, "content": i.content, "cateory": i.cateory.cateory_name, "user": i.user.username, "time": i.time } for i in db] return JsonResponse({"status_code": 0, "data": data}) if get_mp3 is not None: data = [] for i in os.listdir("static/mp3"): data.append({"url": f"/apis/static/mp3/{i}", "name": i}) return JsonResponse({"status_code": 0, "data": data}) def getMemorystate(): phymem = psutil.virtual_memory() line = "Memory: %5s%% %6s/%s" % ( phymem.percent, str(int(phymem.used / 1024 / 1024)) + "M", str(int(phymem.total / 1024 / 1024)) + "M") return line if get_status is not None: data = {} data["status_code"] = 0 data["cpu_utilization"] = int( math.fsum(psutil.cpu_percent(interval=1, percpu=True)) // 4) # 获得cpu当前使用率 data["cpu_status"] = max( psutil.cpu_percent(interval=1, percpu=True)) # 获得cpu当前使用率 data["memory_status"] = int( psutil.virtual_memory().percent) # 获取当前内存使用情况 data["disk_status"] = int(psutil.disk_usage("/").percent) return JsonResponse(data) # ===================================================================================== return JsonResponse({"status_code": 1, "error": "not data"})
def get_all_monitor_info(): return ('CPU [{0:.2%}]\n'.format(psutil.cpu_percent(interval=2)) + 'MeM [{0:.2%}]\n'.format(psutil.virtual_memory().percent / 100) + 'SWAP [{0:.2%}]\n'.format(psutil.swap_memory().percent / 100) + ' / [{0:.2%}]'.format(psutil.disk_usage('/').percent / 100))
def _get_report_files( task, customer_name, authentication, billing_source, provider_type, provider_uuid, report_month, cache_key, report_context, ): """ Task to download a Report. Args: task (Object): Bound celery task. customer_name (String): Name of the customer owning the cost usage report. access_credential (String): Credential needed to access cost usage report in the backend provider. report_source (String): Location of the cost usage report in the backend provider. provider_type (String): Koku defined provider type string. Example: Amazon = 'AWS' provider_uuid (String): Provider uuid. report_month (DateTime): Month for report to download. cache_key (String): The provider specific task cache value. Returns: files (List) List of filenames with full local path. Example: ['/var/tmp/masu/region/aws/catch-clearly.csv', '/var/tmp/masu/base/aws/professor-hour-industry-television.csv'] """ request_id = task.request.id context = {"account": customer_name[4:], "provider_uuid": provider_uuid} month_string = report_month.strftime("%B %Y") report_context["date"] = report_month log_statement = ( f"Downloading report for:\n" f" schema_name: {customer_name}\n" f" provider: {provider_type}\n" f" account (provider uuid): {provider_uuid}\n" f" report_month: {month_string}" ) LOG.info(log_json(request_id, log_statement, context)) try: disk = psutil.disk_usage(Config.PVC_DIR) disk_msg = f"Available disk space: {disk.free} bytes ({100 - disk.percent}%)" except OSError: disk_msg = f"Unable to find available disk space. {Config.PVC_DIR} does not exist" LOG.info(log_json(request_id, disk_msg, context)) report = None try: downloader = ReportDownloader( customer_name=customer_name, access_credential=authentication, report_source=billing_source, provider_type=provider_type, provider_uuid=provider_uuid, report_name=None, account=customer_name[4:], request_id=task.request.id, ) report = downloader.download_report(report_context) except (MasuProcessingError, MasuProviderError, ReportDownloaderError) as err: worker_stats.REPORT_FILE_DOWNLOAD_ERROR_COUNTER.labels(provider_type=provider_type).inc() WorkerCache().remove_task_from_cache(cache_key) LOG.error(log_json(request_id, str(err), context)) with ProviderStatus(provider_uuid) as status: status.set_error(error=err) raise err with ProviderStatus(provider_uuid) as status: status.set_status(ProviderStatusCode.READY) return report
import psutil import multiprocessing import threading import time from datetime import datetime import os # Testing while True: cpus = psutil.cpu_percent(interval=1, percpu=True) ram = [val for val in psutil.virtual_memory()] disk = psutil.disk_io_counters(perdisk=True, nowrap=True) disk_usage = psutil.disk_usage('C:/') print(cpus) print(ram) print(disk) print(disk_usage)
import psutil # 磁盘使用情况 disk_used = psutil.disk_usage('c://') print('磁盘空间所有信息\n', disk_used) print('总磁盘空间', disk_used.total) print('使用的磁盘空间', disk_used.used) print('剩余的磁盘空间', disk_used.free) print('磁盘使用百分比', disk_used.percent) if disk_used.percent > 60: print('磁盘空间达到%s' % disk_used.percent) print('将要切换磁盘')
def get_free_space(dirname): """Return folder/drive free space (in bytes).""" return psutil.disk_usage(dirname).free
def disk_info(dir="/"): disk = psutil.disk_usage(dir) return "SD: %s/%s %.0f%%" % (bytes2human(disk.used), bytes2human( disk.total), disk.percent)
def mediaHDD(): mediaHDD = psutil.disk_usage('/home/pi/media') # Divide from Bytes -> KB -> MB -> GB free = round(mediaHDD.free / 1024.0 / 1024.0 / 1024.0, 1) total = round(mediaHDD.total / 1024.0 / 1024.0 / 1024.0, 1) return str(free) + 'GB free'
version = version.split('-')[0] #获取到的版本号截取到纯数字的版本号 core = psutil.cpu_count(logical=False) #获取到cpu的物理核数 cpu_use = psutil.cpu_percent() #获取cpu的使用率 memory_info = psutil.virtual_memory() #获取内存信息 memory_use = memory_info[2] #获取到第三个项,即使用率,前两个分别为总量和使用量 disk_use = psutil.disk_usage('/')[3] #根目录的的使用率 disk_name = psutil.disk_partitions()[0][0] #磁盘设备的名字 disk_mount = psutil.disk_partitions()[0][1] #磁盘设备的挂载点 print "OS:%s, VERSION:%s" % (os, version) print 'NAME:%s, CPU CORE:%s' % (name, core) print "CPU USED:%s" % cpu_use + "%" print "MEMORY USED:%s" % memory_use + "%" print "DISK USED:%s" % disk_use + "%" print "DERVICE:%s" % disk_name print "MOUNT:%s" % disk_mount
continue #CREATE FOLDERS FOR EVENTS for ev in event: n = str(ev) createFolder(root_folder + "/" + n) for x in range(len(linklist)): obj_Disk = psutil.disk_usage('/') ree_perc = 100.0-obj_Disk.percent print(ree_perc) if(ree_perc < 3):#leave 10 percent disk pace print("SKIP DL FREE DISK SPACE") break if(os.path.exists(root_folder+ "/" +event[x] + "/" + filename[x] + ".mp4")): print("SKIP FILE EXITSTS") continue if linklist[x] in open(root_folder + "/downloaded.txt").read(): print("FILE ALREADY IN DOWNLOADED TXT") continue print("DL:" + linklist[x]) try:
#! /usr/bin/env python import psutil from subprocess import call cpu_percent = psutil.cpu_percent() v_memory = psutil.virtual_memory().percent s_memory = psutil.swap_memory().percent d_usage = psutil.disk_usage('/').percent call(["echo 'cpu_percent:" + cpu_percent + "|c' | nc -u -w0 {{ ip_monitor }} {{ port_monitor }}"]) call(["echo 'v_memory:" + v_memory + "|c' | nc -u -w0 {{ ip_monitor }} {{ port_monitor }}"]) call(["echo 's_memory:" + s_memory + "|c' | nc -u -w0 {{ ip_monitor }} {{ port_monitor }}"]) call(["echo 'd_usage:" + d_usage + "|c' | nc -u -w0 {{ ip_monitor }} {{ port_monitor }}"])
def system_information(handler): '''Handler for system info''' value, error = {}, {} try: import psutil process = psutil.Process(os.getpid()) value['system', 'cpu-count'] = psutil.cpu_count() value['system', 'cpu-usage'] = psutil.cpu_percent() value['system', 'memory-usage'] = psutil.virtual_memory().percent value['system', 'disk-usage'] = psutil.disk_usage('/').percent value['gramex', 'memory-usage'] = process.memory_info()[0] value['gramex', 'open-files'] = len(process.open_files()) except ImportError: app_log.warning('psutil required for system stats') error['system', 'cpu-count'] = 'psutil not installed' error['system', 'cpu-usage'] = 'psutil not installed' error['system', 'memory-usage'] = 'psutil not installed' error['system', 'disk-usage'] = 'psutil not installed' error['gramex', 'memory-usage'] = 'psutil not installed' error['gramex', 'open-files'] = 'psutil not installed' try: import conda value['conda', 'version'] = conda.__version__, except ImportError: app_log.warning('conda required for conda stats') error['conda', 'version'] = 'conda not installed' from shutilwhich import which value['node', 'path'] = which('node') value['git', 'path'] = which('git') from gramex.cache import Subprocess apps = { # shell=True is safe here since the code is constructed entirely in this function # We use shell to pick up the commands' paths from the shell. ('node', 'version'): Subprocess('node --version', shell=True), # nosec ('npm', 'version'): Subprocess('npm --version', shell=True), # nosec ('yarn', 'version'): Subprocess('yarn --version', shell=True), # nosec ('git', 'version'): Subprocess('git --version', shell=True), # nosec } for key, proc in apps.items(): stdout, stderr = yield proc.wait_for_exit() value[key] = stdout.strip() if not value[key]: error[key] = stderr.strip() value['python', 'version'] = '{0}.{1}.{2}'.format(*sys.version_info[:3]) value['python', 'path'] = sys.executable value['gramex', 'version'] = gramex.__version__ value['gramex', 'path'] = os.path.dirname(gramex.__file__) import pandas as pd df = pd.DataFrame({'value': value, 'error': error}).reset_index() df.columns = ['section', 'key'] + df.columns[2:].tolist() df = df[['section', 'key', 'value', 'error']].sort_values(['section', 'key']) df['error'] = df['error'].fillna('') data = gramex.data.filter(df, handler.args) # TODO: handle _format, _meta, _download, etc just like FormHandler raise Return(gramex.data.download(data))
elif (opcion == "12"): if (selected_pid != 0): print("Terminando el proceso...") p.terminate() else: print("no hay PID seleccionado") press_key() elif (opcion == "13"): print("CPUs logicas:") print(psutil.cpu_count()) print("CPUs fisicas:") print(psutil.cpu_count(logical=False)) press_key() elif (opcion == "14"): print("Particiones del sistema:") print(psutil.disk_partitions()) press_key() elif (opcion == "15"): print("Espacio usado por la particion raiz:") print(psutil.disk_usage('/')) press_key() elif (opcion == "16"): print("Info de las interfaces de red:") print(psutil.net_if_addrs()) press_key() elif (opcion == "17"): exit() else: print("Opcion no valida") press_key()
import socket from system_check.msg import System from std_msgs.msg import String pub = rospy.Publisher('/sb_system', System, queue_size=1) rospy.init_node('sb_publish_system') rate = rospy.Rate(0.10) # 10 second publish rate msg= System('hostname', 'ip_address', 'cpu_percent', 'memory_percent_used',\ 'free_memory_bytes', 'swap_memory_percent_used', \ 'disk_percent_used', 'packets_sent', 'packets_received',\ 'in_packets_dropped', 'out_packets_dropped') while not rospy.is_shutdown(): msg.hostname = socket.gethostname() msg.ip_address = socket.gethostbyname(msg.hostname) msg.cpu_percent = psutil.cpu_percent(interval=1) mem = psutil.virtual_memory() msg.memory_percent_used = mem[2] msg.free_memory_bytes = mem[4] msg.swap_memory_percent_used = psutil.swap_memory()[3] msg.disk_percent_used = psutil.disk_usage("/")[3] net = psutil.net_io_counters() msg.packets_sent = net[2] msg.packets_received = net[3] msg.in_packets_dropped = net[6] msg.out_packets_dropped = net[7] pub.publish(msg) rate.sleep() rospy.loginfo("complete")
def _disk_usage(path: str) -> Any: return psutil.disk_usage(path)