1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
"""Reader plugin to record vmstat metrics from /proc on linux.""" |
3
|
|
|
|
4
|
|
|
import time |
5
|
|
|
|
6
|
|
|
import plumd |
7
|
|
|
from plumd.util import Differential |
8
|
|
|
from plumd.util import get_file_list |
|
|
|
|
9
|
|
|
|
10
|
|
|
__author__ = 'Kenny Freeman' |
11
|
|
|
__email__ = '[email protected]' |
12
|
|
|
__license__ = "ISCL" |
13
|
|
|
__docformat__ = 'reStructuredText' |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
class VmStat(plumd.Reader): |
17
|
|
|
"""Plugin to measure various kernel metrics from /proc/vmstat.""" |
18
|
|
|
|
19
|
|
|
defaults = { |
20
|
|
|
'poll.interval': 10, |
21
|
|
|
'proc_path': '/proc', |
22
|
|
|
# awk '{ print "\"" $1 "\"," }' /proc/vmstat |
23
|
|
|
'proc_vmstat_gauges': ["nr_free_pages", "nr_dirtied", "nr_written", |
24
|
|
|
"numa_hit", "numa_miss", "numa_foreign", |
25
|
|
|
"numa_interleave", "numa_local", "numa_other", |
26
|
|
|
"pgpgin", "pgpgout", "pswpin", "pswpout", |
27
|
|
|
"pgalloc_dma", "pgalloc_dma32", "pgalloc_normal", |
28
|
|
|
"pgalloc_movable", "pgfree", "pgactivate", |
29
|
|
|
"pgdeactivate", "pgfault", "pgmajfault", |
30
|
|
|
"slabs_scanned", "kswapd_inodesteal", |
31
|
|
|
"kswapd_low_wmark_hit_quickly", |
32
|
|
|
"kswapd_high_wmark_hit_quickly", |
33
|
|
|
"drop_pagecache", "drop_slab", |
34
|
|
|
"numa_pte_updates", "numa_huge_pte_updates", |
35
|
|
|
"numa_hint_faults", "numa_hint_faults_local", |
36
|
|
|
"numa_pages_migrated", "pgmigrate_success", |
37
|
|
|
"pgmigrate_fail", "compact_migrate_scanned", |
38
|
|
|
"compact_free_scanned", "compact_isolated", |
39
|
|
|
"compact_stall", "compact_fail", |
40
|
|
|
"compact_success"], |
41
|
|
|
'proc_vmstat_rates': [] |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
def __init__(self, log, config): |
45
|
|
|
"""Plugin to measure various kernel metrics from /proc/vmstat. |
46
|
|
|
|
47
|
|
|
:param log: A logger |
48
|
|
|
:type log: logging.RootLogger |
49
|
|
|
:param config: a plumd.config.Conf configuration helper instance. |
50
|
|
|
:type config: plumd.config.Conf |
51
|
|
|
""" |
52
|
|
|
super(VmStat, self).__init__(log, config) |
53
|
|
|
self.config.defaults(VmStat.defaults) |
54
|
|
|
self.calc = Differential() |
55
|
|
|
self.proc_file = "{0}/vmstat".format(config.get('proc_path')) |
56
|
|
|
self.gauges = self.config.get('proc_vmstat_gauges') |
57
|
|
|
self.rates = self.config.get('proc_vmstat_rates') |
58
|
|
|
|
59
|
|
|
def poll(self): |
60
|
|
|
"""Poll for kernel metrics under proc file vmstat. |
61
|
|
|
|
62
|
|
|
:rtype: ResultSet |
63
|
|
|
""" |
64
|
|
|
return plumd.ResultSet(self.check()) |
65
|
|
|
|
66
|
|
|
def check(self): |
67
|
|
|
"""Return metrics from /proc/vmstat. |
68
|
|
|
|
69
|
|
|
:rtype: plumd.Result |
70
|
|
|
""" |
71
|
|
|
# what metrics do we want to record? |
72
|
|
|
result = plumd.Result("vmstat") |
73
|
|
|
|
74
|
|
|
# read the proc file |
75
|
|
|
dat = {} |
76
|
|
|
with open(self.proc_file, 'r') as f: |
77
|
|
|
# get a list of values: metric, val, metric, val, etc |
78
|
|
|
vals = f.read().strip().split() |
79
|
|
|
# every second item starting at 0, every second item starting at 1 |
80
|
|
|
# take that and put into a dict |
81
|
|
|
dat = dict(zip(vals[0::2], vals[1::2])) |
82
|
|
|
|
83
|
|
|
# timestamp for Differential calculations |
84
|
|
|
ts = time.time() |
85
|
|
|
|
86
|
|
|
# now, we have a list of items to record, just need to record them |
87
|
|
|
for i, metric in enumerate(self.gauges): |
88
|
|
|
if metric in dat: |
89
|
|
|
result.add(plumd.Int(metric, dat[metric])) |
90
|
|
|
else: |
91
|
|
|
self.log.warn("vmstat: unknown metric {0}".format(metric)) |
92
|
|
|
del(self.gauges[i]) |
|
|
|
|
93
|
|
|
|
94
|
|
|
# and the rates as well, if any |
95
|
|
|
for i, metric in enumerate(self.rates): |
96
|
|
|
if metric in dat: |
97
|
|
|
mval = self.calc.per_second(metric, int(dat[metric]), ts) |
98
|
|
|
result.add(plumd.Float(metric, mval)) |
99
|
|
|
else: |
100
|
|
|
self.log.warn("vmstat: unknown metric {0}".format(metric)) |
101
|
|
|
del(self.rates[i]) |
|
|
|
|
102
|
|
|
|
103
|
|
|
return [result] |
104
|
|
|
|