Nagios监控部署(转)

一、nagios简介
       nagios是一款用于系统和网络监控的应用程序,它可以在你的设定的条件下对主机和服务进行监控,在状态变差和变好的时候可以给管理员出告警信息。
       nagios所需要的运行条件是机器必须可以运行linux(或是unix变种)并且有c语言编译器。你必须正确地配置tcp/ip协议栈以使大多数的服务检测可以通过网络得以进行。如果需要正确地配置nagios里的cgis程序,必须安装以下这些软件:
web服务(最好是apache)
thomas boutell制作的gd库版本应是1.6.3或更高(在cgis程序模块statusmaptrends这两个模块里需要这个库)
还有一个就是ssl这一个工具,在安装nrpe这一个包时需要,因为在监控其他主机的时候是通过ssl连接来接收数据的;
 
网络环境:
主机                     IP                   OS                                  角色
nagios                   192.168.0.10               rhel5.1                                监控服务器
win2003                192.168.0.100               windows server 2003           被监控主机
linux                     192.168.0.102              rhel5.1                                被监控主机
 
准备软件:
apache2.2.14   //下载地址http://httpd.apache.org/download.cgi
nagios3.2.0
nagios plugins1.4.14
nrpe2.12
上面三个都可以在这个网站上找到http://www.nagios.org/download
nsclient++-0.3.6-win32.msi    //如果要监控windows的主机,还要下载nsclient这一个客户端工具,下载地址:http://sourceforge.net/projects/nscplus/
确认邮件是否能正常发送,我用的是sendmail,确认服务已经启动,下面发一封测试邮件到指定邮箱:
mail –s “this is test mail” kyhack@vip.qq.com
this is nagios test mail
ok
按ctrl + d 结束输入。
检查一下自大的邮箱,看到邮件之后就可以继续以下的环节了。
把所有的工具都下载到src目录下面:
 
二、开始安装
1、安装apache
tar  xvf  httpd-2.2.14.tar.gz   
cd  httpd-2.2.14
./configure  --prefix=/usr/local/apache2   
make
make install
/usr/local/apache/bin/apachectl start     //由于是没有改动的配置文件,可以直接启动
netstat –an |grep 80      //检查80端口是否已经开启了
或者是在别的机子上输入服务器的ip地址,当看到”it works!”时表明apache已经安装成功了
 
 
2、安装nagios
先添加一个nagios的账号
useradd nagios –s /sbin/nologin   //有的文章说要启用账号,其实不用也可以,因为这个账号不需要登录
tar  xvf  nagios-3.2.0.tar.gz
cd  nagios-3.2.0
./condfigure  --prefix=/usr/local/nagios  --with-nagios-user=nagios --with-nagios-group=nagios
make all
make install
make install‐init   //在/etc/rc.d/init.d安装启动脚本
make install‐config  //安装示例配置文件,安装的路径是/usr/local/nagios/etc
make install‐commandmode   //配置目录权限
 
安装完成nagios后就可以在/usr/local/nagios下面看到这几个目录:
bin                 存放nagios执行程序,nagios文件为主程序
etc                 配置文件存放目录
libexec           存放一些脚本程序
sbin               cgi文件所在目录,也就是执行外部命令所需文件所在的目录
share              网页文件存放位置
var                 日志文件、spid 等文件所在的目录
 
 
3、安装nagios插件
tar xvf  nagios-plugins-1.4.14.tar.gz
cd nagios-plugins-1.4.14
./configure  --prefix=/usr/local/nagios    //注意了,是放在/usr/local/nagios 里,别搞错了
make
make install
chown –R nagios:nagios /usr/local/nagios    //改一下文件的属组
#这样的话那些插件都会扔到nagios/libexec下面去
 
