Automatic creation:creates nagios config from a set of sites and monitor their nodes
[infrastructure.git] / nagios / AutomaticConfig / GetMonitoring.py
diff --git a/nagios/AutomaticConfig/GetMonitoring.py b/nagios/AutomaticConfig/GetMonitoring.py
new file mode 100755 (executable)
index 0000000..2d00dfa
--- /dev/null
@@ -0,0 +1,128 @@
+#!/usr/bin/env python
+
+import os, sys, time
+from optparse import OptionParser
+import xmlrpclib
+from GetConfig import *
+
+class GetMonitoring:
+
+    subversion_id = "$Id$"
+
+    def __init__ (self):
+        self.url="https://localhost:443/PLCAPI/"
+        self.server=xmlrpclib.Server(self.url,allow_none=True)
+        
+       #self.path=os.path.dirname(sys.argv[0])
+
+    def auth_root (self):
+       return {'Username':self.args[0],
+               'AuthMethod':'password',
+               'AuthString':self.args[1],
+                'Role' : 'root'
+                } 
+    
+
+#################################
+###############################
+   
+    def GetSiteNodes(self,sites_names):
+        i=0
+        
+        tab_node_infos=[]##its an array containing struct of Site,nodes hostname and ip for each nodes
+                         ##to be monitored
+        while (i< len(sites_names)):
+            filter_site={'abbreviated_name':sites_names[i]}
+            return_value=['site_id']
+            ##Getting the site_id from the given  site name args
+            site_id=self.server.GetSites(self.auth_root(),filter_site,return_value)
+            if (site_id==[]):
+                print "Site Not found or duplicated Site Name please check your Site's Name::",sites_names[i]
+                exit
+                
+            ##Getting nodes_id relative to the site to be monitored
+            filter_node={'site_id':site_id[0]['site_id']}
+            node_ids =self.server.GetNodes(self.auth_root(),filter_node,['node_id','hostname'])
+            ##Getting information for nodes to be monitored
+            j=0
+            while(j < len(node_ids)):
+               
+                sn_info={'Site_name':None,
+                         'node_hostname':None,
+                         'node_ip':None
+                         }
+                sn_info['Site_name']=sites_names[i]
+
+                filter=node_ids[j]['node_id']
+                node_infos=self.server.GetNodeNetworks(self.auth_root(),filter,['ip'])
+                sn_info['node_hostname']=node_ids[j]['hostname']
+                #tab_node_infos.append(self.sn_info['node_hostname'])
+                sn_info['node_ip']=node_infos[0]['ip']
+                tab_node_infos.append(sn_info)
+                j=j+1
+            
+            i=i+1
+        #print node_ids
+        return tab_node_infos
+
+#######################################
+######################################
+
+    
+    def main (self):
+        try:
+            
+            ##################
+            ######options
+            usage = """usage: %prog [options]  UserName(email) passwd Sites Names  [-h for help]"""
+            parser=OptionParser(usage=usage,version=self.subversion_id)
+            # verbosity
+            parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False, 
+                              help="Run in verbose mode")
+           
+            (self.options, self.args) = parser.parse_args()
+            i=0
+            
+            site_name=[]
+            nodes_infos=[]
+
+            getConfig=GetConfig()
+            
+            if (len (self.args) < 3 ):
+                print "please check your command"
+                print usage        
+                    
+            else:
+                i=2
+                while (i < len(self.args)) :
+                    site_name.append(self.args[i])
+                    i=i+1
+
+                nodes_infos=self.GetSiteNodes(site_name)
+                getConfig.generate_file("services-sample.cfg","services.cfg","s",nodes_infos)
+                getConfig.generate_file("hosts-sample.cfg","hosts.cfg","h",nodes_infos)
+                getConfig.generate_file("hostgroups-sample.cfg","hostgroups.cfg","g",nodes_infos)
+                
+                print "Info on what will be monitored grouped by Site:-------->"
+                for node in nodes_infos:
+                    print "------------>",node
+
+                
+                    
+            res=os.system("nagios -v /etc/nagios/nagios.cfg")
+            if (not res):
+                os.system("service nagios reload")
+               
+                
+            ######################
+            ########end options
+            ##################
+                
+          
+        except Exception, e:
+            print str(e)
+            sys.exit(1)
+
+if __name__ == "__main__":
+    GetMonitoring().main()