nagios监控系统

Nagios监控系统
Nagios是一款开源的免费网络监视工具,能够监控Windows、Linux和Unix的主机状态,交换机路由器等网络设备,在系统或服务状态异常时发出邮件或短信报警,第一时间通知网站运维人员。流量监控不是他的强项,流量监控建议使用cacti,能够绘制很是直观的图形。
.php

nagios主要能够监控如下方面:html

  • 主机是否宕机(经过ping命令,若是ping不通会认为主机属于宕机状态,但不影响所监控的其余服务)
  • 服务器资源(cpu使用率、硬盘剩余空间等)
  • 网络服务(smtp\pop3\http\)
  • 监控网络设备(路由器、交换机等)
    .

nagios工做原理
Nagios自己不包括监控主机和服务的功能。全部的监控、监测功能都是经过各类插件来完成的。安装完nagios以后,在nagios主目录下的/libexex里面放有nagios自带的插件,如:check_disk是检查磁盘空间的插件,check_load是检查cpu负载的插件,每个插件能够经过运行./check_xxx -h命令来检查其使用方法和功能。
.mysql

nagios的四种监控状态
Nagios能够识别四种状态返回信息。0(OK)表示状态正常(绿色显示)linux

  • (WARNING)表示出现警告(×××),
  • (CRITICAL)表示出现很是严重错误(红色),
  • (UNKNOWN)表示未知错误(深×××),nagios根据插件返回来的值来判断监控对象的状态,并经过web显示出来,以供管理员即时发现故障。
    .

nagios经过nrpe插件来远程管理服务的工做过程ios

  • Nagios执行安装在它里面的check_nrpe插件,并告诉check_nrpe去检测哪些服务。
  • 经过ssl,check_nrpe链接远端机器上的NRPE daemon。
  • NRPE运行本地的各类插件去检测本地服务器和状态(check_disk,...etc)。
  • NRPE把检测的结果传给主机端的check_nrpe,check_nrpe再把结果送到nagios状态队列中。
  • Nagios依次读取队列中的信息,再把结果显示出来。

实验环境:
nagios监控系统
搭建nagios监控系统
.web

建立nagios用户和用户组
[root@centos1 /]#   mount /dev/cdrom /media/
[root@centos1 /]#   useradd -s /sbin/nologin nagios
[root@centos1 /]#   mkdir /usr/local/nagios
[root@centos1 /]#   chown -R nagios:nagios /usr/local/nagios/

.sql

编译安装nagios(须要提早配置yum)
安装支持包:
[root@centos1 /]#   yum -y install httpd php mysql-devel openssl openssl-devel
[root@centos1 /]#   umount /dev/cdrom /media/
[root@centos1 /]#   mount /dev/cdrom /media/
[root@centos1 /]#   cd /media/

.vim

配置:
[root@centos1 /]#   tar zxf nagios-4.0.1.tar.gz -C /usr/src/
[root@centos1 /]#   cd /usr/src/nagios-4.0.1/
[root@centos1 /]#   ./configure --prefix=/usr/local/nagios/

.centos

编译和安装:

[root@centos1 /]# make install              //安装主程序,CGI和HTML文件 
[root@centos1 /]# make install-init         //在/etc/rc.d/init.d安装启动脚本 
[root@centos1 /]# make install-commandmode  //配置目录权限 
[root@centos1 /]# make install-config       //安装示例配置文件 
[root@centos1 /]# make install-webconf      //安装nagios的web接口,会在/etc/httpd/conf.d目录中建立nagios.conf文件。

.
安装完成以后会在/usr/local/nagios目录下产生6个目录
.
bin:nagios执行程序所在的目录,nagios文件即为主程序。
etc:nagios配置文件目录,当make install-config完之后etc下面就会出现默认的配置文件。
sbin:nagios CGI文件所在目录,这里存放的是一些外部命令执行程序。
share:nagios网页文件目录,存放一些html文件。
var:nagios日志文件、pid等文件目录。
Libexec:系统默认插件的存储位置
.服务器

添加为系统服务器
[root@centos1 /]#   chkconfig --add nagios
[root@centos1 /]#   chkconfig --level 35 nagios on

.

安装nagios插件(监控功能经过插件完成)
[root@centos1 /]#   cd /media/
[root@centos1 /]#   tar zxf nagios-plugins-1.5.tar.gz -C /usr/src/
[root@centos1 /]#   cd /usr/src/nagios-plugins-1.5/
[root@centos1 /]#   ./configure --prefix=/usr/local/nagios/

.

编译并安装:
[root@centos1 /]#   make && make install

.

安装nrpe(为了监控远程服务器)
[root@centos1 /]#   cd /media/
[root@centos1 /]#   tar zxf nrpe-2.15.tar.gz -C /usr/src/
[root@centos1 /]#   cd /usr/src/nrpe-2.15/
[root@centos1 /]#   ./configure && make all && make install-plugin

