1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
__author__ = 'Kenny Freeman' |
4
|
|
|
__email__ = '[email protected]' |
5
|
|
|
__license__ = "ISCL" |
6
|
|
|
__docformat__ = 'reStructuredText' |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
import plumd |
10
|
|
|
import plumd.plugins |
11
|
|
|
from plumd.calc import Differential |
12
|
|
|
from plumd.util import get_file_map, Filter |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class Mem(plumd.plugins.Reader): |
16
|
|
|
"""Plugin to measure various kernel metrics from /proc.""" |
17
|
|
|
defaults = { |
18
|
|
|
'poll.interval': 10, |
19
|
|
|
'proc_path': '/proc', |
20
|
|
|
'proc_meminfo_record': ["MemTotal", "MemFree", "MemAvailable", |
21
|
|
|
"Buffers", "Cached", "Active", "Inactive", |
22
|
|
|
"Unevictable", "Mlocked", "SwapTotal", |
23
|
|
|
"SwapFree", "Dirty", "Mapped", "Slab", |
24
|
|
|
"SReclaimable", "SUnreclaim", "CommitLimit", |
25
|
|
|
"Committed_AS", "VmallocTotal", "VmallocUsed", |
26
|
|
|
"VmallocChunk", "HardwareCorrupted"] |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
def __init__(self, log, config): |
30
|
|
|
"""Plugin to measure various kernel metrics from /proc. |
31
|
|
|
|
32
|
|
|
:param log: A logger |
33
|
|
|
:type log: logging.RootLogger |
34
|
|
|
:param config: a plumd.config.Conf configuration helper instance. |
35
|
|
|
:type config: plumd.config.Conf |
36
|
|
|
""" |
37
|
|
|
super(Mem, self).__init__(log, config) |
38
|
|
|
self.config.defaults(Mem.defaults) |
39
|
|
|
self.calc = Differential() |
40
|
|
|
self.proc_file = "{0}/meminfo".format(config.get('proc_path')) |
41
|
|
|
self.filter = Filter() |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def poll(self): |
45
|
|
|
"""Poll for kernel metrics under /proc. |
46
|
|
|
|
47
|
|
|
:rtype: ResultSet |
48
|
|
|
""" |
49
|
|
|
return plumd.ResultSet(self.check()) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def check(self): |
53
|
|
|
"""Return memory utilization metrics from proc file mem. |
54
|
|
|
|
55
|
|
|
:rtype: plumd.Result |
56
|
|
|
""" |
57
|
|
|
record = self.config.get('proc_meminfo_record') |
58
|
|
|
result = plumd.Result("mem") |
59
|
|
|
dat = [] |
60
|
|
|
metrics = {} |
61
|
|
|
# read and process /proc/meminfo |
62
|
|
|
with open(self.proc_file, 'r') as f: |
63
|
|
|
dat = f.read().strip().split("\n") |
64
|
|
|
|
65
|
|
|
# list of eg. ['MemTotal: 7902804 kB', ...] |
66
|
|
|
for line in dat: |
67
|
|
|
vals = line.split() |
68
|
|
|
name = vals[0][:-1] |
69
|
|
|
val = vals[1] |
70
|
|
|
metrics[name] = val |
71
|
|
|
|
72
|
|
|
# now record each metric configured |
73
|
|
|
for mname in record: |
74
|
|
|
if mname not in metrics: |
75
|
|
|
self.log.error("proc: mem: unknown metric: {0}".format(mname)) |
76
|
|
|
continue |
77
|
|
|
mstr = self.filter.process(mname) |
78
|
|
|
result.add(plumd.Int(mstr, metrics[mname])) |
79
|
|
|
|
80
|
|
|
return [result] |
81
|
|
|
|