def cpu_usage(self, usage):
     if usage == HostMetric.cpu_count:
         return psutil.cpu_count()
     elif usage == HostMetric.cpu_user:
         return psutil.cpu_times_percent().user
     else:
         return psutil.cpu_times_percent().idle
Example #2
0
    def test_no_procfs_on_import(self, tb):
        my_procfs = tempfile.mkdtemp()

        with open(os.path.join(my_procfs, 'stat'), 'w') as f:
            f.write('cpu   0 0 0 0 0 0 0 0 0 0\n')
            f.write('cpu0  0 0 0 0 0 0 0 0 0 0\n')
            f.write('cpu1  0 0 0 0 0 0 0 0 0 0\n')

        try:
            orig_open = open

            def open_mock(name, *args, **kwargs):
                if name.startswith('/proc'):
                    raise IOError(errno.ENOENT, 'rejecting access for test')
                return orig_open(name, *args, **kwargs)

            patch_point = 'builtins.open' if PY3 else '__builtin__.open'
            with mock.patch(patch_point, side_effect=open_mock):
                importlib.reload(psutil)
                assert tb.called

                self.assertRaises(IOError, psutil.cpu_times)
                self.assertRaises(IOError, psutil.cpu_times, percpu=True)
                self.assertRaises(IOError, psutil.cpu_percent)
                self.assertRaises(IOError, psutil.cpu_percent, percpu=True)
                self.assertRaises(IOError, psutil.cpu_times_percent)
                self.assertRaises(
                    IOError, psutil.cpu_times_percent, percpu=True)

                psutil.PROCFS_PATH = my_procfs

                self.assertEqual(psutil.cpu_percent(), 0)
                self.assertEqual(sum(psutil.cpu_times_percent()), 0)

                # since we don't know the number of CPUs at import time,
                # we awkwardly say there are none until the second call
                per_cpu_percent = psutil.cpu_percent(percpu=True)
                self.assertEqual(sum(per_cpu_percent), 0)

                # ditto awkward length
                per_cpu_times_percent = psutil.cpu_times_percent(percpu=True)
                self.assertEqual(sum(map(sum, per_cpu_times_percent)), 0)

                # much user, very busy
                with open(os.path.join(my_procfs, 'stat'), 'w') as f:
                    f.write('cpu   1 0 0 0 0 0 0 0 0 0\n')
                    f.write('cpu0  1 0 0 0 0 0 0 0 0 0\n')
                    f.write('cpu1  1 0 0 0 0 0 0 0 0 0\n')

                self.assertNotEqual(psutil.cpu_percent(), 0)
                self.assertNotEqual(
                    sum(psutil.cpu_percent(percpu=True)), 0)
                self.assertNotEqual(sum(psutil.cpu_times_percent()), 0)
                self.assertNotEqual(
                    sum(map(sum, psutil.cpu_times_percent(percpu=True))), 0)
        finally:
            shutil.rmtree(my_procfs)
            importlib.reload(psutil)

        self.assertEqual(psutil.PROCFS_PATH, '/proc')
Example #3
0
def main(argv):
  opts, loop_opts = parse_args(argv)

  if opts.root_setup:
    return root_setup.root_setup()

  def single_iteration():
    try:
      system_metrics.get_cpu_info()
      system_metrics.get_disk_info()
      system_metrics.get_mem_info()
      system_metrics.get_net_info()
      system_metrics.get_proc_info()
      puppet_metrics.get_puppet_summary()
    finally:
      ts_mon.flush()
    return True

  # This returns a 0 value the first time it's called.  Call it now and discard
  # the return value.
  psutil.cpu_times_percent()

  # Wait a random amount of time before starting the loop in case sysmon is
  # started at exactly the same time on all machines.
  time.sleep(random.uniform(0, opts.interval))

  loop_results = outer_loop.loop(
      task=single_iteration,
      sleep_timeout=lambda: opts.interval,
      **loop_opts)

  return 0 if loop_results.success else 1
 def __init__(self, host_proc, event_prefix):
     self.event_prefix = event_prefix
     # Have to redirect this so we're not reading the container's /proc but the host's
     psutil.PROCFS_PATH = host_proc
     
     # Start the cpu_percent interval - this will return 0 the first time
     psutil.cpu_times_percent()
Example #5
0
 def get(self, *args, **kwargs):
     """
     provide details of cpu and memory usage
     """
     context = {
         'cpu': {
             'user': psutil.cpu_times_percent().user,
             'system': psutil.cpu_times_percent().system,
             'idle': psutil.cpu_times_percent().idle,
             'iowait': psutil.cpu_times_percent().iowait,
             'usage': psutil.cpu_percent()
         },
         'memory': {
             'percent': psutil.virtual_memory().percent,
             'total': psutil.virtual_memory().total,
             'available': psutil.virtual_memory().available,
             'used': psutil.virtual_memory().used,
             'free': psutil.virtual_memory().free,
             'cached': psutil.virtual_memory().cached
         }
     }
     templateRoot = options.TEMPLATE_ROOT
     loader = Loader(templateRoot)
     templateName = 'stats.html'
     response = loader.load(templateName).generate(**context)
     self.write(response)
     self.finish()
Example #6
0
 def starting(self, sender, **kwargs):
     
     psutil.cpu_times_percent()
     psutil.cpu_percent()
     _, _, my_id = self.vip.hello().get(timeout=3)
     self.vip.pubsub.publish(peer='pubsub', topic='/platform',
                             message='available')
Example #7
0
 def __init__(self, name, init_config, agent_config):
     super(Cpu, self).__init__(name, init_config, agent_config)
     # psutil.cpu_percent and psutil.cpu_times_percent are called in
     # __init__ because the first time these two functions are called with
     # interval = 0.0 or None, it will return a meaningless 0.0 value
     # which you are supposed to ignore.
     psutil.cpu_percent(interval=None, percpu=False)
     psutil.cpu_times_percent(interval=None, percpu=False)
Example #8
0
 def test_sys_per_cpu_times_percent_negative(self):
     # see: https://github.com/giampaolo/psutil/issues/645
     psutil.cpu_times_percent(percpu=True)
     zero_times = [x._make([0 for x in range(len(x._fields))]) for x in psutil.cpu_times(percpu=True)]
     with mock.patch("psutil.cpu_times", return_value=zero_times):
         for cpu in psutil.cpu_times_percent(percpu=True):
             for percent in cpu:
                 self._test_cpu_percent(percent, None, None)
Example #9
0
 def test_cpu_times_percent(self):
     last = psutil.cpu_times_percent(interval=0.001)
     for x in range(100):
         new = psutil.cpu_times_percent(interval=None)
         for percent in new:
             self._test_cpu_percent(percent, last, new)
         self._test_cpu_percent(sum(new), last, new)
         last = new
Example #10
0
def get_cup_info():
    global fg
    print "\033[31m%scpu状态信息%s\033[0m" %(fg*30,fg*30)
    print "cpu 逻辑个数:%s个" % psutil.cpu_count()
    print "cpu 物理个数:%s个" % psutil.cpu_count(logical=False)
    print "cpu 用户空间占用百分比:%s" % (psutil.cpu_times_percent(interval=1).user)+"%"
    print "cpu 内核空间占用百分比:%s" % (psutil.cpu_times_percent(interval=1).system)+"%"
    print "cpu 空闲空间剩余百分比:%s" % (psutil.cpu_times_percent(interval=1).idle)+"%"
