Nagios 安装配置详细步骤


一、nagios配置文件的简单介绍:梳理一下nagios监控系统文件以方便我们后来的监控任务顺利实施;
1,Nagios的配置文件:
Nagios.cfg:主配置文件,定义各种配置文件的名称和位置
Cgi.cfg:控制CGI的配置文件
Resource.cfg:资源文件,定义各种变量,以便于其他文件调用
Objects:其他配置文件存放目录,此目录下主要有:
Command.cfg:命令配置文件,定义各种命令格式,以备其他文件调用
contacts.cfg:联系人和组,发邮件等告警信息时可以调用
localhost.cfg:监控本机的配置文件
timeperiods.cfg:定义监控时间的配置文件,便于其他文件调用
Hostgroups.cfg:定义监控的主机(组),需手动创建。


2,配置文件之间的关系:
在nagios的配置过程中涉及的几个定义有主机、主机组、服务、服务组、联系人、联系人组、监控时间和监控命令等。
从这些定义可以看出,nagios各个配置文件之间互为关联、彼此引用的。成功配置出一台nagios监控系统,每个配置文件之间依赖与被依赖的关系,最重要的有四点:
1,定义监控那些主机,主机组,服务和服务组
2,定义这个监控要用什么命令实现
3,定义监控的时间段
4,定义主机或服务器出现问题时要通知的联系人和联系人组


3,配置nagios
为了能更清楚的说明问题,同时也为了维护方便,建议将nagios各个定义的对象创建独立的配置文件。
创建conf目录来定义host主机
创建hostgroups.cfg文件来定义主机组
用默认的contacts.cfg文件来定义联系人和联系人组
用默认的commands.cfg文件来定义命令
用默认的timeperiods.cfg来定义监控时间段
用默认的templetes.cfg文件作为资源引用文件
二、安装环境、软件准备:
CentOS release 6.5 (Final)
nagios-3.5.1.tar.gz
nagios-plugins-1.4.16.tar.gz
nrpe-2.12.tar.gz
Nagios-Server 192.168.0.151 Client:192.168.0.152,192.168.0.150
正式安装前准备:
1,配置YUM源
2,配置环境变量

[root@Nagios-Server soft]# echo 'export LC_ALL=C'>> /etc/profile
[root@Nagios-Server soft]# tail -1 /etc/profile
export LC_ALL=C
[root@Nagios-Server soft]# source /etc/profile

3,关闭防火墙、SELINUX

[root@Nagios-Server soft]# /etc/init.d/iptables stop                           
[root@Nagios-Server soft]# chkconfig iptables off
[root@Nagios-Server soft]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
[root@Nagios-Server soft]# setenforce 0

4,配置定时任务

[root@Nagios-Server soft]# crontab -e
#time sync by tony at 2018-01-29
*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1

5,安装gcc和lamp环境

[root@Nagios-Server soft]#yum install gcc glibc glibc-common -y
[root@Nagios-Server soft]#yum install gd gd-devel -y
[root@Nagios-Server soft]#yum install httpd php php-gd -y
[root@Nagios-Server soft]#yum install mysql* -y

6,添加用户和用户组

[root@Nagios-Server soft]# /usr/sbin/useradd -m nagios
[root@Nagios-Server soft]# /usr/sbin/useradd apache
[root@Nagios-Server soft]# /usr/sbin/groupadd nagcmd
[root@Nagios-Server soft]# /usr/sbin/usermod -a -G nagcmd nagios
[root@Nagios-Server soft]# /usr/sbin/usermod -a -G nagcmd apache

7,安装Nagios-Core

[root@Nagios-Server soft]# tar xf nagios-3.5.1.tar.gz
[root@Nagios-Server soft]#cd nagios
[root@Nagios-Server nagios]# ./configure --with-command-group=nagcmd
[root@Nagios-Server nagios]# make all
[root@Nagios-Server nagios]# make install
[root@Nagios-Server nagios]# make install-init
[root@Nagios-Server nagios]# make install-config
[root@Nagios-Server nagios]# make install-commandmode
[root@Nagios-Server nagios]# make install-webconf

8,配置web认证:

[root@Nagios-Server nagios]# htpasswd -cb /usr/local/nagios/etc/htpasswd.users tony hwg123 
 Adding password for user tony

9,安装Nagios插件

[root@Nagios-Server soft]# yum -y install perl-devel
[root@Nagios-Server soft]# tar xf nagios-plugins-1.4.16.tar.gz 
[root@Nagios-Server soft]# cd nagios-plugins-1.4.16
[root@Nagios-Server nagios-plugins-1.4.16]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules
[root@Nagios-Server nagios-plugins-1.4.16]#make && make install
[root@Nagios-Server nagios-plugins-1.4.16]#cd ../
[root@Nagios-Server soft]# ls /usr/local/nagios/libexec/|wc -l

10,安装NRPE