三、修改配置文件
1、修改apache的配置文件,我只把改的地方贴出来
vi  /usr/local/apache2/conf/httpd.conf
User nagios        //把apache运行用户改成 nagios
Group nagios                //把apache运行组改成 naios
#把下面的内容增加到文件的最后:
Scriptalias /nagios/cgi-bin /usr/local/nagios/sbin
        <directory "/usr/local/nagios/sbin">
        Authtype basic
        Options execcgi
        Allowoverride none
        Order allow,deny
        Allow from all
        Authname "nagios access"
        Authuserfile /usr/local/nagios/etc/htpasswd  //用于此目录访问身份验证的文件
        Require valid-user
        </directory>
 
   Alias /nagios /usr/local/nagios/share
        <directory "/usr/local/nagios/share">
        Authtype basic
        Options none
        Allowoverride none
        Order allow,deny
        Allow from all
        Authname "nagios access"
        Authuserfile /usr/local/nagios/etc/htpasswd   //用于此目录访问身份验证的文件
        Require valid-user
        </directory>
       别忘记了重启apache服务喔。。。。
 
 
2、修改cgi脚本控制文件cgi.cfg
vi  /usr/local/nagios/etc/cgi.cfg
use_authentication=1     //打开验证
default_user_name=test
authorized_for_system_information=nagiosadmin,test
authorized_for_configuration_information=nagiosadmin,test
authorized_for_system_commands=nagiosadmin,test
authorized_for_all_services=nagiosadmin,test
authorized_for_all_hosts=nagiosadmin,test
authorized_for_all_service_commands=nagiosadmin,test
authorized_for_all_host_commands=nagiosadmin,test
//这里添加的用户”test”可以通过浏览器对nagios服务的关闭、重启等操作,在这里为了安全也可以把nagiosadmin这一个用户给删掉,如果有多个用户用逗号隔开,如:nagiosadmin,test
/usr/local/apache2/bin/htpasswd ‐c /usr/local/nagios/etc/htpasswd test
new password: 输入你的密码
re‐type new password: 再次确认
adding password for user test     //这里给前面添加的用户设置密码
 
测试一下,输入你的http://你的服务器IP/nagios之后会弹出以下界面:
 
在这里输入你刚刚设置的用户名密码,就可以登录你的监控平台了,如下:
 
由于我们目前是没有监控任何的主机,所以目前还是看不到什么有用的东西的。
所以说呢,接下来继续配置其他的配置文件;
3、配置nagios主配置文件
在这里定义后面的配置文件的保存路径,下面只贴修改部分
vi /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
#cfg_file=/usr/local/nagios/etc/objects/contacts.cfg    //这一行注释掉,为了方便管理,我们重新写一个联系人的配置文件
cfg_file=/usr/local/nagios/etc/contacts.cfg      //指定联系人配置文件路径
cfg_file=/usr/local/nagios/etc/contactgroups.cfg   //指定联系人组配置文件路径
#cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg   //注释掉,用自己写的监视时段配置文件
cfg_file=/usr/local/nagios/etc/timeperiods.cfg         //指定监视时段配置文件路径
cfg_file=/usr/local/nagios/etc/objects/templates.cfg     //指定临时配置文件路径
cfg_file=/usr/local/nagios/etc/services.cfg           //服务配置文件路径
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg   //注释掉,
cfg_file=/usr/local/nagios/etc/hosts.cfg               //主机配置文件路径
cfg_file=/usr/local/nagios/etc/hostgroups.cfg              //主机组配置文件路径
 
check_external_commands=1  //在web界面下重启nagios,停止主机/服务检查操作,默认关闭;
command_check_interval=10s      //定义这个命令检查时间间隔,默认是1秒;
 
 
4、配置timeperiods.cfg文件
这是个服务器监控时间段的配置文件,一般都是全天24小时,名称是24x7;
vi /usr/local/nagios/etc/timeperiods.cfg
define timeperiod{
        timeperiod_name         24x7
        alias                      24 hours a day,7days a week
        sunday                   00:00-24:00
        monday                  00:00-24:00
        tuesday                  00:00-24:00
        wednesday               00:00-24:00
        thursday                 00:00-24:00
        friday                    00:00-24:00
        saturday                 00:00-24:00
        }
