def __init__(self, log, config): """Plugin to measure various kernel metrics from /proc. :param log: A logger :type log: logging.RootLogger :param config: a plumd.config.Conf configuration helper instance. :type config: plumd.config.Conf """ super(Mem, self).__init__(log, config) self.config.defaults(Mem.defaults) self.proc_file = "{0}/meminfo".format(config.get('proc_path')) self.filter = Filter() self.gauges = self.config.get('proc_meminfo_gauges')
class Mem(plumd.Reader): """Plugin to measure various kernel metrics from /proc.""" defaults = { 'poll.interval': 10, 'proc_path': '/proc', 'proc_meminfo_gauges': ["MemTotal", "MemFree", "MemAvailable", "Buffers", "Cached", "Active", "Inactive", "Unevictable", "Mlocked", "SwapTotal", "SwapFree", "Dirty", "Mapped", "Slab", "SReclaimable", "SUnreclaim", "CommitLimit", "Committed_AS", "VmallocTotal", "VmallocUsed", "VmallocChunk", "HardwareCorrupted"] } def __init__(self, log, config): """Plugin to measure various kernel metrics from /proc. :param log: A logger :type log: logging.RootLogger :param config: a plumd.config.Conf configuration helper instance. :type config: plumd.config.Conf """ super(Mem, self).__init__(log, config) self.config.defaults(Mem.defaults) self.proc_file = "{0}/meminfo".format(config.get('proc_path')) self.filter = Filter() self.gauges = self.config.get('proc_meminfo_gauges') def poll(self): """Poll for kernel metrics under /proc. :rtype: ResultSet """ return plumd.ResultSet(self.check()) def check(self): """Return memory utilization metrics from proc file mem. :rtype: plumd.Result """ result = plumd.Result("mem") dat = [] metrics = {} # read and process /proc/meminfo with open(self.proc_file, 'r') as f: dat = f.read().strip().split("\n") # list of eg. ['MemTotal: 7902804 kB', ...] for line in dat: vals = line.split() name = vals[0][:-1] val = vals[1] metrics[name] = val # now record each metric configured for mname in self.gauges: if mname not in metrics: self.log.error("proc: mem: unknown metric: {0}".format(mname)) self.gauges.remove(mname) continue mstr = self.filter.process(mname) result.add(plumd.Int(mstr, metrics[mname])) return [result]