nagios的安装
wget http://cdnetworks-kr-2.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.2.3/nagios-3.2.3.tar.gz
wget http://cdnetworks-kr-2.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.15/nagios-plugins-1.4.15.tar.gz
wget http://cdnetworks-kr-1.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.12/nrpe-2.12.tar.gz
groupadd nagios
useradd nagios -g nagios
tar zxvf nagios-3.2.3.tar.gz
cd nagios-3.2.3
./configure --prefix=/usr/local/nagios
mkdir -p /etc/httpd/conf.d/
make all //编译
make install //安装nagios主要程序,cgi和html文件等
make install-init //安装nagios启动脚本
make install-config //将配置文件的例子复制到nagios配置文件里
make install-commandmode //配置nagios目录权限。
make install-webconf 因为这个命令执行的时候,会在/etc/httpd/conf.d/创建 nagios.conf文件
cat /etc/httpd/conf.d/nagios.conf >>/usr/local/apache2/conf/httpd.conf
安装nagios插件
tar zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --prefix=/usr/local/nagios/
make && make install
安装完成后,会在/usr/local/nagios/ 目录下面生成libexe插件目录,nagios所有的插件都会在这个目录里面。
chown -R nagios.nagios /usr/local/nagios/
添加apache授权用户 -c选项为创建文件,创建第二个选项不需要
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users itv1
修改cgi.cfg #vi /usr/local/nagios/etc/cgi.cfg
找到这几行,后面添加 erbin用户,可以自己定义用户,使用“,”隔开
authorized_for_system_information=nagiosadmin,itv1
authorized_for_configuration_information=nagiosadmin,itv1
authorized_for_system_commands=nagiosadmin,itv1
authorized_for_all_services=nagiosadmin,itv1
authorized_for_all_hosts=nagiosadmin,itv1
authorized_for_all_service_commands=nagiosadmin,itv1
authorized_for_all_host_commands=nagiosadmin,itv1
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg测试下,如果没有eror就可以启动了,
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg启动就行了
重新启动下apache,然后通过http://IP/nagios访问,输入用户名密码如果成功登陆,到此安装完成。
2、配置nagios监控主机的配置文件
nagios的全部配置文件在:/usr/local/nagios/etc目录下面
首先cgi.cfg
refresh_rate=30 #nagios主页的刷新时间,我设置成30秒自动刷新 use_authentication=0 #关闭认证功能,当nagios配置好后,建议开回来 |
这个文件暂时只修改了这两项
然后轮到nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/commands.cfg #nagios可调用的监控命令 cfg_file=/usr/local/nagios/etc/objects/contacts.cfg #联系人配置 cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg #监控时间配置 cfg_file=/usr/local/nagios/etc/objects/templates.cfg #模板配置 cfg_dir=/usr/local/nagios/etc/services #新添加,把需要添加的主机文件放进去,就不必在这里一行行添加 cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg #新添加,主机组配置 cfg_file=/usr/local/nagios/etc/objects/localhost.cfg #本地信息监迭 |
3、现在可以开始添加被监控机
我们上面定义了所有的主机文件都放在services目录下,那么我们在此目录下新建主机:
内容如下:
define host{ use linux-server host_name 192.168.1.211 alias 192.168.1.211 address 192.168.1.211 } define service{ use generic-service host_name 192.168.1.211 service_description check_ping check_command check_ping!100.0,20%!200.0,50% max_check_attempts 5 normal_check_interval 1 } define service{ use generic-service host_name 192.168.1.211 service_description check_ftp check_command check_ftp!21 max_check_attempts 5 normal_check_interval 1 } define service{ use generic-service host_name 192.168.1.211 service_description check_ssh check_command check_ssh max_check_attempts 5 normal_check_interval 1 } define service{ use generic-service host_name 192.168.1.211 service_description check_http check_command check_http max_check_attempts 5 normal_check_interval 1 }
|
上面监控了ping值,ftp服务,ssh服务还有http服务,我拿一个例子来说明
define host{ use linux-server host_name 192.168.1.211 alias 192.168.1.211 address 192.168.1.211 } define service{ use generic-service host_name 192.168.1.211 service_description check_ping check_command check_ping!100.0,20%!200.0,50% max_check_attempts 5 normal_check_interval 1 } |
例如这一段,首先define host定义了这台被监控主机,它所用的模板是linux-server这个,那这个模板又是在哪里定义的呢,就是在刚才nagios里不是有一行模板配置信息吗,就是那个文件,我打开templates.cfg 文件并找到linux-server模板,这模板的信息是这样的:
define host{ name linux-server ; The name of this host template use generic-host ; This template inherits other values from the generic-host template check_period 24x7 ; By default, Linux hosts are checked round the clock check_interval 5 ; Actively check the host every 5 minutes retry_interval 1 ; Schedule host check retries at 1 minute intervals max_check_attempts 10 ; Check each Linux host 10 times (max) check_command check-host-alive ; Default command to check Linux hosts notification_period workhours ; Linux admins hate to be woken up, so we only notify during the day ; Note that the notification_period variable is being overridden from ; the value that is inherited from the generic-host template! notification_interval 120 ; Resend notifications every 2 hours notification_options d,u,r ; Only send notifications for specific host states contact_groups admins ; Notifications get sent to the admins by default register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE! }
|
这里的信息后面都有说明,把他们拿上谷歌翻译一下便是,
其中check_period 这个是定义监控的时间
check_interval这个是每多少时间执行一次命令
max_check_attempts这个是多少次异常就会报警
这几个便是常用的配置
回到刚才的例子,然后define service就是定义要监控的东西
host_name 就是被监控机的IP
service_description 命令的描述,会在nagios主面上显示
check_command 要执行的命令
max_check_attempts 5 异常多少次会报警
normal_check_interval 1 每1分钟执行一次命令
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
客户端
关于nagios监控 mysql nginx 的一些方法
nagios 就默认配置安装时不能监控mysql 和 nginx的,mysql 还好一点,但nginx的监控看了很多文档还是不成功,不过在一个外国网站找到了一个监控nginx的方法给大家分享一下
通过nrpe 来监控nginx的进程是否运行 还可以顺便监控 CPU 硬盘呢
1.在监控机上安装NRPE
tar -zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure --prefix=/usr/local/nagios
make all
make install-plugin
2.在被监控机上安装nagios插件和NRPE
(1).先增加nagios用户:
useradd nagios
(2.)安装nagios插件:
tar -zxvf nagios-plugins-1.4.14.tar.gz
cd nagios-plugins-1.4.14
./configure --prefix=/usr/local/nagios
make
make install
(3).安装nrpe:
tar -zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure --prefix=/usr/local/nagios
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd nrpe安装为xinetd服务
(4).编辑nrpe配置文件,增加监控机地址:
#vi /etc/xinetd.d/nrpe
only_from = 127.0.0.1 10.1.1.14
(5).修改services文件,增加端口
#vi /etc/services
nrpe 5666/tcp #NRPE
(6).重启xinetd服务
service xinetd restart
(7).查看服务是否启动
#/usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.12
vim /usr/local/nagios/etc/nrpe.cfg
command[check_sda2]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda2 --意思是监控第二个分区
参考#command[check_disk]=/usr/local/nagios/libexec/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10% -监控内存
如果出现Connection refused by host 需要安装yum intall openssl*
至此被监控机上的操作就结束了,如果有需要增加监控项,需修改/etc/xinetd.d/nrpe文件。
3.修改监控机上commands.cfg文件,增加check_nrpe的定义:
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
4.在监控机上测试与被监控机的通讯是否正常:
[root@test1 objects]# /usr/local/nagios/libexec/check_nrpe -H 10.1.1.6(被监控IP) -c check_load
OK - load average: 0.05, 0.06, 0.00|load1=0.050;15.000;30.000;0; load5=0.060;10.000;25.000;0; load15=0.000;5.000;20.000;0;
能够正常获取信息。
5.修改服务配置文件:
增加:
define service {
host_name nginx1.dog.com
max_check_attempts 5
check_period 24x7
notification_interval 10
normal_check_interval 1
retry_check_interval 1
service_description diskINFO
check_command check_nrpe!check_sda2
notification_period 24x7
notification_options w,u,c,r
contact_groups doggroup
}
define service {
host_name nginx1.dog.com
max_check_attempts 5
check_period 24x7
notification_interval 10
normal_check_interval 1
retry_check_interval 1
service_description checkLoad
check_command check_nrpe!check_load
notification_period 24x7
notification_options w,u,c,r
contact_groups doggroup
}
define service {
host_name nginx1.dog.com
max_check_attempts 5
check_period 24x7
notification_interval 10
normal_check_interval 1
retry_check_interval 1
service_description checkswap
check_command check_nrpe!check_swap
notification_period 24x7
notification_options w,u,c,r
contact_groups doggroup
}
3.有关于check_load的用法及意义这个插件是用来检测系统当前的cpu负载,使用的方法为check_load [-r] -w WLOAD1,WLOAD5,WLOAD15 -c CLOAD1,CLOAD5,CLOAD15在unix里面负载的均值通常表示是1分钟,5分钟,15分钟内平均有多少进程处于等待状态.例如check_load -w 15,10,5 -c 30,25,20这个命令的意义如下当1分钟多于15个进程等待,5分钟多于10个,15分钟多于5个则为warning状态当1分钟多于30个进程等待,5分钟多于25个,15分钟多于20个则为critical状态
监控NGINX 进程是否运行(所有进程都适用哦。很有用)
在被监控机(10.1.1.2)上运行,可以测试是否有进程运行
/usr/local/nagios/libexec/check_procs -c 1:30 -C nginx
在被监控机上编辑 /usr/local/nagios/etc/nrpe_local.cfg
加入:
command[check_nginx]=/usr/local/nagios/libexec/check_procs -c 1:30 -C nginx
进入监控机
/usr/local/nagios/etc/services.cfg
define service {
host_name nginx1.dog.com
service_description CHECK_NGINX
check_period 24x7
max_check_attempts 3
normal_check_interval 5
retry_check_interval 3
contact_groups sagroup
notification_interval 480
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_nginx
notifications_enabled 1
}
重启nagios服务
监控mysql
cd /usr/lib/nagios/plugins
./check_mysql -H 10.1.1.2 -u tt -p 123 -P 3306
通这命令可以检测mysql 的状况 注意:tt 是我先再mysql授权的用户,密码是123
vim commands.cfg
增加以下:
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u tt -p 123 -P 3306
}
vi /etc/nagios/services.cfg
加入:
define service {
host_name mysql1.dog.com
max_check_attempts 5
check_period 24x7
notification_interval 10
normal_check_interval 1
retry_check_interval 1
service_description check_mysql
check_command check_mysql ----注意哦。
notification_period 24x7
notification_options w,u,c,r
contact_groups doggroup
}
故障:
不知道为什么我用守护进程启动的时候发现不行的,以前是可以的,郁闷死了,后来找了另一方法:
将nrpe独立运行
vi /etc/server
将原来的nrpe 5666/tcp 注释
vim /usr/local/nagios/etc/nrpe.cfg
加入 allowed_hosts=127.0.0.1,10.1.1.14(监控机)
command[check_sda2]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda2 --意思是监控第二个分区
参考#command[check_disk]=/usr/local/nagios/libexec/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10% -监控内存
保存
删除原来的进程(没有就不需要):
lsof -i:5666
kill -9 nrpe的ID
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d 重新启动
开机运行
vi /etc/rc.d/rc.local
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d