nagios监控加报警的搭建
Nagios是一款开源的免费网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。Nagios 可以监控的功能有:
1.监控网络服务(SMTP、POP3、HTTP、NNTP、PING等);
2.监控主机资源(处理器负荷、磁盘利用率等);
3.简单地插件设计使得用户可以方便地扩展自己服务的检测方法;
4.并行服务检查机制;
5.具备定义网络分层结构的能力,用"parent"主机定义来表达网络主机间的关系,这种关系可被用来发现和明晰主机宕机或不可达状态;
6.当服务或主机问题产生与解决时将告警发送给联系人(通过EMail、短信、用户定义方式);
7.可以定义一些处理程序,使之能够在服务或者主机发生故障时起到预防作用;
8.自动的日志滚动功能;
9.可以支持并实现对主机的冗余监控; 10.可选的WEB界面用于查看当前的网络状态、通知和故障历史、日志文件等;
下面我们开始Nagios的安装
1.安装基础支持套件和添加用户 :
#yum install httpd gcc glibc perl-ExtUtils-Embed -y
#yum localinstall gd-devel-2.0.35-10.el6.x86_64.rpm -y #这里我不推荐这样安装,因为虽然安装了,但是在web端查看拓扑图的时候会出现错误,直接配置成网络源然后安装
# useradd nagios
# usermod -G nagios apache
2.安装Nagios:
# tar jxf nagios-cn-3.2.3.tar.bz2
# cd nagios-cn-3.2.3
# ./configure --enable-embedded-perl
# make all
# make install
# make install-init
# make install-config
# make install-commandmode
# make install-webconf
# htpasswd /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password:
Re-type new password:
Updating password for user nagiosadmin
# cat /usr/local/nagios/etc/htpasswd.users
nagiosadmin:yCS49o40QLgYU
# vim /usr/local/nagios/etc/cgi.cfg
use_authentication=0 #将这一项该为0,不然有的页面不能正常显示
# /etc/init.d/httpd restart
# /etc/init.d/nagios start
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
我们在浏览器里面输入:192.168.1.105/nagios,然后输入用户名(nagiosadmin),密码(westos这是我们之前设置的):
注意:如果登录页面错误,请检查selinux、以及火墙的设置,可以暂时关掉这两项,然后刷新页面。
3.安装nagios-plugins:nagios-plugins是nagios官方提供的一套插件程序,nagios监控主机的功能其实都是通过执行插
件程序来实现的。
# tar zxf nagios-plugins-1.5.tar.gz
# cd nagios-plugins-1.5
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make && make install
# /etc/init.d/nagios restart
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg #重新检测出现下面警告0,错误0表示我们的配置正常
Total Warnings: 0
Total Errors: 0 #然后我们可以在web端查看我们的主机状态了
4.我们对某台主机的监控,实现监控主机与服务分开来监控
# cd /usr/local/nagios/etc/
# vim nagios.cfg
[*]# You can specify individual object config files as shown below:
[*]cfg_file=/usr/local/nagios/etc/objects/commands.cfg
[*]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_file=/usr/local/nagios/etc/objects/hosts.cfg #添加主机监控模块
[*]cfg_file=/usr/local/nagios/etc/objects/services.cfg #添加服务监控模块
# cd objects/
# cp -p localhost.cfg hosts.cfg
# cp -p localhost.cfg services.cfg
# vim hosts.cfg
[*]# Define a host for the local machine
[*]
[*]define host{
[*] use linux-server ; 给网络设备用的
[*] host_name vm1.example.com
[*] alias nagios监控主机
[*] address 192.168.1.104
[*] icon_image router.gif
[*] statusmap_image router.gd2
[*] 2d_coords 300,100
[*] 3d_coords 300,100,100
[*] }
[*]# Define an optional hostgroup for Linux machines
[*]
[*]define hostgroup{
[*] hostgroup_namelinux-servers ; The name of the hostgroup
[*] alias Linux Servers ; Long name of the group
[*] members * ; Comma separated list of hosts that belong to this group
[*] }
# vim services.cfg
[*]###############################################################################
[*]
[*]# Define a service to "ping" the local machine
[*]
[*]define service{
[*] use local-service ; Name of service template to use
[*] host_name *
[*] service_description PING
[*] check_command check_ping!100.0,20%!500.0,60%
[*] }
[*]
[*]# Define a service to check the disk space of the root partition
[*]# on the local machine.Warning if < 20% free, critical if
[*]# < 10% free space on partition.
[*]
[*]define service{
[*] use local-service ; Name of service template to use
[*] host_name vm1.example.com
[*] service_description 根分区
[*] check_command check_local_disk!20%!10%!/
[*] }
[*]
[*]
[*]
[*]# Define a service to check the number of currently logged in
[*]# users on the local machine.Warning if > 20 users, critical
[*]# if > 50 users.
[*]
[*]define service{
[*] use local-service ; Name of service template to use
[*] host_name vm1.example.com
[*] service_description 登录用户数
[*] check_command check_local_users!20!50
[*] }
[*]
[*]
[*]# Define a service to check the number of currently running procs
[*]# on the local machine.Warning if > 250 processes, critical if
[*]# > 400 users.
[*]
[*]define service{
[*] use local-service ; Name of service template to use
[*] host_name vm1.example.com
[*] service_description 进程总数
[*] check_command check_local_procs!250!400!RSZDT
[*] }
[*]
[*]# Define a service to check the load on the local machine.
[*]
[*]define service{
[*] use local-service ; Name of service template to use
[*] host_name vm1.example.com
[*] service_description 系统负荷
[*] check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
[*] }
[*]
[*]
[*]
[*]# Define a service to check the swap usage the local machine.
[*]# Critical if less than 10% of swap is free, warning if less than 20% is free
[*]
[*]define service{
[*] use local-service ; Name of service template to use
[*] host_name vm1.example.com
[*] service_description 交换空间利用率
[*] check_command check_local_swap!20!10
[*] }
[*]
[*]
[*]
[*]# Define a service to check SSH on the local machine.
[*]# Disable notifications for this service by default, as not all users may have SSH enabled.
[*]
[*]define service{
[*] use local-service ; Name of service template to use
[*] host_name vm1.example.com
[*] service_description SSH
[*] check_command check_tcp!22!1.0!10.0
[*] notifications_enabled 0
[*] }
[*]
[*]
[*]
[*]# Define a service to check HTTP on the local machine.
[*]# Disable notifications for this service by default, as not all users may have HTTP enabled.
[*]
[*]define service{
[*] use local-service ; Name of service template to use
[*] host_name vm1.example.com
[*] service_description HTTP
[*] check_command check_http
[*] notifications_enabled 0
[*] }
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg #检测配置文件是否正确
Total Warnings: 0
Total Errors: 0
# /etc/init.d/nagios reload
5.设置被监控主机vm1.example.com(192.168.1.104)
(selinux and iptables is disabled)
# yum install mysql-server -y
# /etc/init.d/mysqld start
# mysql
mysql> create database nagdb;
mysql> grant select on nagdb.* to nagios@'192.168.1.104';
mysql> flush privileges;
# mysql -u nagios -h 192.168.1.104 #授权完成后我们进行一下测试
在vm.example.com(192.168.1.105)装有nagios主机上设置:
# cd /usr/local/nagios/libexec/
# ./check_mysql -H 192.168.1.104 -u nagios -d nagdb #检测104主机mysql的联通性
Uptime: 4020Threads: 1Questions: 44Slow queries: 0Opens: 15Flush tables: 1Open tables: 8Queries per second avg: 0.10|Connections=23c;;; Open_files=16;;; Open_tables=8;;; Qcache_free_memory=0;;; Qcache_hits=0c;;; Qcache_inserts=0c;;; Qcache_lowmem_prunes=0c;;; Qcache_not_cached=0c;;; Qcache_queries_in_cache=0;;; Queries=44c;;; Questions=44c;;; Table_locks_waited=0c;;; Threads_connected=1;;; Threads_running=1;;; Uptime=4020c;;;
# cd ../etc/objects/
# vim commands.cfg #添加mysql监控模块
# 'check_mysql' command definition
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -d $ARG2$
}
# vim services.cfg #添加下面的内容
###############check_mysql###########################
define service{
use local-service
host_name vm1.example.com
service_description mysql
check_command check_mysql!nagios!nagdb #这里的nagios用于上面的$ARG1$,nagdb用于上面的$ARG2$
notifications_enabled 0
}
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg #检测配置是否正确
Total Warnings: 0
Total Errors: 0
# /etc/init.d/nagios reload
接下来我们在web端去查看我们的vm1主机是否已经被监控,出现下面状态证明配置成功
再看看服务是否正常:
此时我们停止104主机上的mysql然后在查看他的监控状态:
6.Nagios 通过 NRPE 监控远程主机系统状况
NRPE是监控软件nagios的一个扩展,它被用于被监控的服务器上,向nagios监控平台提供该服务器的一些本地的情况。例如,cpu负载、内存使用、硬盘使用等等。NRPE可以称为nagios的for linux 客户端。
远程监控主机(192.168.1.104)的设定
# yum install xinetd openssl-devel mysql-devel perl-ExtUtils-MakeMaker.x86_64 -y
# tar zxf nagios-plugins-1.4.15.tar.gz
# cd nagios-plugins-1.4.15
#./configure --with-nagios-user=nagios --with-nagios-group=nagios--enable-extra-opts --enable-perl-modules --enable-libtap
# make
# make install
# tar zxf nrpe-2.15.tar.gz
# cd nrpe-2.15
# ./configure
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
# make install-xinetd
# vim /etc/xinetd.d/nrpe
only_from = 127.0.0.1 192.168.1.104 192.168.1.105 #添加监控主机的ip,以空格隔开
# vim /etc/services
nrpe 5666/tcp # nrpe #在最后添加这一行
# vim /usr/local/nagios/etc/nrpe.cfg #添加对根分区的监控
command=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
# /etc/init.d/xinetd start
# netstat -antlp|grep xinetd
tcp 0 0 :::5666 :::* LISTEN 50424/xinetd
# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
NRPE v2.15 #看到这个信息,证明我们的nrpe配置正常
要查看主机的其他监控信息,可以使用一下命令
在运行nagios的主机上:
# yum install xinetd
# tar zxf nrpe-2.15.tar.gz # cd nrpe-2.15
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
# make install-xinetd
# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.104
NRPE v2.15
检测192.168.1.104的其他状态,将下面的localhost换成192.168.1.104
# vim /usr/local/nagios/etc/objects/commands.cfg #添加下面内容
[*]# 'check_nrpe' command definition
[*]define command{
[*] command_name check_nrpe
[*] command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
[*] }
# vim /usr/local/nagios/etc/objects/services.cfg #添加下面内容
[*]#####################check_nrpe#########################
[*]define service{
[*]use local-service ; Name of service template to use
[*]host_name vm1.example.com
[*]service_description 根分区
[*]check_command check_nrpe!check_disk
[*]}
[*]
[*]# Define a service to check the number of currently logged in
[*]# users on the local machine.Warning if > 20 users, critical
[*] # if > 50 users.
[*]
[*]define service{
[*] use local-service ; Name of service template to use
[*] host_name vm1.example.com
[*] service_description 登录用户数
[*] check_command check_nrpe!check_users
[*] }
[*]
[*]# Define a service to check the number of currently running procs
[*]# on the local machine.Warning if > 250 processes, critical if
[*] # > 400 users.
[*]
[*]define service{
[*] use local-service ; Name of service template to use
[*] host_name vm1.example.com
[*] service_description 进程总数
[*] check_command check_nrpe!check_total_procs
[*] }
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# /etc/init.d/nagios reload
通过web端测试: 可能需要等一会儿将变为正常状态
nagios整合fetion(飞信)实现报警功能
http://www.it-adv.net/ 下载 fetion 主程序与支持库,这里我们将不在下载,直接使用原来下载好的
# mv fetion /usr/local/nagios/libexec/
# chown nagios.nagios /usr/local/nagios/libexec/fetion
# tar zxf linuxso_20101113.tar.gz -C /lib
# chmod +x /lib/lib*
# su - nagios
$ chmod +x /usr/local/nagios/libexec/fetion
$ /usr/local/nagios/libexec/fetion
在安装fetion的时候可能会出现下列报错,解决办法:
-bash: /usr/local/nagios/libexec/fetion: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
#yum install ld-linux.so.2 -y
# su - nagios -c /usr/local/nagios/libexec/fetion
# yum install libstdc++.so.6 -y
# su - nagios -c /usr/local/nagios/libexec/fetion
/usr/local/nagios/libexec/fetion: error while loading shared libraries: libgssapi_krb5.so.2: cannot open shared object file: No such file or directory
# yum install libgssapi_krb5.so.2 -y
# su - nagios -c /usr/local/nagios/libexec/fetion
/usr/local/nagios/libexec/fetion: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
# yum install libz.so.1 -y
安装完成后我们进行设置
# su - nagios 这里的密码是飞信的密码
$ /usr/local/nagios/libexec/fetion --mobile=13649216631 --pwd=westos --to=13649216631 --msg-utf8="good luck"
JAFmblBMcjI+TO7pz06Vo15X4EXXPF9lb+Lf7cZJjbC2ia5sI354Mu3bt+XeMep2da7m2tPE8dzGbjWdKmtgw3oulyJIy9wG88gH32n6VTVtHuSnfVG3RRRSGFee+Jvhtc+KvHGneIpdWGmf2aVWEWkQeaQKdwYu/yqck/LsYY7nOB6FRR1T7B0a7nl3jX4deJPEOt6BqUOs2V6ukXAkSG8iMDyLuViXeMMpYlQOI1AHY963irwp4m1T4ueHvEZ0iO40qwwjLbXaF12sSHZZNgHLDhS3C+uBXrVFC0a8ncHqmu6seL3un6pdftEadrd9oeqw6ZDD5UM/wBmMyE7XVSWi3BAWYn5iCBycVzHxU1G38PfHvRtX85FSJbaW5KncUAYhsgcg7McV9H1Ws9OsdO877DZW9t58hll8iJU8xz1ZsDkn1PNC05fLUHrzX6qxS0e70jX4oPEWmkzLPCYo7go8ZaPdnG1gDjIyCR344PNu30vT7S3mt7axtoYZ2Z5Y4oVVZGb7xYAYJPcnrVuih22Ags7K0060jtLG1htbaMYSGCMIi854UcCp6KKACiiigAooooAKKKKACiiigAooooAKKKKACiiigD/2Q==" />
图形验证码已经生成,文件名为:13649216631.jpg请识别后输入图形验证码:
pmfw
您输入的识别码是:pmfw
这样就完成了。
$ vim fetion.sh
/usr/local/nagios/libexec/fetion --mobile=13669281264 --pwd="haiying.910201" --to="$1" --msg-utf8="$2"
$ chmod +x fetion.sh
执行 fetion.sh 脚本看是否可以发送信息
注: 第一次调用飞信脚本时会要求你输入验证码, 在飞信程序所在的 /usr/local/fetion 目录下会生
成以你手机号命名的 jpg 图片文件,上面存放着验证码。
# /usr/local/nagios/libexec/fetion.sh 13649216631 "hello world"
# vim commands.cfg #添加下面两行
[*] # 'notify-service-by-fetion' command definition
[*]define command{
[*] command_name notify-host-by-fetion
[*] command_line $USER1$/fetion.sh $CONTACTPAGERS "$NOTIFICATIONTYPE$ Host Alert: $HOSTALIAS$ is $HOSTSTATE$
[*] }
[*] # 'notify-service-by-fetion' command definition
[*]define command{
[*] command_name notify-service-by-fetion
[*] command_line $USER1$/fetion.sh $CONTACTPAGERS "$NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ "
[*] }
# vim templates.cfg
[*] # Generic contact definition template - This is NOT a real contact, just a template!
[*]
[*]define contact{
[*] name generic-contact ; The name of this contact template
[*] service_notification_period 24x7 ; service notifications can be sent anytime
[*] host_notification_period 24x7 ; host notifications can be sent anytime
[*] service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events
[*] host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events
[*] service_notification_commands notify-service-by-email, notify-service-by-fetion ; send service notifications via email
[*] host_notification_commands notify-host-by-email, notify-host-by-fetion ; send host notifications via email
[*] register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
[*] }
# vim contacts.cfg
# template which is defined elsewhere.
define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email 1020659371@qq.com ;
页:
[1]