-
Notifications
You must be signed in to change notification settings - Fork 7
/
nagios-clients.py
executable file
·125 lines (100 loc) · 4.88 KB
/
nagios-clients.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python2.7
import os, time
import getpass
import sys
from fabric.api import *
sys.path.insert(0, './lib')
from nagiossrvclivars import *
print("")
print(' It is supposed that you have already added '+ipadd+' list of client hosts to the '+codepath+'/clients.txt'+' file')
print(' The '+password+' for all '+nagios+' clients and '+nserver+' must be the same!!!')
print("")
env.user = raw_input(' '+enter+' superuser: ')
env.password = getpass.getpass(' '+enter+' '+password+' for superuser: ')
print("==================================================================")
nagiosrvip = raw_input(' '+enter+' '+ipadd+' of '+nagios+' '+nserver+': ')
print("")
def print_func():
print(' Installation and configuration of '+nrpe+' necessary plugins are in progress...')
print("")
def nrpeconfigurator(nrpepidarg, nrpeuserarg, commandpatharg, includedirarg, disk, hostname, ipaddress):
tempnrpeVars = { "nagiossrvip" : nagiosrvip,
"nrpepidfile" : nrpepidarg,
"nrpeuser" : nrpeuserarg,
"commandpath" : commandpatharg,
"includedir" : includedirarg,
"disk" : disk,
"clienthostname" : hostname,
"clientip" : ipaddress, }
outputnrpeText = tempnrpe.render( tempnrpeVars )
outputngclientText = tempnagclient.render( tempnrpeVars )
with open(outputdir+"nrpe.cfg", "wb") as nrpeout:
nrpeout.write(outputnrpeText)
with open(ngcloutput+hostname+".cfg", "wb") as nagclient:
nagclient.write(outputngclientText)
if env.user != "root":
print(" The entered user is not 'root'. ")
print(" Sorry, this script supports only 'root' as superuser. ")
print(" Please open 'root' user authentication over SSH. ")
sys.exit()
else:
pass
def ubuntunrpe(nrpepath, checkmempath):
print_func()
run('apt-get update')
run('apt-get install -y nagios-nrpe-server nagios-plugins')
put(tempdir+'check_mem', checkmempath)
disk = run("df -h | grep '/$' | cut -f1 -d' '")
nrpeconfigurator(ubnrpepidfile, bsdandubnrpeuser, ubnrpecompath, ubincludedir, disk, hostname, ip)
put(outputdir+'nrpe.cfg', nrpepath)
run('/etc/init.d/nagios-nrpe-server restart')
def centosnrpe(nrpepath, checkmempath):
print_func()
run('yum -y install epel-release')
run('yum install -y gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel')
run("yum -y install `yum search nagios-plugins | grep '^nagios-plugins.' | cut -f1 -d' '`")
put(tempdir+'check_mem', checkmempath)
disk = run("df -h | grep root | cut -f1 -d' '")
nrpeconfigurator(cosnrpepidfile, cosnrpeuser, cosnrpecompath, cosincludedir, disk, hostname, ip)
put(outputdir+'nrpe.cfg', nrpepath)
run('/etc/init.d/nrpe restart ; chkconfig --level 0123456 nrpe on')
run('systemctl restart nrpe ; systemctl enable nrpe')
def freebsdnrpe(nrpepath, checkmempath):
print_func()
run('pkg install -y nrpe-ssl nagios-plugins')
run('sysrc nrpe2_enable="YES"')
run('mkdir /usr/local/etc/nrpe.d')
disk = run("df -h | grep '/$' | cut -f1 -d' '")
put(tempdir+'free', '/usr/local/bin/free')
put(tempdir+'freebsd_check_mem', checkmempath+'check_mem')
nrpeconfigurator(bsdnrpepidfile, bsdandubnrpeuser, bsdnrpecompath, bsdincudedir, disk, hostname, ip)
put(outputdir+'nrpe.cfg', nrpepath)
run('service nrpe2 restart')
with open(codepath+'/clients.txt') as iplist:
ipler = iplist.readlines()
for ip in ipler:
env.host_string = ip
with settings(hide('warnings', 'running', 'stdout', 'stderr'), warn_only=True):
sysver = run('uname -s')
lintype = run("cat /etc/issue | head -1 | cut -f1 -d' '")
hostname = run('hostname')
if sysver == "Linux" and lintype == "CentOS":
print(' The remote '+nserver+' identified as '+centos+' 6 !!!')
centosnrpe('/etc/nagios/', '/usr/lib64/nagios/plugins/')
run('chmod +x /usr/lib64/nagios/plugins/check_mem')
elif sysver == "Linux" and lintype == "\S":
print(' The remote '+nserver+' identified as '+centos+' 7 !!!')
centosnrpe('/etc/nagios/', '/usr/lib64/nagios/plugins/')
run('chmod +x /usr/lib64/nagios/plugins/check_mem')
elif sysver == "Linux" and lintype == "Ubuntu":
print(' The remote '+nserver+' identified as '+ubuntu+' !!!')
ubuntunrpe('/etc/nagios/', '/usr/lib/nagios/plugins/')
run('chmod +x /usr/lib/nagios/plugins/check_mem')
elif sysver == "FreeBSD":
print(' The remote '+nserver+' identified as '+freebsd+' !!!')
freebsdnrpe('/usr/local/etc/', '/usr/local/libexec/nagios/')
run('chmod +x /usr/local/bin/free /usr/local/libexec/nagios/check_mem')
env.host_string = nagiosrvip
with settings(hide('warnings', 'running', 'stdout', 'stderr'), warn_only=True):
put(ngcloutput+'*.cfg', '/etc/nagios3/conf.d/')
run('/etc/init.d/nagios3 restart')