http://www.mediawiki.org/wiki/Special:Code/MediaWiki/98827

Revision: 98827
Author:   ben
Date:     2011-10-03 23:10:57 +0000 (Mon, 03 Oct 2011)
Log Message:
-----------
Initial import from http://bitbucket.org/maplebed/ganglios/

Added Paths:
-----------
    trunk/debs/ganglios/
    trunk/debs/ganglios/Makefile
    trunk/debs/ganglios/debian/
    trunk/debs/ganglios/debian/README
    trunk/debs/ganglios/debian/changelog
    trunk/debs/ganglios/debian/compat
    trunk/debs/ganglios/debian/control
    trunk/debs/ganglios/debian/copyright
    trunk/debs/ganglios/debian/dirs
    trunk/debs/ganglios/debian/docs
    trunk/debs/ganglios/debian/ganglios.cron
    trunk/debs/ganglios/debian/postinst
    trunk/debs/ganglios/debian/prerm
    trunk/debs/ganglios/debian/pyversions
    trunk/debs/ganglios/debian/rules
    trunk/debs/ganglios/src/
    trunk/debs/ganglios/src/check_ganglios_disk
    trunk/debs/ganglios/src/check_ganglios_diskio
    trunk/debs/ganglios/src/check_ganglios_generic_value
    trunk/debs/ganglios/src/check_ganglios_memory_v2
    trunk/debs/ganglios/src/ganglia/
    trunk/debs/ganglios/src/ganglia/ganglia_parser.log
    trunk/debs/ganglios/src/ganglia/xmlcache/
    trunk/debs/ganglios/src/ganglia/xmlcache/hosts/
    trunk/debs/ganglios/src/ganglia_parser
    trunk/debs/ganglios/src/ganglia_parser.orig
    trunk/debs/ganglios/src/ganglios/
    trunk/debs/ganglios/src/ganglios/__init__.py
    trunk/debs/ganglios/src/ganglios/ganglios.py

Added: trunk/debs/ganglios/Makefile
===================================================================
--- trunk/debs/ganglios/Makefile                                (rev 0)
+++ trunk/debs/ganglios/Makefile        2011-10-03 23:10:57 UTC (rev 98827)
@@ -0,0 +1,28 @@
+SRCDIR=src
+
+SCRIPTS=src/ganglia_parser
+PLUGINS=src/check_ganglios_disk src/check_ganglios_generic_value 
src/check_ganglios_diskio  src/check_ganglios_memory_v2
+MODULES=src/ganglios/__init__.py src/ganglios/ganglios.py
+
+all:
+
+install:
+       install -d ${DESTDIR}/usr/sbin
+       install -m 0755 ${SCRIPTS} ${DESTDIR}/usr/sbin
+       
+       install -d ${DESTDIR}/usr/lib/nagios/plugins
+       install -m 0755 ${PLUGINS} ${DESTDIR}/usr/lib/nagios/plugins
+       
+       install -d ${DESTDIR}/usr/share/pyshared/ganglios
+       install -m 0644 ${MODULES} ${DESTDIR}/usr/share/pyshared/ganglios
+       
+clean:
+
+deb:
+       debuild -uc -us -i -b
+
+source-deb:
+       debuild -uc -us -i -S
+       
+debclean:
+       debuild clean


Property changes on: trunk/debs/ganglios/Makefile
___________________________________________________________________
Added: svn:eol-style
   + native

Added: trunk/debs/ganglios/debian/README
===================================================================
--- trunk/debs/ganglios/debian/README                           (rev 0)
+++ trunk/debs/ganglios/debian/README   2011-10-03 23:10:57 UTC (rev 98827)
@@ -0,0 +1,37 @@
+##################
+##
+##  Ganglios - Integration of Nagios with Ganglia
+##
+##  This directory contains a number of scripts that make it easy to
+##  configure nagios to take action based on data gathered by ganglia
+##
+##  Written for Linden Lab, copyright 2008
+##  License to modify and redistribute granted under the GPL v2 or later
+##
+##################
+
+
+0. Table of Contents
+1. Overview
+2. Installation
+
+
+1. Overview
+
+This document assumes familiarity with both Ganglia (http://ganglia.info) and 
Nagios (http://www.nagios.org).
+
+Ganglios retrieves metrics from a gmond collector node and stores them in the 
local filesystem, one file per host.  Nagios checks can refer to this data to 
perform checks on a wide range of hosts much more efficiently than SSHing into 
every node.
+
+Your ganglia configuration should have one (or more) gmond nodes that are 
serving as collector nodes.  In a multicast environment, any gmond node will 
do.  This nagios / ganglia integration works by retrieving all the ganglia 
stats from one or more gmond nodes and storing them locally for nagios to 
examine.  The nagios plugins then examine the locally stored files to retrieve 
the relevant statistics, using the included python module that understands the 
on-disk ganglia stats.
+
+Note that you must only point ganglia_parser at multiple gmond collector nodes 
if they have different information, rather than for redundancy.  If you have 
separate clusters in your environment, you should point ganglia_parser to one 
gmond for each cluster.
+
+2. Installation
+
+On your nagios host:
+2a. modify the listXMLSources() function in ganglia_parser to correctly return 
a list of your gmond collector nodes.
+2b. install ganglia_parser in /usr/local/sbin/ (or in a location of your 
choice)
+2c. install the 'ganglios' python module in 
/usr/local/lib/python2.3/site-packages/ganglios/
+2d. install the cronjob (ganglios.cron) to collect ganglia data in 
/etc/cron.d/  Set the user as whom this cronjob runs to the owner of 
/var/lib/ganglia/
+
+At this point, you should be populating /var/lib/ganglia/xmlcache/ every 2 
minutes.  Examine this data to make sure it seems right.  You should have 
/var/lib/ganglia/xmlcache/<hostname>.xml (one for each collector node) and 
/var/lib/ganglia/xmlcache/hosts/<hostname>

Added: trunk/debs/ganglios/debian/changelog
===================================================================
--- trunk/debs/ganglios/debian/changelog                                (rev 0)
+++ trunk/debs/ganglios/debian/changelog        2011-10-03 23:10:57 UTC (rev 
98827)
@@ -0,0 +1,5 @@
+ganglios (1.0-1) stable; urgency=low
+
+  * Initial release
+
+ -- Lex Linden <l...@lindenlab.com>  Mon, 26 Oct 2009 10:17:24 -0400

Added: trunk/debs/ganglios/debian/compat
===================================================================
--- trunk/debs/ganglios/debian/compat                           (rev 0)
+++ trunk/debs/ganglios/debian/compat   2011-10-03 23:10:57 UTC (rev 98827)
@@ -0,0 +1 @@
+5

Added: trunk/debs/ganglios/debian/control
===================================================================
--- trunk/debs/ganglios/debian/control                          (rev 0)
+++ trunk/debs/ganglios/debian/control  2011-10-03 23:10:57 UTC (rev 98827)
@@ -0,0 +1,19 @@
+Source: ganglios
+Section: net
+Priority: optional
+Maintainer: Lex Linden <l...@lindenlab.com>
+Build-Depends: debhelper (>= 5), python-support
+Standards-Version: 3.7.2
+
+Package: ganglios
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}, ${python:Depends}, nagios3
+Description: Scripts to help nagios act on data from ganglia
+ Ganglios retrieves metrics from a gmond collector node and stores them in the
+ local filesystem, one file per host.  Nagios checks can refer to this data to
+ perform checks on a wide range of hosts much more efficiently than SSHing into
+ every node.
+ .
+ Ganglios includes a Python module, ganglios.py, to make acting on this data
+ simpler.  Example nagios checks are included to show how to use the data
+ ganglios retrieves.

Added: trunk/debs/ganglios/debian/copyright
===================================================================
--- trunk/debs/ganglios/debian/copyright                                (rev 0)
+++ trunk/debs/ganglios/debian/copyright        2011-10-03 23:10:57 UTC (rev 
98827)
@@ -0,0 +1,18 @@
+This package was debianized by Lex Linden <l...@lindenlab.com> on
+Mon, 26 Oct 2009 10:17:24 -0400.
+
+Copyright:
+
+    Copyright (C) 2008 Linden Lab
+
+License:
+    Released under the GPL v2 or later.
+    For a full description of the license, please examine:
+    /usr/share/common-licenses/GPL-2
+
+The Debian packaging is:
+
+    Copyright (C) 2009 Linden Lab
+
+and is licensed under the GPL v2 or later
+

Added: trunk/debs/ganglios/debian/dirs
===================================================================
--- trunk/debs/ganglios/debian/dirs                             (rev 0)
+++ trunk/debs/ganglios/debian/dirs     2011-10-03 23:10:57 UTC (rev 98827)
@@ -0,0 +1,3 @@
+usr/sbin
+usr/lib/nagios/plugins
+usr/shared/pyshared/ganglios

Added: trunk/debs/ganglios/debian/docs
===================================================================
--- trunk/debs/ganglios/debian/docs                             (rev 0)
+++ trunk/debs/ganglios/debian/docs     2011-10-03 23:10:57 UTC (rev 98827)
@@ -0,0 +1,2 @@
+debian/README
+debian/ganglios.cron
\ No newline at end of file

Added: trunk/debs/ganglios/debian/ganglios.cron
===================================================================
--- trunk/debs/ganglios/debian/ganglios.cron                            (rev 0)
+++ trunk/debs/ganglios/debian/ganglios.cron    2011-10-03 23:10:57 UTC (rev 
98827)
@@ -0,0 +1,6 @@
+#
+#  update /var/lib/ganglia/xmlcache/ every 2 minutes
+#
+
+*/2 * * * * nagios /usr/sbin/ganglia_parser
+

Added: trunk/debs/ganglios/debian/postinst
===================================================================
--- trunk/debs/ganglios/debian/postinst                         (rev 0)
+++ trunk/debs/ganglios/debian/postinst 2011-10-03 23:10:57 UTC (rev 98827)
@@ -0,0 +1,39 @@
+#!/bin/sh
+# postinst script for debiandir
+#
+# see: dh_installdeb(1)
+
+set -e
+
+# summary of how this script can be called:
+#        * <postinst> `configure' <most-recently-configured-version>
+#        * <old-postinst> `abort-upgrade' <new version>
+#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
+#          <new-version>
+#        * <postinst> `abort-remove'
+#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
+#          <failed-install-package> <version> `removing'
+#          <conflicting-package> <version>
+# for details, see http://www.debian.org/doc/debian-policy/ or
+# the debian-policy package
+
+
+case "$1" in
+    configure)
+    ;;
+
+    abort-upgrade|abort-remove|abort-deconfigure)
+    ;;
+
+    *)
+        echo "postinst called with unknown argument \`$1'" >&2
+        exit 1
+    ;;
+esac
+
+# dh_installdeb will replace this with shell code automatically
+# generated by other debhelper scripts.
+
+#DEBHELPER#
+
+exit 0

