一、安装Nagios及其插件
1.Nagios 需要的环境
LAMP 或者LEMP 这里不作论述,请在本站查找。
特别注意:yum安装必备的编译组件、库及依赖软件,必须安装GD GD-DEVEL
到Nagios官方下载下列软件包
下载 nagios-3.0.6.tar.gz -----------------------主程序
nagios-plugins-1.4.13.tar.gz------------------插件
nrpe_2.12.tar.gz --------------------------监控Linux需要
nsclient++0.3.5 ---------------------------监控windows需要

2。安装nagios-3.0.6.tar.gz
=======================
/usr/sbin/useradd nagios
passwd nagios 输入密码
/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -g nagcmd nagios
/usr/sbin/usermod -g nagcmd apache
=====================
tar xzf nagios-3.0.6.tar.gz
=======================
cd nagios-3.0.6
=======================
./configure --with-group=nagios --with-user=nagios --with-command-group=nagcmd --with-gd-lib=/usr/lib --with-gd-inc=/usr/include
================
make all
==================
make install
================
make install-init
=======================
make install-config
=====================
make install-commandmode
===============================
vi /usr/local/nagios/etc/objects/contacts.cfg
更改email地址nagiosadmin的联系人定义信息中的EMail信息为你的EMail信息以接收报警内容。
=================================
make install-webconf
=====================
/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
输入密码(这一步很重要)
========================================================
3。译并安装Nagios插件
nagios-plugins-1.4.13.tar.gz
tar xzf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
============================
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
============================
chkconfig --add nagios
chkconfig nagios on
=====================
验证Nagios的样例配置文件
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
================================
如果没有报错,可以启动Nagios服务
service nagios start
=========================================
4。编辑apache 的httpd.conf
最后面添加:
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
#  SSLRequireSSL
   Options ExecCGI
   AllowOverride None
   Order allow,deny
   Allow from all
#  Order deny,allow
#  Deny from all
#  Allow from 127.0.0.1
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user
</Directory>

Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
#  SSLRequireSSL
   Options None
   AllowOverride None
   Order allow,deny
   Allow from all
#  Order deny,allow
#  Deny from all
#  Allow from 127.0.0.1
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user
</Directory>
=========================================
5。在验证下
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

没有出错的话!重新启动nagios和apache


========================
6。关于selinux,如果selinux是关闭状态,可以忽略这部!
如果selinux开启!那么
如果是SELinux处于强制安全模式时需要做
getenforce
令SELinux处于容许模式
setenforce 0
如果要永久性更变它,需要更改/etc/selinux/config里的设置并重启系统。
不关闭SELinux或是永久性变更它的方法是让CGI模块在SELinux下指定强制目标模式:
chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/ chcon -R -t httpd_sys_content_t /usr/local/nagios/share/

7.登陆web接口!
http://localhost/nagios
输入你刚才设置的用户名密码!ok了!看到界面了吧
ps:如果遇到问题!!!!
如果遇到关于cgi什么权限问题!察看下/usr/local/nagios的属主组权限,
包括里面的内容,
尤其是/usr/local/nagios/etc/htpasswd.users这个文件的权限!都应该是nagios:nagios
另外尝试编辑
vi /usr/local/nagios/etc/cgi.cfg

use_authentication=1
修改为
use_authentication=0
如果出现页面无法显示之类的,去访问bbs.itnms.net里面的
http://www.itnms.net/discuz/forumdisplay.php?fid=10&pageD4
有关于基本的错误的解决方法!
二、监控windows 系统
添加第一台windows2003设备来进行检测   

接下来我们开始监控第一台windows设备!
windows服务器上要安装nsclient++0.3.5.zip
解压后,copy到c盘根目录
1。在nagios服务器上
vi /usr/local/nagios/etc/nagios.cfg
#cfg_file=/usr/local/nagios/etc/objects/windows.cfg 去掉这句话的注释
保存退出!
2。到win服务器上,打开命令窗口,cd到刚才解压的目录
在命令行界面执行 nsclient++ /install
然后 nsclient++ SysTray 如果出错不用管!
此时在“服务”里面已经有了nsclient的服务
3。编辑NES.ini
在 [modules] 选项里
去掉所有的注释符号; 除了
CheckWMI.dll和RemoteConfiguration.dll
4。the [Settings] 选项里
修改allowd_host=61.x.x.x(nagios服务器的ip)
如果这一步要修改passwd,那么nagios服务器里面commands.cfg也要修改!
步骤: 在commands.cfg 找到check_nt 定义命令 中的 Command_line 在其中添加-s 你的密码