[root@Nagios-Server soft]# tar xf nrpe-2.12.tar.gz  
[root@Nagios-Server soft]# cd nrpe-2.12
[root@Nagios-Server nrpe-2.12]# ./configure
[root@Nagios-Server nrpe-2.12]# make all
[root@Nagios-Server nrpe-2.12]# make install-plugin
[root@Nagios-Server nrpe-2.12]# make install-daemon
[root@Nagios-Server nrpe-2.12]# make install-daemon-config
[root@Nagios-Server nrpe-2.12]# cd ../

11,启动Nagios和Http

[root@Nagios-Server soft]# /etc/init.d/nagios start
[root@Nagios-Server soft]# /etc/init.d/httpd start 
[root@Nagios-Server soft]# lsof -i :80

三、客户端配置:
1,配置YUM源:
2,配置环境变量:

[root@NFS-Client ~]# echo 'export LC_ALL=C'>> /etc/profile
[root@NFS-Client ~]# tail -1 /etc/profile
export LC_ALL=C
[root@NFS-Client ~]# source /etc/profile

3,关闭防火墙、SELINUX

[root@Nagios-Server soft]# /etc/init.d/iptables stop                           
[root@Nagios-Server soft]# chkconfig iptables off
[root@Nagios-Server soft]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
[root@Nagios-Server soft]# setenforce 0

4,配置定时任务

[root@Nagios-Server soft]# crontab -e
#time sync by tony at 2018-01-29
*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1

5,添加nagios用户

[root@NFS-Client ~]# /usr/sbin/useradd -m nagios -s /sbin/nologin

6,上传安装包并安装nagios-plugins-1.4.16

[root@NFS-Client nagios-plugins-1.4.16]# yum -y install perl-devel perl-CPAN 
[root@NFS-Client data]# tar xf nagios-plugins-1.4.16.tar.gz 
[root@NFS-Client data]# cd nagios-plugins-1.4.16
[root@NFS-Client nagios-plugins-1.4.16]# ./configure --prefix=/usr/local/nagios --enable-perl-modules --enable-redhat-pthread-workaround
[root@NFS-Client nagios-plugins-1.4.16]# make && make install
[root@NFS-Client nagios-plugins-1.4.16]# cd ../
[root@NFS-Client data]# tar xf nrpe-2.12.tar.gz 
[root@NFS-Client data]# cd nrpe-2.12
[root@NFS-Client nrpe-2.12]# ./configure
[root@NFS-Client nrpe-2.12]# make all
[root@NFS-Client nrpe-2.12]# make install-plugin
[root@NFS-Client nrpe-2.12]# make install-daemon
[root@NFS-Client nrpe-2.12]# make install-daemon-config
[root@NFS-Client nrpe-2.12]# cd ..

7,安装插件监控磁盘IO

[root@NFS-Client data]# tar xf Params-Validate-0.91.tar.gz 
[root@NFS-Client data]# cd Params-Validate-0.91
[root@NFS-Client Params-Validate-0.91]#perl Makefile.PL 
[root@NFS-Client Params-Validate-0.91]# make
[root@NFS-Client Params-Validate-0.91]# make install

[root@NFS-Client data]# tar xf Class-Accessor-0.31.tar.gz 
[root@NFS-Client data]# cd Class-Accessor-0.31
[root@NFS-Client Class-Accessor-0.31]# perl Makefile.PL
[root@NFS-Client Class-Accessor-0.31]# make 
[root@NFS-Client Class-Accessor-0.31]# make install

[root@NFS-Client data]# tar xf Config-Tiny-2.12.tar.gz 
[root@NFS-Client data]# cd Config-Tiny-2.12
[root@NFS-Client Config-Tiny-2.12]# perl Makefile.PL
[root@NFS-Client Config-Tiny-2.12]# make 
[root@NFS-Client Config-Tiny-2.12]# make install

[root@NFS-Client data]# tar xf Math-Calc-Units-1.07.tar.gz 
[root@NFS-Client data]# cd Math-Calc-Units-1.07
[root@NFS-Client Math-Calc-Units-1.07]# perl Makefile.PL 
[root@NFS-Client Math-Calc-Units-1.07]# make
[root@NFS-Client Math-Calc-Units-1.07]# make install

[root@NFS-Client data]# tar xf Regexp-Common-2010010201.tar.gz 
[root@NFS-Client data]# cd Regexp-Common-2010010201
[root@NFS-Client Regexp-Common-2010010201]# perl Makefile.PL 
[root@NFS-Client Regexp-Common-2010010201]# make
[root@NFS-Client Regexp-Common-2010010201]# make install
[root@NFS-Client Regexp-Common-2010010201]# cd ..

[root@NFS-Client data]# tar xf Nagios-Plugin-0.34.tar.gz 
[root@NFS-Client data]# cd Nagios-Plugin-0.34
[root@NFS-Client Nagios-Plugin-0.34]# perl Makefile.PL 
[root@NFS-Client Nagios-Plugin-0.34]# make
[root@NFS-Client Nagios-Plugin-0.34]# make install