在这里要注意时间段名称那里的后面不能有空格出现,
 
 
5、创建联系人配置文件,contacts.cfg
vi /usr/local/nagios/etc/contacts.cfg
 define contact {
     contact_name         kytest
     alias                system administrator
     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
     host_notification_commands     notify-host-by-email
     email                          kyhack@vip.qq.com
    # pager                         13800138000
     }
 
           创建一个名为kytest的联系人,下面列出其中几个重要选项的说明
       #服务出了状况通知的时间段,这个时间段是前面timeperiods.cfg里面定义的。
       service_notification_period    24x7
 
       #主机出现状况时通知的时间段,这个时间段是前面timeperiods.cfg里面定义的。
     host_notification_period       24x7
    
       #当服务出现w—报警(warning),u—未知(unkown),c—严重(critical),r—从异常恢复到正常,在这四种情况下通知联系人
service_notification_options   w,u,c,r
 
#当主机出现d­­­­—当机(down),u—返回不可达(unreachable),r—从异常情况恢复正常,在这3种情况下通知联系人
     host_notification_options       d,u,r
 
       #服务出问题通知采用的命令notify-service-by-email,这个命令是在commands.cfg中定义的,作用是给联系人发邮件. 在nagios2.x的版本上可以不一样,可以自己到commands.cfg里看一下;在这里也可以设置发送短信的方式通知联系人,前提是你要配置有发送知道的脚本,还要到commands.cfg里面添加发送脚本所用到的命令;
   service_notification_commands  notify-service-by-email
 
       #同上,主机出问题时采用的也是发邮件的方式通知联系人
     host_notification_commands     notify-host-by-email
 
       #指定联系的人email地址
     email         kyhack@vip.qq.com
 
       #联系人的手机,前提是要支持短信通知,这里没有启用通过手机短信的方式发送警报
pager        13800138000
 
如果有多个联系人的话,可以通过复制来创建多个联系人;
 
6、创建联系人组配置文件,contactgroups.cfg ,把多个联系人加到一个组里面
vi  /usr/local/nagios/etc/contactgroups.cfg
define contactgroup{
        contactgroup_name       sagroup
        alias                   system administrator group
        members                 kytest
        }
注意:members选项里面的联系人在contacts.cfgj里面要要定义,多个联系从之间用逗号隔开;
 
7、创建hosts.cfg主机配置文件
vi  /usr/local/nagios/etc/hostgroups.cfg
define host{
host_name               nagios-server          //被监控主机的名称,后面不能带空格
        alias                   nagios server      //别名
        address                 192.168.0.10      //被监控主机的ip地址,这里是监控本机
        contact_groups          sagroup     //联系人组,是在前面contactgroups.cfgj里面定义的组        check_command        check-host-alive    //检查主机是否存活,命令来自commadns.cfg文件
        max_check_attempts      5                            //检查失败后重试次数
        notification_interval   10                           //提醒的间隔,每隔10秒提醒一次
        notification_period     24x7      //提醒的周期,24x7这个时间段来自timeperiods.cfg里的定义
        notification_options    d,u,r        //在什么时候提醒,详见contacts.cfg部分的介绍
        }
define host{
        host_name               win2003
        alias                   web server
        address                 192.168.0.100        //这是我的windows 2003的服务器,
        contact_groups          sagroup
        check_command           check-host-alive
        max_check_attempts      5
        notification_interval   10
        notification_period     24x7
        notification_options    d,u,r
        }
define host{
        host_name               linux
        alias                   web server
        address                 192.168.0.102                //被监控的linux服务器
        contact_groups          sagroup
        check_command           check-host-alive
        max_check_attempts      5
        notification_interval   10
        notification_period     24x7
        notification_options    d,u,r
        }
 
             在这里我定义了三台主机,只是作一个例子;如果你有更多的主机可以通过复制来添加主机,再修改一下相应的位置就可以了;
 
 