5。[NSClient] 里面,去掉port=12489的注释!
他靠端口12489侦听,所以防火墙要打开这个端口!
然后启动nsclient
nsclient++ /start
========================================
接下来我们开始配置nagios服务器里面的内容
vi /usr/local/nagios/etc/objects/windows.cfg
=================================
define host{
use windows-server
host_name winserver alias
My Windows Server
address 192.168.1.2

}
修改hostname和address,很重要!!
然后下面的很多定义,都可以不用改,想知道每个定义的意思,去看看官方的文档!!
下面的定义全部修改hostname 都改为自己的!一定要一样!
接下来保存!退出!!
======================
然后重新启动nagios
看看你的web图形是不是变化了!它会自动连接!
如果这个时候出错!尝试去telnet win服务器的ip 12489端口!!
自己排除原因!!

三、监控Linux服务器
添加除了本机外 第一台linux服务器

刚才我们配置完了之后,可以监控本机,也可以监控第一台win服务器(里面自带的windows.cfg),现在我们监控第一台linux服务器!至于为什么要用nrpe,请参考官方文档,这里我就不多说了!
1。被监控端(被监控的服务器)安装nagios-nrpe_2.12.tar.gz和插件nagios-plugins-1.4.13.tar.gz
安装过程
====================
useradd nagios
passwd nagios
tar -zxvf nagios-plugins-1.4.13.tar.gz
cd nagios-plugins-1.4.13
./configure
make
make install
=======================
这一步完成后会在/usr/local/nagios/下生成两个目录libexec和share
======================================
chown nagios.nagios /usr/local/nagios
然后
tar -zxvf nagios-nrpe_2.12.tar.gz
cd nagios-nrpe_2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
========================================================


vi /usr/local/nagios/etc/nrpe.cfg
将allowed_hosts=127.0.0.1
修改成你的nagios服务器的ip
=======================================
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
启动nrpe
================================================
netstat -an|grep 5666
察看是否在5666端口上监听
注意:防火墙 释放端口5666,很重要
===================================
/usr/local/nagios/libexec/check_nrpe -H localhost
察看是否能返回nrpv版本号,返回则正常!
=============================================
vi /usr/local/nagios/etc/nrpe.cfg
可以看到里面监控对象
# The following examples use hardcoded command arguments...
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200


===================================================
然后再启动nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg ?d
这样子,在监控端算是设置完了!!!

2。我们来设置nagios服务器端的设置!
安装nagios-nrpe_2.12.tar.gz
这里安装过程比上面再被监控服务安装少两部
=========================================
tar -zxvf nagios-nrpe_2.12.tar.gz
cd nagios-nrpe_2.12
./configure
make all
make install-plugin
================================
然后我们测试下连通性
/usr/local/nagios/libexec/check_nrpe -H 被监控端ip
如果返回nrpe版本号,那么正常!
如果返回拒绝连接!那么telnet ip 5666看是否正常
======================================================

配置:
=================================
由于nrpe外构组件,所以必须在commands.cfg中定义(我这里的nagios版本是3.0.3和2。x的版本有些不一样)
[root@localhost etc]# vi /usr/local/nagios/etc/objects/commands.cfg
最下面添加
#check nrpe
define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
========================================================
[root@localhost etc]# vi /usr/local/nagios/etc/nagios.cfg
中间添加
cfg_file=/usr/local/nagios/etc/objects/aiyo-mailserver.cfg
最后面的 aiyo-mailserver.cfg你们可以自己改,或者Linux1.cfg也可以!
=================================================
新建aiyo-mailserver.cfg
[root@localhost etc]# vi /usr/local/nagios/etc/objects/aiyo-mailserver.cfg
添加
define host{
           use          linux-server
          host_name     aiyo-mailserver
          alias         aiyo-mailserver
          address               210.51.47.213
        }
define service{
        use             generic-service
        host_name       aiyo-mailserver
        service_description     HTTP
         check_command     check_http
       }