Added: trunk/debs/ganglios/debian/prerm
===================================================================
--- trunk/debs/ganglios/debian/prerm                            (rev 0)
+++ trunk/debs/ganglios/debian/prerm    2011-10-03 23:10:57 UTC (rev 98827)
@@ -0,0 +1,38 @@
+#!/bin/sh
+# prerm script for debiandir
+#
+# see: dh_installdeb(1)
+
+set -e
+
+# summary of how this script can be called:
+#        * <prerm> `remove'
+#        * <old-prerm> `upgrade' <new-version>
+#        * <new-prerm> `failed-upgrade' <old-version>
+#        * <conflictor's-prerm> `remove' `in-favour' <package> <new-version>
+#        * <deconfigured's-prerm> `deconfigure' `in-favour'
+#          <package-being-installed> <version> `removing'
+#          <conflicting-package> <version>
+# for details, see http://www.debian.org/doc/debian-policy/ or
+# the debian-policy package
+
+
+case "$1" in
+    remove|upgrade|deconfigure)
+    ;;
+
+    failed-upgrade)
+    ;;
+
+    *)
+        echo "prerm called with unknown argument \`$1'" >&2
+        exit 1
+    ;;
+esac
+
+# dh_installdeb will replace this with shell code automatically
+# generated by other debhelper scripts.
+
+#DEBHELPER#
+
+exit 0

Added: trunk/debs/ganglios/debian/pyversions
===================================================================
--- trunk/debs/ganglios/debian/pyversions                               (rev 0)
+++ trunk/debs/ganglios/debian/pyversions       2011-10-03 23:10:57 UTC (rev 
98827)
@@ -0,0 +1 @@
+2.4-2.6

Added: trunk/debs/ganglios/debian/rules
===================================================================
--- trunk/debs/ganglios/debian/rules                            (rev 0)
+++ trunk/debs/ganglios/debian/rules    2011-10-03 23:10:57 UTC (rev 98827)
@@ -0,0 +1,91 @@
+#!/usr/bin/make -f
+# -*- makefile -*-
+# Sample debian/rules that uses debhelper.
+# This file was originally written by Joey Hess and Craig Small.
+# As a special exception, when this file is copied by dh-make into a
+# dh-make output file, you may use that output file without restriction.
+# This special exception was added by Craig Small in version 0.37 of dh-make.
+
+# Uncomment this to turn on verbose mode.
+#export DH_VERBOSE=1
+
+
+
+
+
+configure: configure-stamp
+configure-stamp:
+       dh_testdir
+       # Add here commands to configure the package.
+
+       touch configure-stamp
+
+
+build: build-stamp
+
+build-stamp: configure-stamp  
+       dh_testdir
+
+       # Add here commands to compile the package.
+       $(MAKE)
+       #docbook-to-man debian/ganglios.sgml > ganglios.1
+
+       touch $@
+
+clean: 
+       dh_testdir
+       dh_testroot
+       rm -f build-stamp configure-stamp
+
+       # Add here commands to clean up after the build process.
+       $(MAKE) clean
+
+       dh_clean 
+
+install: build
+       dh_testdir
+       dh_testroot
+       # dh_prep  
+       dh_installdirs
+
+       # Add here commands to install the package into debian/ganglios.
+       $(MAKE) DESTDIR=$(CURDIR)/debian/ganglios install
+
+
+# Build architecture-independent files here.
+binary-indep: install
+# We have nothing to do by default.
+
+# Build architecture-dependent files here.
+binary-arch: install
+       dh_testdir
+       dh_testroot
+       dh_installchangelogs 
+       dh_installdocs
+       dh_installexamples
+#      dh_install
+#      dh_installmenu
+#      dh_installdebconf
+#      dh_installlogrotate
+#      dh_installemacsen
+#      dh_installpam
+#      dh_installmime
+       dh_pysupport
+#      dh_installinit
+#      dh_installcron
+#      dh_installinfo
+       dh_installman
+       dh_link
+       dh_strip
+       dh_compress
+       dh_fixperms
+#      dh_perl
+#      dh_makeshlibs
+       dh_installdeb
+       dh_shlibdeps
+       dh_gencontrol
+       dh_md5sums
+       dh_builddeb
+
+binary: binary-indep binary-arch
+.PHONY: build clean binary-indep binary-arch binary install configure


