def read_cpu_utilization(sample_duration_sec=1): """ User defined method :param sample_duration_sec: Sample duration sample is collected :return: percentage of cpu utilized. """ cpu_pcts = cpu_stat.cpu_percents(sample_duration_sec) return round((100 - cpu_pcts['idle']), 2)
def serverStats(): cpuPercents = cpu_stat.cpu_percents(1) statDict = {} statDict["ioWait"] = cpuPercents['iowait'] statDict["system"] = cpuPercents['system'] statDict["idle"] = cpuPercents['idle'] statDict["user"] = cpuPercents['user'] statDict["cpuUsed"] = round(100 - cpuPercents['idle']) statDict["loadAvg"] = cpu_stat.load_avg() statDict["memUsed"], statDict["memTot"], _, _, _, _ = mem_stat.mem_stats() statDict["memFree"] = memTot-memUsed return statDict
def _sendLinuxMetrics(self): cpu_percents = {} for k, v in cpu_stat.cpu_percents().items(): cpu_percents['cpu_' + k] = v load_avg = cpu_stat.load_avg() assert(len(load_avg) == 3) file_desc = cpu_stat.file_desc() assert(len(file_desc) == 3) stats = cpu_percents.items() stats += zip(['load_1min', 'load_5min', 'load_15min'], load_avg) stats += {'procs_running': cpu_stat.procs_running(), 'procs_blocked': cpu_stat.procs_blocked()}.items() stats += zip(['filedesc_allocated', 'filedesc_allocated_free', 'filedesc_max'], file_desc) self.sendEvent('cpu', dict(stats)) for nic in ['wlan0', 'eth0', 'usb0']: rx, tx = net_stat.rx_tx_bytes(nic) self.sendEvent('net', {'nic': nic, 'rx_bytes': rx, 'tx_bytes': tx})
def dump_stats(handler): while True: cpu_pcts = cpu_stat.cpu_percents(1) #instantaneous cpu usage utilization = 100 - cpu_pcts['idle'] #cores = all_cpu_usage() load_ = cpu_stat.load_avg() load_5,load_15 = load_[1],load_[2] mem_tuple = mem_stat.mem_stats() #instantaneous memory usage used,total = mem_tuple[0],mem_tuple[1] free = total - used handler.update({"vm_name":DOMAIN},{"$set":{"vm_name":DOMAIN,"vm_ip":VM_IP,"cpu":utilization,"5_min":load_5,"15_min":load_15,"mem_free":free,"mem_used":used,"mem_total":total,"mouse":"no"}},True) # update if document present else insert the document. update the cpu,mem of the virtual every 5 seconds print "upated the tuple" sleep(5)
def main(): # cpu print 'procs running: %d' % cpu_stat.procs_running() cpu_pcts = cpu_stat.cpu_percents(sample_duration=1) print 'cpu utilization: %.2f%%' % (100 - cpu_pcts['idle']) # disk print 'disk busy: %s%%' % disk_stat.disk_busy('sda', sample_duration=1) r, w = disk_stat.disk_reads_writes('sda') print 'disk reads: %s' % r print 'disk writes: %s' % w # memory used, total = mem_stat.mem_stats() print 'mem used: %s' % used print 'mem total: %s' % total # network rx_bits, tx_bits = net_stat.rx_tx_bits('eth0') print 'net bits received: %s' % rx_bits print 'net bits sent: %s' % tx_bits
def getMetrics(self): now = int(time.time()) * 1000 #JSON for GS Advanced Feed PUT API comp = {} comp['stream'] = [] comp['componentId'] = self._compId comp['time'] = [now] comp['defaults'] = {} comp['defaults']['name'] = self._compName #Assign a component template for auto registration comp['compTmplId'] = 'rPi' #cpu streams cpu_pcts = cpu_stat.cpu_percents(1) stream = {} stream['streamId'] = 'cpu_user' stream['data'] = [cpu_pcts['user']] comp['stream'].append(stream) stream = {} stream['streamId'] = 'cpu_nice' stream['data'] = [cpu_pcts['nice']] comp['stream'].append(stream) stream = {} stream['streamId'] = 'cpu_system' stream['data'] = [cpu_pcts['system']] comp['stream'].append(stream) stream = {} stream['streamId'] = 'cpu_idle' stream['data'] = [cpu_pcts['idle']] comp['stream'].append(stream) stream = {} stream['streamId'] = 'cpu_iowait' stream['data'] = [cpu_pcts['iowait']] comp['stream'].append(stream) load = cpu_stat.load_avg() stream = {} stream['streamId'] = 'cpu_avg1min' stream['data'] = [load[0]] comp['stream'].append(stream) stream = {} stream['streamId'] = 'cpu_avg5min' stream['data'] = [load[1]] comp['stream'].append(stream) stream = {} stream['streamId'] = 'cpu_avg15min' stream['data'] = [load[2]] comp['stream'].append(stream) #processes stream = {} stream['streamId'] = 'procs_running' stream['data'] = [cpu_stat.procs_running()] comp['stream'].append(stream) #memory mem = mem_stat.mem_stats() stream = {} stream['streamId'] = 'mem_active' stream['data'] = [mem[0]] comp['stream'].append(stream) stream = {} stream['streamId'] = 'mem_total' stream['data'] = [mem[1]] comp['stream'].append(stream) stream = {} stream['streamId'] = 'mem_cached' stream['data'] = [mem[2]] comp['stream'].append(stream) stream = {} stream['streamId'] = 'mem_free' stream['data'] = [mem[3]] comp['stream'].append(stream) stream = {} stream['streamId'] = 'swap_total' stream['data'] = [mem[4]] comp['stream'].append(stream) stream = {} stream['streamId'] = 'swap_free' stream['data'] = [mem[5]] comp['stream'].append(stream) #disk disk = disk_stat.disk_usage('/') stream = {} stream['streamId'] = 'disk_size' stream['data'] = [disk[1]] comp['stream'].append(stream) stream = {} stream['streamId'] = 'disk_used' stream['data'] = [disk[2]] comp['stream'].append(stream) #network net = net_stat.rx_tx_bytes('eth0') stream = {} stream['streamId'] = 'net_download' stream['data'] = [net[0]] comp['stream'].append(stream) net = net_stat.rx_tx_bytes('eth0') stream = {} stream['streamId'] = 'net_upload' stream['data'] = [net[1]] comp['stream'].append(stream) jobj = {} jobj['feed'] = {} jobj['feed']['component'] = [] jobj['feed']['component'].append(comp) return jobj
def read_cpu_utilization(sample_duration_sec=1): cpu_pcts = cpu_stat.cpu_percents(sample_duration_sec) return cpu_util_filter_with_window.filter( round((100 - cpu_pcts['idle']), 2))
def read_cpu_utilization(sample_duration_sec=1): cpu_pcts = cpu_stat.cpu_percents(sample_duration_sec) return cpu_util_filter_with_window.filter(round((100 - cpu_pcts['idle']), 2))
#!/usr/bin/python import firmata import time from linux_metrics import cpu_stat PIN_BUTTON = 11 PIN_LED = 10 PIN_METER = 9 print "Connecting to Meter..." board = firmata.FirmataInit('/dev/cu.usbserial-A1016d5') print "Initializing Meter..." board.pinMode(PIN_BUTTON, firmata.MODE_INPUT) board.digitalWrite(PIN_BUTTON, True) board.EnableDigitalReporting(1) # Enable digital reporting on port 1 while(True): cpu_pcts = cpu_stat.cpu_percents(1) i = int((100 - cpu_pcts['idle']) * 2.55) board.analogWrite(PIN_METER, i)
def test_cpu_percents(self): values = cpu_stat.cpu_percents(self.sample_duration) self.assertTrue(len(values) == 7, values)
def test_cpu_percent_user(self): value = cpu_stat.cpu_percents(self.sample_duration)['user'] self.assertTrue(0.0 <= value <= 100.0, value)
lastnumarray =+ 1 ttypath = input("Select Serial device: ") port = serial.Serial(portarray[int(ttypath)-1], 9600) def signal_handler(signal, frame): x = lastnum while x >= 0: port.write([x]) time.sleep(0.001) x = x - 1 port.write([0]) sys.exit(0) signal.signal(signal.SIGINT, signal_handler) while True: val = 255 * round(100 - cpu_stat.cpu_percents()['idle']) / 100 os.system("clear") print(str(round((val / 255) * 100)), "%") if val != lastnum: port.write(bytes([int(round(val))])) lastnum = val oldnumc = 1 else: print("Old Number", oldnumc) oldnumc += 1 time.sleep(0.25) gc.collect()
def read_cpu_utilization(sample_duration_sec=1): cpu_pcts = cpu_stat.cpu_percents(sample_duration_sec) return round((100 - cpu_pcts['idle']), 2)
from linux_metrics import cpu_stat while True: cpu_pcts = cpu_stat.cpu_percents(5) print 'cpu utilization: %.2f%%' % (100 - cpu_pcts['idle'])