Example #11
0
 def test_per_cpu_times_percent(self):
     last = psutil.cpu_times_percent(interval=0.001, percpu=True)
     self.assertEqual(len(last), psutil.cpu_count())
     for x in range(100):
         new = psutil.cpu_times_percent(interval=None, percpu=True)
         for cpu in new:
             for percent in cpu:
                 self._test_cpu_percent(percent, last, new)
             self._test_cpu_percent(sum(cpu), last, new)
         last = new
Example #12
0
    def collect(self,obj_id,part_id,part_name,slot_id):
        """
        Collector cpu stats
        """
        try:
            col_time = time.strftime('%Y-%m-%d %X')
            collect_data_list = []
            def publish(*args,**kwargs):
                item_num = collect_metrics.metrics.get('cpu_hw').get(args[2])
                if item_num is not None:
                    #9#print args
                    collect_data_list.append((args[0],args[1],item_num[0],args[3],item_num[1],col_time))
            if not psutil:
                log_format.logger.error('Unable to import psutil')
                return None

            cpu_count = psutil.cpu_count()
            cpu_info = None
            if part_name == 'cpu_total':
                cpu_info = psutil.cpu_times_percent(percpu=False)
            else:
                #if need collect all cpu,should delete follow row.
                return None
                #
                for i in range(cpu_count):
                    cpu = 'cpu' + str(i)
                    if cpu == part_name:
                        cpu_info = psutil.cpu_times_percent(percpu=True)[i]
            if cpu_info is not None:
                publish(obj_id,part_id,'user',cpu_info.user,precision=2)
                publish(obj_id,part_id,'nice',cpu_info.nice,precision=2)
                publish(obj_id,part_id,'system',cpu_info.system,precision=2)
                publish(obj_id,part_id,'idle',cpu_info.idle,precision=2)
                publish(obj_id,part_id,'iowait',cpu_info.iowait,precision=2)
                publish(obj_id,part_id,'irq',cpu_info.irq,precision=2)
                publish(obj_id,part_id,'softirq',cpu_info.softirq,precision=2)
                publish(obj_id,part_id,'steal',cpu_info.steal,precision=2)
                if part_name == 'cpu_total'  and os.access('/proc/loadavg',os.R_OK):
                    fr = open('/proc/loadavg','r')
                    data = fr.read()
                    fr.close()
                    for line in data.splitlines():
                        try:
                            min1, min5, min15, procs, last_pid = line.split()
                            publish(obj_id,part_id,'load_average_1min',min1,precision=2)
                            publish(obj_id,part_id,'load_average_5min',min5,precision=2)
                            publish(obj_id,part_id,'load_average_15min',min15,precision=2)
                            break
                        except Exception,ex_in_for:
                            log_format.logger.error(str(ex_in_for.args))
            insertdb.insertdb(collect_data_list)
            return True
Example #13
0
 def cpu_times_percent(self):
     ret = {}
     ret['cpu_percent_user'] = psutil.cpu_times_percent().user
     ret['cpu_percent_nice'] = psutil.cpu_times_percent().nice
     ret['cpu_percent_system'] = psutil.cpu_times_percent().system
     ret['cpu_percent_idle'] = psutil.cpu_times_percent().idle
     ret['cpu_percent_iowait'] = psutil.cpu_times_percent().iowait
     ret['cpu_percent_irq'] = psutil.cpu_times_percent().irq
     ret['cpu_percent_softirq'] = psutil.cpu_times_percent().softirq
     ret['cpu_percent_steal'] = psutil.cpu_times_percent().steal
     ret['cpu_percent_guest'] = psutil.cpu_times_percent().guest
     ret['cpu_percent_guest_nice'] = psutil.cpu_times_percent().guest_nice
     return ret
Example #14
0
File: core.py Project: lowks/ttop
    def update(self):
        times_percent = psutil.cpu_times_percent(percpu=True)
        cpu = psutil.cpu_times_percent()
        self.cpu.update(cpu.user, cpu.system, cpu.idle)

        for i, c in enumerate(times_percent):
            self.each_cpu[i].update(c.user, c.system, c.idle)

        self.__update_memory(self.memory, psutil.virtual_memory())
        self.__update_memory(self.swap, psutil.swap_memory())

        self.loadavg.update()
        self.uptime.update()
        self.procs.update()
Example #15
0
    def collect_cpu(self):
        cpu = psutil.cpu_times_percent()

        results = {
            'cpu.cores': psutil.cpu_count(),
            'cpu.percent': (cpu.user + cpu.nice + cpu.system),
            'cpu.user': cpu.user,
            'cpu.nice': cpu.nice,
            'cpu.system': cpu.system,
            'cpu.idle': cpu.idle
        }

        if hasattr(cpu, "iowait"):
            results['cpu.iowait'] = cpu.iowait
        if hasattr(cpu, "irq"):
            results['cpu.irq'] = cpu.irq
        if hasattr(cpu, "softirq"):
            results['cpu.softirq'] = cpu.softirq
        if hasattr(cpu, "steal"):
            results['cpu.steal'] = cpu.steal
        if hasattr(cpu, "guest"):
            results['cpu.guest'] = cpu.guest
        if hasattr(cpu, "guestnice"):
            results['cpu.guestnice'] = cpu.guestnice

        return results
Example #16
0
def init(interval):
    global cpu
    while 1:
        load = psutil.cpu_times_percent()
        cpu["user"]   = int(load.user)
        cpu["system"] = int(load.system)
        time.sleep(interval)
Example #17
0
    def sysinfodash(self):
        ''' used for the dash '''
        d = {}
        # Cpu stuff
        cpu = psutil.cpu_times_percent(interval=0.1, percpu=False)
        cpu = cpu._asdict()
        d['cpu'] = {'user': cpu['user'],
                    'system': cpu['system'],
                    'idle': cpu['idle']
                    }

        # Virtual memory
        vmem = psutil.virtual_memory()
        vmem = vmem._asdict()
        d['virtual'] = {'total': vmem['total'],
                        'percent': vmem['percent'],
                        'available': vmem['available']
                        }
        d['localip'] = self.get_local_ip(dash=True)
        d['externalip'] = self.get_external_ip(dash=True)
        nw_psutil = psutil.net_io_counters()
        dnw_psutil = nw_psutil._asdict()
        d['network'] = dnw_psutil
        d['uptime'] = self.uptime(dash=True)
        d['user'] = self.get_user(dash=True)

        return d
Example #18
0
File: web.py Project: 5n1p/psdash
def index():
    load_avg = os.getloadavg()
    uptime = datetime.now() - datetime.fromtimestamp(psutil.get_boot_time())
    disks = get_disks()
    users = get_users()

    netifs = get_network_interfaces()
    netifs.sort(key=lambda x: x.get("bytes_sent"), reverse=True)

    data = {
        "os": platform.platform().decode("utf-8"),
        "hostname": socket.gethostname().decode("utf-8"),
        "uptime": str(uptime).split(".")[0],
        "load_avg": load_avg,
        "cpus": psutil.NUM_CPUS,
        "vmem": psutil.virtual_memory(),
        "swap": psutil.swap_memory(),
        "disks": disks,
        "cpu_percent": psutil.cpu_times_percent(0),
        "users": users,
        "net_interfaces": netifs,
        "page": "overview",
        "is_xhr": request.is_xhr
    }

    return render_template("index.html", **data)
Example #19
0
def get_cpu():
	times = psutil.cpu_times_percent(percpu=False)
	return jsonify({
		'user': times.user, 'nice': times.nice, 'system': times.system, 'idle': times.idle,
		'iowait': times.iowait, 'irq': times.irq, 'softirq': times.softirq,
		'steal': times.steal, 'guest': times.guest, 'guest_nice': times.guest_nice
	})
