Nagios
 
Nagios 是一款遵循GPLv2 的开源网络监控软件,可用来监控指定的多种系统的主机、服务,并可在它们的工作状态发生变化时通知管理员。
1) 监控网络服务(HTTP 、POP3 、SMTP 、PING 、MySQL 等)
2) 监控主机资源( 磁盘空间利用率、内存利用率、CPU 负载等)
3) 简洁的插件设计接口,使得用户可以轻松开发所需的检测脚本
4) 并行服务模式
5) 轻松描述网络结构,并且能够区辨“ 宕机” 和“ 主机不可达”
6) 通过邮件或用户自定义的方式将主机或服务的工作状态变化情况通知给管理员6 )当服务或主机问题产生与解决时将告警发送给联系人(通过EMail 、短信、用户定义方式)  可以通过飞信,等方式实现时,既可传递给管理员,可高效的保证服务器的维护。
7) 自动日志滚动
8) 支持以冗余方式进行主机监控
9) 可以通过web 方式直观的查看当前网络状态、通知和问题历史、日志文件等等,此组件为可选
 
 
Nagios 通常由一个主程序(Nagios) 、一个插件程序(Nagios-plugins) 和四个可选的ADDON(NRPE 、NSCA 、 NSClient++ 和NDOUtils) 组成。Nagios 的监控工作都是通过插件实现的,因此,Nagios 和Nagios-plugins 是服务器端工作所必须的组件。而四个ADDON
(1 )NRPE :用来在监控的远程Linux/Unix 主机上执行脚本插件以实现对这些主机资源的监控
(2 )NSCA :用来让 被监控的远程Linux/Unix 主机主动将监控信息发送给Nagios 服务器( 这在冗余监控模式中特别要用到)
 
配置:
(1)解决安装Nagios的依赖关系:
 
 
Nagios基本组件的运行依赖于httpd、gcc和gd。可以通过以下命令来检查nagios所依赖的rpm包是否已经完全安装:
两个主机都需要安装:
 
 
(2)所需要安装组件:
 
服务端所用的安装包为 nagios nagios-plugins ndoutils(非必须)
linux客户端 nrpe
windows客户端 NSClient++
 
(3)
进行selinux设置,或者关闭selinux
 
开始搭建:
(1)添加nagios运行所需要的用户和组:
 
 
# groupadd nagcmd
# useradd -m nagios
# usermod -a -G nagcmd nagios
 
 
把apache加入到nagcmd组,以便于在通过web Interface操作nagios时能够具有足够的权限:
# usermod -a -G nagcmd apache
下载软件包 ,这里是用 本地 服务器下载的:
nagios-cn-3.2.3.tar.bz2
nagios-plugins-1.4.15.tar.gz
nrpe-2.12.tar.gz
 
[root@station88 ~]# tar xvf nagios-cn-3.2.3.tar.bz2
[root@station88 ~]# cd nagios-cn-3.2.3
[root@station88 nagios-cn-3.2.3]# ./configure --with-command-group=nagcmd --enable-event-broker --prefix=/var/www/nagios
 
# make all
# make install
# make install-init 开机启动程序
# make install-config
[root@station88 nagios-cn-3.2.3]# make install-webconf     如果用web 管理页面 ,需要装web
 
为email指定您想用来接收nagios警告信息的邮件地址,默认是本机的nagios用户:
email         nagios@localhost       #这个是默认设置
 
 
 
创建一个登录nagios web程序的用户,这个用户帐号在以后通过web登录nagios认证时所用:
 
[root@station88 nagios-cn-3.2.3]# service httpd start
 
root@station88 ~]# tar xvf nagios-plugins-1.4.15.tar.gz 安装nagios 插件
[root@station88 ~]# cd nagios-cn-3.2.3
./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/var/www/nagios --with-mysql
 
[root@station88 nagios-cn-3.2.3]# make install
 
[root@station88 ~]# chkconfig --add nagios 加到开机启动此项
[root@station88 ~]# chkconfig nagios on
 
[root@station88 nagios-plugins-1.4.15]# /var/www/nagios/bin/nagios -v /var/www/nagios/etc/nagios.cfg
 
[root@station88 ~]# vim .bashrc
PATH=$PATH:/var/www/nagios/bin
 
[root@station88 ~]# . .bashrc
 
[root@station88 ~]# ln -sv /var/www/nagios/etc/nagios.cfg /etc/nagios.cfg 做链接
 
[root@station88 ~]# service nagios restart 启动服务
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.
 
用 浏览器 登陆
账号 nagiosadmin
密码 admin
 
 
以上 只是监控本机的
 
 
接下来
如果检测 windows 端
windos端
1.被监控端安装NSClient++-0.3.8-Win32.msi
2.安装完成后修改配置文件NSC.ini把需要的库都打开
 
 
3.在监控服务器上修改nagios配置文件nagios.cfg
去掉注释cfg_file=/usr/local/nagios/etc/objects/windows.cfg
定义windows.cfg
define host{
        use             windows-server ; Inherit default values from a template
        host_name       winserver       ; The name we're giving to this host
        alias           My Windows Server       ; A longer name associated with the host
        address         192.168.0.191   ; 改要监控的主机的IP
        }