.
在/etc/httpd/conf/httpd.conf文件最后添加受权,咱们能够到/etc/httpd/conf.d/nagios.conf文件中复制,不用手打。

[root@centos1 /]#   vim /etc/httpd/conf/httpd.conf 
使用:r导入便可
r /etc/httpd/conf.d/nagios.conf
导入便可,不用修改,保存退出。

.

执行htpasswd命令添加一个访问nagios页面的受权用户
[root@centos1 /]#   /usr/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
用户名和密码都是nagiosadmin

.

启动nagios和httpd
[root@centos1 /]#   service nagios start
[root@centos1 /]#  service httpd start

nagios监控系统
...............
nagios监控系统
.
配置nagios监控系统文件

  1. nagios的配置文件:
  2. Nagios.cfg:主配置文件,定义各类配置文件的名称和位置
  3. Cgi.cfg:控制CGI的配置文件
    Resource.cfg:资源文件,定义各类变量,以便于其余文件调用
  4. Objects:其余配置文件存放目录,此目录下主要有:
    .
    Command.cfg:命令配置文件,定义各类命令格式,以备其余文件调用
    contacts.cfg:联系人和组,发邮件等告警信息时能够调用
    localhost.cfg:监控本机的配置文件
    timeperiods.cfg:定义监控时间的配置文件,便于其余文件调用
    Hostgroups.cfg:定义监控的主机(组),需手动建立。~~
    .

配置文件之间的关系

在nagios的配置过程当中涉及的几个定义有主机、主机组、服务、服务组、联系人、联系人组、监控时间和监控命令等。从这些定义能够看出,nagios各个配置文件之间互为关联、彼此引用的。成功配置出一台nagios监控系统,每一个配置文件之间依赖与被依赖的关系,最重要的有四点:

  • 定义监控那些主机,主机组,服务和服务组
  • 定义这个监控要用什么命令实现
  • 定义监控的时间段
  • 定义主机或服务器出现问题时要通知的联系人和联系人祖
    .

配置nagios

为了能更清楚的说明问题,同时也为了维护方便,建议将nagios各个定义的对象建立独立的配置文件。

  • 建立conf目录来定义host主机
  • 建立hostgroups.cfg文件来定义主机组
  • 用默认的contacts.cfg文件来定义联系人和联系人组
  • 用默认的commands.cfg文件来定义命令
  • 用默认的timeperiods.cfg来定义监控时间段
  • 用默认的templetes.cfg文件做为资源引用文件
    .

配置nagios,修改配置文件

[root@centos1 /]#  vim /usr/local/nagios/etc/nagios.cfg
在cfg_file下面添加两行
cfg_file=/usr/local/nagios/etc/object/hostgroups.cfg
cfg_dir=/usr/local/nagios/etc/conf 
[root@centos1 /]#   mkdir /usr/local/nagios/etc/conf

.

[root@centos1 /]#   vim /usr/local/nagios/etc/objects/commands.cfg 
在最下方添加
define command{
    command_name    check_nrpe
    command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }

.

[root@centos1 /]#    vim /usr/local/nagios/etc/objects/contacts.cfg 
添加到contactgroup_name    admins的下面
define contact{
    contact_name        ydw
    alias           ydw
    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 root
    }

.

新建/usr/local/nagios/etc/objects/hostgroups.cfg(定义主机组)
[root@centos1 /]#   vim /usr/local/nagios/etc/objects/hostgroups.cfg
define hosrgroup{
    hostgroup_name webmysql
    alias webmysql
    members 192.168.1.20
    }

.
在/usr/local/nagios/etc/conf下面新建192.168.1.20.cfg文件(用于监控192.168.1.20的主机存活,负载,进程)

[root@centos1 /]#   cd /usr/local/nagios/etc/conf/
[root@centos1 /]#   vim 192.168.1.20.cfg
define host{
host_name 192.168.1.20
alias 192.168.1.20
address 192.168.1.20
check_command check-host-alive
max_check_attempts 5
check_period 24x7
contact_groups ydw
notification_period 24x7
notification_options d,u,r
}

define service{
host_name 192.168.1.20
service_description check-host-alive
check_command check-host-alive
max_check_attempts 3
normal_check_interval 2
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups ydw
}

define service{
host_name 192.168.1.20
service_description check-proce
check_command check_nrpe!check_total_procs
max_check_attempts 3
normal_check_interval 2
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups ydw
}

