make plc.py simpler to reduce the dependencies for plc_hosts_to_nagios.
authorStephen Soltesz <soltesz@cs.princeton.edu>
Mon, 21 Jun 2010 20:26:05 +0000 (20:26 +0000)
committerStephen Soltesz <soltesz@cs.princeton.edu>
Mon, 21 Jun 2010 20:26:05 +0000 (20:26 +0000)
add cron script to regenerate config files daily.
add dependencies and setup to monitor-nagios rpm
improve monitor-nagios.init script (I still think it may need to only be run once).

Monitor.spec
monitor/wrapper/plc.py
monitor/wrapper/plccache.py
nagios/monitor-nagios.cron [new file with mode: 0644]
nagios/monitor-nagios.init
nagios/plc_hosts_to_nagios.py
nagios/plugins/checkpcu.py

index 8d9ede0..2a39603 100644 (file)
@@ -50,7 +50,11 @@ Requires: mysql-server
 Requires: mysql-devel
 Requires: mysql-libs
 Requires: mailx
+Requires: sendmail
+Requires: php
+Requires: httpd
 
+Requires: cronie
 Requires: nagios
 Requires: nagios-common
 Requires: nagios-devel
@@ -156,7 +160,9 @@ install -d $RPM_BUILD_ROOT/%{python_sitearch}/monitor
 install -D -m 644 monitor.functions $RPM_BUILD_ROOT/%{_sysconfdir}/plc.d/monitor.functions
 install -D -m 755 monitor-server.init $RPM_BUILD_ROOT/%{_sysconfdir}/plc.d/monitor
 install -D -m 755 zabbix/monitor-zabbix.init $RPM_BUILD_ROOT/%{_sysconfdir}/plc.d/zabbix
+
 install -D -m 755 nagios/monitor-nagios.init $RPM_BUILD_ROOT/%{_sysconfdir}/plc.d/monitor-nagios
+install -D -m 644 nagios/monitor-server.cron $RPM_BUILD_ROOT/%{_sysconfdir}/cron.d/monitor-nagios.cron
 
 # cron job for automated polling
 install -D -m 644 monitor-server.cron $RPM_BUILD_ROOT/%{_sysconfdir}/cron.d/monitor-server.cron
@@ -173,6 +179,9 @@ rsync -a --exclude archive-pdb --exclude .cvsignore --exclude .svn --exclude CVS
 # install monitor python package
 rsync -a --exclude .svn  ./monitor/   $RPM_BUILD_ROOT/%{python_sitearch}/monitor/
 
+install -D -m 644 monitor/wrapper/plc.py $RPM_BUILD_ROOT/usr/share/%{name}/nagios/
+install -D -m 644 monitor/generic.py $RPM_BUILD_ROOT/usr/share/%{name}/nagios/
+
 # install third-party module to site-packages
 install -D -m 755 threadpool.py $RPM_BUILD_ROOT/%{python_sitearch}/threadpool.py
 
@@ -201,6 +210,7 @@ rm -rf $RPM_BUILD_ROOT
 %defattr(-,root,root)
 %{_sysconfdir}/plc.d/monitor-nagios
 /usr/share/%{name}/nagios 
+%{_sysconfdir}/cron.d/monitor-nagios.cron
 
 %files server
 %defattr(-,root,root)
index 5e6fb7f..8eba805 100644 (file)
@@ -12,7 +12,6 @@ import xml, xmlrpclib
 import logging
 import time
 import traceback
-from monitor import database
 
 # note: this needs to be consistent with the value in PLEWWW/planetlab/includes/plc_functions.php
 PENDING_CONSORTIUM_ID = 0
@@ -90,54 +89,7 @@ class PLC:
                return self.api.__repr__()
 
 
-class CachedPLC(PLC):
-
-       def _param_to_str(self, name, *params):
-               fields = len(params)
-               retstr = ""
-               retstr += "%s-" % name
-               for x in params:
-                       retstr += "%s-" % x
-               return retstr[:-1]
-
-       def __getattr__(self, name):
-               method = getattr(self.api, name)
-               if method is None:
-                       raise AssertionError("method does not exist")
-
-               def run_or_returncached(*params):
-                       cachename = self._param_to_str(name, *params)
-                       #print "cachename is %s" % cachename
-                       if hasattr(config, 'refresh'):
-                               refresh = config.refresh
-                       else:
-                               refresh = False
-
-                       if 'Get' in name:
-                               if not database.cachedRecently(cachename):
-                                       load_old_cache = False
-                                       try:
-                                               values = method(self.auth, *params)
-                                       except:
-                                               print "Call %s FAILED: Using old cached data" % cachename
-                                               load_old_cache = True
-
-                                       if load_old_cache:
-                                               values = database.dbLoad(cachename)
-                                       else:
-                                               database.dbDump(cachename, values)
-
-                                       return values
-                               else:
-                                       values = database.dbLoad(cachename)
-                                       return values
-                       else:
-                               return method(self.auth, *params)
-
-               return run_or_returncached
-
 api = PLC(auth.auth, auth.server)