define service{
        use             generic-service
        host_name       aiyo-mailserver
        service_description     FTP
        check_command   check_ftp

define service{
        use             generic-service
        host_name       aiyo-mailserver
        service_description     SSH
        check_command   check_ssh
       }

define service{
        use             generic-service
        host_name       aiyo-mailserver
        service_description     SMTP
        check_command   check_smtp
       }

define service{
        use             generic-service
        host_name       aiyo-mailserver
        service_description     POP3
        check_command   check_pop
       }


define service{
        use             generic-service
        host_name       aiyo-mailserver
        service_description     check-swap
         check_command           check_nrpe!check_swap
               }
define service{
        use             generic-service
        host_name       aiyo-mailserver
        service_description     check-load
         check_command           check_nrpe!check_load
               }

define service{
        use             generic-service
        host_name       aiyo-mailserver
        service_description     check-disk
         check_command           check_nrpe!check_had1


define service{
        use             generic-service
        host_name       aiyo-mailserver
        service_description     zombie_procs
         check_command           check_nrpe!check_zombie_procs
               }

define service{
        use             generic-service
        host_name       aiyo-mailserver
        service_description     check-users
         check_command           check_nrpe!check_users
               }

define service{
        use             generic-service
        host_name       aiyo-mailserver
        service_description     total_procs
         check_command           check_nrpe!check_total_procs

                   }
保存,退出!
这个里面定义了对象和服务!
注意:
define host{
           use          linux-server
这个你要能在commands.cfg找到!如果是3.0.x版本,应该就对的
!如果是2。x版本,不一样定了!
=====================================================
我们来运行
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
检测下是不是都正确!
================================================
正确的话,那么我们重新启动nagios
那么在web页面里面就会出现!!
=====================================

三、飞信报警
飞信机器人网站

http://www.it-adv.net/


新版fetion命令是交互式的,需要登录后才能以sms命令发送短信,不方便nagios调用宏变量,所以使用旧版sms命令行
下载并安装Fetionrobot所需要的库文件
#wget

http://down.freebsdsystem.org/libs/fetion/libraryrh5x32.tar.gz


#wget

http://down.freebsdsystem.org/monitor/fetion_linux_20080402.tar.gz


#tar xvfz fetion_linux_20080402.tar.gz
#cd fetion_linux_20080402
#cp sms /usr/local/nagios/libexec/
#tar zvxf libraryrh5x32.tar.gz
#cp lib* /usr/lib
#sms -h
sms -f mobile -p pwd -t mobile1,.. -m message -a message -d
sms -f mobile -p pwd -t mobile1,.. -i file_name[utf8] -a invite_message -d 1
-f:Fetion mobile account(only supports mobile phone No.)
-p:Account password
-testination mobile list
-m:Message
-i:File name(only supports utf8)
-a:Auto send invite using invite_message.
-debug on.
nagios配置
# vi /usr/local/nagios/etc/objects/commands.cfg
# ’sms send’command definiton
define command{
        command_name notify-service-by-sms
        command_line /usr/local/nagios/libexec/sms -f 139xxxxxxxx -p xxxxxx -t $CONTACTPAGER$ -m "$HOSTNAME$ $SERVICEDESC$ is $SERVICESTATE$ on $TIME$ result is $SERVICEOUTPUT$" $CONTACTPAGER$

}
define command{
        command_name notify-host-by-sms
        command_line /usr/local/nagios/libexec/sms -f 139xxxxxxxx -p xxxxxx -t $CONTACTPAGER$ -m "$HOSTNAME$ $SERVICEDESC$ is $SERVICESTATE$ on $TIME$ result is $SERVICEOUTPUT$" $CONTACTPAGER$
}

# vi /usr/local/nagios/etc/objects/contacts.cfg
define contact{
        contact_name nagios_sms
        alias nagios_sms
        host_notifications_enabled 1
        service_notifications_enabled 1
        service_notification_period 24×7
        host_notification_period 24×7
        service_notification_options u,c # UNKNOWN and CRITICAL
        host_notification_options d,u     # DOWN and UNREACHABLE
        service_notification_commands notify-service-by-sms
        host_notification_commands notify-host-by-sms
        pager 138xxxxxxxx                  # 服务器管理员手机号码
        can_submit_commands 1
}