Example #20
0
def main():

    hostname = getfqdn()
    data = ''

    for k, v in psutil.cpu_times_percent(interval=0.5)._asdict().iteritems():
        data += '{0}.cpu.{1} value={2}\n'.format(hostname, k, v)

    for k, v in psutil.virtual_memory()._asdict().iteritems():
        data += '{0}.mem.{1} value={2}\n'.format(hostname, k, v)

    for k, v in psutil.swap_memory()._asdict().iteritems():
        data += '{0}.swap.{1} value={2}\n'.format(hostname, k, v)

    du = { mp.mountpoint: psutil.disk_usage(mp.mountpoint).percent for mp in psutil.disk_partitions() }
    for k, v in du.iteritems():
        data += '{0}.du_percent.{1} value={2}\n'.format(hostname, k, v)

    for k, v in psutil.disk_io_counters(perdisk=True).iteritems():
        for m, p in v._asdict().iteritems():
            data += '{0}.io.{1}.{2} value={3}\n'.format(hostname, k, m, p)

    for k, v in psutil.net_io_counters(pernic=True).iteritems():
        for m, p in v._asdict().iteritems():
            data += '{0}.net.{1}.{2} value={3}\n'.format(hostname, k, m, p)

    connections = map(lambda x: x.status.lower(), psutil.net_connections())
    connections = map( lambda x: 'unknown_state' if x == 'none' else x, connections )
    for conn in set(connections):
        data += '{0}.netstat.{1} value={2}\n'.format(hostname, conn, connections.count(conn))

    if DEBUG: print data
    sendData(data)
def collect_cpu_stats():
    times = psutil.cpu_times()
    per_core_snapshot = psutil.cpu_times_percent(interval=1, percpu=True)

    data = {
        "system_cpu_times": {
            "user":         times.user,
            "nice":         times.nice,
            "system":       times.system,
            "idle":         times.idle,
            "iowait":       times.iowait,
            "irq":          times.irq,
            "softirq":      times.softirq,
            "steal":        times.steal,
            "guest":        times.guest,
            "guest_nice":   times.guest_nice
        },
        "percore_cpu_times": []
    }

    for cpu in per_core_snapshot:
        data['percore_cpu_times'].append({
            "user":         cpu.user,
            "nice":         cpu.nice,
            "system":       cpu.system,
            "idle":         cpu.idle,
            "iowait":       cpu.iowait,
            "irq":          cpu.irq,
            "softirq":      cpu.softirq,
            "steal":        cpu.steal,
            "guest":        cpu.guest,
            "guest_nice":   cpu.guest_nice
        })

    return data
Example #22
0
 def __get_percpu(self):
     """Update and/or return the per CPU list using the psutil library."""
     # Never update more than 1 time per cached_time
     if self.timer_percpu.finished():
         self.percpu_percent = []
         for cpu_number, cputimes in enumerate(psutil.cpu_times_percent(interval=0.0, percpu=True)):
             cpu = {'key': self.get_key(),
                    'cpu_number': cpu_number,
                    'total': round(100 - cputimes.idle, 1),
                    'user': cputimes.user,
                    'system': cputimes.system,
                    'idle': cputimes.idle}
             # The following stats are for API purposes only
             if hasattr(cputimes, 'nice'):
                 cpu['nice'] = cputimes.nice
             if hasattr(cputimes, 'iowait'):
                 cpu['iowait'] = cputimes.iowait
             if hasattr(cputimes, 'irq'):
                 cpu['irq'] = cputimes.irq
             if hasattr(cputimes, 'softirq'):
                 cpu['softirq'] = cputimes.softirq
             if hasattr(cputimes, 'steal'):
                 cpu['steal'] = cputimes.steal
             if hasattr(cputimes, 'guest'):
                 cpu['guest'] = cputimes.guest
             if hasattr(cputimes, 'guest_nice'):
                 cpu['guest_nice'] = cputimes.guest_nice
             # Append new CPU to the list
             self.percpu_percent.append(cpu)
             # Reset timer for cache
             self.timer_percpu = Timer(self.cached_time)
     return self.percpu_percent
    def cpustat(self):
        tags="node:%s %s"%(self.nodename,self.tags)

        val=int(psutil.cpu_percent())
        measurement="cpu_perc"
        key="%s.%s"%(self.nodename,measurement)
        self.measure(key,measurement,tags,val,aggrkey=measurement)

        val=int(psutil.virtual_memory()[1]/1024/1024)
        measurement="mem_free_mb"
        key="%s.%s"%(self.nodename,measurement)
        self.measure(key,measurement,tags,val,aggrkey=measurement)

        val=int(psutil.swap_memory()[3])
        measurement="mem_virt_perc"
        key="%s.%s"%(self.nodename,measurement)
        self.measure(key,measurement,tags,val,aggrkey=measurement)

        val=int(psutil.virtual_memory()[2])
        measurement="mem_phy_perc"
        key="%s.%s"%(self.nodename,measurement)
        self.measure(key,measurement,tags,val,aggrkey=measurement)

        res=psutil.cpu_times_percent()
        names=["cputimeperc_user","cputimeperc_nice","cputimeperc_system","cputimeperc_idle","cputimeperc_iowait","cputimeperc_irq","cputimeperc_softirq","steal","guest","guest_nice"]
        for i in range(len(names)):
            if names[i].startswith("cputime"):
                val=int(res[i])
                measurement=names[i]
                key="%s.%s"%(self.nodename,measurement)
                self.measure(key,measurement,tags,val,aggrkey=measurement)
Example #24
0
def cpu_load_stats():
    cpu_load = psutil.cpu_times_percent(interval=1, percpu=False)

    user_load = cpu_load.user
    system_load = cpu_load.system
    irq_load = cpu_load.irq
    return (user_load, system_load, irq_load)
Example #25
0
def get_new_row():

    # get a new blank row and add a random brightness snowflake to a random column
    row = get_blank_row()
    cpu = psutil.cpu_times_percent(1)
    tot_cpu = cpu.user + cpu.system
    #print tot_cpu
    # identify cpu usage bracket - 6 brackets, each 20% except very low and very high (10%)
    bracket = round(tot_cpu/20)
    #print bracket
    # colour and number chance is proportional to cpu usage
    # randomly decide how many cols will contain sth
    if bracket < 1:
    	cols = randint(0,1)
    elif bracket < 3:
	cols = randint(1,bracket)
    else:
	cols = randint(bracket-2, bracket)
    # determine r g b excess for each bracket
	# 0 - very blue
	# 1 - mostly green, some blue
	# 2 - yellow
	# 3 - orange
	# 4 - red
	# 5 - very red/purple
    # corrections :
    for x in range(cols):
	corr = sample(range(0,40),3)*2
    	row[randint(0, width - 1)] = [rgb[bracket][0] + corr[0], rgb[bracket][1]+ corr[1], rgb[bracket][2] + corr[2]] # selects random col and sets brightness from 50 to 205
    return row
Example #26
0
def doCPU(lights):
    cpu = psutil.cpu_times_percent()

    u = cpu.user + cpu.nice
    s = cpu.system + cpu.steal + cpu.guest_nice + cpu.guest
    w = cpu.irq + cpu.iowait +  cpu.softirq
    i = cpu.idle

    # Cumulative
    ac = s
    bc = ac + w
    cc = bc + u
    tot = cc + i

    aIdx = cpuLEDRange.toLEDIndex(ac / 100)
    bIdx = cpuLEDRange.toLEDIndex(bc / 100)
    cIdx = cpuLEDRange.toLEDIndex(cc / 100)

    for x in cpuLEDRange.range:
        if x <= aIdx:
            r = 30
        elif x <= bIdx:
            r = 0
        elif x <= cIdx:
            r = 250
        # elif x < (tot * norm):
        #     r = 0
        else:
            r = 0

        lights.set(x, r, 0, 0)
