一、nagios在服务端的安装
# vim nagios-install
#!/bin/bash
#### nagios客户端安装请注释了install nagios这段###
##install nagios
tar zxvf nagios-3.0.6.tar.gz
cd nagios-3.0.6
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf
cd ..
##install nagios-plugins
tar zxvf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure --prefix=/usr/local/nagios
make
make install
cd ..
##install nrpe
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure --enable-ssl --enable-command-args
make all
mkdir -p /usr/local/nagios/etc
mkdir -p /usr/local/nagios/bin
cp sample-config/nrpe.cfg /usr/local/nagios/etc/
cp src/nrpe /usr/local/nagios/bin/
sed -i 's/127.0.0.1/127.0.0.1,192.168.1.253/' /usr/local/nagios/etc/nrpe.cfg
sed -i 's/usr\/local/nagios/g' /usr/local/nagios/etc/nrpe.cfg
二、启动nrpe
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
如需重启nrpe,先killall -9 nrpe再执行启动
三、配置nagios
Nagios安装成功后,在/usr/local/nagios下有几个目录,分别是:
bin etc libexec sbin share var
1、在/usr/local/nagios/etc 下建立一个linux的目录,并建立一个hosts.cfg文件。
这个文件里面的内容,是分三大块:
(1)、定义主机,即host
(2)、定义监控组,即 hostgroup
(3)、定义监控主机的服务,即server
例:
# Define webgroup host for the testers machine ##定义主机
define host{
host_name web01
alias web01.com
address 192.168.1.10
check_command check-host-alive
max_check_attempts 10
check_period 24x7
notification_interval 20
notification_period 24x7
notification_options d,r
contact_groups admins
name web01
}
define host{
host_name web02
alias web02.com
address 192.168.1.20
check_command check-host-alive
max_check_attempts 10
check_period 24x7
notification_interval 20
notification_period 24x7
notification_options d,r
contact_groups admins
name web02
}
define host{
host_name web03
alias web03.com
address 192.168.1.20
check_command check-host-alive
max_check_attempts 10
check_period 24x7
notification_interval 20
notification_period 24x7
notification_options d,r
contact_groups admins
name web02
}
##----------------------------------------------------------------------------------------##
### Define all hostgroup for the whole machine ##定义监控组
# Define web group
define hostgroup{
hostgroup_name Web01 Server
alias webserver
members web01,web02 #监控多个机器用逗号隔开
define hostgroup{
hostgroup_name Web02 Server
alias webserver
members web03 #监控多个机器用逗号隔开
##----------------------------------------------------------------------------------------##
#########定义监控主机的服务###########
define service{
host_name web01,web02,web03
service_description check-procs
is_volatile 0
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
notification_options w,u,c,r
notification_interval 1
notification_period 24x7
check_command check_nrpe!check_total_procs
contact_groups admins #联系人组
notifications_enabled 1 #此处设定为0,则不报警
}
define service{
host_name web01,web02,web03
service_description check-swap
is_volatile 0
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
notification_options w,u,c,r
notification_interval 1
notification_period 24x7
check_command check_nrpe!check_swap
contact_groups admins
notifications_enabled 1
}
(4)、配置nagios.cfg文件
# cd /usr/local/nagios/etc
# vim nagios.cfg
找到cfg_file=/usr/local/nagios/etc/objects/localhost.cfg 这行,并把它注释
添加cfg_dir=/usr/local/nagios/etc/linux
(5)、配置commands.cfg命令配置文件
# cd /usr/local/nagios/etc/objects
# vim commands.cfg,在文件最后添加
define command {
command_name service-notify-by-fei
command_line /usr/local/fetion/fetion --mobile=1381137*** --pwd=***** --to=$CONTACTPAGER$ --msg-utf8="$HOSTNAME$ $SERVICEDESC$ is $SERVICESTATE$ on $TIME$ resu
lt is $SERVICEOUTPUT$" $CONTACTPAGER$
}
define command {
command_name host-notify-by-fei
command_line /usr/local/fetion/fetion --mobile=1381137**** --pwd=**** --to=$CONTACTPAGER$ --msg-utf8="Host $HOSTSTATE$ alert for $HOSTNAME$! on '$LO
NGDATETIME$'" $CONTACTPAGER$
}
(6)、配置contacts.cfg联系人配置文件
# cd /usr/local/nagios/etc/objects
# vim contacts.cfg
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members bshrer
}
define contact{
contact_name bshrer
alias bshr_server
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email,service-notify-by-fei
host_notification_commands notify-host-by-email,host-notify-by-fei
pager 1381137****
}
(7)、配置cgi.cfg
# cd /usr/local/nagios/etc
# vim cgi.cfg
use_authentication=0 这里修改为0
四、配置nagios声音报警和飞信报警
(1)# vim /usr/local/nagios/etc/cgi.cfg
host_unreachable_sound=hostdown.wav
host_down_sound=hostdown.wav
service_critical_sound=critical.wav
service_warning_sound=warning.wav
service_unknown_sound=warning.wav
normal_sound=noproblem.wav
#去掉这几项注释,保证/usr/local/nagios/share/media目录下有这些.wav文件,才能正常下行声音报警.
(2)、
# wget http://www.it-adv.net/fetion/downng/fetion20090406003-linux.tar.gz(下载地址)
# tar zxvf fetion20090406003-linux.tar.gz
# mv install fetion
# cp -r fetion /usr/local/
# chmod -R 755 /usr/local/fetion
# chown -R nagios:nagios /usr/local/fetion
# /usr/local/fetion/fetion --mobile=1381137*** --pwd=***** --to 1381137**** --msg-utf8="test"
执行如报错/usr/local/fetion/fetion: error while loading shared libraries: libACE-5.6.8.so: cannot open shared object file: No such file or directory
# cd /usr/local/fetion/fetion
# ldd fetion
linux-gate.so.1 => (0xb7fac000)
libACE-5.6.8.so => not found ##找不到模块
libACE_SSL-5.6.8.so => not found ##找不到模块
libssl.so.0.9.8 => not found ##找不到模块
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb7eae000)
libm.so.6 => /lib/libm.so.6 (0xb7e87000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7e7a000)
libc.so.6 => /lib/libc.so.6 (0xb7d37000)
/lib/ld-linux.so.2 (0xb7fad000)
libcrypto.so.0.9.8 => not found ##找不到模块
下载wget http://www.it-adv.net/fetion/downng/library_linux.tar.gz
把解压出来的文件拷到/usr/lib/下,再执行发短信,成功!
五、现在能实现监控服务器了,如果要监控多台服务器呢?被监控机只需安装两个软件即可。我还是用脚本来安装。
# vim nginx-client.sh (如多台服务器需要监控,则执行这个安装脚本就OK了,前提是下载好这些软件)
##install nagios-plugins
tar zxvf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure --prefix=/usr/local/nagios
make
make install
cd ..
##install nrpe
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure --enable-ssl --enable-command-args
make all
mkdir -p /usr/local/nagios/etc
mkdir -p /usr/local/nagios/bin
cp sample-config/nrpe.cfg /usr/local/nagios/etc/
cp src/nrpe /usr/local/nagios/bin/
sed -i 's/127.0.0.1/127.0.0.1,192.168.1.253/' /usr/local/nagios/etc/nrpe.cfg
sed -i 's/usr\/local/nagios/g' /usr/local/nagios/etc/nrpe.cfg
在nagios 服务端执行
# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.254
如出现nrpe的版本号,则表示服务端与客户端正常通讯。
六、mysql主机监控
(1)、用 plugin 产生的脚本 check_mysql 监控,只附上配置文件:
需要被监控机添加访问权限:
* 数据库授权:(登陆 DB 服务器,进行授权用户名 test,密码 test)
* mysql> grant all privileges
* �\> on *.*
* Query OK, 0 rows affected (0.00 sec)
* mysql> flush privileges;
* Query OK, 0 rows affected (0.00 sec)
(2)、修改commands.cfg命令配置文件
# /usr/local/nagios/etc/objects/commands.cfg
添加define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $ARG1$ -P $ARG2$ -u $ARG3$ -p $ARG4$
}
(3)、 修改hosts.cfg配置文件
# vim /usr/local/nagios/etc/linux/hosts.cfg
添加define service{
host_name db1-bshr
service_description mysql数据库检测
is_volatile 0
check_period 24x7
max_check_attempts 1
normal_check_interval 1
retry_check_interval 1
notification_options w,u,c,r
notification_interval 1
notification_period 24x7
check_command check_mysql!192.168.1.254!3306!test!test
contact_groups admins
notifications_enabled 1
}
全文完