Nagios安装与配置文档
一、安装
测试运行环境Centosx64-6.4
1、服务端的安装脚本,直接运行即可,备注:运行的时候需要输入两个密码,第一个
用户nagios的密码,第二个密码是nagiosadmin登录nagios的密码
###################以下是服务端的安装脚本,直接运行即可####################
#!/bin/bash
yum-yinstallhttpdphpgccglibcglibc-commongdgd-devel
cd/opt
/bin/tar-zxfnagios-3.2.3.tar.gz
/usr/sbin/useradd-mnagios
/usr/sbin/useradd-mapache
passwdnagios
/usr/sbin/groupaddnagcmd
/usr/sbin/usermod-a-Gnagcmdnagios
/usr/sbin/usermod-a-Gnagcmdapache
cdnagios-3.2.3
./configure--with-command-group=nagcmd
makeall
makeinstall
makeinstall-init
makeinstall-config
makeinstall-commandmode
mkdir-p/etc/httpd/conf.d
makeinstall-webconf
/usr/bin/htpasswd-c/usr/local/nagios/etc/htpasswd.usersnagiosadmin
servicehttpdrestart
cd/opt
/bin/tar-zxfnagios-plugins-1.4.15.tar.gz
cdnagios-plugins-1.4.15
./configure--with-nagios-user=nagios--with-nagios-group=nagios--enable-redhat-pthread-workaround
make
makeinstall
chkconfig--addnagios
chkconfignagioson
/usr/local/nagios/bin/nagios-v/usr/local/nagios/etc/nagios.cfg
servicenagiosstart
cd/opt
/bin/tar-zxfnrpe-2.12.tar.gz
cdnrpe-2.12
./configure
makeall
makeinstall-plugin
makeinstall-daemon
makeinstall-daemon-config
###################以上是服务端的安装脚本,直接运行即可##################
2、客户端的安装脚本,直接在客户机安装即可
###################以下是客户端的安装脚本,直接运行即可##################
#!/bin/bash
usr/sbin/useradd-mnagios
passwdnagios
cd/opt
/bin/tar-zxfnagios-plugins-1.4.15.tar.gz
cdnagios-plugins-1.4.15
./configure--with-nagios-user=nagios--with-nagios-group=nagios--enable-redhat-pthread-workaround
make
makeinstall
chownnagios.nagios/usr/local/nagios
chown�\Rnagios.nagios/usr/local/nagios/libexec
chkconfig--addnagios
chkconfignagioson
/usr/local/nagios/bin/nagios-v/usr/local/nagios/etc/nagios.cfg
servicenagiosstart
/usr/bin/chcon-R-thttpd_sys_content_t/usr/local/nagios/sbin/
/usr/bin/chcon-R-thttpd_sys_content_t/usr/local/nagios/share/
cd/opt
/bin/tar-zxfnrpe-2.12.tar.gz
cdnrpe-2.12
./configure
makeall
makeinstall-plugin
makeinstall-daemon
makeinstall-daemon-config
###################以上是客户端的安装脚本,直接运行即可##################
二、安装完之后,测试登录
http://localhost/nagios输入上面的用户名与密码即可正常登录了
三、nrpe相关命令
1、返回当前NRPE的版本:
/usr/local/nagios/libexec/check_nrpe�CHlocalhost
NRPEv2.12
也就是在本地用check_nrpe命令连接nrpedaemon是正常的
注:为了后面工作的顺利进行,注意本地防火墙要打开5666能让外部的监控机访问
2、/usr/local/nagios/libexec/check_nrpe�Chelp查看check_nrpe命令的用法
3、check_nrpe�CH被监控的主机�Cc要执行的监控命令
注:-c后面接的监控命令必须是nrpe.cfg文件定义的,也就是NRPEdaemon只运行nrpe.cfg中所定义的命令
4、在监控机与被监控机上启动NRPE:
/usr/local/nagios/bin/nrpe�Cc/usr/local/nagios/etc/nrpe.cfg�Cd
四、在nagios服务器上配置被监控机器上的80端口:
1、首先在被监控机器上写一个判断80端口的脚本,在这里我自己写了一个判断80端
口的脚本,脚本内容如下,并且把脚本放到/usr/local/nagios/libexec目录下,并赋予其执行权限:
脚本的名称为:check_port,内容如下:
#!/bin/bash
PORT=`netstat-ntpl|grep80|awk-F:'{print$4}'|awk-F""'{print$1}'`
if["$PORT"=="80"];then
echo"80ISOK"
exit0
else
echo"80ISERROR"
exit2
fi
2、启动被监控机器上的nrpe进程
/usr/local/nagios/bin/nrpe�Cc/usr/local/nagios/etc/nrpe.cfg�Cd
3、nrpe运行默认端口为5666,请查看一下端口是否开启
4、检查nrpe的版本
/usr/local/nagios/libexec/check_nrpe�CHlocalhost
NRPEv2.12à版本为2.12
5、配置nrpe的配置文件,只需要修改/usr/local/nagios/etc/nrpe.conf即可,
修改的内容如下:
在第200行左右,添加如下内容:
command[check_port_1]=/usr/local/nagios/libexec/check_port
在第79行左右,修改如下内容:
allowed_hosts=127.0.0.1
allowed_hosts=127.0.0.1,192.168.2.90(监控机的IP地址)
6、被监控端的配置到此结束!!!
7、在监控机上配置:
①为了更清楚的理解主机与服务的概念,先在/usr/local/nagios/etc/objects目录
Touch两个hosts.cfg,service.cfg配置文件
②为了使上述的两个配置文件生效,请修改/usr/local/nagios/etc/nagios.cfg的配置
文件,修改内容如下:
大概在33行左右,先注释掉默认的:
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
再添加:
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/service.cfg
servicenagiosrestart使配置文件生效
③启动nrpe
/usr/local/nagios/bin/nrpe�Cc/usr/local/nagios/etc/nrpe.cfg�Cd
验证监控机与被监控制机之机能否利用nrpe传数据:
/usr/local/nagios/libexec/check_nrpe�CH被监控机IP
④在监控机上的hosts.cfg里添加如下配置文件:
definehost{#定义一个主机
uselinux-server#使用linux-server的模版
host_nameCNC-BJ-H-190#主机名称为:CNC-BJ-H-190(被监控机)
address192.168.2.190#主机的IP地址(被监控机)
}
⑤在监控机上的service.cfg里添加如下配置文件:
defineservice{
uselocal-service#使用local-service的模版
host_nameCNC-BJ-H-190#主机名为:CNC-BJ-H-190(被监控机)
service_descriptionport#描述
check_commandcheck_nrpe_port!check_port_1
}
备注:
上述的Check_nrpe_port命令是command.cfg里定义的关键字,不能随便写
上述的check_port_1命令是被监控端nrpe.cfg里定义的关键字,不能随便写
⑥下面针对command.cfg这个配置文件讲解一下:
这个配置文件主要是定义了命令,如果只需要满足上面的要求,可以把comman
d.cfg这个文件备份一下,然后新建立一个command.cfg,内容如下:
红色代表是错误的时候,主机与服务报警配置文件;
绿色代表是保活主机Ping的配置文件;
黑色代表是上面提到的端口命令配置文件:
#'notify-host-by-email'commanddefinition
definecommand{
command_namenotify-host-by-email
command_line/usr/bin/printf"%b""*****Nagios*****\n\nNotificationType:$NOTIFICATIONTYPE$\nHost:$HOSTNAME$\nState:$HOSTSTATE$\nAddress:$HOSTADDRESS$\nInfo:$HOSTOUTPUT$\n\nDate/Time:$LONGDATETIME$\n"|/bin/mail-s"**$NOTIFICATIONTYPE$HostAlert:$HOSTNAME$is$HOSTSTATE$**"$CONTACTEMAIL$
}
#'notify-service-by-email'commanddefinition
definecommand{
command_namenotify-service-by-email
command_line/usr/bin/printf"%b""*****Nagios*****\n\nNotificationType:$NOTIFICATIONTYPE$\n\nService:$SERVICEDESC$\nHost:$HOSTALIAS$\nAddress:$HOSTADDRESS$\nState:$SERVICESTATE$\n\nDate/Time:$LONGDATETIME$\n\nAdditionalInfo:\n\n$SERVICEOUTPUT$\n"|/bin/mail-s"**$NOTIFICATIONTYPE$ServiceAlert:$HOSTALIAS$/$SERVICEDESC$is$SERVICESTATE$**"$CONTACTEMAIL$
}
#'check-host-alive'commanddefinition
definecommand{
command_namecheck-host-alive
command_line$USER1$/check_ping-H$HOSTADDRESS$-w3000.0,80%-c5000.0,100%-p5
}
definecommand{
command_namecheck_nrpe_port
command_line$USER1$/check_nrpe-H被监控机的IP-ccheck_port_1
}
重启一下nagios服务。Servicenagiosrestart
到此为止,配置监控另一台机器的80端口就结束了,其它的配置类似,其实nagios里,最难的就是要理解{nrpe.cfg,command.cfg,hosts.cfg,service.cfg}这几个配置文件之前的关系,如果能理解这个,nagios就不难了
五、将绘图功能加入nagios
①要安装nagios的绘图工具pnp,如果pnp的版本高于1.2以上,需要先安装
libart_lgpl-2.3.17.tar.gz,负则安装Pnp会报错
tar�Czxflibart_lgpl-2.3.17.tar.gz
./configure
Make
Makeinstall
Cp�Cr/usr/local/include/libart-2.0/usr/include
②Nagios的绘图工具pnp,需要rrdtool工具支持,所以还得安装rrdtool工具
wgethttp://oss.oetiker.ch/rrdtool/pub/rrdtool-1.2.14.tar.gz
tarxfrrdtool-1.2.14.tar.gz
cdrrdtool-1.2.14
./configure--prefix=/usr/local/rrdtool--disable-python--disable-tcl
如果configure报如下错误:
configure: error: Please fix the library issues listed above and try again.
请执行yum -y install libart_lgpl-devel
make
makeinstall
③安装pnp绘图工具
wgethttp://downloads.sourceforge.net/project/pnp4nagios/PNP/pnp-0.4.14/pnp-0.4.14.tar.gz?use_mirror=cdnetworks-kr-2&80495215
tarzxfpnp-0.4.14.tar.gz
cdpnp-0.4.14
./configure\
--with-rrdtool=/usr/local/rrdtool/bin/rrdtool\
--with-perfdata-dir=/usr/local/nagios/share/perfdata/
makeall
makeinstall
makeinstall-config
makeinstall-init
④修改nagios的配置文件
#process_performance_data=0
process_performance_data=1
host_perfdata_command=process-host-perfdata
service_perfdata_command=process-service-perfdata
⑤修改command.cfg配置文件:
vi/usr/local/nagios/etc/objects/commands.cfg+227
#'process-host-perfdata'commanddefinition
definecommand{
command_nameprocess-host-perfdata
command_line$USER1$/process_perfdata.pl
}
#'process-service-perfdata'commanddefinition
definecommand{
command_nameprocess-service-perfdata
command_line$USER1$/process_perfdata.pl
}
⑥访问http://10.0.0.161/nagios/pnp/index.php
⑦在服务service.cfg下面加如下配置行就会出现小太阳图标
process_perf_data1
action_url/nagios/pnp/index.php?host=$HOSTNAME$
8、在主机hosts.cfg下面加如下配置行就会出现上太阳图标
process_perf_data1
action_url/nagios/pnp/index.php?host=$HOSTNAME$
本文出自 “于雷博客” 博客,转载请与作者联系!