Example #27
0
def _getSummary():
  """
  :returns: a _CsvRow object
  """
  timestamp = time.time()
  loadAvg1, loadAvg5, loadAvg15 = os.getloadavg()
  cpuTimesPct = psutil.cpu_times_percent(interval=0)
  virtualMem = psutil.virtual_memory()
  swapMem = psutil.swap_memory()

  row = _CsvRow(
    timestamp=timestamp,
    loadAvg1=loadAvg1,
    loadAvg5=loadAvg5,
    loadAvg15=loadAvg15,
    cpuUserPct=cpuTimesPct.user,
    cpuSystemPct=cpuTimesPct.system,
    cpuNicePct=cpuTimesPct.nice,
    cpuIdlePct=cpuTimesPct.idle,
    memTotalB=virtualMem.total,
    memUsageB=virtualMem.total - virtualMem.available,
    memAvailB=virtualMem.available,
    memUsagePct=virtualMem.percent,
    memBuffersB=virtualMem.buffers if hasattr(virtualMem, "buffers") else None,
    memCachedB=virtualMem.cached if hasattr(virtualMem, "cached") else None,
    swapTotalB=swapMem.total,
    swapUsedB=swapMem.used,
    swapFreeB=swapMem.free,
    swapUsedPct=swapMem.percent,
    swapInsB=swapMem.sin,
    swapOutsB=swapMem.sout
  )

  return row
Example #28
0
 def collect_cpuinfo(self):
     cpuinfo = psutil.cpu_times_percent(interval=1,percpu=False)
     cpuset = {}
     cpuset['user'] = cpuinfo.user
     cpuset['system'] = cpuinfo.system
     cpuset['idle'] = cpuinfo.idle
     cpuset['iowait'] = cpuinfo.iowait
     self.etcdser.setkey('/cpuinfo',cpuset)
     output = subprocess.check_output(["cat /proc/cpuinfo"],shell=True)
     output = output.decode('utf-8')
     parts = output.split('\n')
     info = []
     idx = -1
     for part in parts:
         if not part == '':
             key_val = re.split(':',part)
             key = key_val[0].rstrip()
             if key == 'processor':
                 info.append({})
                 idx += 1
             val = key_val[1].lstrip()
             if key=='processor' or key=='model name' or key=='core id' or key=='cpu MHz' or key=='cache size' or key=='physical id':
                 info[idx][key] = val
     self.etcdser.setkey('/cpuconfig',info)
     return
Example #29
0
 def probe(self):
     times = psutil.cpu_times_percent()
     items = sorted(times._asdict().items())
     if self.extended == False:
         showvals = ", ".join(["%.1f%% %s" % (v,n) for n,v in items if v != 0.0])
     else:
         showvals = ", ".join(["%.1f%% %s" % (v,n) for n,v in items])
     summary = "CPU utilization is " + showvals
     metrics = dict(times._asdict().items())
     if self.userfailed is not None and times.user > self.userfailed:
         return self.failed(summary, metrics)
     if self.systemfailed is not None and times.system > self.systemfailed:
         return self.failed(summary, metrics)
     if self.iowaitfailed is not None and times.iowait > self.iowaitfailed:
         return self.failed(summary, metrics)
     if self.userdegraded is not None and times.user > self.userdegraded:
         return self.degraded(summary, metrics)
     if self.systemdegraded is not None and times.system > self.systemdegraded:
         return self.degraded(summary, metrics)
     if self.iowaitdegraded is not None and times.iowait > self.iowaitdegraded:
         return self.degraded(summary, metrics)
     if self.idlefailed is not None and times.idle < self.idlefailed:
         return self.failed(summary, metrics)
     if self.idledegraded is not None and times.idle < self.idledegraded:
         return self.degraded(summary, metrics)
     return self.healthy(summary, metrics)
Example #30
0
        def write_status(self):
            historian_present = False

            try:
                ping = self.vip.ping('platform.historian', 'awake?').get(timeout=2)
                historian_present = True
            except Unreachable:
                _log.warning('platform.historian unavailable no logging of data will occur.')
                return
            _log.debug('publishing data')
            base_topic = 'datalogger/log/platform/status'
            cpu = base_topic + '/cpu'
            virtual_memory = base_topic + "/virtual_memory"
            disk_partitions = base_topic + "/disk_partiions"

            points = {}

            for k, v in psutil.cpu_times_percent().__dict__.items():
                points['times_percent/' + k] = {'Readings': v,
                                                'Units': 'double'}

            points['percent'] = {'Readings': psutil.cpu_percent(),
                                 'Units': 'double'}

            message = jsonapi.dumps(points)
            self.vip.pubsub.publish(peer='pubsub',
                                    topic=cpu,
                                    message=points)
Example #31
0
    def _get_system_cpu_utilization(self,
                                    observer: metrics.ValueObserver) -> None:
        """Observer callback for system CPU utilization

        Args:
            observer: the observer to update
        """

        for cpu, times_percent in enumerate(
                psutil.cpu_times_percent(percpu=True)):
            for metric in self._config["system.cpu.utilization"]:
                if hasattr(times_percent, metric):
                    self._system_cpu_utilization_labels["state"] = metric
                    self._system_cpu_utilization_labels["cpu"] = cpu + 1
                    observer.observe(
                        getattr(times_percent, metric) / 100,
                        self._system_cpu_utilization_labels,
                    )
