cf_metric

Python gmetric code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys, os, time
import re
import datetime

# Adjust to match your site configuration
DEBUG = 0
PIDFILE = '/var/cfengine/ganglia/cf_metric.pid'
GMETRIC = '/var/cfengine/ganglia/bin/gmetric'
CF_PROMISE_FILE = '/var/cfengine/promise_summary.log'
#1360856794,1360856795: Outcome of version [promises $Rev: 658 $ 3.4.2] (agent-0): Promises observed to be kept 88%, Promises repaired 3%, Promises not repaired 9%
r = re.compile('^([0-9]+?),([0-9]+?):.*?\[(.+?)\s\$Rev:\s([0-9]+?)\s\$\s([0-9.]+?)\].*?([0-9]{1,3})%.*?(\d{1,3})%.*?(\d{1,3})%')

def get_ib_stats():
  s0 = s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = 0
  f = open( CF_PROMISE_FILE,"r" )
  l = f.readlines()
  f.close()
  m = r.match(l[-1])
  s0 = datetime.datetime.fromtimestamp(int(m.groups()[1])).strftime('%d %b %Y %T %z')
  s1 = int(m.groups()[1]) - int(m.groups()[0])
  if ( m.groups()[2] == 'promises' ):
    s2 = 0
  elif ( m.groups()[2] == 'update' ):
    s2 = 1
  elif ( m.groups()[2] == 'failsafe' ):
    s2 = 2
  else:
    s2 = 3
  s3 = m.groups()[3]
  s4 = m.groups()[4].replace(".", "")
  s5 = m.groups()[5]
  s6 = m.groups()[6]
  s7 = m.groups()[7]
  if (DEBUG):
    print (s0,s1,s2,s3,s4,s5,s6,s7)
  return (s0,s1,s2,s3,s4,s5,s6,s7)

def main():
        oldtime = time.time()
        os.putenv('LD_LIBRARY_PATH','/var/cfengine/ganglia/lib')
        while True:
                time.sleep(60)
                newtime = time.time()
                metric = get_ib_stats()
                # 180 seconds averaging. Adjust if necessary.
                if (newtime - oldtime) >= 180.0:
                        os.spawnl(os.P_WAIT, GMETRIC, 'gmetric',
                                '--name=cf_last',
                                '--group=cfengine',
                                '--conf=/var/cfengine/ganglia/etc/gmond.conf',
                                '--value=%s' % (metric[0]),
                                '--type=string')
                        os.spawnl(os.P_WAIT, GMETRIC, 'gmetric',
                                '--name=cf_state',
                                '--slope=both',
                                '--group=cfengine',
                                '--conf=/var/cfengine/ganglia/etc/gmond.conf',
                                '--value=%i' % int(metric[2]),
                                '--type=float',
                                '--units=state')
                        os.spawnl(os.P_WAIT, GMETRIC, 'gmetric',
                                '--name=cf_release',
                                '--slope=both',
                                '--group=cfengine',
                                '--conf=/var/cfengine/ganglia/etc/gmond.conf',
                                '--value=%i' % int(metric[4]),
                                '--type=float',
                                '--units=cfengine version')
                        os.spawnl(os.P_WAIT, GMETRIC, 'gmetric',
                                '--name=cf_duration',
                                '--slope=both',
                                '--group=cfengine',
                                '--conf=/var/cfengine/ganglia/etc/gmond.conf',
                                '--value=%s' % int(metric[1]),
                                '--type=int16',
                                '--units=s')
                        os.spawnl(os.P_WAIT, GMETRIC, 'gmetric',
                                '--name=cf_kept',
                                '--slope=both',
                                '--group=cfengine',
                                '--conf=/var/cfengine/ganglia/etc/gmond.conf',
                                '--value=%i' % int(metric[5]),
                                '--type=float',
                                '--units=%')
                        os.spawnl(os.P_WAIT, GMETRIC, 'gmetric',
                                '--name=cf_repaired',
                                '--slope=both',
                                '--group=cfengine',
                                '--conf=/var/cfengine/ganglia/etc/gmond.conf',
                                '--value=%i' % int(metric[6]),
                                '--type=float',
                                '--units=%')
                        os.spawnl(os.P_WAIT, GMETRIC, 'gmetric',
                                '--name=cf_not_repaired',
                                '--slope=both',
                                '--group=cfengine',
                                '--conf=/var/cfengine/ganglia/etc/gmond.conf',
                                '--value=%i' % int(metric[7]),
                                '--type=float',
                                '--units=%')
                        os.spawnl(os.P_WAIT, GMETRIC, 'gmetric',
                                '--name=cf_svn_release',
                                '--slope=both',
                                '--group=cfengine',
                                '--conf=/var/cfengine/ganglia/etc/gmond.conf',
                                '--value=%i' % int(metric[3]),
                                '--type=float',
                                '--units=%')
                        oldtime = newtime
# Double-fork daemonization
if __name__ == "__main__":
  if not (DEBUG):
    try:
      pid = os.fork()
      if pid > 0:
        # exit first parent
        sys.exit(0)
    except OSError, e:
      sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
      sys.exit(1)

    # decouple from parent environment
    os.chdir("/")
    os.setsid()
    os.umask(0)

    # do second fork
    try:
      pid = os.fork()
      if pid > 0:
        # exit from second parent
        sys.exit(0)
    except OSError, e:
      sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
      sys.exit(1)

    # redirect standard file descriptors
    sys.stdout.flush()
    sys.stderr.flush()
    si = file('/dev/null', 'r')
    so = file('/dev/null', 'a+')
    se = file('/dev/null', 'a+', 0)
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())

    # write pidfile
    pid = str(os.getpid())
    file(PIDFILE,'w+').write("%s\n" % pid)
    main()
  else:
    main()