Nagios 监控
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nagios 监控相关的知识,希望对你有一定的参考价值。
一、Nagios服务器端安装
1、环境准备
1.1基础环境
[[email protected] yum.repos.d]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[[email protected] yum.repos.d]# uname -r
2.6.32-573.el6.x86_64
[[email protected] yum.repos.d]# uname -m
x86_64
1.2准备3台服务器
管理IP 角色 备注
10.0.0.61 nagios Nagios服务器端(管理服务器)
10.0.0.8 web01 被监控的客户端服务器
10.0.0.7 web02 被监控的客户端服务器
1.3设置yum源安装
ping www.baidu.com(确保可以上网)
cd /etc/yum.repos.d/
/bin/mv CentOS-Base.repo CentOS-Base.repo.oldboy.ori
wget -O /etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-6.repo
[[email protected] ~]# ping www.baidu.com
PING www.baidu.com (119.75.218.70) 56(84) bytes of data.
64 bytes from 119.75.218.70: icmp_seq=1 ttl=128 time=19.0ms
64 bytes from 119.75.218.70: icmp_seq=2 ttl=128 time=4.46ms
64 bytes from 119.75.218.70: icmp_seq=3 ttl=128 time=3.66ms
^C
--- www.baidu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time2291ms
rtt min/avg/max/mdev = 3.666/9.070/19.078/7.084 ms
[[email protected] ~]# cd /etc/yum.repos.d
[[email protected] yum.repos.d]# ls
CentOS-Base.repo CentOS-Media.repo epel-testing.repo
CentOS-Debuginfo.repo CentOS-Vault.repo
CentOS-fasttrack.repo epel.repo
[[email protected] yum.repos.d]# /bin/mv CentOS-Base.repo CentOS-Base.repo.oldboy.ori
[[email protected] yum.repos.d]# wget -O/etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
[[email protected] yum.repos.d]# cd ~
1.4解决Perl软件编译问题
cd ~
echo ‘export LC_ALL=C‘>>/etc/profile
tail -1 /etc/profile
source /etc/profile
echo $LC_ALL
cd ~
[[email protected] ~]# echo‘export LC_ALL=C‘>>/etc/profile
[[email protected] ~]# tail -1/etc/profile
export LC_ALL=C
[[email protected] ~]# source/etc/profile
[[email protected] ~]# echo$LC_ALL
C
[[email protected] ~]# cd ~
1.5.关闭防火墙及selinux
在测试环境下为了方便,最好关掉防火墙及selinex,如果是生产环境中,因为有外部IP,所以在调试完毕后需要开启防火墙。
/etc/init.d/iptables stop
/etc/init.d/iptables status
chkconfig iptables off
chkconfig --list iptables
sed -i ‘s#SELINUX=enforcing#SELINUX=disabled#g‘/etc/selinux/config
getenforce
关闭防火墙
[[email protected] ~]#/etc/init.d/iptables stop
[[email protected] ~]#/etc/init.d/iptables status
iptables: Firewall isnot running.
[[email protected] ~]# chkconfigiptables off
[[email protected] ~]# chkconfig--list iptables
iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
关闭SElinux
[[email protected] ~]# sed -i‘s#SELINUX=enforcing#SELINUX=disabled#g‘ /etc/selinux/config
[[email protected] ~]#getenforce 查看
Disabled
[[email protected] ~]# setenforce0 临时生效
setenforce: SELinux isdisabled
[[email protected] ~]#getenforce
Disabled
1.6解决系统时间同步问题
利用NTP时间同步或者配合定时任务来执行
#time sync by oldboy at2010-2-1
*/5 * * * */usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
1.7安装Nagios服务端所需安装包
Nagios服务器端需要有web界面展示监控效果,界面的展示主要使用php程序,因此,需要LAMP环境。
注意:yum安装LAMP环境是配合Nagios服务端展示界面的最佳环境。(不要安装LNMP环境)
yum install gcc glibc glibc-common -y 编译软件升级
yum install gd gd-devel -y 用于后面PNP出图的包
yum install mysql-server -y ##非必须的,如果有监控数据库,那么需要先安装mysql,否则mysql的相关插件不会被安装
yum install httpd php php-gd -y ##Apache,PHP环境
rpm -qa mysql httpd php
(3个包)
提示:通过yum工具安装上述所有软件包,且这些环境一般不需要在Nagios客户端安装。
上面软件包安装好了之后的版本为:Apache2.2.15、PHP5.3.3、mysql5.1.73
[[email protected] ~]# rpm -qa mysql httpd php
httpd-2.2.15-47.el6.centos.4.x86_64
php-5.3.3-46.el6_7.1.x86_64
mysql-5.1.73-5.el6_7.1.x86_64
1.8、创建Nagios服务器端需要的用户及组
/usr/sbin/useradd nagios ###这个地方最好创建家目录,否则,启动Nagios会提醒没家目录
/usr/sbin/useradd apache -M -s /sbin/nologin
/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd apache
id -n -G nagios
id -n -G apache
[[email protected] ~]#/usr/sbin/useradd nagios
[[email protected] ~]#/usr/sbin/useradd apache -M -s /sbin/nologin
useradd: user ‘apache‘already exists
[[email protected] ~]#/usr/sbin/groupadd nagcmd
[[email protected] ~]#/usr/sbin/usermod -a -G nagcmd nagios
[[email protected] ~]#/usr/sbin/usermod -a -G nagcmd apache
[[email protected] ~]# id -n -Gnagios
nagios nagcmd
[[email protected] ~]# id -n -Gapache
apache nagcmd
[[email protected] ~]#
1.9上传软件包到指定目录或通过URL下载
mkdir -p /home/oldboy/tools/nagios
cd /home/oldboy/tools/nagios
rz
[[email protected] ~]# mkdir -p /home/oldboy/tools/nagios
[[email protected] ~]# cd /home/oldboy/tools/nagios/
[[email protected] nagios]# rz
rz waiting to receive.
???a? zmodem ′???£ °′ Ctrl+C ???£
??′??oldboy_training_nagios_soft.zip...
100% 7387 KB 7387 KB/s 00:00:01 0 ′?
?[[email protected] nagios]# ll
total 7388
-rw-r--r-- 1 root root 7564715 May 22 16:05oldboy_training_nagios_soft.zip
[[email protected] nagios]# unzip oldboy_training_nagios_soft.zip
Archive: oldboy_training_nagios_soft.zip
inflating:check_memory.pl
inflating:check_mysql
inflating:Class-Accessor-0.31.tar.gz
extracting:Config-Tiny-2.12.tar.gz
inflating:libart_lgpl-2.3.17.tar.gz
inflating:Math-Calc-Units-1.07.tar.gz
inflating:Nagios-Plugin-0.34.tar.gz
inflating:nrpe-2.12.tar.gz
inflating:Params-Validate-0.91.tar.gz
inflating:pnp-0.4.14.tar.gz
inflating:Regexp-Common-2010010201.tar.gz
inflating:rrdtool-1.2.14.tar.gz
inflating:check_iostat
inflating:nagios-3.5.1.tar.gz
inflating:nagios-plugins-1.4.16.tar.gz
inflating:rrdtool-1.2.30.tar.gz
[[email protected] nagios]# tree
.
|-- Class-Accessor-0.31.tar.gz
|-- Config-Tiny-2.12.tar.gz
|-- Math-Calc-Units-1.07.tar.gz
|-- Nagios-Plugin-0.34.tar.gz
|-- Params-Validate-0.91.tar.gz
|-- Regexp-Common-2010010201.tar.gz
|-- check_iostat
|-- check_memory.pl
|-- check_mysql
|-- libart_lgpl-2.3.17.tar.gz
|-- nagios-3.5.1.tar.gz
|-- nagios-plugins-1.4.16.tar.gz
|-- nrpe-2.12.tar.gz
|-- oldboy_training_nagios_soft.zip
|-- pnp-0.4.14.tar.gz
|-- rrdtool-1.2.14.tar.gz
`-- rrdtool-1.2.30.tar.gz
0 directories, 17 files
启动LAMP环境的HTTP服务
[[email protected] nagios]# /etc/init.d/httpd start
Startinghttpd: httpd: Could not reliably determine the server‘s fully qualified domainname, using 172.16.1.61 for ServerName ##可忽略或表示httpd.conf中缺少ServerName配置,可在/etc/httpd/conf/httpd.conf中加入ServerName127.0.0.1:80
[ OK ]
[[email protected] nagios]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine theserver‘s fully qualified domain name, using 172.16.1.61 for ServerName
[ OK ]
[[email protected] nagios]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODENAME
httpd 5527 root 4u IPv6 46082 0t0 TCP *:http (LISTEN)
httpd 5529apache 4u IPv6 46082 0t0 TCP *:http (LISTEN)
httpd 5530apache 4u IPv6 46082 0t0 TCP *:http (LISTEN)
httpd 5531apache 4u IPv6 46082 0t0 TCP *:http (LISTEN)
httpd 5532apache 4u IPv6 46082 0t0 TCP *:http (LISTEN)
httpd 5533apache 4u IPv6 46082 0t0 TCP *:http (LISTEN)
httpd 5534apache 4u IPv6 46082 0t0 TCP *:http (LISTEN)
httpd 5535apache 4u IPv6 46082 0t0 TCP *:http (LISTEN)
httpd 5536apache 4u IPv6 46082 0t0 TCP *:http (LISTEN)
2、安装Nagios服务器端
unzip -q oldboy_training_nagios_soft.zip
tar xf nagios-3.5.1.tar.gz
cd nagios
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
[[email protected] httpd]# cd /home/oldboy/tools/nagios/
[[email protected] nagios]# pwd
/home/oldboy/tools/nagios
[[email protected] nagios]#tar xf nagios-3.5.1.tar.gz
[[email protected] nagios]#ls
Class-Accessor-0.31.tar.gz libart_lgpl-2.3.17.tar.gz
Config-Tiny-2.12.tar.gz nagios
Math-Calc-Units-1.07.tar.gz nagios-3.5.1.tar.gz
Nagios-Plugin-0.34.tar.gz nagios-plugins-1.4.16.tar.gz
Params-Validate-0.91.tar.gz nrpe-2.12.tar.gz
Regexp-Common-2010010201.tar.gz oldboy_training_nagios_soft.zip
check_iostat pnp-0.4.14.tar.gz
check_memory.pl rrdtool-1.2.14.tar.gz
check_mysql rrdtool-1.2.30.tar.gz
[[email protected] nagios]# cd nagios
[[email protected] nagios]#./configure --with-command-group=nagcmd
出现下面的说明编译成功
Review the options abovefor accuracy. If they look okay,
type ‘make all‘ tocompile the main program and CGIs.
[[email protected] nagios]# make all
出现这表示正常
*************************************************************
Enjoy.
[[email protected] nagios]# make install
出现这表示成功了
make[1]: Leaving directory`/home/oldboy/tools/nagios/nagios‘
[[email protected] nagios]# make install-init ##安装初始化脚本到/etc/rc.d/init.d
/usr/bin/install -c -m755 -d -o root -g root /etc/rc.d/init.d
/usr/bin/install -c -m755 -o root -g root daemon-init /etc/rc.d/init.d/nagios
*** Init scriptinstalled ***
[[email protected] nagios]# make install-config ##生成Nagios模块配置文件到/usr/local/nagios/etc
出现下面表示安装成功了
Remember, these are *SAMPLE* config files. You‘ll need to read
the documentation for more information on how to actuallydefine
services, hosts, etc. to fit your particular needs.
[[email protected] nagios]# make install-commandmode #安装配置目录许可外部命令文件
/usr/bin/install -c -m 775 -o nagios -g nagcmd -d/usr/local/nagios/var/rw
chmod g+s /usr/local/nagios/var/rw
*** External command directory configured ***
2.1、安装Nagios Web配置文件及创建登录用户
make install-webconf
htpasswd -bc /usr/local/nagios/etc/htpasswd.users oldboy123456
cat /usr/local/nagios/etc/htpasswd.users
/etc/init.d/httpd start
创建nagios web监控界面后,登录时会需要用户和名,密码(用户:oldboy密码:123456)
[[email protected] nagios]# makeinstall-webconf ##生成/etc/httpd/conf.d/nagios.conf配置文件
/usr/bin/install -c -m644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
*** Nagios/Apache conffile installed ***
[[email protected] nagios]# htpasswd -bc/usr/local/nagios/etc/htpasswd.users oldboy 123456
Adding password for useroldboy
重新加载Apache
[[email protected] nagios]# /etc/init.d/httpd reload
Reloading httpd:
[[email protected] nagios]#
2.2、添加监控报警信息接受的Email地址
cp /usr/local/nagios/etc/objects/contacts.cfg{,.ori}
sed -i ‘s#[email protected]#[email protected]#g‘/usr/local/nagios/etc/objects/contacts.cfg
使用第三方邮件服务商提供的邮箱,把下列一行添加达到/etc/mail.rc里
[[email protected] tools]# tail -1 /etc/mail.rc
set [email protected] smtp=smtp.163.comsmtp-auth-user=13265571477 smtp-auth-password=DA62073621 smtp-auth=login
操作前先备份
[[email protected] nagios]# cp/usr/local/nagios/etc/objects/contacts.cfg{,.ori}
[[email protected] nagios]# sed-i ‘s#[email protected]#[email protected]#g‘/usr/local/nagios/etc/objects/contacts.cfg
[[email protected] nagios]# sed-n ‘35p‘ /usr/local/nagios/etc/objects/contacts.cfg
email [email protected] ;<<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
使用第三方邮件服务商提供的邮箱,把下列一行添加达到/etc/mail.rc里
[[email protected] tools]# tail -1 /etc/mail.rc
[email protected] smtp=smtp.163.com smtp-auth-user=13265571477smtp-auth-password=DA62073621 smtp-auth=login
2.3、配置Apache服务并加入系统开机自启动
/etc/init.d/httpd start
/etc/init.d/httpd restart
chkconfig httpd on
netstat -lntup|grep httpd
lsof -i :80
在浏览器登录
10.0.0.61/nagios
输入用户名和密码
oldboy
123456
显示nagios core就正常了
[[email protected] nagios]#/etc/init.d/httpd start
Starting httpd:
[[email protected] nagios]#/etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[[email protected] nagios]#chkconfig httpd on
[[email protected] nagios]#netstat -lntup|grep httpd
tcp 0 0 :::80 :::* LISTEN 10922/httpd
在浏览器输入
10.0.0.61/nagios
输入用户名和密码
oldboy
123456
显示nagios core就正常了
2.4、安装Nagios插件软件包
安装基础依赖包
yum install perl-devel openssl-devel -y
安装Nagiospluginx插件包
cd ..
ls nagios-plugins-1.4.16.tar.gz
tar xf nagios-plugins-1.4.16.tar.gz
cd nagios-plugins-1.4.16
./configure --with-nagios-user=nagios --with-nagios-group=nagios--enable-perl-modules --with-mysql
make
make install
2.5、安装nrpe软件
cd /home/oldboy/tools/nagios
ls nrpe-2.12.tar.gz
tar xf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
ls /usr/local/nagios/libexec/check_nrpe
ls /usr/local/nagios/libexec/|wc -l
到此为止Nagios服务器端的软件安装部分就配置完成了
2.6、配置并启动Nagios服务
添加Nagios服务到开机自启动
echo "/etc/init.d/nagiosstart">>/etc/rc.local
tail -1 /etc/rc.local
/etc/init.d/nagios checkconfig
启动Nagios服务
/etc/init.d/nagios start
检查Nagios服务器端进程及端口
ps -ef |grep nagios|grep -v grep
netstat -lntup|grep nagios(无内容就是正确的)
检查语法:
[[email protected] nrpe-2.12]#/etc/init.d/nagios checkconfig
Running configurationcheck... OK.
2.7最后在浏览器输入http://10.0.0.61/nagios/,出现下面结果表明正确
有关出错问题看书(p570)
1、出现Forbidden403一般是由服务器端权限引起的,解决办法执行:yum install php -y
二、Nagios客户端安装
2.1Nagios客户端无需安装LAMP环境,
2.安装前准备
2.1、基础环境
[[email protected] yum.repos.d]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[[email protected] yum.repos.d]# uname -r
2.6.32-573.el6.x86_64
[[email protected] yum.repos.d]# uname -m
x86_64
2.2、准备2台服务器
管理IP 角色 备注
10.0.0.8 web01 被监控的客户端服务器
10.0.0.7 web02 被监控的客户端服务器
2.3.解决Perl软件编译问题
echo ‘export LC_ALL=C‘>>/etc/profile
tail -1 /etc/profile
source /etc/profile
echo $LC_ALL
2.4.关闭防火墙及selinux
getenforce #(显示Disabled下面就不用执行了)
/etc/init.d/iptables stop
/etc/init.d/iptables status
chkconfig --list iptables
sed -i ‘s#SELINUX=enforcing#SELINUX=disabled#g‘/etc/selinux/config
修改配置文件则永久生效,但是必须重启系统
getenforce
2.5、解决系统时间同步问题
crontab -l
也可以做NTP时间同步
3、正式安装
1、安装基础系统软件
yum install gcc glibc glibc-common -y
yum install mysql-server -y
rpm -qa mysql
2、上传软件包到指定目录或通过URL下载
mkdir -p /home/oldboy/tools/nagios
cd /home/oldboy/tools/nagios
rz
unzip -q oldboy_training_nagios_soft.zip
3、添加Nagios用户
useradd nagios-M -s /sbin/nologin
id nagios
4、安装nagios-plugins插件
yum install perl-devel perl-CPAN openssl-devel -y
tar xf nagios-plugins-1.4.16.tar.gz
cd nagios-plugins-1.4.16
./configure --with-nagios-user=nagios --with-nagios-group=nagios--enable-perl-modules --with-mysql
make
make install
cd ..
ls /usr/local/nagios/libexec/|wc -l
5、安装nagios客户端nrpe软件
cd ..
ls /usr/local/nagios/libexec/check_nrpe
tar xf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
ls /usr/local/nagios/libexec/check_nrpe
ls /usr/local/nagios/libexec/|wc -l
6、安装其他相关的插件
cd ..
#----------Dear,我是分隔符---------------------
tar zxf Params-Validate-0.91.tar.gz
cd Params-Validate-0.91
perl Makefile.PL
make
make install
cd ..
#----------Dear,我是分隔符---------------------
tar zxf Class-Accessor-0.31.tar.gz
cd Class-Accessor-0.31
perl Makefile.PL
make
make install
cd ..
#----------Dear,我是分隔符---------------------
tar zxf Config-Tiny-2.12.tar.gz
cd Config-Tiny-2.12
perl Makefile.PL
make
make install
cd ..
#----------Dear,我是分隔符---------------------
tar zxf Math-Calc-Units-1.07.tar.gz
cd Math-Calc-Units-1.07
perl Makefile.PL
make
make install
cd ..
#----------Dear,我是分隔符---------------------
tar zxf Regexp-Common-2010010201.tar.gz
cd Regexp-Common-2010010201
perl Makefile.PL
make
make install
cd ..
#----------Dear,我是分隔符---------------------
tar zxf Nagios-Plugin-0.34.tar.gz
cd Nagios-Plugin-0.34
perl Makefile.PL
make
make install
cd ..
#----------Dear,我是分隔符---------------------
yum install sysstat -y
如果报错就是前面的perl环境变量没提前设置好
以上是关于Nagios 监控的主要内容,如果未能解决你的问题,请参考以下文章