8、创建hostgroups.cfg文件
vi  /usr/local/nagios/etc/hostgroups.cfg
define hostgroup{
        hostgroup_name  sa-servers
        alias           sa servers
        members         nagios-server,win2003,linux
        }
这个跟联系人组配置差不多,要是有多台主机可以用逗号隔开;members里的主机成员必须也是要在hosts.cfg里面定义的,其实这个文件也可以不要;
 
ok,到这里就差可以说是完成了最基础的一部份了,现在就是最关键的一部分了,前面已经定义好了联系人,被监控主机,但是还没有定义好要监控主机上的什么东东;现在在这一部分就可以对主机上的各种信息进行监控,nagios监控的信息主要有:本地资源,对外的服务等;本地资源主要包括cpu,硬盘,swap,内存等;对外服务有web,fpt,smtp,pop3等;
9、定义监控的项目,也叫服务,创建services.cfg
vi  /usr/local/nagios/etc/services.cfg
#监控主机是否存活
define service{
        host_name               nagios-server
        service_description     check-host-alive
        check_command           check-host-alive
        max_check_attempts      5
        normal_check_interval   5
        retry_check_interval    2
        check_period            24x7
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          sagroup
        }
#监控主机的web服务
define service{
        host_name               nagios-server
        service_description     check_tcp 80
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        check_command           check_tcp!80
        }
#监控主机的cpu负载情况
define service{
        host_name               nagios-server
        service_description     cpu load
        check_command           check_nrpe!check_load
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        }
#监控主机的进程数
define service{
        host_name               nagios-server
        service_description     total-procs
        check_command           check_nrpe!check_total_procs
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        }
 
说明:
host_name:必须是主机配置文件hosts.cfg中定义的主机。
check_command:在commands.cfg文件中定义或在nrpe.cfg里面定义的命令;
max_check_attempts: 最大重试次数,一般设置为4次左右;
normal_check_interval 和retry_check_interval检查间隔的单位是分钟。
notification_interval  通知间隔指探测到故障后,每隔多长时间发送一次报警信息,单位是分钟。
notification_options:通知选项跟联系人配置文件相同。
contact_groups:配置文件contactgroup.cfg定义的组名称。
注意:check_command选项后面跟的命令一定要在commands.cfg里有定义;
如果要监控其他的主机的信息,可以通过复制并修改想应的选项来进行添加;
通过复制添加下面这两台服务器的监控项目:
win2003和linux
 
四、安装nrpe
tar  xvf  nrpe-2.12.tar.gz
cd  nrpe-2.12.
./configure  --prefix=/usr/local/nrpe
make
make install
#复制文件,因为在nrpe安装目录/usr/local/nrpe/libexec里只有cneck_nrpe这一个文件,而在nagios/libexec里却没有,还有一个就是nrpe.cfg文件里面默认定义的那几个命令后面的路径是放在/usr/local/nrpe/libexec的目录里面,也要把那几个文件复制过来,如果不复制过来的话必须要修改nrpe.cfg里面定义的命令的路径,免得在services.cfg里面定义check_command时提示找不到命令;现在把下面的文件复制过来:
   cp /usr/local/nrpe/libexec/check_nrpe  /usr/local/nagios/libexec
   cp /usr/local/nagios/libexec/check_disk  /usr/local/nrpe/libexec
   cp /usr/local/nagios/libexec/check_load  /usr/local/nrpe/libexec
   cp /usr/local/nagios/libexec/check_ping  /usr/local/nrpe/libexec
   cp /usr/local/nagios/libexec/check_procs  /usr/local/nrpe/libexec
cp /usr/local/nagios/libexec/check_users  /usr/local/nrpe/libexec
 
