def tell_system_status(self):
import psutil
import platform
import datetime
os, name, version, _, _, _ = platform.uname()
version = version.split('-')[0]
cores = psutil.cpu_count()
cpu_percent = psutil.cpu_percent()
memory_percent = psutil.virtual_memory()[2]
disk_percent = psutil.disk_usage('/')[3]
boot_time = datetime.datetime.fromtimestamp(psutil.boot_time())
running_since = boot_time.strftime("%A %d. %B %Y")
response = "I am currently running on %s version %s. " % (os, version)
response += "This system is named %s and has %s CPU cores. " % (name, cores)
response += "Current disk_percent is %s percent. " % disk_percent
response += "Current CPU utilization is %s percent. " % cpu_percent
response += "Current memory utilization is %s percent. " % memory_percent
response += "it's running since %s." % running_since
return response
python类virtual_memory()的实例源码
def test_procfs_path(self):
tdir = tempfile.mkdtemp()
try:
psutil.PROCFS_PATH = tdir
self.assertRaises(IOError, psutil.virtual_memory)
self.assertRaises(IOError, psutil.cpu_times)
self.assertRaises(IOError, psutil.cpu_times, percpu=True)
self.assertRaises(IOError, psutil.boot_time)
# self.assertRaises(IOError, psutil.pids)
self.assertRaises(IOError, psutil.net_connections)
self.assertRaises(IOError, psutil.net_io_counters)
self.assertRaises(IOError, psutil.net_if_stats)
self.assertRaises(IOError, psutil.disk_io_counters)
self.assertRaises(IOError, psutil.disk_partitions)
self.assertRaises(psutil.NoSuchProcess, psutil.Process)
finally:
psutil.PROCFS_PATH = "/proc"
os.rmdir(tdir)
def check(request):
return {
'hostname': socket.gethostname(),
'ips': ips,
'cpus': psutil.cpu_count(),
'uptime': timesince(datetime.fromtimestamp(psutil.boot_time())),
'memory': {
'total': filesizeformat(psutil.virtual_memory().total),
'available': filesizeformat(psutil.virtual_memory().available),
'used': filesizeformat(psutil.virtual_memory().used),
'free': filesizeformat(psutil.virtual_memory().free),
'percent': psutil.virtual_memory().percent
},
'swap': {
'total': filesizeformat(psutil.swap_memory().total),
'used': filesizeformat(psutil.swap_memory().used),
'free': filesizeformat(psutil.swap_memory().free),
'percent': psutil.swap_memory().percent
}
}
def stream_host_stats():
while True:
net = psutil.net_io_counters(pernic=True)
time.sleep(1)
net1 = psutil.net_io_counters(pernic=True)
net_stat_download = {}
net_stat_upload = {}
for k, v in net.items():
for k1, v1 in net1.items():
if k1 == k:
net_stat_download[k] = (v1.bytes_recv - v.bytes_recv) / 1000.
net_stat_upload[k] = (v1.bytes_sent - v.bytes_sent) / 1000.
ds = statvfs('/')
disk_str = {"Used": ((ds.f_blocks - ds.f_bfree) * ds.f_frsize) / 10 ** 9, "Unused": (ds.f_bavail * ds.f_frsize) / 10 ** 9}
yield '[{"cpu":"%s","memory":"%s","memTotal":"%s","net_stats_down":"%s","net_stats_up":"%s","disk":"%s"}],' \
% (psutil.cpu_percent(interval=1), psutil.virtual_memory().used, psutil.virtual_memory().free, \
net_stat_download, net_stat_upload, disk_str)
def doit(channel):
cpu_pc = psutil.cpu_percent()
mem_avail = psutil.virtual_memory().percent
try:
response = channel.update({1:cpu_pc, 2:mem_avail})
print cpu_pc
print mem_avail_mb
print strftime("%a, %d %b %Y %H:%M:%S", localtime())
print response
except:
print "connection failed"
#sleep for 16 seconds (api limit of 15 secs)
def remove_oldest_entries(storage, percentage=90):
# compute current memory usage (%)
old_mem = psutil.virtual_memory().percent
# if we have data in storage and utilization exceeds 90%
while storage and old_mem > percentage:
# removed oldest entry
storage.popitem(last=False)
# garbage collect
gc.collect(1)
# comute used memory again
new_mem = psutil.virtual_memory().percent
# if the used memory did not decrease stop
if new_mem >= old_mem:
break
# net new measurement for memory usage and loop
old_mem = new_mem
def get_hugepage_number():
# TODO: defaults to 2M - this should probably be configurable
# and support multiple pool sizes - e.g. 2M and 1G.
# NOTE(jamespage): 2M in bytes
hugepage_size = 2048 * 1024
hugepage_config = config('hugepages')
hugepages = None
if hugepage_config:
if hugepage_config.endswith('%'):
# NOTE(jamespage): return units of virtual_memory is
# bytes
import psutil
mem = psutil.virtual_memory()
hugepage_config_pct = hugepage_config.strip('%')
hugepage_multiplier = float(hugepage_config_pct) / 100
hugepages = int((mem.total * hugepage_multiplier) / hugepage_size)
else:
hugepages = int(hugepage_config)
return hugepages
def __init__(self):
super().__init__()
# From the psutil documentation https://pythonhosted.org/psutil/#psutil.cpu_percent:
#
# Warning the first time this function is called
# with interval = 0.0 or None it will return a
# meaningless 0.0 value which you are supposed
# to ignore.
psutil.cpu_percent()
# '...is recommended for accuracy that this function be called with at least 0.1 seconds between calls.'
time.sleep(0.1)
# a sliding window of 60 contiguous 5 sec utilization (up to five minutes)
self.cpuutils = collections.deque([psutil.cpu_percent()], maxlen=60)
self.system_virtual_memory = psutil.virtual_memory()
logger.debug('System Utilization handler initialized.')
def get_stats():
root.authorized()
params = {}
# number of jobs in queued, running, and completed states
params['nq'] = db(jobs.state=='Q').count()
params['nr'] = db(jobs.state=='R').count()
params['nc'] = db(jobs.state=='C').count()
params['cpu'] = psutil.cpu_percent()
params['vm'] = psutil.virtual_memory().percent
params['disk'] = psutil.disk_usage('/').percent
params['cid'] = request.query.cid
params['app'] = request.query.app
return template("stats", params)
def psutil_phymem_usage():
"""
Return physical memory usage (float)
Requires the cross-platform psutil (>=v0.3) library
(http://code.google.com/p/psutil/)
"""
# This is needed to avoid a deprecation warning error with
# newer psutil versions
if settings.__PSUTIL__ == False:
return 0.0
try:
percent = psutil.virtual_memory().percent
except:
percent = psutil.phymem_usage().percent
return percent
def collect_system_information(self):
import psutil
mem = psutil.virtual_memory()
with self.git.batch_commit('JOB_SYSTEM_INFORMATION'):
self.set_system_info('memory_total', mem.total)
import aetros.cuda_gpu
try:
self.set_system_info('cuda_version', aetros.cuda_gpu.get_version())
except Exception: pass
import cpuinfo
cpu = cpuinfo.get_cpu_info()
self.set_system_info('cpu_name', cpu['brand'])
self.set_system_info('cpu', [cpu['hz_actual_raw'][0], cpu['count']])
def sysInfoRaw():
import time
import psutil
cpuPercent = psutil.cpu_percent(interval=1)
memPercent = psutil.virtual_memory().percent
sda2Percent = psutil.disk_usage('/').percent
sda3Percent = psutil.disk_usage('/home').percent
seconds = int(time.time()) - int(psutil.boot_time())
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
uptime = [d, h, m, s]
return [cpuPercent, memPercent, sda2Percent, sda3Percent, uptime]
def test_serialization(self):
def check(ret):
if json is not None:
json.loads(json.dumps(ret))
a = pickle.dumps(ret)
b = pickle.loads(a)
self.assertEqual(ret, b)
check(psutil.Process().as_dict())
check(psutil.virtual_memory())
check(psutil.swap_memory())
check(psutil.cpu_times())
check(psutil.cpu_times_percent(interval=0))
check(psutil.net_io_counters())
if LINUX and not os.path.exists('/proc/diskstats'):
pass
else:
if not APPVEYOR:
check(psutil.disk_io_counters())
check(psutil.disk_partitions())
check(psutil.disk_usage(os.getcwd()))
check(psutil.users())
def test_total_phymem(self):
w = wmi.WMI().Win32_ComputerSystem()[0]
self.assertEqual(int(w.TotalPhysicalMemory),
psutil.virtual_memory().total)
# @unittest.skipIf(wmi is None, "wmi module is not installed")
# def test__UPTIME(self):
# # _UPTIME constant is not public but it is used internally
# # as value to return for pid 0 creation time.
# # WMI behaves the same.
# w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
# p = psutil.Process(0)
# wmic_create = str(w.CreationDate.split('.')[0])
# psutil_create = time.strftime("%Y%m%d%H%M%S",
# time.localtime(p.create_time()))
# Note: this test is not very reliable
def test_virtual_memory(self):
mem = psutil.virtual_memory()
assert mem.total > 0, mem
assert mem.available > 0, mem
assert 0 <= mem.percent <= 100, mem
assert mem.used > 0, mem
assert mem.free >= 0, mem
for name in mem._fields:
value = getattr(mem, name)
if name != 'percent':
self.assertIsInstance(value, (int, long))
if name != 'total':
if not value >= 0:
self.fail("%r < 0 (%s)" % (name, value))
if value > mem.total:
self.fail("%r > total (total=%s, %s=%s)"
% (name, mem.total, name, value))
def test_proc_cpu_times(self):
tested = []
out = sh('procstat -r %s' % self.pid)
p = psutil.Process(self.pid)
for line in out.split('\n'):
line = line.lower().strip()
if 'user time' in line:
pstat_value = float('0.' + line.split()[-1].split('.')[-1])
psutil_value = p.cpu_times().user
self.assertEqual(pstat_value, psutil_value)
tested.append(None)
elif 'system time' in line:
pstat_value = float('0.' + line.split()[-1].split('.')[-1])
psutil_value = p.cpu_times().system
self.assertEqual(pstat_value, psutil_value)
tested.append(None)
if len(tested) != 2:
raise RuntimeError("couldn't find lines match in procstat out")
# --- virtual_memory(); tests against sysctl
def remove_oldest_entries(storage, percentage=90):
# compute current memory usage (%)
old_mem = psutil.virtual_memory().percent
# if we have data in storage and utilization exceeds 90%
while storage and old_mem > percentage:
# removed oldest entry
storage.popitem(last=False)
# garbage collect
gc.collect(1)
# comute used memory again
new_mem = psutil.virtual_memory().percent
# if the used memory did not decrease stop
if new_mem >= old_mem:
break
# net new measurement for memory usage and loop
old_mem = new_mem
def __init__(self):
self.conn = None
self.dirty_scene = False
self.guest = None
self.guest_mapping_by_uuid = dict()
self.hostname = ji.Common.get_hostname()
self.node_id = uuid.getnode()
self.cpu = psutil.cpu_count()
self.memory = psutil.virtual_memory().total
self.interfaces = dict()
self.disks = dict()
self.guest_callbacks = list()
self.interval = 60
# self.last_host_cpu_time = dict()
self.last_host_traffic = dict()
self.last_host_disk_io = dict()
self.last_guest_cpu_time = dict()
self.last_guest_traffic = dict()
self.last_guest_disk_io = dict()
self.ts = ji.Common.ts()
self.ssh_client = None
def remove_oldest_entries(storage, percentage=90):
# compute current memory usage (%)
old_mem = psutil.virtual_memory().percent
# if we have data in storage and utilization exceeds 90%
while storage and old_mem > percentage:
# removed oldest entry
storage.popitem(last=False)
# garbage collect
gc.collect(1)
# comute used memory again
new_mem = psutil.virtual_memory().percent
# if the used memory did not decrease stop
if new_mem >= old_mem:
break
# net new measurement for memory usage and loop
old_mem = new_mem
def get_memory():
memory = dict()
virtual = psutil.virtual_memory()
swap = psutil.swap_memory()
memory['virtual_total'] = '%.2fMB' % (virtual.total/1024/1024)
memory['virtual_available'] = '%.2fMB' % (virtual.available/1024/1024)
memory['virtual_percent'] = str(virtual.percent)+'%'
memory['virtual_used'] = '%.2fMB' % (virtual.used/1024/1024)
memory['virtual_free'] = '%.2fMB' % (virtual.free/1024/1024)
memory['virtual_active'] = '%.2fMB' % (virtual.active/1024/1024)
memory['virtual_inactive'] = '%.2fMB' % (virtual.inactive/1024/1024)
memory['virtual_buffers'] = '%.2fMB' % (virtual.buffers/1024/1024)
memory['virtual_cached'] = '%.2fMB' % (virtual.cached/1024/1024)
memory['virtual_shared'] = '%.2fMB' % (virtual.shared/1024/1024)
memory['swap_total'] = '%.2fMB' % (swap.total/1024/1024)
memory['swap_used'] = '%.2fMB' % (swap.used/1024/1024)
memory['swap_free'] = '%.2fMB' % (swap.free/1024/1024)
memory['swap_percent'] = str(swap.percent)+'%'
memory['swap_sin'] = '%.2fMB' % (swap.sin/1024/1024)
memory['swap_sout'] = '%.2fMB' % (swap.sout/1024/1024)
return memory
def _get_memory_stats(self):
mem_info = {'virtual': {}, 'swap': {}}
v_mem = ps.virtual_memory()
s_mem = ps.swap_memory()
_c = mem_info['virtual']
_c['total'] = v_mem.total
_c['used'] = v_mem.used
_c['free'] = v_mem.free
_c['used_percent'] = v_mem.percent
_c = mem_info['swap']
_c['total'] = s_mem.total
_c['used'] = s_mem.used
_c['free'] = s_mem.free
_c['used_percent'] = s_mem.percent
return mem_info
def _init_in_memory_chunks(self, size):
available_mem = psutil.virtual_memory().available
required_mem = self._calculate_required_memory(size)
if required_mem <= available_mem:
self._in_memory_chunks = np.empty(shape=(size, self.data_producer.dimension()),
order='C', dtype=np.float32)
else:
if self.oom_strategy == 'raise':
self.logger.warning('K-means failed to load all the data (%s required, %s available) into memory. '
'Consider using a larger stride or set the oom_strategy to \'memmap\' which works '
'with a memmapped temporary file.'
% (bytes_to_string(required_mem), bytes_to_string(available_mem)))
raise MemoryError()
else:
self.logger.warning('K-means failed to load all the data (%s required, %s available) into memory '
'and now uses a memmapped temporary file which is comparably slow. '
'Consider using a larger stride.'
% (bytes_to_string(required_mem), bytes_to_string(available_mem)))
self._in_memory_chunks = np.memmap(tempfile.mkstemp()[1], mode="w+",
shape=(size, self.data_producer.dimension()), order='C',
dtype=np.float32)
def memory():
c = statsd.StatsClient(STATSD_HOST, 8125, prefix=PREFIX + 'system.memory')
while True:
swap = psutil.swap_memory()
c.gauge('swap.total', swap.total)
c.gauge('swap.used', swap.used)
c.gauge('swap.free', swap.free)
c.gauge('swap.percent', swap.percent)
virtual = psutil.virtual_memory()
c.gauge('virtual.total', virtual.total)
c.gauge('virtual.available', virtual.available)
c.gauge('virtual.used', virtual.used)
c.gauge('virtual.free', virtual.free)
c.gauge('virtual.percent', virtual.percent)
c.gauge('virtual.active', virtual.active)
c.gauge('virtual.inactive', virtual.inactive)
c.gauge('virtual.buffers', virtual.buffers)
c.gauge('virtual.cached', virtual.cached)
time.sleep(GRANULARITY)
def POST(self):
cpuused = psutil.cpu_percent(interval=None, percpu=False)
memused = psutil.virtual_memory()[2]
diskused = psutil.disk_usage('/')[3]
pidcount = len(psutil.pids())
uptimeshell = subprocess.Popen(['uptime'], stderr=subprocess.PIPE,stdout=subprocess.PIPE)
uptimeshell.wait()
uptime = uptimeshell.communicate()
return json.dumps(
{
"code": 0,
"current": {
"cpuused": cpuused,
"memused": memused,
"diskused": diskused,
"pidcount": pidcount,
"uptime": uptime
}
}
)
def cpuinfo():
men=psutil.virtual_memory()
menab=(men.available)/(1024*1024)
menta=(men.total)/(1024*1024)
menus=(men.used)/(1024*1024)
disk=psutil.disk_usage('/')
diskta=(disk.total)/(1024*1024*1024)
diskus=(disk.used)/(1024*1024*1024)
diskfr=(disk.free)/(1024*1024*1024)
time = datetime.datetime.now()
with open('eggs.csv', 'a') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow({"??????%s"%time})
spamwriter.writerow({"cpu?????%d%s"%(psutil.cpu_percent(interval=1),"%")})
spamwriter.writerow({"???????%sMB" % int(menta)})
spamwriter.writerow({"?????????%sMB" % int(menus)})
spamwriter.writerow({"????????%sMB" % int(menab)})
spamwriter.writerow({"???????%sG" % int(diskta)})
spamwriter.writerow({"???????%sG" % int(diskus)})
spamwriter.writerow({"???????%sG" % int(diskfr)})
with open('eggs.csv', 'r') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in spamreader:
print(row)
def _get_docker_node_info(info):
"""Gets the node info specific to docker.
"""
cpucapacity = int(cpu_count() * 100)
memcapacity = (psutil.virtual_memory().total * 0.9) // _BYTES_IN_MB
# TODO: manage disk space a little better
if os.name == 'nt':
path = 'C:\\ProgramData\\docker'
else:
path = '/var/lib/docker'
diskfree = disk_usage(path).free // _BYTES_IN_MB
info.update({
'memory': '%dM' % memcapacity,
'disk': '%dM' % diskfree,
'cpu': '%d%%' % cpucapacity,
'up_since': up_since(),
})
return info
def monitor(arglar):
logger.info("Starting main loop")
while True:
if arglar.mode in "ca":
cpu_reading = max(psutil.cpu_percent(arglar.resolution, percpu=True))
logger.debug("Cpu percents {}".format(cpu_reading))
if cpu_reading > arglar.cpu_crit:
logger.debug("Cpu percentage is higher than critical level")
print("CPU usage is high", file=arglar.crit_out)
elif arglar.cpu_warn is not None and cpu_reading > arglar.cpu_warn:
logger.debug("Cpu percentage is higher than warning level")
print("CPU usage is critical", file=arglar.warn_out)
if arglar.mode in "ra":
ram_reading = psutil.virtual_memory().percent
logger.debug("Ram percents {}".format(ram_reading))
if ram_reading > arglar.ram_crit:
logger.debug("Ram percentage is higher than critical level")
print("RAM usage is high", file=arglar.crit_out)
elif arglar.ram_warn is not None and ram_reading > arglar.ram_warn:
logger.debug("Ram percentage is higher than warning level")
print("RAM usage is critical", file=arglar.warn_out)
logger.debug("Sleeping")
time.sleep(arglar.resolution)
def get_memory_usage(output_dict):
my_dict = {}
memory_usage = psutil.virtual_memory()
memory_total_gb = float(memory_usage.total)/(1024*1024*1024)
memory_used_gb = float(memory_usage.used)/(1024*1024*1024)
memory_available_gb = float(memory_usage.available)/(1024*1024*1024)
memory_buffers_gb = float(memory_usage.buffers)/(1024*1024*1024)
my_dict["ram_total_gb"] = "{:.2f}".format(memory_total_gb)
my_dict["ram_used_gb"] = "{:.2f}".format(memory_used_gb)
my_dict["ram_available_gb"] = "{:.2f}".format(memory_available_gb)
my_dict["ram_buffers_gb"] = "{:.2f}".format(memory_buffers_gb)
percent_ratio = float(my_dict["ram_used_gb"])*100/float(my_dict["ram_total_gb"])
my_dict["used_percentage"] = "%s(%sgb/%sgb)" % \
("{:.2f}".format(percent_ratio) + "%", \
my_dict["ram_used_gb"], my_dict["ram_total_gb"])
output_dict["ram"] = my_dict
# https://stackoverflow.com/questions/276052/how-to-get-current-cpu-and-ram-usage-in-python
def updateGui(screen, boxes, data):
maxy, maxx = screen.getmaxyx()
date = str(time.strftime("%c"))
screen.addstr(1, maxx - len(date) - 2, date)
screen.addstr(3, 15, '%3d' % (psutil.cpu_percent()))
# svmem(total=10367352832, available=6472179712, percent=37.6, used=8186245120, free=2181107712, active=4748992512, inactive=2758115328, buffers=790724608, cached=3500347392, shared=787554304)
screen.addstr(4, 15, str(psutil.virtual_memory()[4] / (1024 * 1024)))
screen.refresh()
n = 0
for box in boxes:
testspersecond = '%8d' % data[n]["testspersecond"]
testcount = '%8d' % data[n]["testcount"]
crashcount = '%8d' % data[n]["crashcount"]
box.addstr(1, 1, testspersecond)
box.addstr(2, 1, testcount)
box.addstr(3, 1, crashcount)
box.refresh()
n += 1
def status(cmd, message, args):
os_icon, os_color = get_os_icon()
general_text = f'Latency: **{int(cmd.bot.latency * 1000)}ms**'
general_text += f'\nPlatform: **{sys.platform.upper()}**'
general_text += f'\nStarted: **{arrow.get(psutil.boot_time()).humanize()}**'
cpu_clock = psutil.cpu_freq()
if cpu_clock:
cpu_clock = cpu_clock.current
else:
cpu_clock = 'Unknown'
cpu_text = f'Count: **{psutil.cpu_count()} ({psutil.cpu_count(logical=False)})**'
cpu_text += f'\nUsage: **{psutil.cpu_percent()}%**'
cpu_text += f'\nClock: **{cpu_clock} MHz**'
used_mem = humanfriendly.format_size(psutil.virtual_memory().available, binary=True)
total_mem = humanfriendly.format_size(psutil.virtual_memory().total, binary=True)
mem_text = f'Used: **{used_mem}**'
mem_text += f'\nTotal: **{total_mem}**'
mem_text += f'\nPercent: **{int(100 - psutil.virtual_memory().percent)}%**'
response = discord.Embed(color=os_color)
response.set_author(name=socket.gethostname(), icon_url=os_icon)
response.add_field(name='General', value=general_text)
response.add_field(name='CPU', value=cpu_text)
response.add_field(name='Memory', value=mem_text)
await message.channel.send(embed=response)