Ved-vampir | 602a517 | 2015-03-16 15:22:36 +0300 | [diff] [blame] | 1 | from discover import provides |
| 2 | from utils import SensorInfo, is_dev_accepted |
| 3 | |
| 4 | # 0 - cpu name |
| 5 | # 1 - user: normal processes executing in user mode |
| 6 | # 2 - nice: niced processes executing in user mode |
| 7 | # 3 - system: processes executing in kernel mode |
| 8 | # 4 - idle: twiddling thumbs |
| 9 | # 5 - iowait: waiting for I/O to complete |
| 10 | # 6 - irq: servicing interrupts |
| 11 | # 7 - softirq: servicing softirqs |
| 12 | |
| 13 | io_values_pos = [ |
Ved-vampir | bb783a1 | 2015-03-18 11:21:48 +0300 | [diff] [blame] | 14 | (1, 'user_processes', True), |
| 15 | (2, 'nice_processes', True), |
| 16 | (3, 'system_processes', True), |
Ved-vampir | 602a517 | 2015-03-16 15:22:36 +0300 | [diff] [blame] | 17 | (4, 'idle_time', True), |
| 18 | ] |
| 19 | |
| 20 | |
| 21 | @provides("system-cpu") |
| 22 | def syscpu_stat(disallowed_prefixes=('intr', 'ctxt', 'btime', 'processes', |
| 23 | 'procs_running', 'procs_blocked', 'softirq'), |
| 24 | allowed_prefixes=None): |
| 25 | results = {} |
Ved-vampir | 602a517 | 2015-03-16 15:22:36 +0300 | [diff] [blame] | 26 | |
| 27 | for line in open('/proc/stat'): |
| 28 | vals = line.split() |
| 29 | dev_name = vals[0] |
| 30 | |
| 31 | dev_ok = is_dev_accepted(dev_name, |
| 32 | disallowed_prefixes, |
| 33 | allowed_prefixes) |
| 34 | |
| 35 | if dev_ok: |
| 36 | for pos, name, accum_val in io_values_pos: |
| 37 | sensor_name = "{0}.{1}".format(dev_name, name) |
| 38 | results[sensor_name] = SensorInfo(int(vals[pos]), accum_val) |
Ved-vampir | 602a517 | 2015-03-16 15:22:36 +0300 | [diff] [blame] | 39 | return results |
| 40 | |