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 (i36PL@1^)As_uvW8xiw$x&Vtg`l0KY%$ zaK-7M4dBs0@qk?bznMByS0|&!>c+(Y9%}cXt}e`YWf157it~R4z(I^qVnM*oX@Q(_ z)<)5mo)ASSl?X*7l@8L=L5D|M8gTS^ag=jk{@C$=``_maE6jjQ!38ia){Wx3xHfK- z;!w!rSc!NXD~mS^W&7^{xO8r6?+=G%osY`P+PeE6eDMD8+ZcSwG9F zy3+RB0EXJ-!)i1(wH@a2_V3xzWLoN>T+-(7V28Iro2@P{9KChblyY^~ld_rSr8##I zfK%Okr5l;~+vZA3pI!G&NAA+mWLEBpL(%}ifH084eV3Lsb?t`JGI!d#Aq!KDhbt%j zaQd~QL0M)hX!ed#EUbh zU>0zwSG3Waw{e}QSqPV~`$pk0P=5$DYqLEI{j|pc>LaLGyy}e5J1jaloX4D0ggBgs zc=~g^u$OybuNLYIYPJJ&3p1hsYSxiPEDdk)OU_!`G;ia-_jtJ(A?i2&*NG=IoWCz; z9X$MOwu6VCT|3Moxt7lITK)hvYr*WI*zs2TMhw^G+#7|%BEg}&kRQ556K4^62OFKc zm~}QfBY%4lp8ig$FS&4!{3h{F2VGLYkyIl?5I8AF>r>-&R*fdwaMW>PRTnxVJJ>CY z-nDm#mO@{cO7}T5ZtAOP|BwB|eviCb_Q+2CZI9*liG5QzW=3|~9C?myVvhk#^yFMO zFPnF4lm5J)4EC3#tEajWPF{vW39*HHVev1`Yjf`)AV%~ zhb#`3{1%d@+`zp8AaC@@d56fYv(J7aRNhz7>TfS-0cxNQ)SeosuIj2t>Rq)L+?ICI z3RS3DVC$~Bqwc7El~ZY`zmx+J+kbj^P+$X!!Qny;LV||9A<27K1fmfU5omTuUK0~< z0?-g95%6(vQIlvu0k{jwwb|X+jPwnlsxO2YMSFAJ-j;dq_<7;xy!|cn)B^sL-A4k7 z=3M?gI~P=3N@=AW4|uWZt1JkgRP>yf@QR-iSl)%?jkwk0?8hl%i{PlyqauBSE#7Nc zk4+_8Z1_;1;P%#zrS_X#r+xaLuJFgdwLBz!hqX0;-rj-}(sryX-4H1>^e42Nh(d6n z4%BUxs6^aO*LS`8)5Mkf|8?Q^=hVNA>pxRno#^;N*R2;Yi3v+V@mw$0j+pupwva(NpR!g(7{U#FH#AD*iGoAIzg(G) zZIV((EJ=A~bsWcWX{xL1_k2|TCZaVeibmW2BU+A57LhemQC&$LJn}>=L?=Wd?yg)< zhzYVJo!P1^T(Qm)L_pH-1Nd}>1rZD{06o=JJ=LQGkop8W@gNe=Qj%}YBRD1bfaiLP z&7wtqGCgSH`X6M!6G2!&<8M&cf1!K5rR-C^o?lB z_({dcFLnMT%j?NxR23WJ{yDa99^1Z;q0}M|Hxa)z1gSJ@2ueOlfh$erBc8kVanw4R zK~yhH6D_n_KiPmS5w;b!a?1NF8}4mE@ei1&aC?EbiqyW^7aTL>?W$b~Z=oQ%t6o({ zYF{PFC*(Xf6Y^)7x@Q`B%EJrmiwmq|_7<;`CkC$R0U-O{;ZqPN}Yp zEb=K31yK-3@mujLao3;BAW~b&8|>FU&2VP2$oE}is<9YXPu-MXS?>9-u9Vs=hxn+# iuPkZ$fe+c_mr?pt79YKMZ4%ag!Mw}T%p=uRaqB;|l3h>$ 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.45.2