define service{
host_name 192.168.1.20
service_description check-load
check_command check_nrpe!check_load
max_check_attempts 3
normal_check_interval 2
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7

.
.

命令解释:
define host{  
       use         linux-server            //定义使用的模板
       host_name   nagios             //被监控主机的名称,最好别带空格  
       alias         nagios               //别名        
       address      127.0.0.1          //被监控主机的IP地址        
       check_command    check-host-alive  
normal_check_interval   3           //正常检测间隔时间
retry_check_interval    2              //重试检测间隔时间
        //监控的命令check-host-alive,这个命令来自commands.cfg,用来监控主机是否存活  
       max_check_attempts    5     //检查失败后重试的次数  
       check_period        24x7       //检查的时间段24x7,一样来自timeperiods.cfg中定义 
notification_interval  10            //提醒的间隔,每隔10秒提醒一次 
notification_period   24x7       //提醒的周期, 24x7,一样来自timeperiods.cfg中定义
contact_groups   admins       //联系人组,上面在contactgroups.cfg中定义的admins
notification_options       d,u,r     //指定什么状况下提醒 
        }  
.

进入/usr/local/nagios/etc/objects/contacts.cfg在最后添加

[root@centos1 /]#   vim /usr/local/nagios/etc/objects/commands.cfg 
define contacatgroup{
    contactgroup_name   ydw
    alias                           ydw
    members                     ydw
    }

.

重启nagios服务
[root@centos1 /]#   service nagios restart

.
开防火墙例外

chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/ 
chcon -R -t httpd_sys_content_t /usr/local/nagios/share/

配置被控端192.168.1.20(mysql和web)
直接用脚本安装

[root@centos1 /]#  mount /dev/cdrom /media/
[root@centos1 /]#  cd /media/
[root@centos1 /]#  chmod +x nagiosclient.sh

.
将脚本和软件复制到/usr/src

[root@centos1 /]#  cp nagios-plugins-1.5.tar.gz /usr/src/
[root@centos1 /]#  cp nrpe-2.15.tar.gz /usr/src/
[root@centos1 /]#  cp nagiosclient.sh /usr/src/

.
换6.5光盘

[root@centos1 /]#   umount /dev/cdrom /media/
[root@centos1 /]#  mount /dev/cdrom /media/
[root@centos1 /]#  cd /usr/src/
.
执行脚本:
[root@centos1 /]#  sh nagiosclient.sh

.
安装完成以后,须要打开vim /usr/local/nagios/etc/nrpe.cfg
添加nagios服务器的地址

[root@centos1 /]#  vim /usr/local/nagios/etc/nrpe.cfg 
allowed_hosts=127.0.0.1,192.168.1.10
.
启动nrpe,
[root@centos1 /]#  /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

.
如在服务端测试不行能够重启主机后重试
[root@centos1 /]# reboot
.

(服务器测试)

[root@centos1 /]#   /usr/local/nagios/libexec/check_nrpe -H 192.168.1.20
NRPE v2.15

nagios监控系统
..........
nagios监控系统
.
补充:也可在services.cfg文件中添加192.168.1.20.cgf文件中的参数

#vi  /usr/local/nagios/etc/objects/services.cfg
内容以下:

1define service{
    use         local-service
    host_name       nagios
    service_description ping
    check_command       check-host-alive
    }
2define service{
    use         local-service
    host_name       nagios
    service_groups      系统情况检查
    service_description 登录用户数
    check_command       check-host-users!20!50
    }
3define service{
    use         local-service
    host_name       nagios
    service_groups      系统情况检查
    service_description 根分区
    check_command       check-local_disk!20%!10%!/
    }
4define service{
    use         local-service
    host_name       nagios
    service_groups      系统情况检查
    service_description 进程总数
    check_command       check-local_procs!250!400!RSZDT
    }
5define service{
    use         local-service
    host_name       nagios
    service_groups      系统情况检查
    service_description 系统负载
    check_command       check-host-load!5.0,4.0,3.0!10.0,6.0,4.0
    }
6define service{
    use         local-service
    host_name       nagios
    service_groups      系统情况检查
    service_description 交换空间利用率
    check_command       check-local_swap!20!10
    }
7define service{
    servicegroup_name   系统情况检查
    alias           系统概况
    }
check_local_users!20!50       //监测远程主机当前的登陆用户数量,若是大于20用户则报warning,若是大于50则报critical
.
check_local_disk!20%!10%!/        //若是可用空间低于20%会报Warning,若是可用空间低于10%则报Critical:
.
check_local_procs!250!400!RSZDT   //监测远程主机当前的进程总数,若是大于250进程则报warning,若是大于400进程则报critical,S(休眠)、R(运行)、Z(僵死)、D (不可中断)、T (中止)
.
check_load -w 5,4,3 -c 10,6,4这个命令的意义以下
* 当1分钟多于5个进程等待,5分钟多于4个,15分钟多于3个则为warning状态
* 当1分钟多于10个进程等待,5分钟多于6个,15分钟多于4个则为critical状态
服务组并非必须的,这是配合nagios的监控页面的显示