-
Notifications
You must be signed in to change notification settings - Fork 18
/
nagiosautoupdate.py
executable file
·293 lines (242 loc) · 9.8 KB
/
nagiosautoupdate.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Mise à jour automatique de Nagios sous Ubuntu/Debian
# Nicolas Hennion aka Nicolargo
# Script libre: GPLv3
#
# Syntaxe: (as root)
# ./nagiosautoupdate.py
#
# Nicolargo (aka) Nicolas Hennion
# http://www.nicolargo.com
#
"""
Update Nagios Core / Nagios Plugin / NRPE to the latest version
"""
import os, sys, platform, getopt, shutil, logging, getpass
# Global variables
#-----------------------------------------------------------------------------
_VERSION = "4.0.2_01"
_DEBUG = 0
log_file = "/tmp/nagiosautoupdate.log"
nagios_core_version = "4"
nagios_core_subversion = "4.0.2"
nagios_plugins_version = "1.5"
nrpe_version = "2.15"
nagios_user = "nagios"
nagios_group = "nagios"
###################################
# Do not touch code under this line
# Classes
#-----------------------------------------------------------------------------
class colors:
RED = '\033[91m'
GREEN = '\033[92m'
BLUE = '\033[94m'
ORANGE = '\033[93m'
NO = '\033[0m'
def disable(self):
self.RED = ''
self.GREEN = ''
self.BLUE = ''
self.ORANGE = ''
self.NO = ''
# Functions
#-----------------------------------------------------------------------------
def init():
"""
Init the script
"""
# Globals variables
global _VERSION
global _DEBUG
# Set the log configuration
logging.basicConfig(
filename=log_file,
level=logging.DEBUG,
format='%(asctime)s %(levelname)s - %(message)s',
datefmt='%d/%m/%Y %H:%M:%S',
)
def syntax():
"""
Print the script syntax
"""
print "This script should be run as root."
print "Some options: -d to debug / -v to print the version and exit"
def version():
"""
Print the script version
"""
sys.stdout.write ("Script version %s" % _VERSION)
sys.stdout.write (" (running on %s %s)\n" % (platform.system() , platform.machine()))
def isroot():
"""
Check if the user is root
Return TRUE if user is root
"""
return (os.geteuid() == 0)
def showexec(description, command, exitonerror = 0):
"""
Exec a system command with a pretty status display (Running / Ok / Warning / Error)
By default (exitcode=0), the function did not exit if the command failed
"""
if _DEBUG:
logging.debug ("%s" % description)
logging.debug ("%s" % command)
# Manage very long description
if (len(description) > 65):
description = description[0:65] + "..."
# Display the command
status = "[Running]"
statuscolor = colors.BLUE
sys.stdout.write (colors.NO + "%s" % description + statuscolor + "%s" % status.rjust(79-len(description)) + colors.NO)
sys.stdout.flush()
# Run the command
returncode = os.system ("/bin/sh -c \"%s\" >> %s 2>&1" % (command, log_file))
# Display the result
if returncode == 0:
status = "[ OK ]"
statuscolor = colors.GREEN
else:
if exitonerror == 0:
status = "[Warning]"
statuscolor = colors.ORANGE
else:
status = "[ Error ]"
statuscolor = colors.RED
sys.stdout.write (colors.NO + "\r%s" % description + statuscolor + "%s\n" % status.rjust(79-len(description)) + colors.NO)
if _DEBUG:
logging.debug ("Returncode = %d" % returncode)
# Stop the program if returncode and exitonerror != 0
if ((returncode != 0) & (exitonerror != 0)):
if _DEBUG:
logging.debug ("Forced to quit")
exit(exitonerror)
def getpassword(description = ""):
"""
Read password (with confirmation)
"""
if (description != ""):
sys.stdout.write ("%s\n" % description)
password1 = getpass.getpass("Password: ");
password2 = getpass.getpass("Password (confirm): ");
if (password1 == password2):
return password1
else:
sys.stdout.write (colors.ORANGE + "[Warning] Password did not match, please try again" + colors.NO + "\n")
return getpassword()
def nagiosbackup():
"""
Backup the current Nagios configuration in the /tmp/nagios-backup.tgz file
"""
showexec ("Backup the current Nagios configuration",
"tar zcvfh /tmp/nagios-backup.tgz /usr/local/nagios --exclude var/archives")
def nagiosupdate():
"""
Update Nagios Core + plugins + NRPE
"""
# Double check if the libperl-dev is installed
showexec ("Install prerequisites",
"apt-get -q -y --force-yes install libperl-dev libssl-dev", 1)
# Download sources
showexec ("Download Nagios Core version %s" % nagios_core_subversion,
"wget --no-check-certificate -c -O /tmp/nagios-%s.tar.gz http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-%s.tar.gz" % (nagios_core_subversion, nagios_core_subversion), 1)
showexec ("Download Nagios Plugins version %s" % nagios_plugins_version,
"wget --no-check-certificate -c -O /tmp/nagios-plugins-%s.tar.gz https://www.nagios-plugins.org/download/nagios-plugins-%s.tar.gz" % (nagios_plugins_version, nagios_plugins_version), 1)
showexec ("Download NRPE version %s" % nrpe_version,
"wget --no-check-certificate -c -O /tmp/nrpe-%s.tar.gz http://surfnet.dl.sourceforge.net/sourceforge/nagios/nrpe-%s.tar.gz" % (nrpe_version, nrpe_version), 1)
# Update Nagios Core
showexec ("Uncompress Nagios Core" ,
"cd /tmp ; tar zxvf nagios-%s.tar.gz" % nagios_core_subversion, 1)
showexec ("Configure Nagios Core" ,
"cd /tmp/nagios-%s ; ./configure --with-nagios-user=%s --with-nagios-group=%s --with-command-user=%s --with-command-group=%s --enable-event-broker --enable-nanosleep --enable-embedded-perl --with-perlcache" % (nagios_core_subversion, nagios_user, nagios_group, nagios_user, nagios_group), 1)
showexec ("Make Nagios Core" ,
"cd /tmp/nagios-%s ; make all" % nagios_core_subversion, 1)
# showexec ("Correct a bug in the installer (http://bit.ly/roq2ea)" ,
# "cd /tmp/nagios/html ; sed -i 's/for file in includes\/rss\/\*\;/for file in includes\/rss\/\*\.\*\;/g' ./Makefile ; sed -i 's/for file in includes\/rss\/extlib\/\*\;/for file in includes\/rss\/extlib\/\*\.\*\;/g' ./Makefile", 1)
showexec ("Install Nagios Core" ,
"cd /tmp/nagios-%s ; make fullinstall" % nagios_core_subversion, 1)
# Solve start daemon issue: /etc/init.d/nagios: 20: .: Can't open /etc/rc.d/init.d/functions
showexec ("Hack for Nagios 4.0 and 4.0.1" ,
"apt-get install daemon && if [ ! -e /etc/rc.d/init.d/functions ]; then sed -i 's/^\.\ \/etc\/rc.d\/init.d\/functions$/\.\ \/lib\/lsb\/init-functions/g' /etc/init.d/nagios; sed -i 's/status\ /status_of_proc\ /g' /etc/init.d/nagios; sed -i 's/daemon\ --user=\$user\ \$exec\ -ud\ \$config/daemon\ --user=\$user\ --\ \$exec\ -d\ \$config/g' /etc/init.d/nagios; sed -i 's/\/var\/lock\/subsys\/\$prog/\/var\/lock\/\$prog/g' /etc/init.d/nagios; sed -i 's/\/sbin\/service\ nagios\ configtest/\/usr\/sbin\/service\ nagios\ configtest/g'; /etc/init.d/nagios sed -i 's/\"\ \=\=\ \"/\"\ \=\ \"/g' /etc/init.d/nagios; sed -i 's/\#\#killproc\ \-p\ \$\{pidfile\}\ \-d\ 10/killproc\ \-p \$\{pidfile\}/g' /etc/init.d/nagios; sed -i 's/runuser/su/g' /etc/init.d/nagios; fi;", 1)
# Update Nagios Plugins
showexec ("Uncompress Nagios Plugins" ,
"cd /tmp ; tar zxvf nagios-plugins-%s.tar.gz" % nagios_plugins_version, 1)
showexec ("Configure Nagios Plugins" ,
"cd /tmp/nagios-plugins-%s ; ./configure --with-nagios-user=%s --with-nagios-group=%s" % (nagios_plugins_version, nagios_user, nagios_group), 1)
showexec ("Make Nagios Plugins" ,
"cd /tmp/nagios-plugins-%s ; make" % nagios_plugins_version, 1)
showexec ("Install Nagios Core" ,
"cd /tmp/nagios-plugins-%s ; make install; make install-root" % nagios_plugins_version, 1)
# Update NRPE
showexec ("Uncompress Nagios NRPE" ,
"cd /tmp ; tar zxvf nrpe-%s.tar.gz" % nrpe_version, 1)
if (platform.architecture()[0].startswith('64')):
showexec ("Configure Nagios NRPE" ,
"cd /tmp/nrpe-%s ; ./configure --with-nagios-user=%s --with-nagios-group=%s --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib/x86_64-linux-gnu --enable-command-args --enable-ssl" % (nrpe_version, nagios_user, nagios_group), 1)
else:
showexec ("Configure Nagios NRPE" ,
"cd /tmp/nrpe-%s ; ./configure --with-nagios-user=%s --with-nagios-group=%s --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib --enable-command-args --enable-ssl" % (nrpe_version, nagios_user, nagios_group), 1)
showexec ("Make Nagios NRPE" ,
"cd /tmp/nrpe-%s ; make all" % nrpe_version, 1)
showexec ("Install Nagios NRPE" ,
"cd /tmp/nrpe-%s ; make install-plugin" % nrpe_version, 1)
# Set files rights
showexec ("Set files rights to nagios:nagios" ,
"chown -R %s:%s /usr/local/nagios" % (nagios_user, nagios_group), 1)
def nagioscheck():
"""
Check the Nagios configuration after the upgrade
"""
showexec ("Check the current Nagios configuration" ,
"/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg")
def nagiosrestart():
"""
Restart Nagios and NRPE
"""
showexec ("Restart NRPE", "/etc/init.d/nagios-nrpe-server restart")
showexec ("Restart Nagios", "/etc/init.d/nagios restart")
def main(argv):
"""
Main function
"""
try:
opts, args = getopt.getopt(argv, "hvd", ["help", "version", "debug"])
except getopt.GetoptError:
syntax()
exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
syntax()
exit()
elif opt == '-v':
version()
exit()
elif opt == '-d':
global _DEBUG
_DEBUG = 1
#=================
# Start the script
#=================
# Check if user is root
if (not isroot()):
print "This script should be run as root."
exit(2)
# Check if Nagios is already installed
if (not os.path.isfile("/usr/local/nagios/bin/nagios")):
print "Nagios is not installed on your system"
print "or Nagios has been installed via a package manager"
print "This script can not upgrade this configuration"
exit(2)
# Update
nagiosbackup()
nagiosupdate()
nagioscheck()
nagiosrestart()
# Main program
#-----------------------------------------------------------------------------
if __name__ == "__main__":
init()
main(sys.argv[1:])
exit()