forked from innogames/igcollect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_cpu_usage.py
executable file
·79 lines (64 loc) · 3.12 KB
/
linux_cpu_usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
#
# igcollect - Linux CPU usage
#
# Copyright (c) 2016, InnoGames GmbH
#
from __future__ import print_function
import socket, time, sys
def get_cpustats_dict(header):
''' returns a dictionary made from /proc/diskstats '''
try:
sd = open('/proc/stat','r')
stat_data = sd.readlines(16384)
sd.close()
except:
sys.exit(1)
total_dict = {}
uptime = 0
cpustats_dict = {}
count = 0
for line in stat_data:
''' here we have to handle some kind of disk
first the name than the counters as mentioned
in the header'''
if line.startswith('cpu '):
if len(line.strip().split()) == 11:
a, total_dict['user'], total_dict['nice'], total_dict['system'], total_dict['idle'], total_dict['iowait'], total_dict['irq'], total_dict['softirq'], total_dict['steal'], total_dict['guest'], total_dict['guest_nice'] = line.split()
total_dict['time'] = int(total_dict['user']) + int(total_dict['nice']) + int(total_dict['system']) + int(total_dict['iowait']) + int(total_dict['irq']) + int(total_dict['softirq']) + int(total_dict['steal'])
else:
total_dict['steal'] = 0
total_dict['guest'] = 0
total_dict['guest_nice'] = 0
a, total_dict['user'], total_dict['nice'], total_dict['system'], total_dict['idle'], total_dict['iowait'], total_dict['irq'], total_dict['softirq'], a = line.split(' ',8)
total_dict['time'] = int(total_dict['user']) + int(total_dict['nice']) + int(total_dict['system']) + int(total_dict['iowait']) + int(total_dict['irq'])
elif line.startswith('cpu'):
count += 1
x = line.strip().split()
name = x.pop(0).lstrip('cpu')
cpustats_dict[name] = {}
for i in header:
cpustats_dict[name][i] = x.pop(0)
elif line.startswith('btime '):
uptime = int(time.time()) - int(line.split(' ',1)[1])
elif line.startswith('intr '):
interrupts = int(line.split(' ',2)[1])
elif line.startswith('ctxt '):
context_switches = int(line.split(' ',1)[1])
return(cpustats_dict, total_dict, uptime, count, interrupts, context_switches)
now = str(int(time.time()))
graphite_data=''
hostname = socket.gethostname().replace('.','_')
sector_size=512
header=['user','nice','system','idle','iowait','irq','softirq','steal']
cs, totals, uptime, count, intr, ctxt = get_cpustats_dict(header)
for cpu in cs:
for metric in header:
graphite_data += 'servers.%s.system.cpu.%s.%s %s %s\n' % (hostname,cpu, metric, str(cs[cpu][metric]), now )
for value in totals:
graphite_data += 'servers.%s.system.cpu.%s %s %s\n' % (hostname, str(value), totals[value], now )
graphite_data += 'servers.%s.system.cpu.count %s %s\n' % (hostname, str(count), now )
graphite_data += 'servers.%s.system.cpu.uptime %s %s\n' % (hostname, str(uptime), now )
graphite_data += 'servers.%s.system.cpu.intr %s %s\n' % (hostname, str(intr,), now )
graphite_data += 'servers.%s.system.cpu.ctxt %s %s\n' % (hostname, str(ctxt), now )
print(graphite_data)