学海无涯学海无涯

I am a slow walker,
but i never walk backwards.

centos安装nagios

1.安装开始

rpm -ivh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
yum install nagios nagios-plugins nagios-plugins-all nagios-plugins-nrpe nrpe rrdtool* pnp4nagios httpd php php-gd

2、修改nagios.cfg

vim nagios.cfg

修改如下:

process_performance_data=1
service_perfdata_command=process-service-perfdata
host_perfdata_command=process-host-perfdata

3、修改nagios command配置文件

cd /usr/local/nagios/etc/objects
vim commands.cfg

#在文件末尾追加如下几行

define command {
command_name process-service-perfdata
command_line /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl
}
define command {
command_name process-host-perfdata
command_line /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA
}

4、修改nagios template 配置文件

vim templates.cfg

#在文件末尾追加如下几行

define host {
    name host-pnp
    action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
    register 0
}
define service {
    name srv-pnp
    action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
    register 0
}

5、修改 hosts.cfg 和 services.cfg

这两个文件默认是不存在的,是用来定义你要监控的主机和服务,需要你手工创建.

vim hosts.cfg

define host{
    use linux-server,host-pnp
    host_name web01
    alias pub_web01
    address 192.168.0.51
}
define host{
    use linux-server,host-pnp
    host_name oracle
    alias pub_oracle
    address 192.168.0.180
}

vim services.cfg

##################### pub web01 #####################
define service{
    use local-service,srv-pnp
    host_name web01
    service_description PING
    check_command check_ping!100.0,20%!500.0,60%
}
define service{
    use local-service,srv-pnp
    host_name web01
    service_description SSH
    check_command check_ssh
}
define service{
    use local-service,srv-pnp
    host_name web01
    service_description http
    check_command check_http
}

1、验证配置文件,重启服务

2、验证nagios配置文件

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

3、重启

/etc/init.d/nagios restart

Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.

/etc/init.d/npcd restart

Stopping npcd: done.
Starting npcd: done.

遇到错误:

Please check the documentation for information about the following error.perfdata directory "/usr/local/pnp4nagios/var/perfdata/" is empty. Please check your Nagios config. Read FAQ online

改pnp4nagios模式为 "Bulk-mode".

编辑/usr/local/nagios/etc/nagios.cfg末尾添加:

# Bulk / NPCD mode
process_performance_data=1
# *** the template definition differs from the one in the original nagios.cfg
service_perfdata_file=/usr/local/pnp4nagios/var/service-perfdata
service_perfdata_file_template=DATATYPE::SERVICEPERFDATAtTIMET::$TIMET$tHOSTNAME::$HOSTNAME$tSERVICEDESC::$SERVICEDESC$tSERVICEPERFDATA::$SERVICEPERFDATA$tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$tHOSTSTATE::$HOSTSTATE$tHOSTSTATETYPE::$HOSTSTATETYPE$tSERVICESTATE::$SERVICESTATE$tSERVICESTATETYPE::$SERVICESTATETYPE$
service_perfdata_file_mode=a
service_perfdata_file_processing_interval=15
service_perfdata_file_processing_command=process-service-perfdata-file
# *** the template definition differs from the one in the original nagios.cfg
host_perfdata_file=/usr/local/pnp4nagios/var/host-perfdata
host_perfdata_file_template=DATATYPE::HOSTPERFDATAtTIMET::$TIMET$tHOSTNAME::$HOSTNAME$tHOSTPERFDATA::$HOSTPERFDATA$tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$tHOSTSTATE::$HOSTSTATE$tHOSTSTATETYPE::$HOSTSTATETYPE$
host_perfdata_file_mode=a
host_perfdata_file_processing_interval=15
host_perfdata_file_processing_command=process-host-perfdata-file

编辑 /usr/local/nagios/etc/objects/commands.cfg. 最后添加一下内容:

# Bulk with NPCD mode
define command {
    command_name process-service-perfdata-file
    command_line /bin/mv /usr/local/pnp4nagios/var/service-perfdata /usr/local/pnp4nagios/var/spool/service-perfdata.$TIMET$
}
define command {
    command_name process-host-perfdata-file
    command_line /bin/mv /usr/local/pnp4nagios/var/host-perfdata /usr/local/pnp4nagios/var/spool/host-perfdata.$TIMET$
}

重启 npcd and nagios:

service npcd restart && service nagios restart

正常.

nrpe监控安装:

yum install nagios-plugins-nrpe nrpe

修改nrpe配置文件,加入允许的IP:

vi/usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,192.169.0.201

启动nrpe:

/usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg –d

在监控主机(200)上执行:

/usr/local/nagios/libexec/check_nrpe-H 192.168.30.150 #安装nrpe的被监控主机

如果成功会显示:

NRPE v2.13

编写测试代码(监控150负载均衡)

在监控主机上(200)编辑/usr/local/nagios/etc/objects/commands.cfg,加入如下命令:

# 'check_nrp' command definition
define command{
    command_name check_nrpe
    command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

/usr/local/nagios/etc/objects中添加文件150.cfg,对于每一台机器可以添加一个文件:

define host{
    use generic-host
    host_name 150
    alias 150
    address 192.168.30.150 ;被测试机ip
    max_check_attempts 10
    check_command check-host-alive ; Default command to check if servers are "alive"
}
define service{
    use generic-service
    host_name 150
    service_description nrpe_load
    check_command check_nrpe!check_load
}

检测配置是否正确后,重启nagios.

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
service nagios restart

报错NRPE: Unable to read output解决办法:

vim /usr/local/nagios/etc/nrpe.cfg

command[check_mysql_myisam_lock]=/usr/bin/sudo /usr/local/nagios/libexe/check_mysql_myisam_lock.sh -w 1 -c 1 # 修正命令路径 参数

主监控端 commend里面的commond-line命令要正确.

本原创文章未经允许不得转载 | 当前页面:学海无涯 » centos安装nagios

评论