def __update__(self): """ Update the stats """ # Get informations from libstatgrab and others... self.host = statgrab.sg_get_host_info() # TODO: platform.platform(True) instead of statgrab.sg_get_host_info() self.system = statgrab.sg_get_host_info() self.cpu = statgrab.sg_get_cpu_percents() self.load = statgrab.sg_get_load_stats() self.mem = statgrab.sg_get_mem_stats() self.memswap = statgrab.sg_get_swap_stats() self.networkinterface = statgrab.sg_get_network_iface_stats() self.network = statgrab.sg_get_network_io_stats_diff() self.diskio = statgrab.sg_get_disk_io_stats_diff() # Replace the bugged self.fs = statgrab.sg_get_fs_stats() self.fs = self.glancesgrabfs.get() self.processcount = statgrab.sg_get_process_count() self.process = statgrab.sg_get_process_stats() self.now = datetime.datetime.now() # Get the number of core (CPU) # Used to display load alerts self.core_number = multiprocessing.cpu_count()
def get_all_values(): cpu = statgrab.sg_get_cpu_percents() mem = statgrab.sg_get_mem_stats() load = statgrab.sg_get_load_stats() swap = statgrab.sg_get_swap_stats() my_stats = { "cpu": { "kernel": cpu.kernel, "user": cpu.user, "iowait": cpu.iowait, "nice": cpu.nice, "swap": cpu.swap, "idle": cpu.idle, }, "load": { "min1": load.min1, "min5": load.min5, "min15": load.min15, }, "mem": { "used": mem.used, "cache": mem.cache, "free": mem.free, "total": mem.total, }, "swap": { "used": swap.used, "free": swap.free, "total": swap.total, }, } return my_stats
def __update__(self): """ Update the stats """ # Get informations from libstatgrab and others... self.host = statgrab.sg_get_host_info() self.system = statgrab.sg_get_host_info() self.cpu = statgrab.sg_get_cpu_percents() self.load = statgrab.sg_get_load_stats() self.mem = statgrab.sg_get_mem_stats() self.memswap = statgrab.sg_get_swap_stats() self.network = statgrab.sg_get_network_io_stats_diff() self.processcount = statgrab.sg_get_process_count() self.process = statgrab.sg_get_process_stats() # BUG: https://bugs.launchpad.net/ubuntu/+source/libstatgrab/+bug/886783 # TODO: self.fs = statgrab.sg_get_fs_stats() self.now = datetime.datetime.now()
def __update__(self): """ Update the stats """ # Get informations from libstatgrab and others... self.host = statgrab.sg_get_host_info() # TODO: platform.platform(True) instead of statgrab.sg_get_host_info() self.system = statgrab.sg_get_host_info() self.cpu = statgrab.sg_get_cpu_percents() self.load = statgrab.sg_get_load_stats() self.mem = statgrab.sg_get_mem_stats() self.memswap = statgrab.sg_get_swap_stats() self.network = statgrab.sg_get_network_io_stats_diff() self.diskio = statgrab.sg_get_disk_io_stats_diff() # Replace the bugged self.fs = statgrab.sg_get_fs_stats() self.fs = self.glancesgrabfs.get() self.processcount = statgrab.sg_get_process_count() self.process = statgrab.sg_get_process_stats() self.now = datetime.datetime.now()
sg_get_load_stats() #Return the average since 1 minute, 5 minutes and 15 minutes sg_get_mem_stats() #Return total, free, cache and used of the RAM sg_get_network_iface_stats() #Retur network interfaces and theirs status sg_get_network_io_stats() #Return input ouput in bytes on each interfaces (and errors..) sg_get_network_io_stats_diff() #Return the difference with the last io_stats() sg_get_page_stats() sg_get_page_stats_diff() sg_get_process_count() #Return informations about processes sg_get_process_stats() #Return informations about all process sg_get_swap_stats() #Same as mem_stats but not with cache sg_get_user_stats() # ? if not stg.sg_init(): sys.exit(1) res = stg.sg_get_cpu_percents() #The problem is the object returned is a Result class you can loop trough items knowing #in advance all the keys. ex: for k in ('kernel','iowait','idle','swap','nice','user'): #time_taken .. print k,": ",res[k],"%" ''' kernel : 0.531929373741 % iowait : 0.00408129952848 % idle : 97.5757064819 % swap : 0.0 % nice : 0.0 % user : 1.8923625946 % ''' #OR you can convert Result class to dictionnary
if __name__ == '__main__': import sys import statgrab if len(sys.argv) != 2: print("usage: %s <colorcode>"%sys.argv[0]) exit(1) maxColor = int(sys.argv[1], 16) maxR = (maxColor & 0xff0000) >> 16 maxG = (maxColor & 0x00ff00) >> 8 maxB = (maxColor & 0x0000ff) >> 0 while(True): cpuLoad = 100 - statgrab.sg_get_cpu_percents()['idle'] r = scale(cpuLoad, 0, 100, 0, maxR) g = scale(cpuLoad, 0, 100, 0, maxG) b = scale(cpuLoad, 0, 100, 0, maxB) packet = "#%02x%02x%02x"%(r, g, b) n = sock.sendto(packet, UDSPath) if (n != len(packet)): print("somthing wrong!") exit(1) print("cpu = %d spear = %s"%(cpuLoad, packet)) time.sleep(1) exit(0)
def _reportCPU(): cpuLoad = 100 - statgrab.sg_get_cpu_percents()['idle'] return cpuLoad
from django.core.management import setup_environ import settings setup_environ(settings) import time import logging import statgrab from status.models import * kb = 1024 mb = kb * kb while True: ### Host data ### cpu = statgrab.sg_get_cpu_percents() load = statgrab.sg_get_load_stats() host = statgrab.sg_get_host_info() mem = statgrab.sg_get_mem_stats() hs = HostStats(hostname=host['hostname'], uptime=host['uptime'], arch=host['platform'], min1=str(load['min1']), min5=str(load['min5']), min15=str(load['min15']), mem_total=mem['total'] / mb, mem_free=(mem['cache'] + mem['free']) / mb, mem_used=mem['used'] / mb) hs.save() ### FS data ###
from django.core.management import setup_environ import settings setup_environ(settings) import time import logging import statgrab from status.models import * kb = 1024 mb = kb * kb while True: ### Host data ### cpu = statgrab.sg_get_cpu_percents() load = statgrab.sg_get_load_stats() host = statgrab.sg_get_host_info() mem = statgrab.sg_get_mem_stats() hs = HostStats(hostname = host['hostname'], uptime = host['uptime'], arch = host['platform'], min1 = str(load['min1']), min5 = str(load['min5']), min15 = str(load['min15']), mem_total = mem['total'] / mb, mem_free = (mem['cache'] + mem['free']) / mb, mem_used = mem['used'] / mb) hs.save() ### FS data ### filesystems = statgrab.sg_get_fs_stats() for fs in filesystems: fs_stat = FilesystemStats(device = fs['device_name'], used_perc = str(float(fs['used']) / float(fs['size']) * 100.0), size = fs['size'] / mb, used = fs['used'] / mb, avail = fs['avail'] / mb, fs_type = fs['fs_type']) fs_stat.save() ### Network data ### statgrab.sg_get_network_io_stats() time.sleep(1) # Collect data over 1 second diff = statgrab.sg_get_network_io_stats_diff()