-cacheapi = CachedPLC(auth.auth, auth.server)
 
 
 def getAPI(url):
index 3ddf6f2..60dbd22 100755 (executable)
@@ -4,6 +4,7 @@ import sys
 from monitor.wrapper import plc
 from monitor.generic import *
 from monitor.database.info.model import *
+from monitor import database
 import profile
 
 
@@ -15,6 +16,54 @@ plcdb_hn2lb = None
 plcdb_lb2hn = None
 plcdb_id2lb = None
 
+class CachedPLC(PLC):
+
+       def _param_to_str(self, name, *params):
+               fields = len(params)
+               retstr = ""
+               retstr += "%s-" % name
+               for x in params:
+                       retstr += "%s-" % x
+               return retstr[:-1]
+
+       def __getattr__(self, name):
+               method = getattr(self.api, name)
+               if method is None:
+                       raise AssertionError("method does not exist")
+
+               def run_or_returncached(*params):
+                       cachename = self._param_to_str(name, *params)
+                       #print "cachename is %s" % cachename
+                       if hasattr(config, 'refresh'):
+                               refresh = config.refresh
+                       else:
+                               refresh = False
+
+                       if 'Get' in name:
+                               if not database.cachedRecently(cachename):
+                                       load_old_cache = False
+                                       try:
+                                               values = method(self.auth, *params)
+                                       except:
+                                               print "Call %s FAILED: Using old cached data" % cachename
+                                               load_old_cache = True
+
+                                       if load_old_cache:
+                                               values = database.dbLoad(cachename)
+                                       else:
+                                               database.dbDump(cachename, values)
+
+                                       return values
+                               else:
+                                       values = database.dbLoad(cachename)
+                                       return values
+                       else:
+                               return method(self.auth, *params)
+
+               return run_or_returncached
+
+cacheapi = CachedPLC(plc.auth.auth, plc.auth.server)
+
 def init():
        import traceback
        #print "IMPORTING PLCCACHE: ",
diff --git a/nagios/monitor-nagios.cron b/nagios/monitor-nagios.cron
new file mode 100644 (file)
index 0000000..1852f33
--- /dev/null
@@ -0,0 +1,4 @@
+# run daily to regenerate the nagios configuration files
+0 0 * * * root /usr/share/monitor/nagios/plc_hosts_to_nagios.py > /etc/nagios/objects/plc.cfg
+5 0 * * * root /usr/share/monitor/nagios/plc_users_to_nagios.py > /etc/nagios/objects/plcusers.cfg
+10 0 * * * root /sbin/service nagios restart
index ebab5b8..8223ee3 100644 (file)
 # $Id$
 #
 
-. /etc/plc.d/functions
-. /etc/plc.d/monitor.functions
-. /etc/planetlab/plc_config
+for f in /etc/plc.d/functions /etc/plc.d/monitor.functions /etc/planetlab/plc_config ; do
+       if [ -f /etc/plc.d/functions ] ; then 
+               source $f
+       fi
+done
 
 # NOTE: expect that mysqld is already started.
 
 start () {
        
-       if ! $( mysqladmin ping 2> /dev/null ) ; then
+       if ! ( mysqladmin ping &> /dev/null ) ; then
                echo "mysqld is not running; cannot continue; exit..."
                exit
        fi
 
        # if it's a fresh install, there is no root passwd.
-       if $( mysqladmin -u root create nagios ) ; then
+       if ( mysqladmin -u root create nagios &> /dev/null ) ; then
                cat <<EOF > /tmp/nagios.priv
    GRANT ALL PRIVILEGES ON nagios.* TO ndouser@localhost IDENTIFIED BY 'ndopassword';
 EOF
@@ -38,32 +40,46 @@ EOF
                perl ./installdb -u ndouser -p ndopassword -d nagios
        fi
 
-       if ! $( grep -q /usr/lib/nagios/brokers/ndomod.so  /etc/nagios/nagios.cfg ) ; then 
+       if ! ( grep -q /usr/lib/nagios/brokers/ndomod.so  /etc/nagios/nagios.cfg ) ; then 
                echo "broker_module=/usr/lib/nagios/brokers/ndomod.so config_file=/etc/nagios/ndomod.cfg" >> /etc/nagios/nagios.cfg
-               echo "foo"
        fi
 
-       /sbin/chkconfig ndo2db on
-       #/sbin/service ndo2db start
+       if ( chkconfig --list ndo2db | grep -q 3:off ) ; then
+               /sbin/chkconfig ndo2db on
+               /sbin/service ndo2db start
+       fi
        #/sbin/service nagios restart
 
        ## set passwd for /etc/nagios/passwd
-       htpasswd -b -c /etc/nagios/passwd nagiosadmin nagiospassword
+       if ! ( grep nagiosadmin /etc/nagios/passwd &> /dev/null ) ; then
+               htpasswd -b -c /etc/nagios/passwd nagiosadmin nagiospassword
+       fi
 
        # disable /etc/httpd/conf.d/nagios.conf restriction to only 127.0.0.1
-       if $( grep -q -E "^   deny from all" /etc/httpd/conf.d/nagios.conf ) ; then
-               sed -i -e 's/   deny from all/   allow from all/g' /etc/httpd/conf.d/nagios.conf 
-               sed -i -e 's/   order deny,allow/   order allow,deny/g' /etc/httpd/conf.d/nagios.conf 
+       if ( grep -q -E "^   deny from all" /etc/httpd/conf.d/nagios.conf ) ; then
+               sed -i -e 's/   deny from all/   #allow from all/g' /etc/httpd/conf.d/nagios.conf 
+               sed -i -e 's/   order deny,allow/   #order allow,deny/g' /etc/httpd/conf.d/nagios.conf 
                sed -i -e 's/   allow from 127.0.0.1/   /g' /etc/httpd/conf.d/nagios.conf 
        fi
 
-       touch /etc/nagios/objects/plc.cfg
-       touch /etc/nagios/objects/plcusers.cfg
-       echo "cfg_file=/etc/nagios/objects/plc.cfg" >> /etc/nagios/nagios.cfg
-       echo "cfg_file=/etc/nagios/objects/plcusers.cfg" >> /etc/nagios/nagios.cfg
+       if [ ! -f /etc/nagios/objects/plc.cfg ] ; then 
+               /usr/share/monitor/nagios/plc_hosts_to_nagios.py > /etc/nagios/objects/plc.cfg
+       fi
+
+       if [ ! -f /etc/nagios/objects/plcusers.cfg ] ; then 
+               /usr/share/monitor/nagios/plc_users_to_nagios.py > /etc/nagios/objects/plcusers.cfg
+       fi
+
 
-       /sbin/chkconfig sendmail on
-       /sbin/service sendmail start
+       if ! ( grep -q "cfg_file=/etc/nagios/objects/plc.cfg" /etc/nagios/nagios.cfg) ; then
+               echo "cfg_file=/etc/nagios/objects/plc.cfg" >> /etc/nagios/nagios.cfg
+               echo "cfg_file=/etc/nagios/objects/plcusers.cfg" >> /etc/nagios/nagios.cfg
+       fi
+
+       if ( chkconfig --list sendmail | grep -q 3:off ) ; then
+               /sbin/chkconfig sendmail on
+               /sbin/service sendmail start
+       fi
 
        # change /etc/nagios/objects/contacts.cfg to some real email addr
        # change /etc/nagios/nagios.cfg admin_email to some real email addr
index 302db9e..f45fa76 100755 (executable)
@@ -1,5 +1,8 @@
 #!/usr/bin/python
+
+import plc
 from nagiosobjects import *
+from generic import *
 
 command_auto = Command(command_name="check_mode",
                                           command_line="""/usr/share/monitor/nagios/plugins/checkmode.py -H $HOSTNAME$ --sn $SERVICENOTIFICATIONNUMBER$ """)
@@ -123,8 +126,6 @@ globalhost = [Host( name="planetlab-host",
 for obj in globalhost + globalservices:
        print obj.toString()
 
-from monitor.wrapper import plc
-from monitor.generic import *
 
 l_sites = plc.api.GetSites({'login_base' : ['asu', 'gmu', 'gt']})
 #l_sites = plc.api.GetSites([10243, 22, 10247, 138, 139, 10050, 10257, 18, 20, 
@@ -300,7 +301,7 @@ for site in l_sites:
                                        service_description="cPCU",
                                        display_name="cPCU",
                                        servicegroups="NET,PCU",
-                                       notifications_enabled="0",
+                                       notifications_enabled="1",
                                        check_command="check_pcu")
 
                # NOTE: try to repair the host, if it is online and 'mode' indicates a problem
index 4524cd0..a7156a0 100755 (executable)
@@ -43,7 +43,7 @@ def main():
        t1 = 0
        t2 = time.time()
 
-       if True:
+       if False:
                print "FAKE-OK: PCU test successful"
                sys.exit(0)
        elif False: