creation : creates nagios config from a set of sites
[infrastructure.git] / nagios / configurator / configurator.py
diff --git a/nagios/configurator/configurator.py b/nagios/configurator/configurator.py
new file mode 100755 (executable)
index 0000000..e9f8090
--- /dev/null
@@ -0,0 +1,111 @@
+#!/usr/bin/env python
+
+# WARNING - you might need to set your locale before you can run this script
+# e.g.
+# export LANG=en_US.UTF-8
+# nagios.py ...
+
+import sys 
+import getopt
+
+from NagiosConfig import NagiosConfig
+
+SERVER="www.planet-lab.org"
+DIRNAME="/etc/nagios"
+
+options='h:o:Os:d:nNv'
+
+usage_string="""Usage : %s [options] plc-id plc-passwd role
+Outputs a nagios config for watching a given set of sites
+Also writes a static HTML page for bookmarking site-centric queries to comon
+Options:
+  -h plc-hostname       To provide an alternate plc hostname
+                        (defaults to %s)
+  -o e-mail             If you provide an e-mail address, all notifications
+                        will be sent there. Otherwise, you'll need admin role
+                        so that we can determine the respective PI's e-mails
+  -O                    same as -e with the plc-id as dest. e-mail
+  -s site-file          You can provide a list of sites in a separate file
+                        expected format is one line per site, site_id first
+                        the default is to list all sites
+  -d dirname            To specify an alternate output dir
+                        (defaults to %s)
+  -n                    Does not generate the nagios config
+  -N                    Does not generate the HTML page
+  -v                   Runs in verbose mode
+"""%(sys.argv[0],SERVER,DIRNAME)
+
+def usage ():
+    print usage_string
+    sys.exit(1)
+
+
+def main ():
+    command = sys.argv[0]
+    argv = sys.argv[1:]
+
+    try:
+        opts,args = getopt.getopt(argv, options)
+    except getopt.GetoptError:
+        print "Unknown option"
+        usage()
+
+    plc_server=SERVER
+    output_mail=None
+    opt_same_email=False
+    site_file=None
+    dirname=DIRNAME
+    opt_nagios = True
+    opt_html = True
+    opt_verbose = False
+    
+    for o,a in opts:
+        if o in ['-h']:
+            plc_server = a
+        elif o in ['-o']:
+            output_mail = a
+        elif o in ['-O']:
+            opt_same_email = True
+        elif o in ['-s']:
+            site_file = a
+        elif o in ['-d']:
+            dirname = a
+        elif o in ['-n']:
+            opt_nagios = False
+        elif o in ['-N']:
+            opt_html = False
+       elif o in ['-v']:
+           opt_verbose = True
+        else:
+            print "Unknown option",o
+            usage()
+
+    if not len(args) == 3:
+        usage()
+
+    (plc_id,passwd,role) = args
+    if opt_same_email:
+        output_mail=plc_id
+
+#    print plc_server,output_mail,dirname,opt_nagios,opt_html
+#    print plc_id,passwd,role,site_file
+
+    print "Connecting to %s and gathering data"%plc_server
+    config = NagiosConfig (plc_server,plc_id,passwd,role)
+    if opt_verbose:
+       config.set_verbose()
+    config.GatherData (output_mail,site_file)
+
+    if opt_nagios:
+        print "Creating nagios config in %s"%dirname
+        config.WriteNagiosConfig (dirname)
+
+    if opt_html:
+        html_page = "%s/comon.html"%dirname
+        config.WriteComonLinks (html_page)
+
+    
+####################
+if __name__ == "__main__":
+    main ()
+