Completed
Branch master (e60537)
by Kenny
01:27
created

VmStat.check()   B

Complexity

Conditions 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
c 1
b 0
f 1
dl 0
loc 30
rs 8.5806
1
# -*- coding: utf-8 -*-
2
3
__author__ = 'Kenny Freeman'
4
__email__ = '[email protected]'
5
__license__ = "ISCL"
6
__docformat__ = 'reStructuredText'
7
8
import time
9
from collections import deque
10
11
import plumd
12
import plumd.plugins
13
from plumd.calc import Differential
14
from plumd.util import get_file_list
15
16
17
class VmStat(plumd.plugins.Reader):
18
    """Plugin to measure various kernel metrics from /proc/vmstat."""
19
    defaults = {
20
        'poll.interval': 10,
21
        'proc_path': '/proc',
22
        # awk '{ print "\"" $1 "\"," }' /proc/vmstat
23
        'proc_vmstat_record': ["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
    }
42
43
    def __init__(self, log, config):
44
        """Plugin to measure various kernel metrics from /proc/vmstat.
45
46
        :param log: A logger
47
        :type log: logging.RootLogger
48
        :param config: a plumd.config.Conf configuration helper instance.
49
        :type config: plumd.config.Conf
50
        """
51
        super(VmStat, self).__init__(log, config)
52
        self.config.defaults(VmStat.defaults)
53
        self.calc = Differential()
54
        self.proc_file = "{0}/vmstat".format(config.get('proc_path'))
55
        self.record = self.config.get('proc_vmstat_record')
56
57
58
    def poll(self):
59
        """Poll for kernel metrics under proc file net/netstat.
60
61
        :rtype: ResultSet
62
        """
63
        return plumd.ResultSet(self.check())
64
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
        record = self.record
73
        result = plumd.Result("vmstat")
74
75
        # read the proc file
76
        dat = {}
77
        with open(self.proc_file, 'r') as f:
78
            # get a list of values: metric, val, metric, val, etc
79
            vals = f.read().strip().split()
80
            # every second item starting at 0, every second item starting at 1
81
            # take that and put into a dict
82
            dat = dict(zip(vals[0::2], vals[1::2]))
83
84
        # timestamp for Differential calculations
85
        #ts = time.time()
86
87
        # now, we have a list of items to record, just need to record them
88
        for metric in record:
89
            if metric in dat:
90
                #mval = self.calc.per_second(mstr, int(values[mname]), ts)
91
                result.add(plumd.Int(metric, dat[metric]))
92
            else:
93
                self.log.warn("netstat: unknown metric {0}".format(metric))
94
95
        return [result]
96