[root@NFS-Client data]# yum install sysstat -y
[root@NFS-Client data]# /bin/cp ./check_memory.pl  /usr/local/nagios/libexec
[root@NFS-Client data]# /bin/cp ./check_iostat /usr/local/nagios/libexec
[root@NFS-Client data]# chmod 755 /usr/local/nagios/libexec/check_memory.pl      
[root@NFS-Client data]# chmod 755 /usr/local/nagios/libexec/check_iostat
[root@NFS-Client data]# dos2unix /usr/local/nagios/libexec/check_memory.pl
[root@NFS-Client data]# dos2unix /usr/local/nagios/libexec/check_iostat

[root@NFS-Client etc]# cp nrpe.cfg  nrpe.cfg.ori
[root@NFS-Client etc]# sed -i '199,203d' /usr/local/nagios/etc/nrpe.cfg
[root@NFS-Client etc]#echo "command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,6 -c 30,25,20">>/usr/local/nagios/etc/nrpe.cfg
[root@NFS-Client etc]#echo "command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -w 6% -c 3%">>/usr/local/nagios/etc/nrpe.cfg
[root@NFS-Client etc]#echo "command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 8% -p /">>/usr/local/nagios/etc/nrpe.cfg
[root@NFS-Client etc]#echo "command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%">>/usr/local/nagios/etc/nrpe.cfg
[root@NFS-Client etc]#echo "command[check_iostat]=/usr/local/nagios/libexec/check_iostat -w 6 -c 10">>/usr/local/nagios/etc/nrpe.cfg
[root@NFS-Client ~]# pkill nrpe
[root@NFS-Client ~]# sleep 2
[root@NFS-Client ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@NFS-Client ~]# echo  "#nagios nrpe process cmd by tony 2018-2-2" >> /etc/rc.local
[root@NFS-Client ~]# echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d">>/etc/rc.local
[root@NFS-Client ~]# netstat -lnt|grep 5666 && echo "nagios client is ok"
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      
nagios client is ok

四、配置server端Nagios监控服务
1,配置客户端nagios.cfg

[root@Nagios-Server ~]# ll /usr/local/nagios/
total 32
drwxrwxr-x  2 nagios nagios 4096 Feb  1 22:33 bin #可执行文件
drwxrwxr-x  3 nagios nagios 4096 Feb  8 14:25 etc #Nagios主要配置文件
drwxr-xr-x  2 root   root   4096 Feb  1 22:25 include
drwxrwxr-x  2 nagios nagios 4096 Feb  1 22:32 libexec
drwxr-xr-x  5 root   root   4096 Feb  1 22:25 perl
drwxrwxr-x  2 nagios nagios 4096 Feb  1 22:09 sbin   #cgi程序
drwxrwxr-x 11 nagios nagios 4096 Feb  1 22:25 share  #Nagios展示程序PHP
drwxrwxr-x  5 nagios nagios 4096 Feb  8 21:57 var    #数据及日志文件

[root@Nagios-Server etc]# vim nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
cfg_file=/usr/local/nagios/etc/objects/services
# Definitions for monitoring the local (Linux) host
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

2,配置主机服务

[root@Nagios-Server objects]# vim hosts.cfg 
define host{
        use                     linux-server
        host_name               152-NFS
        alias                   152-NFS
        address                 192.168.0.152
        }
define host{
        use                     linux-server
        host_name               151-Nagios
        alias                   152-Nagios
        address                 192.168.0.151
        }
# Define an optional hostgroup for Linux machines
define hostgroup{
        hostgroup_name  linux-servers
        alias           Linux Servers
        members         152-NFS,151-Nagios
        }

3,配置主机服务

[root@Nagios-Server objects]# vim services.cfg 
define service {
                use                          generic-service
                host_name                    152-NFS
                service_description          Disk Partition
                check_command check_nrpe!    check_disk

                  }

define service {
                use                          generic-service
                host_name                    152-NFS
                service_description          Swap Useage
                check_command check_nrpe!    check_swap

                  }

define service {
                use                          generic-service
                host_name                    152-NFS
                service_description          MEM Useage
                check_command check_nrpe!    check_mem

                  }
define service {
                use                          generic-service
                host_name                    151-Nagios
                service_description          Disk Partition
                check_command check_nrpe!    check_disk

                  }

define service {
                use                          generic-service
                host_name                    151-Nagios
                service_description          Swap Useage
                check_command check_nrpe!    check_swap

                  }

define service {
                use                          generic-service
                host_name                    151-Nagios
                service_description          MEM Useage
                check_command check_nrpe!    check_mem

                  }

4,命令配置

[root@Nagios-Server objects]# vim commands.cfg 
在配置文件结尾加上这些内容
#'check_nrpe 'command definintion
define command{
        command_name  check_nrpe
        command_line  $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }