1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
__author__ = 'Kenny Freeman' |
4
|
|
|
__email__ = '[email protected]' |
5
|
|
|
__license__ = "ISCL" |
6
|
|
|
__docformat__ = 'reStructuredText' |
7
|
|
|
|
8
|
|
|
import os |
9
|
|
|
import time |
10
|
|
|
import traceback |
11
|
|
|
import multiprocessing |
12
|
|
|
|
13
|
|
|
import plumd |
14
|
|
|
import plumd.plugins |
15
|
|
|
from plumd.util import get_file |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class Uptime(plumd.plugins.Reader): |
19
|
|
|
"""Plugin to measure various kernel metrics from /proc.""" |
20
|
|
|
defaults = { |
21
|
|
|
'poll.interval': 10, |
22
|
|
|
'proc_path': '/proc' |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
def __init__(self, log, config): |
26
|
|
|
"""Plugin to measure kernel metrics from /proc/uptime. |
27
|
|
|
|
28
|
|
|
:param log: A logger |
29
|
|
|
:type log: logging.RootLogger |
30
|
|
|
:param config: a plumd.config.Conf configuration helper instance. |
31
|
|
|
:type config: plumd.config.Conf |
32
|
|
|
""" |
33
|
|
|
super(Uptime, self).__init__(log, config) |
34
|
|
|
self.config.defaults(Uptime.defaults) |
35
|
|
|
self.proc_file = "{0}/uptime".format(config.get('proc_path')) |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def poll(self): |
39
|
|
|
"""Return uptime from proc file swap. |
40
|
|
|
|
41
|
|
|
:rtype: plumd.ResultSet |
42
|
|
|
""" |
43
|
|
|
return plumd.ResultSet(self.check()) |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def check(self): |
47
|
|
|
"""Return uptime from proc file swap. |
48
|
|
|
|
49
|
|
|
:rtype: list |
50
|
|
|
""" |
51
|
|
|
result = plumd.Result("uptime") |
52
|
|
|
# read and process /proc/stat |
53
|
|
|
try: |
54
|
|
|
up, idle = get_file(self.proc_file).split() |
55
|
|
|
except Exception as e: |
56
|
|
|
tb = traceback.format_exc() |
57
|
|
|
self.log.error("proc_uptime: exception: {0}: {1}".format(e, tb)) |
58
|
|
|
return plumd.ResultSet([]) |
59
|
|
|
pidle = float(idle)/float(up) * 100 / multiprocessing.cpu_count() |
60
|
|
|
result.add(plumd.Float("up", up)) |
61
|
|
|
result.add(plumd.Float("idle", idle)) |
62
|
|
|
result.add(plumd.Float("idle_percent", pidle)) |
63
|
|
|
return [result] |
64
|
|
|
|