1. 程式人生 > >Nagios監控安裝之一

Nagios監控安裝之一

groupadd tac 方便 認證 apache servers config 命令格式 vim

配置nagios監控系統文件
Nagios的配置文件:
Nagios.cfg:主配置文件,定義各種配置文件的名稱和位置
Cgi.cfg:控制CGI的配置文件
Resource.cfg:資源文件,定義各種變量,以便於其他文件調用
Objects:其他配置文件存放目錄,此目錄下主要有:
Command.cfg:命令配置文件,定義各種命令格式,以備其他文件調用
contacts.cfg:聯系人和組,發郵件等告警信息時可以調用
localhost.cfg:監控本機的配置文件
timeperiods.cfg:定義監控時間的配置文件,便於其他文件調用
Hostgroups.cfg:定義監控的主機(組),需手動創建。

配置文件之間的關系

在nagios的配置過程中涉及的幾個定義有主機、主機組、服務、服務組、聯系人、聯系人組、監控時間和監控命令等。
從這些定義可以看出,nagios各個配置文件之間互為關聯、彼此引用的。成功配置出一臺nagios監控系統,每個配置
文件之間依賴與被依賴的關系,最重要的有四點:
1,定義監控那些主機,主機組,服務和服務組
2,定義這個監控要用什麽命令實現
3,定義監控的時間段
4,定義主機或服務器出現問題時要通知的聯系人和聯系人組

配置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,
[root@NFS-Client ~]# /usr/sbin/useradd -m nagios -s /sbin/nologin
6,上傳安裝包
[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
7,
[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 ..
8,安裝插件監控磁盤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監控服務
[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
1,主機服務
[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
}

2,服務配置
[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

              }

3,命令配置
[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$
}

Nagios監控安裝之一