-
Notifications
You must be signed in to change notification settings - Fork 18
/
nagiosautoinstall-ubuntu.sh
executable file
·215 lines (193 loc) · 7.87 KB
/
nagiosautoinstall-ubuntu.sh
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
#!/bin/sh
#
# Installation automatique de Nagios sous Ubuntu/Debian
# Nicolas Hennion aka Nicolargo
# Script libre: GPLv3
#
# Syntaxe: # sudo ./nagiosautoinstall-ubuntu.sh
#
version="4.0.2_01"
nagios_core_version="4"
nagios_core_subversion="4.0.1"
nagios_plugins_version="1.5"
nrpe_version="2.15"
nagios_user="nagios"
nagios_group="nagios"
nagiosweb_user="nagiosadmin"
###################################
# Do not touch code under this line
apt="apt-get -q -y --force-yes"
wget="wget --no-check-certificate -c"
check_x64=`uname -a | grep -e "_64"`
# Fonction: installation
installation() {
# Pre-requis
echo "----------------------------------------------------"
echo "Install common libs and configuration Postfix"
echo "----------------------------------------------------"
$apt install apache2 wget libapache2-mod-php5 build-essential libgd2-xpm-dev libperl-dev rrdtool librrds-perl
$apt install bind9-host dnsutils bind9utils libradius1 qstat radiusclient1 snmp snmpd
$apt install libpng12-dev libjpeg62 libjpeg62-dev
$apt install fping libnet-snmp-perl libldap-dev libmysqlclient-dev libgnutls-dev libradiusclient-ng-dev
$apt install libssl-dev openssl-blacklist openssl-blacklist-extra
$apt install bsd-mailx mailutils postfix
ln -s /usr/bin/mail /bin/mail
# Creation de l'utilisateur nagios et du groupe nagios
echo "----------------------------------------------------"
echo "Create the Nagios user and group"
echo "Nagios user: ${nagios_user}"
echo "Nagios group: ${nagios_group}"
echo "----------------------------------------------------"
echo "Add the Nagios user account (${nagios_user}) in the www-data group"
useradd -m -G www-data -s /bin/bash ${nagios_user}
echo "Set a password for the Nagios user account (${nagios_user})"
passwd ${nagios_user}
# Recuperation des sources
echo "----------------------------------------------------"
echo "Download sources"
echo "Nagios Core version: ${nagios_core_subversion}"
echo "Nagios Plugin version: ${nagios_plugins_version}"
echo "NRPE version: ${nrpe_version}"
echo "----------------------------------------------------"
mkdir ~/nagiosinstall
cd ~/nagiosinstall
$wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-${nagios_core_subversion}.tar.gz
$wget https://www.nagios-plugins.org/download/nagios-plugins-${nagios_plugins_version}.tar.gz
$wget http://surfnet.dl.sourceforge.net/sourceforge/nagios/nrpe-${nrpe_version}.tar.gz
# Compilation de Nagios Core
echo "----------------------------------------------------"
echo "Nagios Core compilation"
echo "----------------------------------------------------"
cd ~/nagiosinstall
tar zxvf nagios-${nagios_core_subversion}.tar.gz
cd nagios-${nagios_core_subversion}
./configure --with-nagios-user=${nagios_user} --with-nagios-group=${nagios_group} --with-command-user=${nagios_user} --with-command-group=$nagios_group --enable-event-broker --enable-nanosleep --enable-embedded-perl --with-perlcache
make all
make fullinstall
make install-config
ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios
echo "----------------------------------------------------"
echo "Hack for Nagios 4.0 and 4.0.1"
echo "Solve following issue on daemon script"
echo "----------------------------------------------------"
apt-get install daemon
if [ ! -e /etc/rc.d/init.d/functions ];
then
sudo sed -i 's/^\.\ \/etc\/rc.d\/init.d\/functions$/\.\ \/lib\/lsb\/init-functions/g' /etc/init.d/nagios
sudo sed -i 's/status\ /status_of_proc\ /g' /etc/init.d/nagios
sudo sed -i 's/daemon\ --user=\$user\ \$exec\ -ud\ \$config/daemon\ --user=\$user\ --\ \$exec\ -d\ \$config/g' /etc/init.d/nagios
sudo sed -i 's/\/var\/lock\/subsys\/\$prog/\/var\/lock\/\$prog/g' /etc/init.d/nagios
sudo sed -i 's/\/sbin\/service\ nagios\ configtest/\/usr\/sbin\/service\ nagios\ configtest/g' /etc/init.d/nagios
sudo sed -i 's/\"\ \=\=\ \"/\"\ \=\ \"/g' /etc/init.d/nagios
sudo sed -i 's/\#\#killproc\ \-p\ \$\{pidfile\}\ \-d\ 10/killproc\ \-p \$\{pidfile\}/g' /etc/init.d/nagios
sudo sed -i 's/runuser/su/g' /etc/init.d/nagios
fi
echo "----------------------------------------------------"
echo "Set the password for the Nagios Web interface account"
echo "Nagios web interface account: $nagiosweb_user"
echo "----------------------------------------------------"
htpasswd -c /usr/local/nagios/etc/htpasswd.users ${nagiosweb_user}
/etc/init.d/apache2 reload
# Compilation de Nagios plugins
echo "----------------------------------------------------"
echo "Nagios plugins compilation"
echo "----------------------------------------------------"
cd ~/nagiosinstall
tar zxvf nagios-plugins-${nagios_plugins_version}.tar.gz
cd nagios-plugins-${nagios_plugins_version}
./configure --with-nagios-user=${nagios_user} --with-nagios-group=${nagios_group} --enable-extra-opts
make
make install
# Compilation de NRPE
cd ~/nagiosinstall
echo "----------------------------------------------------"
echo "NRPED compilation"
echo "----------------------------------------------------"
tar zxvf nrpe-${nrpe_version}.tar.gz
cd nrpe-${nrpe_version}
if [[ ${check_x64} -ne 0 ]]; then
./configure --with-nagios-user=${nagios_user} --with-nagios-group=${nagios_group} --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib/x86_64-linux-gnu --enable-command-args --enable-ssl
else
./configure --with-nagios-user=${nagios_user} --with-nagios-group=${nagios_group} --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib --enable-command-args --enable-ssl
fi
make all
make install-plugin && make install-daemon && make install-daemon-config && make install-xinetd
# Installation des plugins additionnels
plugins_list="check_ddos.pl check_memory check_url.pl"
echo "----------------------------------------------------"
echo "Install additionals plugins for Nagios"
echo ${plugins_list}
echo "----------------------------------------------------"
cd /usr/local/nagios/libexec
for i in `echo ${plugins_list}`
do
rm -f $i > /dev/null
$wget https://raw.github.com/nicolargo/nagiosautoinstall/master/$i
chmod a+rx $i
chown ${nagios_user}:${nagios_group} $i
# Conf file
grep $i /usr/local/nagios/etc/objects/commands.cfg > /dev/null
if [ $? -ne 0 ]
then
case $i in
"check_ddos.pl")
cat >> /usr/local/nagios/etc/objects/commands.cfg << EOF
# check_ddos
define command{
command_name check_ddos
command_line \$USER1\$/check_ddos.pl -w \$ARG1\$ -c \$ARG2\$
}
EOF
;;
"check_memory")
cat >> /usr/local/nagios/etc/objects/commands.cfg << EOF
# CheckMemory
define command{
command_name check_memory
command_line \$USER1\$/check_memory -w \$ARG1\$ -c \$ARG2\$
}
EOF
;;
"check_url.pl")
cat >> /usr/local/nagios/etc/objects/commands.cfg << EOF
# CheckURL
# \$ARG1\$: URL a tester (exemple: http://blog.nicolargo.com/sitemap.xml)
define command{
command_name check_url
command_line \$USER1\$/check_url.pl \$ARG1\$
}
EOF
;;
esac
fi
done
cd -
# On supprime les fichiers temporaires
cd ~
rm -rf ~/nagiosinstall
}
# Fonction: Verifie si Nagios les fichiers de conf sont OK
check() {
echo "----------------------------------------------------"
echo "Check the Nagios configuration"
echo "----------------------------------------------------"
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
}
# Fonction: Lancement de Nagios
start() {
echo "----------------------------------------------------"
echo "Run Nagios"
echo "----------------------------------------------------"
/etc/init.d/nagios start
echo "Nagios Web interface URL: http://localhost/nagios/"
echo "Nagios Web interface account: ${nagiosweb_user}"
}
# Programme principal
if [ "$(id -u)" != "0" ]; then
echo "You need admin (root) right to run the script."
echo "Syntax: sudo $0"
exit 1
fi
installation
check
start