Automatic creation:creates nagios config from a set of sites and monitor their nodes
[infrastructure.git] / nagios / AutomaticConfig / GetMonitoring.py
1 #!/usr/bin/env python
2
3 import os, sys, time
4 from optparse import OptionParser
5 import xmlrpclib
6 from GetConfig import *
7
8 class GetMonitoring:
9
10     subversion_id = "$Id$"
11
12     def __init__ (self):
13         self.url="https://localhost:443/PLCAPI/"
14         self.server=xmlrpclib.Server(self.url,allow_none=True)
15         
16         #self.path=os.path.dirname(sys.argv[0])
17
18     def auth_root (self):
19         return {'Username':self.args[0],
20                 'AuthMethod':'password',
21                 'AuthString':self.args[1],
22                 'Role' : 'root'
23                 } 
24  
25     
26
27 #################################
28 ###############################
29    
30     def GetSiteNodes(self,sites_names):
31         i=0
32         
33         tab_node_infos=[]##its an array containing struct of Site,nodes hostname and ip for each nodes
34                          ##to be monitored
35         while (i< len(sites_names)):
36             filter_site={'abbreviated_name':sites_names[i]}
37             return_value=['site_id']
38             ##Getting the site_id from the given  site name args
39             site_id=self.server.GetSites(self.auth_root(),filter_site,return_value)
40             if (site_id==[]):
41                 print "Site Not found or duplicated Site Name please check your Site's Name::",sites_names[i]
42                 exit
43                 
44             ##Getting nodes_id relative to the site to be monitored
45             filter_node={'site_id':site_id[0]['site_id']}
46             node_ids =self.server.GetNodes(self.auth_root(),filter_node,['node_id','hostname'])
47             ##Getting information for nodes to be monitored
48             j=0
49             while(j < len(node_ids)):
50                
51                 sn_info={'Site_name':None,
52                          'node_hostname':None,
53                          'node_ip':None
54                          }
55                 sn_info['Site_name']=sites_names[i]
56
57                 filter=node_ids[j]['node_id']
58                 node_infos=self.server.GetNodeNetworks(self.auth_root(),filter,['ip'])
59                 sn_info['node_hostname']=node_ids[j]['hostname']
60                 #tab_node_infos.append(self.sn_info['node_hostname'])
61                 sn_info['node_ip']=node_infos[0]['ip']
62                 tab_node_infos.append(sn_info)
63                 j=j+1
64             
65             i=i+1
66         #print node_ids
67         return tab_node_infos
68
69 #######################################
70 ######################################
71
72     
73     def main (self):
74         try:
75             
76             ##################
77             ######options
78             usage = """usage: %prog [options]  UserName(email) passwd Sites Names  [-h for help]"""
79             parser=OptionParser(usage=usage,version=self.subversion_id)
80             # verbosity
81             parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False, 
82                               help="Run in verbose mode")
83            
84             (self.options, self.args) = parser.parse_args()
85             i=0
86             
87             site_name=[]
88             nodes_infos=[]
89
90             getConfig=GetConfig()
91             
92             if (len (self.args) < 3 ):
93                 print "please check your command"
94                 print usage        
95                     
96             else:
97                 i=2
98                 while (i < len(self.args)) :
99                     site_name.append(self.args[i])
100                     i=i+1
101
102                 nodes_infos=self.GetSiteNodes(site_name)
103                 getConfig.generate_file("services-sample.cfg","services.cfg","s",nodes_infos)
104                 getConfig.generate_file("hosts-sample.cfg","hosts.cfg","h",nodes_infos)
105                 getConfig.generate_file("hostgroups-sample.cfg","hostgroups.cfg","g",nodes_infos)
106                 
107                 print "Info on what will be monitored grouped by Site:-------->"
108                 for node in nodes_infos:
109                     print "------------>",node
110
111                 
112                     
113             res=os.system("nagios -v /etc/nagios/nagios.cfg")
114             if (not res):
115                 os.system("service nagios reload")
116                
117                 
118             ######################
119             ########end options
120             ##################
121                 
122           
123         except Exception, e:
124             print str(e)
125             sys.exit(1)
126
127 if __name__ == "__main__":
128     GetMonitoring().main()