#修改nrpe配置文件,只把改过的地方写出来
vi  /usr/local/nrpe/etc/nrpe.cfg
server_address=192.168.0.10      //以单独的守护进程运行
allowed_hosts=127.0.0.1,192.168.0.10  //设置允许nagios监控服务器可以访问
command[check_users]=/usr/local/nrpe/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nrpe/libexec/check_load -w 15,10,5 -c 30,25,20
#command[check_hda1]=/usr/local/nrpe/libexec/check_disk -w 20 -c 10 -p /dev/hda1   //注释掉
command[check_df]=/usr/local/nrpe/libexec/check_disk -w 20 -c 10              //添加这一行,监控整个磁盘利用率
command[check_zombie_procs]=/usr/local/nrpe/libexec/check_procs -w 5 -c 10 -s z
command[check_total_procs]=/usr/local/nrpe/libexec/check_procs -w 150 -c 200
command[check_ips]=/usr/local/nrpe/libexec/ip_conn.sh 8000 10000     //监控ip连接数
 
说明:
     command[check_users]=/usr/local/nrpe/libexec/check_users –w 5 –c 10在默认情况下check_users的插件是放在/usr/local/nrpe/libexec/目录里面,而目录里面在默认情况下是没有这一个文件的,所以说要从/usr/local/nagios/libexec/目录下拷贝一个过来;或者说的它后面的它改成:  command[check_users]=/usr/local/nagios/libexec/check_users –w 5 –c 10 这样的话就可以了,要不然在引用check_users的时候会提示没有那命令;
ps:我这里为了方便,就是从/usr/local/nagios/libexec下把那几个文件拷贝过来;
     在上面的nrpe.cfg配置文件里面,在中括号 “ [ ] “里面部分是命令名,也就是check_nrep –c 后面可以接的内容,等号=后面的就是实际执行的插件程序的路径;从上往下分别是检测登录用户数,cpu使用率,磁盘的容量,僵尸进程,总进程,连接数;
     要是还要添加其它监控项目,不要忘记了在这里定义相应的命令;例:如果要监控主机的swap分区使用情况,当空闲空间小于20%时为警告状态,当空闲空间小于10%时为严重状态。需要在nrpe.cnf里面添加下面的命令:/usr/local/nagios/libexec/check_swap -w 20% -c 10% 如还有其它的,添加相就应的就可以了;关于命令用法可以能过/usr/local/nagios/libexec/check_swap –h这样的命令来查询;
     command[check_ips]=/usr/local/nrpe/libexec/ip_conn.sh 8000 10000 ip连接数,ip_conn.sh脚本需要自己写,下面给出脚本的内容:
#!/bin/sh
#if [ $#-ne 2 ]
#then
# echo "usage:$0 -w num1 -c num2"
#exit 3
#fi
 
 
ip_conns=`netstat -an |grep tcp |grep est |wc -l`
 
        if [ $ip_conns -lt $1 ]
        then
        echo "ok -connectcounts is $ip_conns"
        exit 0
        fi
 
        if [ $ip_conns -gt $1 -a $ip_conns -lt $2 ]
        then
        echo "warning -connectcounts is $ip_conns"
        exit 1
        fi
 
        if [ $ip_conns -gt $2 ]
        then
        echo "critical -connectcounts is $ip_conns"
        exit 2
        fi
     我在nrpe配置文件nrpe.cfg把脚本所需的两个参数写上了,因此这个脚本就不需判断两个参数输入值的情况。只要当前ip连接数大于8000,系统就发warning报警,超过10000,则发“critical”报警信息。把这个脚本放在目录/usr/local/nrpe/libexec下,并给于执行权限;
注:脚本来自田逸的《开源监控利器nagios》
 
修改/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$
        }
添加check_nrpe的命令支持,要不是加的话,在”check_cmmmands   check_nrpe!check_nrpe”这样的情况下的时候,会提示没有check_nrpe这一个命令。
原文地址:https://www.cnblogs.com/reynold-lei/p/3143522.html