def gather_info(self): matcher = ExcludeInfoMatcher() if OptionsEnabled.SIGAR_ENABLED: self.cpus = [] info = sigar.open() list = info.cpu_info_list() for val in list: self.cpus.append(CpuInformation(val)) if matcher.enabled('interfaces'): list = info.net_interface_list() for val in list: curr = info.net_interface_config(val) tmp = InterfaceInformation(curr) for each in ['address', 'address6', 'hwaddr', 'netmask', 'name']: if not matcher.match(each, getattr(tmp, each), 'interfaces'): delattr(tmp, each) self.interfaces.append(tmp) else: del self.interfaces info.close() if OptionsEnabled.PSUTIL_ENABLED: self.memory = psutil.TOTAL_PHYMEM if matcher.enabled('partitions'): self.partitions = psutil.disk_partitions() self.uuid = str(uuid.uuid1())
def get_physical_memory(): """ Return physical memory in bytes """ sg = sigar.open() mem = sg.mem() sg.close() return int(mem.total())
def get_info(): info = {"host_name": HOSTNAME} sg = sigar.open() # 系统启动时间 uptime = sg.uptime() # info['uptime'] = uptime.uptime() # CPU负载 cpuload = os.popen("cat /proc/loadavg").readlines()[0].strip().split() info["cpu_load_one"] = cpuload[0] info["cpu_load_five"] = cpuload[1] info["cpu_load_fifteen"] = cpuload[2] # 登陆用户数 whos = os.popen("who").readlines() info["login_users"] = len(whos) # 进程总数 process_num = os.popen("ps -ef | wc -l").readlines()[0].strip() info["process_num"] = process_num # CPU占用 cpu = sg.cpu() # info['cpu_user'] = cpu.user() # info['cpu_system'] = cpu.sys() info["cpu_idle"] = round((float(cpu.idle()) / float(cpu.total())) * 100, 2) info["cpu_used"] = round(100 - info["cpu_idle"], 2) # 内存使用 mem = sg.mem() info["mem_total"] = int(mem.total() / 1024 / 1024) info["mem_used"] = int(mem.used() / 1024 / 1024) info["mem_free"] = int(mem.free() / 1024 / 1024) info["mem_used_percent"] = round(float(mem.used()) / float(mem.total()) * 100, 2) info["mem_free_percent"] = round(100 - info["mem_used_percent"], 2) # info['mem_actual_free'] = mem.actual_free() swap = sg.swap() info["swap_total"] = int(swap.total() / 1024 / 1024) info["swap_free"] = int(swap.free() / 1024 / 1024) info["swap_used"] = int(swap.used() / 1024 / 1024) # 网络链接状态 info["tcps"] = os.popen("ss -t -a | wc -l").readlines()[0].strip() info["udps"] = os.popen("ss -u -a | wc -l").readlines()[0].strip() # 磁盘使用 disk = os.statvfs("/") info["disk_total"] = (disk.f_bsize * disk.f_blocks) / 1024 / 1024 info["disk_used"] = (disk.f_bsize * disk.f_bfree) / 1024 / 1024 info["disk_free"] = (disk.f_bsize * disk.f_bavail) / 1024 / 1024 info["disk_used_percent"] = round(float(info["disk_used"]) / float(info["disk_total"]) * 100, 2) info["disk_free_percent"] = round(float(info["disk_free"]) / float(info["disk_total"]) * 100, 2) return json.JSONEncoder().encode(info)
import os, sigar sg = sigar.open() fslist = sg.file_system_list() def format_size(size): return sigar.format_size(size * 1024) print 'Filesystem\tSize\tUsed\tAvail\tUse%\tMounted on\tType\n' for fs in fslist: dir_name = fs.dir_name() usage = sg.file_system_usage(dir_name) total = usage.total() used = total - usage.free() avail = usage.avail() pct = usage.use_percent() * 100 if pct == 0.0: pct = '-' print fs.dev_name(), format_size(total), format_size(used), format_size(avail),\ pct, dir_name, fs.sys_type_name(), '/', fs.type_name()
#! /usr/bin/python __author__="dell" __date__ ="$2009-10-14 16:57:57$" import cx_Oracle import os import sigar sg = sigar.open() mem = sg.mem() swap = sg.swap() cpu = sg.cpu() sg.close() mem_total = mem.total()/1024 mem_used = mem.used()/1024 mem_free = mem.free()/1024 swap_total = swap.total()/1024 swap_used = swap.used() / 1024 swap_free = swap.free() / 1024 cpu_user = cpu.user()/1024 cpu_sys = cpu.sys()/1024 cpu_idle = cpu.idle()/1024 db = cx_Oracle.connect('dsm', 'dsm', 'localhost:1521/dsmdb') db.autocommit = True cur = db.cursor() cur.execute("insert into stat_computer_monitor(id,name,mem_total,mem_used,mem_free,swap_total,swap_used,swap_free,cpu_us,cpu_sy,cpu_id,create_time) VALUES(STAT_COMPUTER_MONITOR_SEQ.NEXTVAL, 'oracledb',:a,:b,:c,:d,:e,:f,:g,:h,:i,sysdate)",{'a':mem_total,'b':mem_used,'c':mem_free,'d':swap_total,'e':swap_used,'f':swap_free,'g':cpu_user,'h':cpu_sys,'i':cpu_idle}) db.close
def __init__(self): self.sigar = sigar.open()
def setUp(self): self.sigar = sigar.open()