def read_cpu_wait(data=None): collectd.debug("Reading: " + repr(data)) for pid, host in discover().items(): # /var/lib/collectd/rrd/kvm_HOST/cpu_kvm/cpu-wait.rrd M = collectd.Values("gauge") M.host = "kvm_" + host M.plugin = "cpu_kvm" M.type_instance = "cpu_wait" (user, system) = open("/proc/%s/stat" % pid, 'r').readline().split(' ')[15:17] M.values = [int(user) + int(system)] M.dispatch()
def read_cpu(data=None): collectd.debug("Reading: " + repr(data)) for pid, host in discover().items(): # /var/lib/collectd/rrd/kvm_HOST/cpu_kvm/cpu-usage.rrd M = collectd.Values("derive") # or try "counter" M.host = "kvm_" + host M.plugin = "cpu_kvm" M.type_instance = "cpu_usage" # import os # os.sysconf("SC_CLK_TCK") (user, system) = open("/proc/%s/stat" % pid, 'r').readline().split(' ')[13:15] M.values = [int(user) + int(system)] M.dispatch()
def read_memory(data=None): collectd.debug("Reading: " + repr(data)) for pid, host in discover().items(): # /var/lib/collectd/rrd/kvm_HOST/memory_kvm/memory-usage.rrd M = collectd.Values("bytes") M.host = "kvm_" + host M.plugin = "memory_kvm" M.type_instance = "memory_usage" if os.path.exists("/proc/%s/smaps" % pid): # slow but probably exact estimate digester = md5_new() shared, private, pss = 0, 0, 0 F = open("/proc/%s/smaps" % pid, "rb") for line in F.readlines(): digester.update(line) line = line.decode('ascii') if line.startswith("Shared"): shared += int(line.split()[1]) elif line.startswith("Private"): private += int(line.split()[1]) elif line.startswith("Pss"): pss += 0.5 + float(line.split()[1]) F.close() if pss > 0: shared = pss - private M.values = [1024 * int(private + shared)] # in bytes else: # rough, but quick estimate # I'd use `with` statement, but not sure if it's present in Python 2.6 statm = open("/proc/%s/statm" % pid, "rt") S = statm.readline().split() statm.close() statm = S shared = int(statm[2]) * PAGESIZE Rss = int(statm[1]) * PAGESIZE private = Rss - shared M.values = [int(private) + int(shared)] M.dispatch()
def read_io(data=None): collectd.debug("Reading: " + repr(data)) for pid, host in discover().items(): # /var/lib/collectd/rrd/kvm_HOST/io_kvm/io-{read,write}.rrd M_read = collectd.Values("counter") M_read.host = "kvm_" + host M_read.plugin = "io_kvm" M_read.type_instance = "io_read" M_write = collectd.Values("counter") M_write.host = "kvm_" + host M_write.plugin = "io_kvm" M_write.type_instance = "io_write" for line in open("/proc/%s/io" % pid, "r"): if "read_bytes" in line: M_read.values = [int(line.strip().split()[1])] elif "write_bytes" in line: M_write.values = [int(line.strip().split()[1])] M_read.dispatch() M_write.dispatch()
def read_net(data=None): collectd.debug("Reading: " + repr(data)) for pid, host in discover().items(): nics = discover_nic(host) if len(nics) < 1: continue # /var/lib/collectd/rrd/kvm_HOST/net_kvm/net-{in,out}.rrd M_in = collectd.Values("counter") M_in.host = "kvm_" + host M_in.plugin = "net_kvm" M_in.type_instance = "net_in" M_out = collectd.Values("counter") M_out.host = "kvm_" + host M_out.plugin = "net_kvm" M_out.type_instance = "net_out" for line in open("/proc/net/dev", "r"): if nics[0] in line: s = line.strip().split()[1:] M_in.values = [s[0]] M_out.values = [s[8]] M_in.dispatch() M_out.dispatch()