1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
__author__ = 'Kenny Freeman' |
4
|
|
|
__email__ = '[email protected]' |
5
|
|
|
__license__ = "ISCL" |
6
|
|
|
__docformat__ = 'reStructuredText' |
7
|
|
|
|
8
|
|
|
import time |
9
|
|
|
|
10
|
|
|
import plumd |
11
|
|
|
import plumd.plugins |
12
|
|
|
from plumd.calc import Differential |
13
|
|
|
from plumd.util import get_file_map, Filter |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class Mem(plumd.plugins.Reader): |
17
|
|
|
"""Plugin to measure various kernel metrics from /proc.""" |
18
|
|
|
defaults = { |
19
|
|
|
'poll.interval': 10, |
20
|
|
|
'proc_path': '/proc', |
21
|
|
|
'skip_proc_meminfo': ["Active(anon)","Active(file)", |
22
|
|
|
"AnonHugePages","AnonPages","Bounce","DirectMap2M","CommitLimit", |
23
|
|
|
"DirectMap4k","HugePages_Free","Hugepagesize", |
24
|
|
|
"HugePages_Rsvd","HugePages_Surp","HugePages_Total", |
25
|
|
|
"Inactive","Inactive(anon)","Inactive(file)", |
26
|
|
|
"KernelStack","NFS_Unstable","PageTables", |
27
|
|
|
"Shmem","Slab","SReclaimable","SUnreclaim", |
28
|
|
|
"SwapCached","SwapFree","SwapTotal","Writeback","WritebackTmp"] |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
def __init__(self, log, config): |
32
|
|
|
"""Plugin to measure various kernel metrics from /proc. |
33
|
|
|
|
34
|
|
|
:param log: A logger |
35
|
|
|
:type log: logging.RootLogger |
36
|
|
|
:param config: a plumd.config.Conf configuration helper instance. |
37
|
|
|
:type config: plumd.config.Conf |
38
|
|
|
""" |
39
|
|
|
super(Mem, self).__init__(log, config) |
40
|
|
|
self.config.defaults(Mem.defaults) |
41
|
|
|
self.calc = Differential() |
42
|
|
|
self.proc_file = "{0}/meminfo".format(config.get('proc_path')) |
43
|
|
|
self.filter = Filter() |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def poll(self): |
47
|
|
|
"""Poll for kernel metrics under /proc. |
48
|
|
|
|
49
|
|
|
:rtype: ResultSet |
50
|
|
|
""" |
51
|
|
|
return plumd.ResultSet(self.check()) |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def check(self): |
55
|
|
|
"""Return memory utilization metrics from proc file mem. |
56
|
|
|
|
57
|
|
|
:rtype: plumd.Result |
58
|
|
|
""" |
59
|
|
|
skip = self.config.get('skip_proc_meminfo') |
60
|
|
|
result = plumd.Result("mem") |
61
|
|
|
# read and process /proc/stat |
62
|
|
|
dat = get_file_map(self.proc_file, 0, 0) |
63
|
|
|
ts = time.time() |
64
|
|
|
# parse |
65
|
|
|
for key, val in dat.items(): |
66
|
|
|
mstr = self.filter.process(key) |
67
|
|
|
# cpu is the only special metric |
68
|
|
|
if mstr in skip: |
69
|
|
|
continue |
70
|
|
|
else: |
71
|
|
|
#mval = dcalc.per_second(key, float(val[0]), ts) |
72
|
|
|
result.add(plumd.Int(mstr, val[0])) |
73
|
|
|
return [result] |
74
|
|
|
|