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