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(DiskStats, self).__init__(log, config) config.defaults(DiskStats.defaults) self.calc = Differential() self.proc_file = "{0}/diskstats".format(config.get("proc_path")) self.diskstats_dev_re = re.compile(config.get("diskstats_dev_re")) self.diskstats_cols = self.config.get("diskstats_cols") self.devices = [] # list of device names to record metrics for self.enabled = True # get list of available devices: dat = get_file_map(self.proc_file, 2, 0) # key is the device name, exclude ones matching the re for key, val in dat.items(): if not self.diskstats_dev_re.match(key): self.devices.append(key) # check format of proc file ncols = len(get_file(self.proc_file).split("\n")[0].split()) self.enabled = ncols == DiskStats.proc_colums if not self.enabled: msg = "DiskStats: invalid format: {0} has {1} cols, not {2}" self.log.error(msg.format(self.proc_file, ncols, DiskStats.proc_colums))
def check(self): """Return cpu utilization and process metrics from proc file stat. :rtype: collections.deque """ results = deque() result = plumd.Result("stat") dat = get_file_map(self.proc_file, 0, 0) ts = time.time() # record gauges for i, metric in enumerate(self.gauges): if metric not in dat: self.log.warn("stat: unknown metric {0}".format(metric)) del (self.gauges[i]) continue result.add(plumd.Int(metric, dat[metric][0])) # record rates for i, metric in enumerate(self.rates): if metric not in dat: self.log.warn("stat: unknown metric {0}".format(metric)) del (self.rates[i]) continue mval = self.calc.per_second(metric, float(dat[metric][0]), ts) result.add(plumd.Int(metric, mval)) # record cpu if "cpu" in dat: results.append(self.proc_stat_cpu("cpu", "cpu", dat["cpu"], ts)) # record each cpu if configured if self.per_cpu: for i in xrange(0, len(dat)): mstr = "cpu{0}".format(i) if mstr not in dat: break results.append(self.proc_stat_cpu("cpus", mstr, dat[mstr])) results.append(result) return results
def check(self): """Return disk io metrics from proc file diskstats. :rtype: plumd.Result """ # times in ms cols = self.diskstats_cols result = plumd.Result("diskstats") if not self.enabled: return [result] dat = {} # read and process /proc/diskstats dat = get_file_map(self.proc_file, 2, 0) ts = time.time() for dev in self.devices: if dev not in dat: continue for mname in cols: mval = float(dat[dev].popleft()) mstr = "{0}.{1}".format(dev, mname) dval = self.calc.per_second(mstr, mval, ts) result.add(plumd.Float(mstr, dval)) return [result]