From 7ec36a95ef5d1d46e7a49af26b4d1dbfb7247661 Mon Sep 17 00:00:00 2001 From: amine Date: Fri, 3 Aug 2007 14:54:05 +0000 Subject: [PATCH] Automatic creation:creates nagios config from a set of sites and monitor their nodes --- nagios/AutomaticConfig/GetConfig.py | 101 +++++++++++++++++++ nagios/AutomaticConfig/GetConfig.pyc | Bin 0 -> 3671 bytes nagios/AutomaticConfig/GetMonitoring.py | 128 ++++++++++++++++++++++++ nagios/AutomaticConfig/ServiceName | 2 + 4 files changed, 231 insertions(+) create mode 100644 nagios/AutomaticConfig/GetConfig.py create mode 100644 nagios/AutomaticConfig/GetConfig.pyc create mode 100755 nagios/AutomaticConfig/GetMonitoring.py create mode 100644 nagios/AutomaticConfig/ServiceName diff --git a/nagios/AutomaticConfig/GetConfig.py b/nagios/AutomaticConfig/GetConfig.py new file mode 100644 index 0000000..d876fe2 --- /dev/null +++ b/nagios/AutomaticConfig/GetConfig.py @@ -0,0 +1,101 @@ +import os, sys, time, string +from optparse import OptionParser +import xmlrpclib + + +class GetConfig: + + subversion_id = "$Id$" + + def __init__ (self): + self.url="https://localhost:443/PLCAPI/" + self.server=xmlrpclib.Server(self.url,allow_none=True) + self.path="/etc/nagios/config-sample/" + self.path1="/etc/nagios/" + + def create_hostgroup(self, file, nodes_infos): + f1=open(file,"a") + i=0 + j=0 + memberhost="" + alias=[] + list=[] + site_name=nodes_infos[0]['Site_name']##initialize the first site name + + while 1: + if(i=len(nodes_infos)): + alias.append(site_name) + list.append(memberhost) + break + + + print list + print alias + while (j < len(list)): + f1.write("\ndefine hostgroup { \n") + f1.write("hostgroup_name "+str(j)+"\n") + f1.write("alias %s\n"%alias[j]) + f1.write("members %s\n } \n"%list[j]) + j=j+1 + f1.close() + + def generate_file(self,sample_file, file,login,nodes_infos): + + f1=open(self.path+sample_file,"rb") + f2=open(self.path1+file,"wb") + f2.write(f1.read()) + f1.close() + f2.close() + if (login=="h"): + self.create_host(self.path1+file,nodes_infos) + elif (login=="s"): + self.create_service(self.path1+file,nodes_infos) + elif (login=="g"): + self.create_hostgroup(self.path1+file, nodes_infos) + + + + def create_host(self,file,nodes_infos): + f1=open(file,"a") + i=0 + while (i&?ci+r_H+N5-KC>|v4UXgF6y|F{I{fzb>K{$8=C*TS^2)H>t z&{Ni@B%V@DRpP34;6_b5(-N+=rm=p-(t)K%@lu!jvQcpl&EAH<&buO!5f?~WVL!!j zfw(EzH-=N#T=fw)pHk!Dn^m!V#l8W#<=nvDZnv|d+sU>H>u%q?d857h#l25gAGKWr z7M(0Bo=16+C-!v=+llM8^G+`bd1(^nd#N- z^s{o%&C(55gLW+`zsH@@@~d(?Q5`OMq#JbHmZQt;T-Shv0fsF!FWj5xwacP#?LpRc ziFIv?H1D;?L3(%a!qK59O7ql35#P;HA*OE5qjl*Zwc}g<6L*d%?rIeG1KC6TAaSjb zxYLU4)uku0H=}Y|b~VzAxa`HfDcNb5-KyXcq24$0M5MSSW*3nN1QTlZB#>eW%KBJY zlf{L2S{AT1jDU^>9Qf>G*c{r+{cEp^6cLm&k{%NrgEP(Uv<~Vz@D9J^tcg!>{hxch z+>NRf7yj$SRXWc04IFv!5ZgX@xZ`BErq?n(x|Z*-M@7=(63=+A{h%&xmbQ42g}5OL z`a*sRFF3=g#j^*L#|5WMl!y>-uO`pFHgEt7=VTw@j+xxE2=t0$Qgu=Y#u!!G^CC_nW(U=}=&C&feIrgb(>!T%s#g}xEH#yiL6U1{GtClkgvjP~^*vSjM6=uP zNpCg7)Dc~jrBEHiBEGvt&TPVl*JDT5qY0hi45n~rfYV~#<9P`SowTl0O*boS(qd|9 z-iD{xTckKmL0So4qu`x}=O`8^&O%I*Jmd!M1Vpz?I*8Huj;XVsLx`C-wed3-Afrvo zoHcWhvv_LSpEYyAX&pBzRZ}$;;pn_MgJ<4!$u#6&(!o76`v(MR00fI9Ar_604Umss zTV?VtfE1{$5rHYdS(OxNhmyiH#Gs+zklN5wcNVZf=c*c(iEFVGw zL&NHE_I}9PqBugR;_w1Tyw|MgrMYf#AchJBH;3t=lH8wmlduVumO_90B@8`wuCO-& z&}G`8dEs%wwCyE%QW7b2^aCg?5hcM{z~HoL;9=N(Q}(BeWA^_A0|xjI`-9hiBD>=4 zCcJL-`cth5BSP{;7l=VM01-`?3KPm{%6S-Q-8jk$AQ~>OkCx-Z0YnIfes+$e3Qhxw zjyR3!x)SnRB}Eg<$TgY=I7;b>gu~Q%IWvJuLehw3Qf}L%j=wC;w|B1js{AUX)rsRW zvHwF_j`^k}n+}w9_0z#4Psu`gg5HGZDKzwim?EpiupP-PTyY2(;oH#NNAp-v!teyJ z2qY}x0R=Gi33hI*6i@+IBkPge$P6OF=X!<17)wnU+uI|js_kTD1Gr572XQ8GFgHGS(QV@QQcyF z% zzbXb9aF0P>Aj_m*xI~eNQ#}-iTSur=-7?kh^aItD^^=Uz-AcMoqi)gfck=kayzegC zi_5m@Ybd$MS0p9B2?WVB6A0?}Bnb}5;VYio@pTlH4{i|EL)VlGZIV1yX7sQn!j6Qk zo^oUC!*%oyqxc=@*3hg@ps>y3A&voEa~u!LT~!f1YfhRa%r=bwAm_1>CVv#^H$W8i zi+DRrFw~%JZ{W+@; literal 0 HcmV?d00001 diff --git a/nagios/AutomaticConfig/GetMonitoring.py b/nagios/AutomaticConfig/GetMonitoring.py new file mode 100755 index 0000000..2d00dfa --- /dev/null +++ b/nagios/AutomaticConfig/GetMonitoring.py @@ -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() diff --git a/nagios/AutomaticConfig/ServiceName b/nagios/AutomaticConfig/ServiceName new file mode 100644 index 0000000..dd67613 --- /dev/null +++ b/nagios/AutomaticConfig/ServiceName @@ -0,0 +1,2 @@ +check_ssh +check-host-alive -- 2.43.0