Example #32
0
    def validate(self, X_test, Y_test, test_results, outputs, j, positives,
                 losses, accuracies, T):
        """
        Compute the loss function on the validation data.
        Also output a random example of a digit after every epoch
        and the network's guess.
        """

        loss = self.cost_function.fn(outputs, T) / len(T.T)

        # Throttle code - DO NOT REMOVE THIS. Prevents system crashes.
        if psutil.virtual_memory().percent > 95 or psutil.cpu_times_percent(
        ).idle < 5:
            time.sleep(1)
        losses.append(loss)
        if self.type == 'sigmoid':
            pass
        else:
            testi = random.choice(range(Y_test.shape[1]))
            test = X_test.T[testi]
            plt.imshow(test.reshape(28, 28), cmap='gray')
            plt.show()
            cls = int(test_results.T[testi])
            print("Prediction: %d confidence=%0.2f" %
                  (cls, np.asarray(outputs).T[testi][cls] /
                   np.sum(outputs.T[testi])))

        accpct = 100 * positives / X_test.shape[1]
        accuracies.append(accpct)

        if j % 10 == 9:

            print("    Speed: %0.2f s/pass" % ((time.time() - self.tm) /
                                               (j + 0.01)))
            print("    Accuracy: %d/%d, %0.3f%%" %
                  (positives, X_test.shape[1], accpct))

            plt.plot(np.log(losses), color='blue')
            plt.title("Log loss")
            plt.show()
            plt.plot(accuracies, color='blue')
            plt.title("Accuracy")
            plt.show()
            '''
Example #33
0
def put_info(local_ip, local_prot):
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.bind((local_ip, local_prot))
    serversocket.listen(20)
    while True:
        s, addr = serversocket.accept()
        message = s.recv(1024).decode('utf-8')
        #返回操作系统信息
        if message == 'get_platform':
            s.send(sys.platform.encode('utf-8'))
        #返回打开文件信息
        if message == 'get_openfiles':
            openfiles_info = [
                p.info for p in psutil.process_iter(['pid', 'open_files'])
            ]
            openfiles_info_json = json.dumps(openfiles_info)
            s.send(openfiles_info_json.encode('utf-8'))
        #返回进程信息
        if message == 'get_process':
            process_info = [
                p.info
                for p in psutil.process_iter(['pid', 'username', 'name'])
            ]
            process_info_json = json.dumps(process_info)
            s.send(process_info_json.encode('utf-8'))
        #返回内存信息
        if message == 'get_memory':
            mem_list = [[], []]
            for x in psutil.virtual_memory():
                mem_list[0].append(x)
            for y in psutil.swap_memory():
                mem_list[1].append(y)
            mem_json = json.dumps(mem_list)
            s.send(mem_json.encode('utf-8'))
        #返回cpu信息
        if message == 'get_cpu':
            cpu_list = [[], [], []]
            for x in psutil.cpu_times_percent(interval=1, percpu=False):
                cpu_list[0].append(x)
            cpu_list[1].append(psutil.cpu_count(logical=True))
            for y in psutil.getloadavg():
                cpu_list[2].append(y)
            cpu_json = json.dumps(cpu_list)
            s.send(cpu_json.encode('utf-8'))
Example #34
0
def exec_combination(device, pipeline, pi_dict, f_input, combination):
    '''
    Execute the whole pipeline for the given function and combination of parameters.
    Note that the order of the parameter values must be the same as in the ordered
    dictionary function.params.keys().
    '''
    row_dict = pi_dict.copy()
    base_f_c = 0
    for f in pipeline:
        params_list = list(f.params.keys())
        params_dict = {}
        params_data = {}
        for param_i in range(len(params_list)):
            p_value = combination[base_f_c + param_i]
            params_dict[params_list[param_i]] = p_value
            row_dict[f.data.name + '_' +
                     str(params_list[param_i])] = str(p_value)
            params_data[str(params_list[param_i])] = str(p_value)

        row_dict['cpu_idle'] = psutil.cpu_times_percent(interval=0.5).idle
        row_dict['memory_available'] = psutil.virtual_memory().available
        row_dict[f.data.name + '_start'] = time.time()
        f_output_value = f.function(f_input.io_value, **params_dict)
        row_dict[f.data.name + '_end'] = time.time()
        base_f_c += len(f.params.keys())

        f_output = MFAIO(
            io_id='io_' +
            MFAUtil.params_input_to_string(params_dict, f_input.data),
            io_size=sys.getsizeof(f_output_value),
            io_format=type(f_output_value),
            io_value=f_output_value)

        f.data.add_exec_sample(
            device.device_id, f_input.data, f_output.data, params_data, {
                'latency': (row_dict[f.data.name + '_end'] -
                            row_dict[f.data.name + '_start'])
            })

        f_input = f_output

    row_dict['output'] = f_input.io_value

    return row_dict
Example #35
0
def server():
    global cpu_times

    def calculate(t1, t2):
        t1_all = sum(t1)
        t1_busy = t1_all - t1.idle

        t2_all = sum(t2)
        t2_busy = t2_all - t2.idle

        # this usually indicates a float precision issue
        if t2_busy <= t1_busy:
            return 0.0

        busy_delta = t2_busy - t1_busy
        all_delta = t2_all - t1_all
        busy_perc = (busy_delta / all_delta) * 100
        return round(busy_perc, 1)

    ret = {}
    ret.update({
        "mem_" + key: val
        for key, val in psutil.virtual_memory().__dict__.iteritems()
    })
    ret.update({
        "cpu_ptime_" + key: val
        for key, val in psutil.cpu_times_percent().__dict__.iteritems()
    })
    if None not in cpu_times:
        ret['cpu_percent'] = calculate(*cpu_times)
    else:
        ret['cpu_percent'] = 0
    ret.update({
        "diskio_" + key: val
        for key, val in psutil.disk_io_counters().__dict__.iteritems()
    })
    ret.update({
        "disk_" + key: val
        for key, val in psutil.disk_usage('/').__dict__.iteritems()
    })
    users = psutil.get_users()
    ret['user_count'] = len(users)
    ret['user_info'] = [(u.name, u.host) for u in users]
    return jsonify(**ret)
Example #36
0
    def _publish_stats(self):
        """
        Publish the platform statistics to the local bus as well as to the
        connected volttron central.
        """
        vc_topic = None
        local_topic = LOGGER(subtopic="platform/status/cpu")
        _log.debug('Publishing platform cpu stats')
        if self._platform_uuid:

            vc_topic = LOGGER(
                subtopic="platforms/{}/status/cpu".format(self._platform_uuid))
            _log.debug('Stats will be published to: {}'.format(
                vc_topic.format()))
        else:
            _log.debug('Platform uuid is not valid')
        points = {}

        for k, v in psutil.cpu_times_percent().__dict__.items():
            points['times_percent/' + k] = {'Readings': v, 'Units': 'double'}

        points['percent'] = {
            'Readings': psutil.cpu_percent(),
            'Units': 'double'
        }

        self.vip.pubsub.publish(peer='pubsub',
                                topic=local_topic.format(),
                                message=points)

        # Handle from external platform
        if vc_topic and self._agent_connected_to_vc:
            self._agent_connected_to_vc.vip.pubsub.publish(
                peer='pubsub', topic=vc_topic.format(), message=points)
        # Handle if platform agent on same machine as vc.
        elif vc_topic and \
                        self._my_discovery_address == self._vc_discovery_address:

            self.vip.pubsub.publish(peer='pubsub',
                                    topic=vc_topic.format(),
                                    message=points)
        else:
            _log.info("status not written to volttron central.")
Example #37
0
    def load_initial(self):
        print("Waiting for slaves to launch..")
        self.cpu = psutil.cpu_times_percent(interval=0.01, percpu=False)
        self.mem = psutil.virtual_memory()
        self.cores_phys = psutil.cpu_count(logical=False)
        self.cores_virt = psutil.cpu_count(logical=True)
        self.stats = self.read_file("stats")

        num_slaves = self.stats.get("num_slaves",0)
        for slave_id in range(0, num_slaves):
            self.slave_stats.append(self.read_file("slave_stats_%d" % slave_id))

        # TODO frontend is using time.time() when we actually need time.clock(), plus perhaps the startup time/date
        self.starttime = min([x["start_time"] for x in self.slave_stats])

        self.nodes = {}
        for metadata in glob.glob(self.workdir + "/metadata/node_*"):
            self.load_node(metadata)
        self.aggregate()
Example #38
0
def cpu_times_percent():
    c = statsd.StatsClient('localhost', 8125, prefix='system.cpu')
    while True:
        value = psutil.cpu_percent(interval=1)
        c.gauge('system_wide.percent', value)

        cpu_times_percent = psutil.cpu_times_percent(interval=1)
        c.gauge('system_wide.times_percent.user', cpu_times_percent.user)
        c.gauge('system_wide.times_percent.nice', cpu_times_percent.nice)
        c.gauge('system_wide.times_percent.system', cpu_times_percent.system)
        c.gauge('system_wide.times_percent.idle', cpu_times_percent.idle)
        c.gauge('system_wide.times_percent.iowait', cpu_times_percent.iowait)
        c.gauge('system_wide.times_percent.irq', cpu_times_percent.irq)
        c.gauge('system_wide.times_percent.softirq', cpu_times_percent.softirq)
        c.gauge('system_wide.times_percent.steal', cpu_times_percent.steal)
        c.gauge('system_wide.times_percent.guest', cpu_times_percent.guest)
        c.gauge('system_wide.times_percent.guest_nice',
                cpu_times_percent.guest_nice)
        time.sleep(10)
Example #39
0
    def get_cpu_usage(cls):
        """ Discovers CPU usage on this node.

    Returns:
      A dictionary containing the idle, system and user percentages.
    """

        cpu_stats = psutil.cpu_times_percent(percpu=False)
        cpu_stats_dict = {
            StatsKeys.CPU: {
                StatsKeys.IDLE: cpu_stats.idle,
                StatsKeys.SYSTEM: cpu_stats.system,
                StatsKeys.USER: cpu_stats.user,
                StatsKeys.COUNT: len(psutil.cpu_times(percpu=True))
            }
        }
        logger.debug("CPU stats: {}".format(cpu_stats_dict))

        return cpu_stats_dict
Example #40
0
    def cpu_times(self,
                  per_cpu=False,
                  percent=False) -> Union[CpuTimesResponse, CpuResponseList]:
        """
        Get the CPU times stats.

        :param per_cpu: Get per-CPU stats (default: False).
        :param percent: Get the stats in percentage (default: False).
        :return: :class:`platypush.message.response.system.CpuTimesResponse`
        """
        import psutil

        times = psutil.cpu_times_percent(percpu=per_cpu) if percent else \
            psutil.cpu_times(percpu=per_cpu)

        if per_cpu:
            return CpuResponseList([
                CpuTimesResponse(
                    user=t.user,
                    nice=t.nice,
                    system=t.system,
                    idle=t.idle,
                    iowait=t.iowait,
                    irq=t.irq,
                    softirq=t.softirq,
                    steal=t.steal,
                    guest=t.guest,
                    guest_nice=t.guest_nice,
                ) for t in times
            ])

        return CpuTimesResponse(
            user=times.user,
            nice=times.nice,
            system=times.system,
            idle=times.idle,
            iowait=times.iowait,
            irq=times.irq,
            softirq=times.softirq,
            steal=times.steal,
            guest=times.guest,
            guest_nice=times.guest_nice,
        )
Example #41
0
def process_details(pid, py):
    # print('in process_details N', os.getpid())
    mem = py.memory_info()._asdict()
    swap_memory = psutil.swap_memory()._asdict()
    total_memory = float((swap_memory['total'] / (2 ** 30)))
    used_vms_gib = mem['vms'] / 2 ** 30
    cpu_percent = psutil.cpu_times_percent(interval=0.4, percpu=False)._asdict()

    values = [mem['rss'] / 2 ** 10, mem['vms'] / 2 ** 10, mem['num_page_faults']]
    values1 = [total_memory, used_vms_gib]
    cpu_values = [cpu_percent['user'], cpu_percent['idle'],
                  cpu_percent['system'] + cpu_percent['interrupt'] + cpu_percent['dpc']]
    file_size = [os.path.getsize((__file__)), psutil.disk_usage('C:').total]

    result = {
        f"Process ID: {pid}, RSS: {mem['rss']}, Virtual Memory: {mem['vms']}, Page Faults:{mem['num_page_faults']} "
        f"CPU %: {cpu_percent['user']} Memory usage: {round(used_vms_gib, 4)} GiB out of {round(swap_memory['total'] / (2 ** 30), 4)} GiB, file size {file_size[0]} Bytes"}

    print(f"Naive Implementation: {result}")
Example #42
0
def main():
    arg_qty = len(sys.argv) - 1

    if (arg_qty != 1):
        print("Please provide 1 mandatory argument\n")
        use_help()
    else:
        stat_type = str(sys.argv[1])

        if (stat_type == "cpu"):
            cpu_stats = psutil.cpu_times_percent(interval=1, percpu=False)
            print_cpu_stats(cpu_stats)
        elif (stat_type == "mem"):
            mem_virt = psutil.virtual_memory()
            mem_swap = psutil.swap_memory()
            print_mem_stats(mem_virt, mem_swap)
        else:
            print("Wrong argument\n")
            use_help()
def echo_socket(ws):
    print "connect"
    while True:
        try:
            message = json.loads(ws.receive())
        except:
            print "json fail"

        print "incoming!: ", message

        if message.get("event", "") == "heartbeat":
            cpu = psutil.cpu_times_percent(interval=1)
            ram = psutil.virtual_memory().percent
            data = {'cpu': cpu.user + cpu.system, 'ram': ram}
            s = []
            for service in services:
                s.append({'name': service['service'], 'pid': 0})
            data['services'] = s
            ws.send(json.dumps({'event': "heartbeat", 'data': data}))
Example #44
0
 def render_GET(self, request):
     # self.log_call(request)
     import psutil
     section = request.postpath and request.postpath.pop() or None
     if not section:
         data = {
             'platform': platform.platform(),
             'machine': platform.machine(),
             'name': platform.node(),
             'system': platform.system(),
             'version': platform.version(),
             'boot_time': psutil.boot_time()
         }
     elif section == 'cpu':
         cpu_times_percent = psutil.cpu_times_percent()
         data = {
             'total': cpu_times_percent.user + cpu_times_percent.system,
             'system': cpu_times_percent.system,
             'user': cpu_times_percent.user,
             'idle': cpu_times_percent.idle,
             # 'total_per_cpu': psutil.cpu_percent(percpu=True),
         }
     elif section == 'memory':
         vmem = psutil.virtual_memory()
         data = {
             'total': vmem.total,
             'available': vmem.available,
             'percent': vmem.percent,
             'used': vmem.used,
             'free': vmem.free,
         }
     elif section == 'disk':
         disk = psutil.disk_usage(settings.PROJECT_ROOT)
         data = {
             'total': disk.total,
             'used': disk.used,
             'free': disk.free,
             'percent': disk.percent,
         }
     else:
         data = {}
     self.write_headers(request)
     return json.dumps(data)
Example #45
0
    def check(self, instance):
        usage = psutil.cpu_times_percent(percpu=True)
        tags = instance.get('tags', [])
        for core_idx in range(len(usage)):
            metric_tags = tags + ['core:{}'.format(core_idx)]
            core_usage = usage[core_idx]
            for attr in self.PSUTIL_USAGE_ATTRS:
                try:
                    value = getattr(core_usage, attr)
                    self.gauge("system.cpu.{}".format(attr),
                               value,
                               tags=metric_tags)
                except AttributeError:
                    if self.first_run:
                        self.log.debug(
                            'CPU usage attribute %s not available on this platform',
                            attr)

        self.first_run = False
Example #46
0
def cpu_info():
    payload = []

    times = psutil.cpu_times_percent(percpu=True, interval=1)
    system, user, nice, idle = 0.0, 0.0, 0.0, 0.0

    for i, cpu in enumerate(times):
        system += cpu.system
        user += cpu.user
        nice += cpu.nice
        idle += cpu.idle
        payload.append(create_cpu_payload(f"core{i}", cpu))

    num_cpu = float(len(times))
    averages = Times(round(system / num_cpu, 1), round(user / num_cpu, 1),
                     round(nice / num_cpu, 1), round(idle / num_cpu, 1))
    payload.append(create_cpu_payload('cpu', averages))

    send_data_to_influx(payload)
Example #47
0
def cpu_times_percent():
    c = statsd.StatsClient(STATSD_HOST, 8125, prefix=PREFIX + 'system.cpu')
    while True:
        value = psutil.cpu_percent(interval=1)
        c.gauge('system_wide.percent', value)

        cpu_t_percent = psutil.cpu_times_percent(interval=1)
        c.gauge('system_wide.times_percent.user', cpu_t_percent.user)
        c.gauge('system_wide.times_percent.nice', cpu_t_percent.nice)
        c.gauge('system_wide.times_percent.system', cpu_t_percent.system)
        c.gauge('system_wide.times_percent.idle', cpu_t_percent.idle)
        c.gauge('system_wide.times_percent.iowait', cpu_t_percent.iowait)
        c.gauge('system_wide.times_percent.irq', cpu_t_percent.irq)
        c.gauge('system_wide.times_percent.softirq', cpu_t_percent.softirq)
        c.gauge('system_wide.times_percent.steal', cpu_t_percent.steal)
        c.gauge('system_wide.times_percent.guest', cpu_t_percent.guest)
        c.gauge('system_wide.times_percent.guest_nice',
                cpu_t_percent.guest_nice)
        time.sleep(GRANULARITY)
Example #48
0
    def _collect_and_write(self,
                           process,
                           writer,
                           ts=None,
                           iostat=None,
                           netstat=None):
        import psutil
        if ts is None:
            ts = time.time()
        cpu_percent = process.get_cpu_percent()
        cpu_times = process.get_cpu_times()
        mem_info = process.get_memory_info()
        data = [
            int(ts), process.pid, process.name, cpu_percent, cpu_times.user,
            cpu_times.system,
            process.get_memory_percent(), mem_info.rss, mem_info.vms
        ]

        box_data = None
        if iostat and netstat:
            box_cpu = psutil.cpu_times_percent()
            io = psutil.disk_io_counters()
            netio = psutil.net_io_counters()
            box_mem = psutil.phymem_usage()
            box_data = [
                box_cpu.user, box_cpu.system, box_cpu.idle,
                io.read_bytes - iostat.read_bytes,
                io.write_bytes - iostat.write_bytes,
                netio.bytes_recv - netstat.bytes_recv,
                netio.bytes_sent - netstat.bytes_sent,
                box_mem.used, box_mem.free,
                (psutil.used_phymem() - psutil.cached_phymem()), box_mem.total
            ]
        else:
            box_data = [0, 0, 0, 0, 0, 0, 0, 0, 0]
        data.extend(box_data)
        # write data
        print >> writer, "\t".join([str(s) for s in data])
        # process children
        for child in process.get_children():
            self._collect_and_write(child, writer, ts=ts)
        pass
Example #49
0
def update_device_info():
    global drive_usage
    global cpu_usage
    global ram_usage
    global swap_usage
    global device_temp

    # Get drive info
    try:
        # Get throttled info (raspi only)
        reload_throttled_data()

        # Get drive actual usage
        #results = subprocess.check_output(["du","-sh","/mnt/hdd/mynode/"])
        #drive_usage = results.split()[0]

        # Get drive percent usage
        results = subprocess.check_output(
            "df -h /mnt/hdd | grep /dev | awk '{print $5}'", shell=True)
        drive_usage = results

        # Get RAM usage
        ram_info = psutil.virtual_memory()
        ram_usage = "{}%".format(ram_info.percent)

        # Get Swap Usage
        swap_info = psutil.swap_memory()
        swap_usage = "{}%".format(swap_info.percent)

        # Get CPU usage
        #cpu_usage = "{}%".format(psutil.cpu_percent(interval=30.0))
        cpu_info = psutil.cpu_times_percent(interval=30.0, percpu=False)
        cpu_usage = "{}%".format(100.0 - cpu_info.idle)

        # Get device temp
        results = subprocess.check_output(
            "cat /sys/class/thermal/thermal_zone0/temp", shell=True)
        device_temp = int(results) / 1000

    except Exception as e:
        print("CAUGHT update_device_info EXCEPTION: " + str(e))
        return
Example #50
0
def m_agent():
    #payload = []
    _rd = redis.StrictRedis(host=YOUHOSTIP, port=6379, password='', db=0)

    cpu_status = psutil.cpu_times_percent(interval=cpu_interval)
    mem_status = psutil.virtual_memory()
    net_addrs_status = psutil.net_if_addrs()

    lstart = str(net_addrs_status).find('192.168.')
    lend = str(net_addrs_status)[lstart:].find('\', netmask')

    datadict = {
        "Host": str(net_addrs_status)[lstart:lstart + lend],
        "Mem_total": round(mem_status.total / 1024 / 1024 / 1024, 1),
        "ts": int(time.time()),
        "CPU_up": round(100 - cpu_status.idle, 1),
        "Mem_up": mem_status.percent
    }
    _rd.hmset(endpoint, datadict)
    _rd.expire(endpoint, refresh_interval)
Example #51
0
    def poll_system(self):
        cputimes = psutil.cpu_times_percent(percpu=True)
        for i, ct in enumerate(cputimes):
            self.cpu_data[i].append(ct)

            if len(self.cpu_data[i]) > self.max_history:
                self.cpu_data[i] = self.cpu_data[i][-self.max_history:]

        self.cpu_pct.append(int(psutil.cpu_percent()))
        if len(self.cpu_pct) > self.max_history:
            self.cpu_pct = self.cpu_pct[-self.max_history:]

        vm = psutil.virtual_memory()
        self.mem_pct.append(vm.used / vm.total)
        if len(self.mem_pct) > self.max_history:
            self.mem_pct = self.mem_pct[-self.max_history:]

        self.net_conns.append(len(psutil.net_connections()))
        if len(self.net_conns) > self.max_history:
            self.net_conns = self.net_conns[-self.max_history:]
Example #52
0
def get_host_stats(key):
    return {
        'key': key,
        'type': 'host',
        'payload': {
            'platform': platform.platform(),
            'hostname': platform.node(),
            'machine': platform.machine(),
            'version': platform.version(),
            'cores': psutil.cpu_count(),
            'cpu_stats': psutil.cpu_percent(interval=1, percpu=True),
            'usage': psutil.cpu_times_percent().user,
            'memory_total': psutil.virtual_memory().total,
            'memory_used': psutil.virtual_memory().used,
            'disk_total': psutil.disk_usage('/').total,
            'disk_free': psutil.disk_usage('/').used,
            'timestamp': datetime.datetime.now().isoformat(),
            'processes': len(psutil.pids())
        }
    }
Example #53
0
 def refresh(self, system_info: SystemInfo) -> SystemInfo:
     system_info.cpu_usage.cores = psutil.cpu_percent(percpu=True)
     cpu_times_percent = psutil.cpu_times_percent()
     system_info.cpu_usage.user = cpu_times_percent.user
     system_info.cpu_usage.nice = cpu_times_percent.nice
     system_info.cpu_usage.system = cpu_times_percent.system
     system_info.cpu_usage.io_wait = cpu_times_percent.iowait
     system_info.cpu_usage.irq = cpu_times_percent.irq
     system_info.cpu_usage.soft_irq = cpu_times_percent.softirq
     system_info.cpu_usage.steal = cpu_times_percent.steal
     system_info.cpu_usage.guest = cpu_times_percent.guest
     system_info.cpu_usage.guest_nice = cpu_times_percent.guest_nice
     system_info.load_avg.load_avg_1, system_info.load_avg.load_avg_5, system_info.load_avg.load_avg_15 \
         = psutil.getloadavg()
     system_info.load_avg.cpu_count = psutil.cpu_count()
     virtual_memory = psutil.virtual_memory()
     system_info.mem_usage.total = virtual_memory.total
     system_info.mem_usage.available = virtual_memory.available
     system_info.mem_usage.percent = virtual_memory.percent
     return system_info
Example #54
0
def cpu_info():
    logical_core_num = psutil.cpu_count()
    physical_core_num = psutil.cpu_count(logical=False)
    load_avg = os.getloadavg()
    cpu_time_percent = psutil.cpu_times_percent()
    else_percent = 0.0
    for i in range(5, 10):
        else_percent += cpu_time_percent[i]
    try:
        cpu_freq = psutil.cpu_freq()
    except AttributeError:
        cpu_freq = None

    return render_template('cpu.html',
                           physical_core_num=physical_core_num,
                           logical_core_num=logical_core_num,
                           load_avg=load_avg,
                           cpu_time_percent=cpu_time_percent,
                           else_percent=else_percent,
                           cpu_freq=cpu_freq)
Example #55
0
def web_statistics(driver):
    # print cpu and memory stats
    print(colored("CPU: ", 'blue'))
    print(psutil.cpu_times_percent(interval=1, percpu=False))
    print(colored("MEMORY: ", 'blue'))
    print(psutil.virtual_memory())

    """ Use Navigation Timing  API to calculate the timings that matter the most """
    navigationStart = driver.execute_script("return window.performance.timing.navigationStart")
    responseStart = driver.execute_script("return window.performance.timing.responseStart")
    domComplete = driver.execute_script("return window.performance.timing.domComplete")

    ''' Calculate the performance'''
    backendPerformance_calc = (responseStart - navigationStart) / 1000
    frontendPerformance_calc = (domComplete - responseStart) / 1000

    print(colored("TTFB = Time to first byte in seconds: %s", 'green') % backendPerformance_calc)
    print(colored("PLT = Page loading time in seconds: %s", 'green') % frontendPerformance_calc)

    return (backendPerformance_calc, frontendPerformance_calc)
Example #56
0
def cpu_times_percent(host, port, prefix):
    prefix = '.'.join([prefix, 'system.cpu'])
    c = statsd.StatsClient(host, port, prefix=prefix)
    while True:
        value = psutil.cpu_percent(interval=1)
        c.gauge('system_wide.percent', value)

        cpu_times_percent = psutil.cpu_times_percent(interval=1)
        c.gauge('system_wide.times_percent.user', cpu_times_percent.user)
        c.gauge('system_wide.times_percent.nice', cpu_times_percent.nice)
        c.gauge('system_wide.times_percent.system', cpu_times_percent.system)
        c.gauge('system_wide.times_percent.idle', cpu_times_percent.idle)
        c.gauge('system_wide.times_percent.iowait', cpu_times_percent.iowait)
        c.gauge('system_wide.times_percent.irq', cpu_times_percent.irq)
        c.gauge('system_wide.times_percent.softirq', cpu_times_percent.softirq)
        c.gauge('system_wide.times_percent.steal', cpu_times_percent.steal)
        c.gauge('system_wide.times_percent.guest', cpu_times_percent.guest)
        c.gauge('system_wide.times_percent.guest_nice',
                cpu_times_percent.guest_nice)
        time.sleep(10)
def check_active_jobs(active_jobs, available_cpus, timeout=1):

    # The first thing done when checking jobs is to snapshot the CPU_USAGE
    t = time.time()
    CPU_USAGE[t] = psutil.cpu_times_percent(percpu=True)
    MEMORY[t]    = psutil.virtual_memory()

    local_finished_jobs = []
    for i, job in enumerate(active_jobs):
        try:
            retcode = job.proc.wait(timeout=timeout)
        except TimeoutExpired:
            retcode = None
            continue

        job.state = 1
        job.endtime = time.time()
        job.runtime = job.endtime - job.starttime
        # Close the output file
        job.outfile.close()


        # Read back the last line of the output file which prints the internal execution time
        with open(job.outfile.name, 'r') as _f:
            job.exec_time = float(_f.readlines()[-1])

        # Set the output file to just the name:
        job.outfile = job.outfile.name

        # Ensure the process is closed:
        job.proc.communicate()
        job.proc = None

        # Return the CPU to available CPUs:
        available_cpus.append(job.used_cpu)
        # if the job is finished, move it from one list to the other:
        local_finished_jobs.append(active_jobs.pop(i))



    return active_jobs, local_finished_jobs
Example #58
0
    def gather_system_info(self):
        disk_partitions = psutil.disk_partitions()
        disk_usage = {}
  
        for disk in disk_partitions:
            disk_usage[disk.device] = self.to_dict(psutil.disk_usage(disk.mountpoint))
  
        net_io_counters = {}
        for name, nic in psutil.net_io_counters(pernic=True).items():
            net_io_counters[name] = self.to_dict(nic)
            
            if self.prev_info:
                net_io_counters[name]['bytes_sent_sec'] = net_io_counters[name]['bytes_sent'] - self.prev_info['net_io'][name]['bytes_sent']
                net_io_counters[name]['bytes_recv_sec'] = net_io_counters[name]['bytes_recv'] - self.prev_info['net_io'][name]['bytes_recv']
                net_io_counters[name]['packets_sent_sec'] = net_io_counters[name]['packets_sent'] - self.prev_info['net_io'][name]['packets_sent']
                net_io_counters[name]['packets_recv_sec'] = net_io_counters[name]['packets_recv'] - self.prev_info['net_io'][name]['packets_recv']
            else:
                net_io_counters[name]['bytes_sent_sec'] = 0
                net_io_counters[name]['bytes_recv_sec'] = 0
                net_io_counters[name]['packets_sent_sec'] = 0
                net_io_counters[name]['packets_recv_sec'] = 0
                
        info = {
            'cpu_count': psutil.cpu_count(),
            'cpu_count_physical': psutil.cpu_count(logical=False),
            'cpu_percent': self.to_dict(psutil.cpu_percent(percpu=True)),
            'cpu_times': self.to_dict(psutil.cpu_times()),
            'cpu_times_percent': self.to_dict(psutil.cpu_times_percent()),
            'memory_virtual': self.to_dict(psutil.virtual_memory()),
            'memory_swap': self.to_dict(psutil.swap_memory()),
            'disk_partitions': self.to_dict(disk_partitions),
            'disk_usage': disk_usage,
            'net_io': net_io_counters,
            'users': self.to_dict(psutil.users()),
            'boot_time': psutil.boot_time(),
            'hostname': socket.gethostname(),
        }
  
        self.prev_info = info

        return info
Example #59
0
    def _publish_stats(self):
        """
        Publish the platform statistics to the bus.
        """

        topic = LOGGER(subtopic="platform/status/cpu")

        points = {}

        for k, v in psutil.cpu_times_percent().__dict__.items():
            points['times_percent/' + k] = {'Readings': v, 'Units': 'double'}

        points['percent'] = {
            'Readings': psutil.cpu_percent(),
            'Units': 'double'
        }
        try:
            self.vip.pubsub.publish('pubsub', topic.format(), message=points)

        except Exception as e:
            _log.warn("Failed to publish to topic {}".format(topic.format()))
Example #60
0
def os_stats(update, context):
    logger.info('command: [os_stats] started')
    cpu = psutil.cpu_times_percent(interval=0.5)
    memory = psutil.virtual_memory()
    network_io = psutil.net_io_counters()
    time.sleep(1)
    network_io_new = psutil.net_io_counters()
    net_send = network_io_new.bytes_sent - network_io.bytes_sent
    net_recv = network_io_new.bytes_recv - network_io.bytes_recv
    msg = SERVER_STATS_RESPONSE.format(os_name=platform.platform(),
                                       user=cpu.user,
                                       system=cpu.system,
                                       idle=cpu.idle,
                                       total=memory.total / 1024. / 1024.,
                                       used=memory.used / 1024. / 1024.,
                                       sent=net_send / 1024.,
                                       recv=net_recv / 1024.)
    context.bot.send_message(chat_id=update.message.chat_id,
                             text=msg,
                             parse_mode='markdown')
    logger.info('command: [os_stats] finished')