重启 一下服务 service nagios restart
 
 
监控 linux 客户端:
linux端
 
被监控端
先添加nagios用户
useradd nagios
为了安装nrpe,先安装nagios-plugins-1.4.15.tar.gz 插件
# tar xvf nagios-plugins-1.4.15.tar.gz
# cd nagios-plugins-1.4.15
# make
# make install
 
tar xvf nrpe-2.12.tar.gz
cd nrpe-2.12.tar.gz
./configure (会自动加载SSL)
#如果后面make报错,加如下参数
./configure --enable-ssl --with-ssl-lib=/usr/lib/(当然前提要有openssl)
make all
make install-plugin
make install-daemon
make install-daemon-config
 
4.配置nrpe信息
vim /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1 //允许监控的IP
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d
 
 
 
服务端
3.安装NRPE
cd nagios-nrpe_2.8.1
./configure    #默认自动添加了openssl
#因为传送过程要加密,如果后面make报错,加如下参数
rpm -qa| grep ssl
openssl-devel-0.9.7a-43.17.el4_6.1
rpm -ql openssl-devel-0.9.7a-43.17.el4_6.1 | more
./configure --enable-ssl --with-ssl-lib=/lib/(当然前提要有openssl)
make all
make install-plugin
 
4.commands.cfg定义外部构件nrpe
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$
        }
定义 mylinux.cfg
define host{
           use             linux-server
          host_name     mylinux
          alias            mylinux
          address        192.168.0.27(客户端IP既被监控的IP)
        }
define service{
        use                      generic-service
        host_name             mylinux
        service_description     check-swap
        check_command        check_nrpe!check_swap
               }
define service{
        use                      generic-service
        host_name             mylinux
        service_description     check-load
       check_command         check_nrpe!check_load
               }
define service{
        use                      generic-service
        host_name             mylinux
       service_description     check-disk
       check_command        check_nrpe!check_had1
       }
define service{
        use                      generic-service
        host_name             mylinux
       service_description     check-users
       check_command        check_nrpe!check_users
               }
define service{
        use                    generic-service
        host_name           mylinux
        service_description otal_procs
        check_command     check_nrpe!check_total_procs
}
 
#################################################################################
这个事 hhtp的:
define service{
        use                    generic-service
        host_name           mylinux
        service_description httpd
        check_command     check_http! -H 192.168.0.27
}
 
 
Nagios 监控mysql:
 
 
1、建立专用数据库:
[root@svr3 ~]# /usr/local/webserver/mysql/bin/mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 51910
Server version: 5.5.3-m3-log Source distribution
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> create database nagios;
Query OK, 1 row affected (0.00 sec)
 
mysql> grant select on nagios.* to nagios@'%' identified by '123qweasd';
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql> select User,Password,Host from user;
+----------------+-------------------------------------------+------------------+
| User            | Password                                  | Host             |
+----------------+-------------------------------------------+------------------+
| root            | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 | localhost        |
| nagios          | *A1F1CB851D62F002C09A0C9C4A76262473432F55 | %                |
+----------------+-------------------------------------------+------------------+
14 rows in set (0.00 sec)
12 rows in set (0.00 sec)
 
[root@svr3 ~]# /var/www/nagios/libexec/check_mysql -H 10.1.1.30 -u nagios -d nagios -p 123qweasd
Uptime: 970046 Threads: 1 Questions: 684583 Slow queries: 3 Opens: 672 Flush tables: 1 Open tables: 
 
369 Queries per second avg: 0.705
 
[root@svr4 nrpe_install]# /var/www/nagios/libexec/check_mysql -H 10.1.1.30 -u nagios -d nagios -p 123qweasd
Uptime: 970348 Threads: 1 Questions: 684584 Slow queries: 3 Opens: 672 Flush tables: 1 Open tables: 369 Queries per second avg: 0.705
 开始设置:
#定义用户
#定义组,及隶属用户
#用户增加
 
在command.cfg里增加插件命令:
#check mysql
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u nagios -d nagios -p 123qweasd
}
 
#定义服务:
define service {
        host_name mysql
     
        check_period 24x7
        max_check_attempts 2
        normal_check_interval 1
        retry_check_interval 1
        contact_groups admins
        notification_interval 5
        notification_period 24x7
        notification_options w,u,c,r
        check_command check_mysql
        }
 
 
 
       
飞信报警
下载 飞信
#mv fetion /usr/bn
#Chmod +x /usr/bin 这时已经把下载的飞信 移动到 /usr/bin ,所以在这里加权限
#mv * /usr/lib
#fetion 查看一下
#fetion --mobile 15003880800 --pwd 123qweQWE --to 15003880800 --msg-tpye=0 --msg-utf8 $HOSTNAME
#cd /etc/objects/
# vim conmmands.cfg
下边
1.修改模板文件
修改concont.cfg
2.
修改
 commands.cfg
define command{
        command_name notify-service-by-fetion