Nagios搭建与139邮箱短信报警设置
最近因为工作需要搭建nagios,初次接触,看了很多网上很多nagios的教程,历尽千辛万苦最终搭建了一个简单的nagios,结合网上的教程我把自己做的配置贴出来,有错误欢迎指正
一:环境准备
由于要搭建的nagios主机是一个裸机,所以搭建nagios监控系统前需要搭建一个简单的LAMP平台,
这里我就直接用yum安装了
yum -y install http* mysql* php-*
对于英语不好的我,每做完一步我就喜欢用echo $? 验证一下 看看返回值是不是0 如果是0说明操作正确,否则错误,这种方法对我很实用,谁让英语差呢!
[root@localhost ~]# service httpd start
启动 httpd: [确定]
[root@localhost ~]# chkconfig httpd on
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
231 User apache
232 Group apache
391 DirectoryIndex index.php index.html index.html.var
992 #setting for nagios
993 ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
994 <Directory "/usr/local/nagios/sbin">
995 AuthType Basic
996 Options ExecCGI
997 AllowOverride None
998 Order allow,deny
999 Allow from all
1000 AuthName "nagios access"
1001 AuthUserFile /usr/local/nagios/etc/htpasswd
1002 Require valid-user
1003 </Directory>
1004 Alias /nagios "/usr/local/nagios/share"
1005 <Directory "/usr/local/nagios/share">
1006 AuthType Basic
1007 Options ExecCGI
1008 AllowOverride None
1009 Order allow,deny
1010 Allow from all
1011 AuthName "nagios access"
1012 AuthUserFile /usr/local/nagios/etc/htpasswd
1013 Require valid-user
1014 </Directory>
创建apache目录的验证文件touch /usr/local/nagios/etc/htpasswd
添加用户
[root@localhost ~]# htpasswd -c /usr/local/nagios/etc/htpasswd cainiao
二:nagios安装
(1)创建nagios程序用户、组
[root@localhost ~]# useradd -s /sbin/nologin nagios
[root@localhost ~]# mkdir /usr/local/nagios/
[root@localhost ~]# chown -R nagios:nagios /usr/local/nagios/
( 2) 安装和编译nagios
搭建nagios所需要的软件已经放在/nagios
[root@localhost nagios]# tar zxf nagios-3.2.0.tar.gz
[root@localhost nagios]# cd nagios-3.2.0
[root@localhost nagios-3.2.0]# ./configure --prefix=/usr/local/nagios/
[root@localhost nagios-3.2.0]# make all ; make install ; make install-init ; make install-commandmode ; make install-config
[root@localhost nagios-3.2.0]# chkconfig --add nagios
[root@localhost nagios-3.2.0]# chkconfig nagios on
(3)安装nagios插件
[root@localhost nagios]# tar zxf nagios-plugins-1.4.14.tar.gz
[root@localhost nagios]# cd nagios-plugins-1.4.14
[root@localhost nagios-plugins-1.4.14]# ./configure --prefix=/usr/local/nagios/ ; make ; make insta
(4)安装Nagios的汉化包
[root@localhost nagios]# tar jxf nagios-cn-3.2.0.tar.bz2
[root@localhost nagios]# cd nagios-cn-3.2.0
[root@localhost nagios-cn-3.2.0]# ./configure ; make all ; make install
接下来通过浏览器验证nagios是否安装成功
http:.//nagios的ip/nagios 这里就假如nagios的服务器的ip是:192.168.1.1
用户就是刚才新建的 cainiao 密码是刚才新建此用户的输入的
接下来就是难点了
三:nagios服务器配置过程
这里先说一下nagios的几个配置文件的作用
Nagios默认有如下几个配置文件修改:
# ll /usr/local/nagios/etc/objects
commands.cfg 设定默认的指令来执行某个监控,也可以自己设定
contacts.cfg 设定联系人,出问题时的联系人与联系组
localhost.cfg 设定对本服务器的监控,配置其他服务时可参考此文件
timeperiods.cfg 设定周一至周五7X24小时不间断,或自定义其他时间段
hosts.cfg 设定被监控的主机(自己创建)
services.cfg 设定被监控的服务(自己创建)
(1) 定义hosts.cfg文件
[root@localhost ~]# cd /usr/local/nagios/etc/
[root@localhost etc]# vim hosts.cfg
define host{
use linux-server
host_name web
alias benet-web
address 192.168.1.100
define host{
use linux-server
host_name mysql
alias benet-mysql
address 192.168.1.200
}
define hostgroup{
hostgroup_name sa-servers
alias sa servers
members web,mysql
}
( 2 )定义services.cfg
################- yuanlin web -##########################
define service{
use local-service
host_name web
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service
host_name web
service_description SSH
check_command check_ssh
}
define service{
use local-service
host_name web
service_description SSHD
check_command check_tcp!22
}
define service{
use local-service
host_name web
service_description http
check_command check_http
}
####################- yuanlin MYSQL -##########################
define service{
use local-service
host_name mysql
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service
host_name mysql
service_description SSH
check_command check_ssh
}
define service{
use local-service
host_name mysql
service_description ftp
check_command check_ftp
}
define service{
use local-service
host_name mysql
service_description mysqlport
check_command check_tcp!3306
}
(3) 定义cgi.cfg
[root@localhost etc]# cd /usr/local/nagios/etc/
[root@localhost etc]# vim cgi.cfg
107 default_user_name=benet
119 authorized_for_system_information=nagiosadmin,cainiao
131 authorized_for_configuration_information=nagiosadmin,cainiao
144 authorized_for_system_commands=cainiao
157 authorized_for_all_services=nagiosadmin,cainiao
158 authorized_for_all_hosts=nagiosadmin,cainiao
171 authorized_for_all_service_commands=nagiosadmin,cainiao
172 authorized_for_all_host_commands=nagiosadmin,cainiao
定义nagios.cfg
[root@localhost etc]# vim nagios.cfg
19 log_file=/usr/local/nagios/var/nagios.log
30 cfg_file=/usr/local/nagios/etc/hosts.cfg
31 cfg_file=/usr/local/nagios/etc/services.cfg
32 cfg_file=/usr/local/nagios/etc/commands.cfg
33 cfg_file=/usr/local/nagios/etc/contacts.cfg
34 cfg_file=/usr/local/nagios/etc/timeperiods.cfg
35 cfg_file=/usr/local/nagios/etc/templates.cfg
[root@localhost etc]# cd objects/
[root@localhost objects]# cp commands.cfg contacts.cfg timeperiods.cfg templates.cfg localhost.cfg /usr/local/nagios/etc/
四:Nagios的运行和维护
1) 验证nagios主配置文件的语法正确性
[root@localhost etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
2) 启动与停止nagios
[root@localhost etc]# service nagios start
Starting nagios:This account is currently not available.
done.
[root@localhost etc]# service nagios status
nagios (pid 5106) is running...
[root@localhost etc]# service nagios stop
Stopping nagios: .done.
[root@localhost etc]# service nagios restart
Running configuration check...done.
Stopping nagios: No lock file found in /usr/local/nagios//var/nagios.lock
Starting nagios:This account is currently not available.
done.
一、 Nagios性能分析图表的实现
1) 安装PNP支持包rrdtool工具
[root@localhost nagios]# tar zxf rrdtool-1.4.5.tar.gz
[root@localhost nagios]# cd rrdtool-1.4.5
[root@localhost rrdtool-1.4.5]# ./configure --prefix=/usr/local/rrdtool ; make ; make install
2)安装PNP
[root@localhost rrdtool-1.4.5]# cd /aaa/
[root@localhost nagios]# tar zxf pnp-0.4.13.tar.gz
[root@localhost nagios]# cd pnp-0.4.13
[root@localhost pnp-0.4.13]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool --with-perfdata-di
r=/usr/local/nagios/share/perfdata ; make all ; make install ; make install-config ; make install-init
3) 配置PNP
A,创建默认配置文件
[root@localhost pnp-0.4.13]# cd /usr/local/nagios/etc/pnp/
[root@localhost pnp]# cp process_perfdata.cfg-sample process_perfdata.cfg
[root@localhost pnp]# cp npcd.cfg-sample npcd.cfg
[root@localhost pnp]# cp rra.cfg-sample rra.cfg
[root@localhost pnp]# chown -R nagios:nagios /usr/local/nagios/etc/pnp/
B,修改process_perfdata.cfg
[root@localhost pnp]# vi /usr/local/nagios/etc/pnp/process_perfdata.cfg
44 LOG_LEVEL = 2
4) 修改Nagios主配置文件
A,增加小太阳图标
[root@localhost pnp]# cd /usr/local/nagios/etc/
[root@localhost etc]# vi templates.cfg
define host{
name hosts-pnp
register 0
action_url /nagios/pnp/index.php?host=$HOSTNAME$
process_perf_data 1
}
define service{
name services-pnp
register 0
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
process_perf_data 1
}
B、修改nagios.cfg
找到如下几行去掉注释
[root@localhost etc]# vi nagios.cfg
833 process_performance_data=1
845 host_perfdata_command=process-host-perfdata
846 service_perfdata_command=process-service-perfdata
C、修改commands.cfg
227 # 'process-host-perfdata' command definition
228 define command{
229 command_name process-host-perfdata
230 command_line /usr/local/nagios/libexec/process_perfdata.pl
231 }
232
233
234 # 'process-service-perfdata' command definition
235 define command{
236 command_name process-service-perfdata
237 command_line /usr/local/nagios/libexec/process_perfdata.pl
238 }
D、修改hosts.cfg文件和services.cfg文件
[root@localhost ~]# vi /usr/local/nagios/etc/hosts.cfg
define host{
use linux-server,hosts-pnp
host_name web
alias benet-web
address 192.168.1.100
}
define host{
use linux-server,hosts-pnp
host_name mysql
alias benet-mysql
address 192.168.1.200
}
define hostgroup{
hostgroup_name sa-servers
alias sa servers
members web,mysql
}
[root@localhost ~]# vi /usr/local/nagios/etc/services.cfg
################- benet web -##########################
define service{
use local-service,services-pnp
host_name web
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service,services-pnp
host_name web
service_description SSH
check_command check_ssh
}
注意项:别心急看小太阳数据,等会儿就有了。
点击小太阳,出现对主机或服务的监控页面
五,利用插件扩展Nagios的监控功能
下面我们通过在nagios服务端和客户端安装NRPE插件来搭建一个更加完善的Nagios监控系统。
1)配置nagios客户端(即远端被监控主机192.168.1.200)
A)安装Nagios插件
登陆192.168.1.200客户端
[root@localhost ~]# cd /nagios/
[root@localhost nagios]# useradd -s /sbin/nologin nagios
[root@localhost nagios]# tar zxf nagios-plugins-1.4.14.tar.gz
[root@localhost nagios]# cd nagios-plugins-1.4.14
[root@localhost nagios-plugins-1.4.14]# ./configure ; make ;make install
[root@localhost nagios-plugins-1.4.14]# chown nagios:nagios /usr/local/nagios/
[root@localhost nagios-plugins-1.4.14]# chown -R nagios.nagios /usr/local/nagios/libexec/
B)安装NRPE插件
[root@localhost nagios]# cd /aaa/
[root@localhost nagios]# tar zxf nrpe-2.12.tar.gz
[root@localhost nagios]# cd nrpe-2.12
[root@localhost nrpe-2.12]# ./configure ; make all ; make install-plugin ; make install-daemon ; make install-daemon-config
C)修改NRPE配置文件
[root@localhost nrpe-2.12]# vi /usr/local/nagios/etc/nrpe.cfg
79 allowed_hosts=127.0.0.1,192.168.1.1(nagios服务器ip)
192.168.6.199:指定nagios服务器地址
D)启动NRPE
[root@localhost nrpe-2.12]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@localhost nrpe-2.12]# netstat -antl |grep 5666
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN
E)测试NRPE
[root@localhost nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
NRPE v2.12
如果出现NRPE v2.12表明NRPE已经正常工作
定义让nagios服务器监控我本机上的内容(当前用户数,CPU,内存,硬盘等信息)
[root@localhost libexec]# vi /usr/local/nagios/etc/nrpe.cfg
199 command[check_users_1]=/usr/local/nagios/libexec/check_users -w 5 -c 10
200 command[check_load_1]=/usr/local/nagios/libexec/check_load -w 15,10,5 - c 30,25,20
201 command[check_sda1_1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10 % -p /dev/sda1
202 command[check_zombie_procs_1]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
203 command[check_total_procs_1]=/usr/local/nagios/libexec/check_procs -w 1 50 -c 200
重启NRPE
[root@localhost ~]# ps -elf |grep nrpe
5 S nagios 22379 1 0 75 0 - 1243 - 17:59 ? 00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
0 R root 22586 4247 0 78 0 - 1039 - 18:09 pts/0 00:00:00 grep nrpe
[root@localhost ~]# kill -9 22379
[root@localhost ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d
被监控端192.168.1.100与192.168.1.200的步骤一样,这里就略过
2配置nagios服务器端
登陆nagios服务器端:192.168.1.1
1)安装NRPE插件
[root@localhost etc]# cd /aaa/
[root@localhost aaa]# tar zxf nrpe-2.12.tar.gz
[root@localhost aaa]# cd nrpe-2.12
[root@localhost nrpe-2.12]# ./configure ; make ; make install-plugin
2) 测试插件能否取得与客户端192.168.1.200的联系
[root@localhost nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.200
NRPE v2.12
出现NRPE v2.12就证明成功联系上客户端
3)定义一个check_nrpe监控命令
[root@localhost nrpe-2.12]# vi /usr/local/nagios/etc/commands.cfg
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
5) 添加远程主机监控项
[root@localhost nrpe-2.12]# vi /usr/local/nagios/etc/services.cfg
define service{
use local-service
host_name mysql
service_description users
check_command check_nrpe!check_users
}
define service{
use local-service
host_name mysql
service_description load
check_command check_nrpe!check_load
}
define service{
use local-service
host_name mysql
service_description disk
check_command check_nrpe!check_sda1
}
define servicegroup{
servicegroup_name servergroup
alias server-group
members web,PING,web,SSH,web,SSHD,web,http,mysql,users,mysql,load,mysql,disk
}
6)测试并重新启动nagios服务
[root@localhost ~]# service nagios restart
139邮件报警和短信报警
首先要去注册139邮箱,然后收到邮件时手机就会收到邮件
这个配置特别简单
确定你的sendmail是否运行 service sendmail status
如果没有安装就直接 yum -y install sendmail
修改配置文件:contacts.cfg 只需要修改邮箱即可,如果有多个联系人可以用逗号隔开