Property changes on: trunk/debs/ganglios/debian/rules
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/debs/ganglios/src/check_ganglios_disk
===================================================================
--- trunk/debs/ganglios/src/check_ganglios_disk                         (rev 0)
+++ trunk/debs/ganglios/src/check_ganglios_disk 2011-10-03 23:10:57 UTC (rev 
98827)
@@ -0,0 +1,149 @@
+#!/usr/bin/python -tt
+# 
+# Copyright (C) 2009 Linden Lab
+#
+# Released under the GPL v2 or later.  For a full description of the license,
+# please visit http://www.gnu.org/licenses/gpl-2.0.html
+#
+
+"""
+@file check_ganglios_disk
+@brief look for over-full disks
+
+Copyright (c) 2007, Linden Research, Inc.
+$License$
+$Id: check_ganglios_disk,v 1.12 2008/03/05 21:35:51 benoc Exp $
+"""
+
+import sys
+import ganglios.ganglios as ganglios
+import socket
+
+__revision__ = '0'
+
+
+def main ():
+    """
+    This check examines the 'disk_usage' gangila metric on all hosts.
+
+    Usage:
+      check_ganglios_disk [warn|critical] threshold [include|exclude host list]
+        threshould should be a number, above which the alert will trigger
+        include host list - only check the hosts listed (DEPRECATED - do not 
use)
+        exclude host list - check all hosts *except* those listed.  Useful if 
some unimportant hosts are permanently in the triggered state.
+    """
+    if sys.argv[ 1 ] == 'warn':
+        err_status = 1
+    elif sys.argv[ 1 ] == 'critical':
+        err_status = 2
+    else:
+        print 'bad arguments'
+        sys.exit (2)
+
+    cutoff = int (sys.argv[ 2 ])
+    try:
+        inexcl_flag = sys.argv[ 3 ]
+    except:
+        inexcl_flag = ''
+
+    # i want to be able to list cnames in the nagios config.  turn
+    # each alias into the real hostname (ganglios always reports by
+    # the real host name) for the purpose of matching, but say
+    # the alias in the output of this plugin.
+    incl_excl_hosts = {}
+
+    for host in sys.argv[ 4: ]:
+        if ( not host.endswith('.com') and not host.endswith('.net')):
+            host += '.lindenlab.com'
+
+        if host.find ('%d') >= 0:
+            # a %d in the hostname means to include all hosts with
+            # the name and %d expanded to a number.  pull the
+            # actual names from DNS until we stop seeing them.
+            i = 0
+            missed = 0
+            while True:
+                try:
+                    invdb_name = host % i
+                    invdb_cname = socket.gethostbyname_ex (invdb_name)
+                    incl_excl_hosts[ invdb_cname[ 0 ] ] = invdb_name
+                    incl_excl_hosts[ "int." + invdb_cname[ 0 ] ] = "int." + 
invdb_name
+                    incl_excl_hosts[ "eth0-0." + invdb_cname[ 0 ] ] = 
"eth0-0." + invdb_name
+                    incl_excl_hosts[ "eth1-0." + invdb_cname[ 0 ] ] = 
"eth1-0." + invdb_name
+                    incl_excl_hosts[ "tunnel0." + invdb_cname[ 0 ] ] = 
"tunnel0." + invdb_name
+                    i += 1
+                except socket.gaierror:
+                    missed += 1
+                    i += 1
+                    if missed > 4:
+                        break
+        else:
+            # this is just a hostname (no expansion).  figure out its
+            # real hostname so we can spot its data in the ganglia data
+            try:
+                (name, aliaslist, addresslist) = socket.gethostbyname_ex (host)
+            except socket.gaierror:
+                sys.stdout.write ("CRITICAL: " + host + " does not resolve 
(fix the nagios config)")
+                ganglios.done (2)
+            incl_excl_hosts[ name ] = host
+
+    # incl_excl_hosts now contains a dictionary of {cname => alias} eg. 
{db1.lindenlab.com => mysql.agni.lindenlab.com}
+
+    #####
+    # for name in incl_excl_hosts.keys ():
+    #     print '%s -> %s' % (name, incl_excl_hosts[ name ])
+    # sys.exit (0)
+    #####
+
+    report = {}
+    hostmetrics = {}
+
+    def build_metrics_include( host, metric, value):
+        ''' callback from ganglios.parse_ganglia '''
+        if host in incl_excl_hosts:
+            hostmetrics[ incl_excl_hosts[host] ] = int(value)
+
+    def build_metrics_exclude( host, metric, value):
+        ''' callback from ganglios.parse_ganglia '''
+        if not host in incl_excl_hosts:
+            hostmetrics[ host ] = int(value)
+
+    status = 0 # OK
+    if( inexcl_flag == 'include' ):
+        build_metrics = build_metrics_include
+    else:
+        # either exclude or empty
+        build_metrics = build_metrics_exclude
+
+    # build hostmetrics using build_metrics callback
+    ganglios.parse_ganglia (['disk_usage'], build_metrics)
+
+    # pull out hosts that have metric > cutoff
+    report = dict([
+        (host.replace('.lindenlab.com',''), hostmetrics[host])
+        for host in hostmetrics
+        if hostmetrics[host] >= cutoff
+        ])
+
+    hosts = report.keys ()
+    hosts.sort ()
+    if len (hosts) > 0:
+        sys.stdout.write ('<b>DISK</b>:')
+        for host in hosts:
+            sys.stdout.write ('%s:%s ' % (host, report[ host ]))
+        status = err_status # warn or critical
+
+    ganglios.done (status)
+
+
+if __name__ == "__main__":
+    try:
+        main ()
+    except SystemExit, exp:
+        # this exception is raised when the check exits normally
+        raise exp
+    except:
+        # if anything raises an exception in the test, die critical
+        sys.stdout.write ("CRITICAL: check raised an exception!")
+        ganglios.done (2)
+


Property changes on: trunk/debs/ganglios/src/check_ganglios_disk
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/debs/ganglios/src/check_ganglios_diskio
===================================================================
--- trunk/debs/ganglios/src/check_ganglios_diskio                               
(rev 0)
+++ trunk/debs/ganglios/src/check_ganglios_diskio       2011-10-03 23:10:57 UTC 
(rev 98827)
@@ -0,0 +1,59 @@
+#!/usr/bin/python -tt
+# 
+# Copyright (C) 2009 Linden Lab
+#
+# Released under the GPL v2 or later.  For a full description of the license,
+# please visit http://www.gnu.org/licenses/gpl-2.0.html
+#
+
+"""
+@file check_ganglios_diskio
+@brief look in ganglia for heavy disk-io
+
+Copyright (c) 2007, Linden Research, Inc.
+$License$
+"""
+
+import sys
+import ganglios.ganglios as ganglios
+
+__revision__ = '0'
+
+
+def main ():
+    """
+    checks all hosts for the 'disk_io' metric.  
+    Usage:
+      cehck_ganglios_diskio threshold
+        threshold should be an integer above which hosts will trigger the alert
+    """
+    report = {}
+
+    cutoff = int (sys.argv[ 1 ])
+
+    def thunk (host, metric, value):
+        """ callback """
+        num = int (float (value))
+        if (num > cutoff):
+            report[ host.replace ('.lindenlab.com', '') ] = str (num)
+
+    status = 0 # OK
+    ganglios.parse_ganglia (['disk_io'], thunk)
+
+    hosts = report.keys ()
+    hosts.sort ()
+    if len (hosts) > 0:
+        sys.stdout.write ('<b>disk-IO</b>:')
+        for host in hosts:
+            if not host.startswith ('sim'):
+                sys.stdout.write ('%s:%s ' % (host, report[ host ]))
+        for host in hosts:
+            if host.startswith ('sim'):
+                sys.stdout.write ('%s:%s ' % (host, report[ host ]))
+        status = 1 # warn
+
+    ganglios.done (status)
+
+
+if __name__ == "__main__":
+    main ()


Property changes on: trunk/debs/ganglios/src/check_ganglios_diskio
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/debs/ganglios/src/check_ganglios_generic_value
===================================================================
--- trunk/debs/ganglios/src/check_ganglios_generic_value                        
        (rev 0)
+++ trunk/debs/ganglios/src/check_ganglios_generic_value        2011-10-03 
23:10:57 UTC (rev 98827)
@@ -0,0 +1,93 @@
+#!/usr/bin/python -tt
+# 
+# Copyright (C) 2009 Linden Lab
+#
+# Released under the GPL v2 or later.  For a full description of the license,
+# please visit http://www.gnu.org/licenses/gpl-2.0.html
+#
+
+"""
+@file check_ganglios_generic_value
+@brief a ganglios module that checks a given metric against a reference value
+
+Copyright (c) 2008, Linden Research, Inc.
+$License$
+"""
+
+import sys
+import ganglios.ganglios as ganglios
+import operator
+from optparse import OptionParser
+
+__revision__ = 1;
+
+def main():
+    """Check $host in the ganglia cache that $metric $op than $value
+example:
+check_ganglios_generic_value alan.lindenlab.com disk_usage gt 90
+    this will return CRIT if alan's disk_usage goes above 90
+possible ops:
+    gt ge lt le eq ne
+each means "warn if the $cur_value $op $expected_value
+so passing 'gt 10' means CRIT if the host's value greater than 10
+
+
+    """
+    # actually, any valid function supplied by the 'operator' module that 
return a
+    # boolean will probably work, but the above functions are what I expect to 
use
+
+    parser = OptionParser()
+
+    parser.add_option('-H', dest='hostname', help='hostname')
+    parser.add_option('-m', dest='metric', help='metric to check')
+    parser.add_option('-o', dest='op', help='valid operators are: gt ge lt le 
eq ne')
+    parser.add_option('-w', dest='warning', help='warning threshold for the 
metric')
+    parser.add_option('-c', dest='critical', help='critical threshold for the 
metric')
+    parser.add_option('-g', dest='getmetric', action="store_true", help='only 
return the metric, don\'t do any comparisons')
+
+    options, args = parser.parse_args()
+    host_name = options.hostname
+    metric = options.metric
+    getmetric = options.getmetric
+
+    cur_val_raw = ganglios.get_metric_for_host(host_name, metric)
+    try:
+        cur_val = float(cur_val_raw)
+    except TypeError, e:
+        # ganglios didn't return a number - probably None
+        if (cur_val_raw is None):
+            output = "UNKNOWN - check failed, metric not found"
+        else:
+            output = "UNKNOWN - check failed (returned '%s' when casting 
'%s')" % (e, cur_val_raw)
+        status = 3
+        sys.stdout.write(output)
+        ganglios.done(status)
+
+    if(getmetric):
+        # this feature was added for scripts that want the value for their own 
processing
+        output = "%s" % cur_val
+        sys.stdout.write(output)
+        status = 0
+        ganglios.done(status)
+
+    # if called with -g, these metrics are optional.  Move them here so you 
don't trigger an exception on casting an empty option
+    op = options.op
+    warn_value = float(options.warning)
+    crit_value = float(options.critical)
+
+    if getattr(operator, op)(cur_val, crit_value):
+        status = 2
+        output = "CRITICAL: %s is %s (%s %s)" % (metric, cur_val, op, 
crit_value)
+    elif getattr(operator, op)(cur_val, warn_value):
+        status = 1
+        output = "WARN: %s is %s (%s %s)" % (metric, cur_val, op, warn_value)
+    else:
+        status = 0
+        output = "OK: %s is %s" % (metric, cur_val)
+
+    sys.stdout.write(output)
+    ganglios.done(status)
+
+if __name__ == "__main__":
+    main()
+


Property changes on: trunk/debs/ganglios/src/check_ganglios_generic_value
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/debs/ganglios/src/check_ganglios_memory_v2
===================================================================
--- trunk/debs/ganglios/src/check_ganglios_memory_v2                            
(rev 0)
+++ trunk/debs/ganglios/src/check_ganglios_memory_v2    2011-10-03 23:10:57 UTC 
(rev 98827)
@@ -0,0 +1,95 @@
+#!/usr/bin/python -tt
+# 
+# Copyright (C) 2009 Linden Lab
+#
+# Released under the GPL v2 or later.  For a full description of the license,
+# please visit http://www.gnu.org/licenses/gpl-2.0.html
+#
+
+"""
+@file check_ganglios_
+@brief a ganglios module that checks memory used for a host
+
+Copyright (c) 2008, Linden Research, Inc.
+$License$
+"""
+
+import sys
+import ganglios.ganglios as ganglios
+import operator
+from optparse import OptionParser
+
+__revision__ = 1;
+
+def main():
+    """
+    Check $host in the ganglia uses less than $limit memory
+    This check is interesting because memory used is not reported by ganglia.
+    Only total and memory free etc. are reported.  This module will do the
+    right math to calculate memory used.
+    """
+
+    parser = OptionParser()
+
+    parser.add_option('-H', dest='hostname', help='hostname')
+    parser.add_option('-u', '--used', action='store_true', dest='used', 
default=False, help='check memory used is less than threshold (default)')
+    parser.add_option('-f', '--avail', action='store_true', dest='avail', 
default=False, help='check memory available is greater than threshold')
+    parser.add_option('-w', dest='warning', help='warning threshold for the 
metric')
+    parser.add_option('-c', dest='critical', help='critical threshold for the 
metric')
+
+    options, args = parser.parse_args()
+    host_name = options.hostname
+    warn_value = float(options.warning)
+    crit_value = float(options.critical)
+    if( options.used and options.avail ):
+        # error - both used and avail were set
+        parser.error("options --used and --avail are mutually exclusive")
+
+    mem_total_raw = ganglios.get_metric_for_host(host_name, 'mem_total')
+    mem_shared_raw = ganglios.get_metric_for_host(host_name, 'mem_shared')
+    mem_free_raw = ganglios.get_metric_for_host(host_name, 'mem_free')
+    mem_cached_raw = ganglios.get_metric_for_host(host_name, 'mem_cached')
+    mem_buffers_raw = ganglios.get_metric_for_host(host_name, 'mem_buffers')
+
+    try:
+        mem_total = float(mem_total_raw)
+        mem_shared = float(mem_shared_raw)
+        mem_free = float(mem_free_raw)
+        mem_cached = float(mem_cached_raw)
+        mem_buffers = float(mem_buffers_raw)
+    except TypeError, e:
+        # ganglios didn't return a number - probably None
+        status = 2
+        output = "CRITICAL - check failed (returned '%s' when casting '%s, %s, 
%s, %s, %s')" % (e, mem_total_raw, mem_shared_raw, mem_free_raw, 
mem_cached_raw, mem_buffers_raw)
+        sys.stdout.write(output)
+        ganglios.done(2)
+
+    if( options.free ):
+        # testing against free memory
+        mem = mem_shared + mem_free + mem_cached + mem_buffers
+        metric = 'mem_avail'
+        op = 'lt'
+    else:
+        # testing against used memory.
+        mem = mem_total - mem_shared - mem_free - mem_cached - mem_buffers
+        metric = 'mem_used'
+        op = 'gt'
+
+
+    if getattr(operator, op)(cur_val, crit_value):
+    if mem_used > crit_value:
+        status = 2
+        output = "CRITICAL: %s is %s (op %s)" % (metric, cur_val, op, 
crit_value)
+    elif mem_used > warn_value:
+        status = 1
+        output = "WARN: %s is %s (op %s)" % (metric, cur_val, op, warn_value)
+    else:
+        status = 0
+        output = "OK: %s is %s" % (metric, cur_val)
+
+    sys.stdout.write(output)
+    ganglios.done(status)
+
+if __name__ == "__main__":
+    main()
+


Property changes on: trunk/debs/ganglios/src/check_ganglios_memory_v2
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/debs/ganglios/src/ganglia/ganglia_parser.log
===================================================================
--- trunk/debs/ganglios/src/ganglia/ganglia_parser.log                          
(rev 0)
+++ trunk/debs/ganglios/src/ganglia/ganglia_parser.log  2011-10-03 23:10:57 UTC 
(rev 98827)
@@ -0,0 +1,6 @@
+2011-10-03 23:01:28,936 CRITICAL Starting ganglia_parser
+2011-10-03 23:01:28,962 CRITICAL Failed to rename /tmp/tmp8TIOms to 
./ganglia/xmlcache/spence.xml
+2011-10-03 23:01:28,973 CRITICAL Finished ganglia_parser
+2011-10-03 23:02:42,089 CRITICAL Starting ganglia_parser
+2011-10-03 23:02:42,106 CRITICAL Failed to rename /tmp/tmpTSpsNG to 
./ganglia/xmlcache/spence.xml
+2011-10-03 23:02:42,107 CRITICAL Finished ganglia_parser

Added: trunk/debs/ganglios/src/ganglia_parser
===================================================================
--- trunk/debs/ganglios/src/ganglia_parser                              (rev 0)
+++ trunk/debs/ganglios/src/ganglia_parser      2011-10-03 23:10:57 UTC (rev 
98827)
@@ -0,0 +1,266 @@
+#!/usr/bin/python -tt
+# 
+# Copyright (C) 2009 Linden Lab
+#
+# Released under the GPL v2 or later.  For a full description of the license,
+# please visit http://www.gnu.org/licenses/gpl-2.0.html
+#
+
+
+"""
+Iterates over all the gmond collector nodes and fetch metrics where available.
+Separates metrics into one file per host for easier processing by nagios 
+plugins.
+"""
+
+import sys
+import socket
+import os
+import glob
+import tempfile
+import errno
+import re
+import logging
+
+__revision__ = '0'
+
+dataDir = '/var/lib/ganglia/xmlcache'
+logDir = '/var/log/ganglia'
+logger = logging.getLogger('ganglia_parser')
+hdlr = logging.FileHandler('%s/ganglia_parser.log' % logDir)
+formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
+hdlr.setFormatter(formatter)
+logger.addHandler(hdlr) 
+logger.setLevel(logging.WARNING)
+
+def unionMetrics( curMetrics, hostFilePath ):
+    '''When a single host is present in more than one gmond collector node
+    (because of DNS errors or something else), write out the union of metrics
+    from each collector node, taking the more recent metric when the same
+    metric is present in multiple nodes.'''
+    metrics = dict()
+    metricsAge = dict()
+    # parseMetric grabs the metric name and age
+    parseMetric = re.compile('^<METRIC NAME="([^"]*).*TN="([^"]*)')
+
+    # populate metrics and metricsAge from the existing file
+    oldFileHandle = open(hostFilePath, 'r')
+    fileFirstLine = oldFileHandle.readline()
+    for line in oldFileHandle:
+        regMatch = parseMetric.match(line)
+        if regMatch:
+            met = regMatch.group(1)
+            age = regMatch.group(2)
+            metrics[met] = line
+            metricsAge[met] = age
+        else:
+            # the match failed, which will happen on </HOST> but nowhere else?
+            # theoretically, more logging could go in here
+            pass
+
+    # selectively overwrite metrics with stuff from the buf if it is newer
+    bufFirstLine = curMetrics[0]
+    for line in curMetrics[1:-1]:
+        regMatch = parseMetric.match(line)
+        if regMatch:
+            met = regMatch.group(1)
+            # age represents seconds since the metric was reported.  larger == 
older
+            age = regMatch.group(2)
+            if metrics.has_key(met):
+                try:
+                    if float(age) < float(metricsAge[met]):
+                        # if this metric exists and is older, replace it
+                        metrics[met] = line
+                except Exception, e:
+                    logger.warning("exception caught when comparing TN values: 
%s", e)
+            else:
+                # if the metric doesn't exist, add it
+                metrics[met] = line
+        else:
+            # the match failed, which shouldn't happen (because first and last 
lines are cut by the slice)
+            logger.warning("regMatch failed unexpectedly.  current line: %s" % 
(line))
+
+
+    # compare first lines to see which to use
+    parseHost = re.compile('^<HOST.*TN="([^"]*)')
+    fileAge = parseHost.match(fileFirstLine).group(1)
+    bufAge = parseHost.match(bufFirstLine).group(1)
+    if fileAge < bufAge:
+        firstLine = fileFirstLine
+    else:
+        firstLine = bufFirstLine
+
+    # construct array to pass back to the calling function
+    newBuf = []
+    newBuf.append(firstLine)
+    newBuf.extend(metrics.values())
+    newBuf.append('</HOST>\n')
+
+    # return the union metrics buffer
+    return newBuf
+
+
+def listXMLSources():
+    """ returns a list of hosts to fetch ganglia stats from (the gmond
+    collector nodes).  In a small network, this will likely be a single host.
+    You could simply hardcode the list if there is no coherent naming scheme.
+    In our network, the hosts are named 'nannybot1', 'nannybot2', etc.  This
+    function polls nannybots of increasing number until it finds 3 that don't
+    respond and considers itself done."""
+    i = 0
+    missed = 0
+    nannybots = []
+    logger.info("Retrieving list of nannybots...")
+    while True:
+        try:
+            nannybot_addr = socket.gethostbyname('nannybot%d.lindenlab.com' % 
i)
+            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+            s.settimeout(15.0)
+            s.connect((nannybot_addr, 8649))
+            s.close()
+            nannybots.append('nannybot%d.lindenlab.com' % i)
+            logger.info('Checking nannybot%d.lindenlab.com: [OK]' % i)
+            missed = 0
+        except (socket.gaierror):
+            logger.info('Checking nannybot%d.lindenlab.com: [FAILED]' % i)
+            missed += 1
+            if missed > 3:
+                break
+        except (socket.error, socket.timeout), e:
+            logger.warning('Checking nannybot%d.lindenlab.com: [FAILED] with 
error %s' % (i, e))
+            # a connection error (rather than non-existent) should not count 
towards 
+            # the three missing that mark the end of the nannybots.
+        except Exception, e:
+            logger.critical('Caught unexpected exception while checking 
nannybot%d: %s' % (i, e))
+            logger.critical('EXITING...')
+            raise e
+        i += 1
+
+    return nannybots
+
+def storeXMLData(srcHosts, dataDir):
+    """ Fetch ganglia xml data from remote hosts and store it locally in 
$dataDir """
+
+    logger.info("Retrieving ganglia data from selected hosts...")
+    for host in srcHosts:
+        try:
+            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+            s.settimeout(15.0)
+            s.connect((host, 8649))
+            (fdTF,tmpFile) = tempfile.mkstemp()
+            fdTmpFile = os.fdopen(fdTF, "w+b")
+            while True:
+                data = s.recv(1024)
+                if len(data) == 0:
+                    break
+                fdTmpFile.write(data)
+            s.close()
+            fdTmpFile.close()
+
+            # hostname is being used as the filename here
+            targetfile = '%s/%s.xml' % (dataDir, host)
+            try:
+                os.rename(tmpFile, targetfile)
+                os.chmod(targetfile, 0644)
+            except:
+                logger.critical("Failed to rename %s to %s" % (tmpFile, 
targetfile))
+
+            logger.info('parsing %s: [OK]' % host)
+        except Exception, e:
+            logger.warning('parsing %s: [FAILED] | reason: %s'  % (host, e))
+
+def splitXML(dataDir):
+    """ find all .xml files in $dataDir and split them up on a per-host basis.
+    Grid and Cluster are discarded. We dont care about the xml data, so we can
+    save CPU cycle treating the file as txt one. """
+
+    # These patterns don't include whitespace, which might be a problem if
+    # someday ganglia starts emitting formatted xml
+    hostOpen = re.compile('^<HOST NAME="([^"]*)', re.I) #retrieve hostname
+    hostClose = re.compile('^</HOST>$', re.I)
+    totalHostCount = 0
+    try:
+        os.mkdir("%s/hosts" % dataDir,0755)
+    except OSError, e:
+        if e.errno != errno.EEXIST:
+            logger.critical('Problems creating %s. Aborting data splitting' % 
dataDir)
+            sys.exit(2)
+
+    logger.info("Splitting xml data in per-host files...")
+    # Sean suggested this could backfire. Cant find a practical case myself
+    xmlFiles = glob.glob('%s/*.xml' % dataDir)
+
+    # flagDupeHost tells you whether this host has been seen before (in this 
run)
+    # if it has, union the current host file and the stuff read in
+    flagDupeHost = False
+
+    hostsource = dict()
+    for xmlFile in xmlFiles:
+        fdXML = open(xmlFile)
+        thisHostCount = 0
+        for line in fdXML:
+            mHO = hostOpen.match(line)
+            if mHO:
+                hostname = mHO.group(1)
+                if hostsource.has_key(hostname):
+                    hostsource[hostname].append(xmlFile)
+                    v = hostsource[hostname]
+                    flagDupeHost = True
+                    logger.info("host '%s' seen more than once: %s" % 
(hostname, v))
+                else:
+                    hostsource[hostname] = [xmlFile]
+
+                hostFile = '%s/hosts/%s' % (dataDir, hostname)
+                hostBuf = [line]
+                # now we want to continue iterating over the same file
+                # until we reach the next close line
+                while True:
+                    line = fdXML.next()
+                    hostBuf.append(line)
+                    if hostClose.match(line):
+                        break
+
+                if ( flagDupeHost):
+                    oldHostBufLen = len(hostBuf)
+                    try:
+                        hostBuf = unionMetrics(hostBuf, hostFile)
+                    except Exception, e:
+                        hostBuf = []
+                        logger.warning("duped host: unionMetrics failed with 
exception %s" % e)
+                    newHostBufLen = len(hostBuf)
+                    logger.debug("duped host: old length: %s, new length: %s" 
% (oldHostBufLen, newHostBufLen))
+                    flagDupeHost = False
+
+                if ( len(hostBuf) != 0 ):
+                    # if hostBuf is empty, don't touch the existing file (so 
it will go stale rather than writing bad data)
+                    fdHostFile = open(hostFile, 'w+b')
+                    fdHostFile.write(''.join(hostBuf))
+                    fdHostFile.close()
+                else:
+                    logger.warning("didn't write file %s because hostBuf is 
empty" % hostFile)
+                thisHostCount += 1
+                totalHostCount += 1
+        logger.info("Parsed hosts in source '%s': %s" % (xmlFile, 
thisHostCount))
+    logger.info("Parsed hosts total: %s" % totalHostCount)
+
+def main():
+    """ main docstring """
+
+    # logging at level crit to give verification that the process is running, 
but not spew too much.
+    # this should probably be changed to logger.info
+    logger.critical('Starting ganglia_parser')
+    try:
+        os.makedirs(dataDir,2755)
+    except OSError, e:
+        if e.errno != errno.EEXIST:
+            logger.critical('Problems creating %s. Aborting data splitting' % 
dataDir)
+            sys.exit(2)
+
+
+    storeXMLData(listXMLSources(), dataDir)
+    splitXML(dataDir)
+    logger.critical('Finished ganglia_parser')
+
+
+if __name__ == "__main__":
+    main()


Property changes on: trunk/debs/ganglios/src/ganglia_parser
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/debs/ganglios/src/ganglia_parser.orig
===================================================================
--- trunk/debs/ganglios/src/ganglia_parser.orig                         (rev 0)
+++ trunk/debs/ganglios/src/ganglia_parser.orig 2011-10-03 23:10:57 UTC (rev 
98827)
@@ -0,0 +1,266 @@
+#!/usr/bin/python -tt
+# 
+# Copyright (C) 2009 Linden Lab
+#
+# Released under the GPL v2 or later.  For a full description of the license,
+# please visit http://www.gnu.org/licenses/gpl-2.0.html
+#
+
+
+"""
+Iterates over all the gmond collector nodes and fetch metrics where available.
+Separates metrics into one file per host for easier processing by nagios 
+plugins.
+"""
+
+import sys
+import socket
+import os
+import glob
+import tempfile
+import errno
+import re
+import logging
+
+__revision__ = '0'
+
+dataDir = './ganglia/xmlcache'
+logDir = './ganglia'
+logger = logging.getLogger('ganglia_parser')
+hdlr = logging.FileHandler('%s/ganglia_parser.log' % logDir)
+formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
+hdlr.setFormatter(formatter)
+logger.addHandler(hdlr) 
+logger.setLevel(logging.WARNING)
+
+def unionMetrics( curMetrics, hostFilePath ):
+    '''When a single host is present in more than one gmond collector node
+    (because of DNS errors or something else), write out the union of metrics
+    from each collector node, taking the more recent metric when the same
+    metric is present in multiple nodes.'''
+    metrics = dict()
+    metricsAge = dict()
+    # parseMetric grabs the metric name and age
+    parseMetric = re.compile('^<METRIC NAME="([^"]*).*TN="([^"]*)')
+
+    # populate metrics and metricsAge from the existing file
+    oldFileHandle = open(hostFilePath, 'r')
+    fileFirstLine = oldFileHandle.readline()
+    for line in oldFileHandle:
+        regMatch = parseMetric.match(line)
+        if regMatch:
+            met = regMatch.group(1)
+            age = regMatch.group(2)
+            metrics[met] = line
+            metricsAge[met] = age
+        else:
+            # the match failed, which will happen on </HOST> but nowhere else?
+            # theoretically, more logging could go in here
+            pass
+
+    # selectively overwrite metrics with stuff from the buf if it is newer
+    bufFirstLine = curMetrics[0]
+    for line in curMetrics[1:-1]:
+        regMatch = parseMetric.match(line)
+        if regMatch:
+            met = regMatch.group(1)
+            # age represents seconds since the metric was reported.  larger == 
older
+            age = regMatch.group(2)
+            if metrics.has_key(met):
+                try:
+                    if float(age) < float(metricsAge[met]):
+                        # if this metric exists and is older, replace it
+                        metrics[met] = line
+                except Exception, e:
+                    logger.warning("exception caught when comparing TN values: 
%s", e)
+            else:
+                # if the metric doesn't exist, add it
+                metrics[met] = line
+        else:
+            # the match failed, which shouldn't happen (because first and last 
lines are cut by the slice)
+            logger.warning("regMatch failed unexpectedly.  current line: %s" % 
(line))
+
+
+    # compare first lines to see which to use
+    parseHost = re.compile('^<HOST.*TN="([^"]*)')
+    fileAge = parseHost.match(fileFirstLine).group(1)
+    bufAge = parseHost.match(bufFirstLine).group(1)
+    if fileAge < bufAge:
+        firstLine = fileFirstLine
+    else:
+        firstLine = bufFirstLine
+
+    # construct array to pass back to the calling function
+    newBuf = []
+    newBuf.append(firstLine)
+    newBuf.extend(metrics.values())
+    newBuf.append('</HOST>\n')
+
+    # return the union metrics buffer
+    return newBuf
+
+
+def listXMLSources():
+    """ returns a list of hosts to fetch ganglia stats from (the gmond
+    collector nodes).  In a small network, this will likely be a single host.
+    You could simply hardcode the list if there is no coherent naming scheme.
+    In our network, the hosts are named 'nannybot1', 'nannybot2', etc.  This
+    function polls nannybots of increasing number until it finds 3 that don't
+    respond and considers itself done."""
+    i = 0
+    missed = 0
+    nannybots = ['spence', ]
+    logger.info("Retrieving list of nannybots...")
+#    while True:
+#        try:
+#            nannybot_addr = socket.gethostbyname('nannybot%d.lindenlab.com' % 
i)
+#            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+#            s.settimeout(15.0)
+#            s.connect((nannybot_addr, 8649))
+#            s.close()
+#            nannybots.append('nannybot%d.lindenlab.com' % i)
+#            logger.info('Checking nannybot%d.lindenlab.com: [OK]' % i)
+#            missed = 0
+#        except (socket.gaierror):
+#            logger.info('Checking nannybot%d.lindenlab.com: [FAILED]' % i)
+#            missed += 1
+#            if missed > 3:
+#                break
+#        except (socket.error, socket.timeout), e:
+#            logger.warning('Checking nannybot%d.lindenlab.com: [FAILED] with 
error %s' % (i, e))
+#            # a connection error (rather than non-existent) should not count 
towards 
+#            # the three missing that mark the end of the nannybots.
+#        except Exception, e:
+#            logger.critical('Caught unexpected exception while checking 
nannybot%d: %s' % (i, e))
+#            logger.critical('EXITING...')
+#            raise e
+#        i += 1
+#
+    return nannybots
+
+def storeXMLData(srcHosts, dataDir):
+    """ Fetch ganglia xml data from remote hosts and store it locally in 
$dataDir """
+
+    logger.info("Retrieving ganglia data from selected hosts...")
+    for host in srcHosts:
+        try:
+            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+            s.settimeout(15.0)
+            s.connect((host, 8649))
+            (fdTF,tmpFile) = tempfile.mkstemp()
+            fdTmpFile = os.fdopen(fdTF, "w+b")
+            while True:
+                data = s.recv(1024)
+                if len(data) == 0:
+                    break
+                fdTmpFile.write(data)
+            s.close()
+            fdTmpFile.close()
+
+            # hostname is being used as the filename here
+            targetfile = '%s/%s.xml' % (dataDir, host)
+            try:
+                os.rename(tmpFile, targetfile)
+                os.chmod(targetfile, 0644)
+            except:
+                logger.critical("Failed to rename %s to %s" % (tmpFile, 
targetfile))
+
+            logger.info('parsing %s: [OK]' % host)
+        except Exception, e:
+            logger.warning('parsing %s: [FAILED] | reason: %s'  % (host, e))
+
+def splitXML(dataDir):
+    """ find all .xml files in $dataDir and split them up on a per-host basis.
+    Grid and Cluster are discarded. We dont care about the xml data, so we can
+    save CPU cycle treating the file as txt one. """
+
+    # These patterns don't include whitespace, which might be a problem if
+    # someday ganglia starts emitting formatted xml
+    hostOpen = re.compile('^<HOST NAME="([^"]*)', re.I) #retrieve hostname
+    hostClose = re.compile('^</HOST>$', re.I)
+    totalHostCount = 0
+    try:
+        os.mkdir("%s/hosts" % dataDir,0755)
+    except OSError, e:
+        if e.errno != errno.EEXIST:
+            logger.critical('Problems creating %s. Aborting data splitting' % 
dataDir)
+            sys.exit(2)
+
+    logger.info("Splitting xml data in per-host files...")
+    # Sean suggested this could backfire. Cant find a practical case myself
+    xmlFiles = glob.glob('%s/*.xml' % dataDir)
+
+    # flagDupeHost tells you whether this host has been seen before (in this 
run)
+    # if it has, union the current host file and the stuff read in
+    flagDupeHost = False
+
+    hostsource = dict()
+    for xmlFile in xmlFiles:
+        fdXML = open(xmlFile)
+        thisHostCount = 0
+        for line in fdXML:
+            mHO = hostOpen.match(line)
+            if mHO:
+                hostname = mHO.group(1)
+                if hostsource.has_key(hostname):
+                    hostsource[hostname].append(xmlFile)
+                    v = hostsource[hostname]
+                    flagDupeHost = True
+                    logger.info("host '%s' seen more than once: %s" % 
(hostname, v))
+                else:
+                    hostsource[hostname] = [xmlFile]
+
+                hostFile = '%s/hosts/%s' % (dataDir, hostname)
+                hostBuf = [line]
+                # now we want to continue iterating over the same file
+                # until we reach the next close line
+                while True:
+                    line = fdXML.next()
+                    hostBuf.append(line)
+                    if hostClose.match(line):
+                        break
+
+                if ( flagDupeHost):
+                    oldHostBufLen = len(hostBuf)
+                    try:
+                        hostBuf = unionMetrics(hostBuf, hostFile)
+                    except Exception, e:
+                        hostBuf = []
+                        logger.warning("duped host: unionMetrics failed with 
exception %s" % e)
+                    newHostBufLen = len(hostBuf)
+                    logger.debug("duped host: old length: %s, new length: %s" 
% (oldHostBufLen, newHostBufLen))
+                    flagDupeHost = False
+
+                if ( len(hostBuf) != 0 ):
+                    # if hostBuf is empty, don't touch the existing file (so 
it will go stale rather than writing bad data)
+                    fdHostFile = open(hostFile, 'w+b')
+                    fdHostFile.write(''.join(hostBuf))
+                    fdHostFile.close()
+                else:
+                    logger.warning("didn't write file %s because hostBuf is 
empty" % hostFile)
+                thisHostCount += 1
+                totalHostCount += 1
+        logger.info("Parsed hosts in source '%s': %s" % (xmlFile, 
thisHostCount))
+    logger.info("Parsed hosts total: %s" % totalHostCount)
+
+def main():
+    """ main docstring """
+
+    # logging at level crit to give verification that the process is running, 
but not spew too much.
+    # this should probably be changed to logger.info
+    logger.critical('Starting ganglia_parser')
+    try:
+        os.makedirs(dataDir,2755)
+    except OSError, e:
+        if e.errno != errno.EEXIST:
+            logger.critical('Problems creating %s. Aborting data splitting' % 
dataDir)
+            sys.exit(2)
+
+
+    storeXMLData(listXMLSources(), dataDir)
+    splitXML(dataDir)
+    logger.critical('Finished ganglia_parser')
+
+
+if __name__ == "__main__":
+    main()


Property changes on: trunk/debs/ganglios/src/ganglia_parser.orig
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/debs/ganglios/src/ganglios/__init__.py
===================================================================
--- trunk/debs/ganglios/src/ganglios/__init__.py                                
(rev 0)
+++ trunk/debs/ganglios/src/ganglios/__init__.py        2011-10-03 23:10:57 UTC 
(rev 98827)
@@ -0,0 +1,8 @@
+"""\
+@file __init__.py
+@brief Initialization file for the ganglios module.
+
+Copyright (c) 2006-2007, Linden Research, Inc.
+License to use, modify, and distribute under the GPLv2 or later
+http://www.gnu.org/licenses/gpl-2.0.html
+"""


Property changes on: trunk/debs/ganglios/src/ganglios/__init__.py
___________________________________________________________________
Added: svn:eol-style
   + native

Added: trunk/debs/ganglios/src/ganglios/ganglios.py
===================================================================
--- trunk/debs/ganglios/src/ganglios/ganglios.py                                
(rev 0)
+++ trunk/debs/ganglios/src/ganglios/ganglios.py        2011-10-03 23:10:57 UTC 
(rev 98827)
@@ -0,0 +1,160 @@
+
+"""
+@file ganglios.py
+@brief utilities to make ganglios checks (nagios plugins) less ugly
+
+Copyright (c) 2007, Linden Research, Inc.
+License to use, modify, and distribute under the GPLv2 or later
+http://www.gnu.org/licenses/gpl-2.0.html
+
+There are two ways to use this module.
+a) examine *all* hosts for a given metric and analyze them using a callback 
function
+b) examine a *single* host for a specific metric and return that metric
+(a) is appropriate if you want to be notified if any host crosses a certain
+  threshold.  It is more efficient than creating a nagios check for every host 
on
+  that metric.  An example would be checking to see if any host has >95% disk
+  space used.  To set this up, create a 'ganglia' host in the nagios config and
+  put the service checks under that host.  Note that you won't be able to 
resolve
+  the nagios alert until *all* hosts are back below the threshold.  This can 
lead
+  to alerts just hanging out on your nagios page for too long.
+(b) is appropriate to check a specific host for a condition.  If you have 1000
+  hosts, of which 20 are web servers, you should use this method to test your 
web
+  servers for Stuff so as to avoid wasting cycles for the other 980 hosts.
+"""
+
+import os
+import sys
+import time
+import stat
+import glob
+import socket
+
+import elementtree.ElementTree as ET
+import xml.parsers.expat as expat
+
+__revision__ = '0'
+
+_cachedir = '/var/lib/ganglia/xmlcache/'
+_stale_time = 300
+_hostdir = os.path.join(_cachedir, 'hosts')
+
+def parse_ganglia (metrics, thunk):
+    """
+    metrics is a list of strings.
+    thunk is a callback.
+
+    This parses the xml files in /tmp/ganglia-cache/ and calls thunk
+    every time a METRIC with NAME in metrics is seen.  Use this function for
+    method (a) above
+
+    thunk should take 3 arguments: (host-name, metric-name, value)
+    """
+    status = 0 # ok
+    bad = []
+
+    def go_bad (xml_file, bad):
+        """ change status to bad, and output the stale nannybot """
+        bad_host = xml_file.replace ('.xml', '')
+        if not bad_host in bad:
+            bad += [bad_host]
+
+    try:
+        os.mkdir(_cachedir)
+    except:
+        pass
+
+    for xml_file in os.listdir(_cachedir):
+        filename = _cachedir+xml_file
+        if xml_file.endswith ('.xml'):
+            # make sure the data is fresh
+            mod_time = os.stat (filename)[stat.ST_MTIME]
+            if time.time () - mod_time > _stale_time:
+                go_bad (xml_file, bad)
+                status = 2
+            # read the xml file, look for certain metrics
+            f_hndl = open (filename)
+            try:
+                tree = ET.parse (f_hndl)
+                ganglia_xml = tree.getroot()
+                for cluster in ganglia_xml.getchildren ():
+                    for host in cluster.getchildren ():
+                        for metric in host.getchildren ():
+                            # found a metric we care about.
+                            if metric.get ('NAME') in metrics:
+                                thunk (host.get ('NAME'),
+                                       metric.get ('NAME'),
+                                       metric.get ('VAL'))
+            except expat.ExpatError:
+                go_bad (xml_file, bad)
+                status = 2
+            f_hndl.close ()
+    if len (bad) > 0:
+        if status == 0:
+            status = 2 # critical
+        sys.stdout.write ('<b>STALE</b>:')
+        for bad_host in bad:
+            sys.stdout.write (bad_host + ' ')
+    return status
+
+def get_metric_for_host(hostname, metricname):
+    """
+    using the new-style (one file per host), this 
+    takes a hostname, looks up the metric, and returns its value
+    This is method (b) above
+    """
+
+    # first, find the canonical name for the host passed in
+    # i.e. translate inv5-mysql.agni.lindenlab.com to db1c3.lindenlab.com
+    try:
+        new_hostname = socket.gethostbyaddr(hostname)[0]
+    except socket.gaierror, e:
+        # name not found.  it will probably fail anyways... but pass it 
through just in case
+        new_hostname = hostname
+    hostname = new_hostname
+
+    #strip off leading int., eth0., etc.
+    split_name = hostname.split('.')
+    if(split_name[0] in ('int', 'eth0', 'eth1', 'tunnel0', 'tunnel1')):
+        hostname = '.'.join(split_name[1:])
+
+    # find the one file that best matches what came in.  checks for names like
+    # int.$name and $name and complains if it doesn't find a unique match.
+    filelist = glob.glob(os.path.join(_hostdir, "*.%s" % hostname))
+    if len(filelist) == 0:
+        filelist = glob.glob(os.path.join(_hostdir, "%s" % hostname))
+###
+###  for the VPNs, it's a valid state that there exist >1 files for each vpn 
+###  (a tunnel address and a private interface).  What's the right action to 
take
+###  here?  not sure...  -green 2008-05-28
+###
+#    if len(filelist) != 1:
+#        sys.stdout.write("not exactly one match for '%s' (found %s)" % 
(hostname, len(filelist)))
+#        done(2)
+
+    filename = filelist[0]    # there can be only one
+    # make sure it's not old data
+    mod_time = os.stat(filename)[stat.ST_MTIME]
+    if time.time () - mod_time > _stale_time:   # seconds
+        sys.stdout.write('STALE')
+        done(2)
+
+    # read the xml file, look for certain metrics
+    f_hndl = open(filename)
+    try:
+        tree = ET.parse (f_hndl)
+        host = tree.getroot()
+        for metric in host.getchildren ():
+            # found a metric we care about.
+            if metric.get ('NAME') == metricname:
+                return metric.get('VAL')
+    except expat.ExpatError:
+        sys.stdout.write("XML parse error")
+        done(2)
+    f_hndl.close()
+
+
+
+def done (status):
+    """ print newline if needed, exit with status """
+    print ''
+    sys.exit (status)


Property changes on: trunk/debs/ganglios/src/ganglios/ganglios.py
___________________________________________________________________
Added: svn:eol-style
   + native


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to