blob: e2f53b7b77ae9665772f9ac9b9d9b771c09950e6 [file] [log] [blame]
Ved-vampir602a5172015-03-16 15:22:36 +03001import time
2
3from discover import provides
4from utils import SensorInfo, is_dev_accepted
5
6# 0 - cpu name
7# 1 - user: normal processes executing in user mode
8# 2 - nice: niced processes executing in user mode
9# 3 - system: processes executing in kernel mode
10# 4 - idle: twiddling thumbs
11# 5 - iowait: waiting for I/O to complete
12# 6 - irq: servicing interrupts
13# 7 - softirq: servicing softirqs
14
15io_values_pos = [
16 (1, 'user_processes', False),
17 (2, 'nice_processes', False),
18 (3, 'system_processes', False),
19 (4, 'idle_time', True),
20]
21
22
23@provides("system-cpu")
24def syscpu_stat(disallowed_prefixes=('intr', 'ctxt', 'btime', 'processes',
25 'procs_running', 'procs_blocked', 'softirq'),
26 allowed_prefixes=None):
27 results = {}
28 usage_sum = {}
29 usage_with_idle = {}
30 for line in open('/proc/stat'):
31 vals = line.split()
32 dev_name = vals[0]
33
34 dev_ok = is_dev_accepted(dev_name,
35 disallowed_prefixes,
36 allowed_prefixes)
37
38 if dev_ok:
39 usage_sum[dev_name] = float(int(vals[1]) + int(vals[2]) + int(vals[3]))
40 usage_with_idle[dev_name] = usage_sum[dev_name] + int(vals[4])
41 # wait for average CPU utilization calc
42 time.sleep(1)
43
44 for line in open('/proc/stat'):
45 vals = line.split()
46 dev_name = vals[0]
47
48 dev_ok = is_dev_accepted(dev_name,
49 disallowed_prefixes,
50 allowed_prefixes)
51
52 if dev_ok:
53 for pos, name, accum_val in io_values_pos:
54 sensor_name = "{0}.{1}".format(dev_name, name)
55 results[sensor_name] = SensorInfo(int(vals[pos]), accum_val)
56 # utilization calc
57 sensor_name = "{0}.{1}".format(dev_name, "usage_percent")
58
59 usage_sum_last = float(int(vals[1]) + int(vals[2]) + int(vals[3]))
60 usage_with_idle_last = usage_sum_last + int(vals[4])
61 proc = usage_sum_last - usage_sum[dev_name]
62 idle = usage_with_idle_last - usage_with_idle[dev_name]
63 usage = proc / idle
64
65 results[sensor_name] = SensorInfo(usage*100, False)
66 return results
67