Nagios监控配置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nagios监控配置相关的知识,希望对你有一定的参考价值。
1 使用Nagios插件命令监控本机
1.1 问题
本案例要求先部署一台Nagios监控主机,并安装插件软件包,通过插件命令实现如下监控目标:
监控本机Web服务
监控本机FTP服务
监控本机SSH服务
监控本机根分区使用量
监控本机登录用户数量
监控本机运行的进程数量
1.2 方案
使用1台RHEL6虚拟机,安装部署Nagios及配套插件软件包,在本机使用插件命令监控本机服务及资源状态。
在Nagios插件软件包中,包含了大量的监控脚本,常用的插件脚本及对应功能如表-1所示。
表-1 Nagios常用插件及功能
1.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:部署Nagios监控平台
1)安装Apacha、php,配置httpd默认首页为index.php
copytextpop-up
[[email protected] ~]# yum -y install httpd php gcc glibc glibc-common gd gd-devel
> libpng libjpeg zlib
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.php index.html index.html.var
[[email protected] ~]# service httpd start
[[email protected] ~]# chkconfig httpd on
[[email protected] ~]# yum -y install httpd php gcc glibc glibc-common gd gd-devel > libpng libjpeg zlib [[email protected] ~]# vim /etc/httpd/conf/httpd.conf DirectoryIndex index.php index.html index.html.var [[email protected] ~]# service httpd start [[email protected] ~]# chkconfig httpd on
2)安装Nagios Core
安装Nagios前需要做些准备工作,提前创建一个无法登录系统的普通账户,创建Nagios组账户并将apache用户加入到Nagios组中,默认httpd配置文件中定义的User(工作账户)为apache。
copytextpop-up
[[email protected] ~]# groupadd nagcmd //创建组
[[email protected] ~]# useradd -s /sbin/nologin -g nagcmd nagios //创建用户
[[email protected] ~]# usermod -G nagcmd apache //将apache用户加入nagcmd组
[[email protected] ~]# tar -zxvf nagios-3.2.1.tar.gz -C /usr/src
[[email protected] ~]# cd /usr/src/nagios-3.2.1/
[[email protected] ~]# ./configure --with-command-group=nagcmd
[[email protected] ~]# make all
[[email protected] ~]# make install
[[email protected] ~]# make install-init
[[email protected] ~]# make install-config
[[email protected] ~]# make install-commandmode
[[email protected] ~]# make install-webconf
[[email protected] ~]# groupadd nagcmd //创建组 [[email protected] ~]# useradd -s /sbin/nologin -g nagcmd nagios //创建用户 [[email protected] ~]# usermod -G nagcmd apache //将apache用户加入nagcmd组 [[email protected] ~]# tar -zxvf nagios-3.2.1.tar.gz -C /usr/src [[email protected] ~]# cd /usr/src/nagios-3.2.1/ [[email protected] ~]# ./configure --with-command-group=nagcmd [[email protected] ~]# make all [[email protected] ~]# make install [[email protected] ~]# make install-init [[email protected] ~]# make install-config [[email protected] ~]# make install-commandmode [[email protected] ~]# make install-webconf
3)安装Nagios插件
前面安装的Nagios Core主要提供的是监控系统的核心功能,包含核心检测引擎和一个基本的Web页面,真正执行监控的程序和指令还需要通过安装Nagios插件来获得。
copytextpop-up
[[email protected] ~]# tar -zxvf nagios-plugins-1.4.14.tar.gz
[[email protected] ~]# cd nagios-plugins-1.4.14
[[email protected]svr5 nagios-plugins-1.4.14]#./configure --with-nagios-user=nagios \
> --with-nagios-group=nagcmd
[[email protected]svr5 nagios-plugins-1.4.14]# make && make install
[[email protected]svr5 libexec]# ls /usr/local/nagios/libexec/ //查看监控脚本
check_apt check_ftp check_mrtgtraf check_ping check_time
check_breeze check_http check_mysql check_pop check_udp
check_by_ssh check_icmp check_mysql_query check_procs check_ups
check_clamd check_ide_smart check_nagios check_real check_uptime
check_cluster check_ifoperstatus check_nntp check_rpc check_users
check_dhcp check_ifstatus check_nntps check_sensors check_wave
check_dig check_imap check_nt check_simap negate
check_disk check_ircd check_ntp check_smtp urlize
check_disk_smb check_jabber check_ntp_peer check_spop utils.pm
check_dns check_load check_ntp_time check_ssh utils.sh
check_dummy check_log check_nwstat check_ssmtp
check_file_age check_mailq check_oracle check_swap
check_flexlm check_mrtg check_overcr check_tcp
[[email protected] ~]# tar -zxvf nagios-plugins-1.4.14.tar.gz [[email protected] ~]# cd nagios-plugins-1.4.14 [[email protected] nagios-plugins-1.4.14]#./configure --with-nagios-user=nagios > --with-nagios-group=nagcmd [[email protected] nagios-plugins-1.4.14]# make && make install [[email protected] libexec]# ls /usr/local/nagios/libexec/ //查看监控脚本 check_apt check_ftp check_mrtgtraf check_ping check_time check_breeze check_http check_mysql check_pop check_udp check_by_ssh check_icmp check_mysql_query check_procs check_ups check_clamd check_ide_smart check_nagios check_real check_uptime check_cluster check_ifoperstatus check_nntp check_rpc check_users check_dhcp check_ifstatus check_nntps check_sensors check_wave check_dig check_imap check_nt check_simap negate check_disk check_ircd check_ntp check_smtp urlize check_disk_smb check_jabber check_ntp_peer check_spop utils.pm check_dns check_load check_ntp_time check_ssh utils.sh check_dummy check_log check_nwstat check_ssmtp check_file_age check_mailq check_oracle check_swap check_flexlm check_mrtg check_overcr check_tcp
步骤二:使用监控脚本监控资源
1)监控Web服务器资源
check_http脚本的作用主要作用是检查Web服务,-I指定需要监控的服务器域名或IP地址,-w设置服务器响应时间的警报值(服务器响应时间超过该值则警告,默认单位为秒),-c设置服务器响应时间的严重故障警报值(服务器响应时间超过该值则提示严重错误,默认单位为秒),更多参数可以使用check_httpd -h来获得。
copytextpop-up
[[email protected] ~]# /usr/local/nagios/libexec
[[email protected] ~]# ./check_http -w 5 -c 10 –I 127.0.0.1
[[email protected] ~]# /usr/local/nagios/libexec [[email protected] ~]# ./check_http -w 5 -c 10 –I 127.0.0.1
2)监控FTP服务
check_ftp脚本的作用主要作用是检查FTP服务,-H指定需要监控的服务器域名或IP地址,-w设置服务器响应时间的阈值(服务器响应时间超过该值则警告,默认单位为秒),-c设置服务器响应时间的严重错误的阈值(服务器响应时间超过该值则提示严重错误,默认单位为秒),更多参数可以使用check_ftp -h来获得。
copytextpop-up
[[email protected] ~]# /usr/local/nagios/libexec
[[email protected] ~]# ./check_ftp -w 5 -c 10 -H 127.0.0.1
[[email protected] ~]# /usr/local/nagios/libexec [[email protected] ~]# ./check_ftp -w 5 -c 10 -H 127.0.0.1
3)监控SSH服务
check_ssh脚本的作用主要作用是检查SSH服务,-H指定监控的主机名称或IP地址,-p指定SSH服务的端口号信息。
copytextpop-up
[[email protected] ~]# /usr/local/nagios/libexec
[[email protected] ~]# ./check_ssh -H 127.0.0.1 –p 22
[[email protected] ~]# /usr/local/nagios/libexec [[email protected] ~]# ./check_ssh -H 127.0.0.1 –p 22
4)监控根分区使用情况
check_disk脚本的作用主要作用是检查磁盘使用情况,-u设置磁盘存储空间的单位(可以使用的单位有:kB、MB、GB、TB,默认单位为MB),-w设定存储剩余空间的警告提示的报警值,-c设定磁盘剩余空间严重错误的报警值,-p指定监控的分区挂载点。-w和-c除了可以监控磁盘的剩余空间外,还可以监控剩余磁盘的百分比如,-w 50%,-c 40%。
copytextpop-up
[[email protected] ~]# /usr/local/nagios/libexec
[[email protected] ~]#./check_disk -u MB -w 9000 -c 5000 -p /
[[email protected] ~]# /usr/local/nagios/libexec [[email protected] ~]#./check_disk -u MB -w 9000 -c 5000 -p /
5)监控登录用户数量
check_users脚本的作用主要作用是检查当前系统登录账户的情况,-w设置警告的阈值,-c设置严重错误的阈值。
copytextpop-up
[[email protected] ~]# /usr/local/nagios/libexec
[[email protected] ~]# ./check_users -w 5 -c 8
[[email protected] ~]# /usr/local/nagios/libexec [[email protected] ~]# ./check_users -w 5 -c 8
6)监控本机运行的进程数量
check_procs脚本的作用是监控本地进程,-w设置进程数量警告的阈值,-c设置严重错误的阈值。该脚本时可以使用--metric设置监控类型,默认监控进程数量,--metric=CPU则监控进程占用的CPU百分比,任何一个进程占用CPU百分比超过-w设置的值将出现警告信息。
copytextpop-up
[[email protected] ~]# /usr/local/nagios/libexec
[[email protected] ~]# ./check_procs -w 100 -c 120 //监控进程数量
[[email protected] ~]# check_procs -w 10 -c 20 --metric=CPU //监控CPU占用
[[email protected] ~]# /usr/local/nagios/libexec [[email protected] ~]# ./check_procs -w 100 -c 120 //监控进程数量 [[email protected] ~]# check_procs -w 10 -c 20 --metric=CPU //监控CPU占用
2 搭建Nagios监控服务器
2.1 问题
沿用练习一,通过调整Nagios服务端配置,实现以下目标:
Web服务
ssh服务
登录的用户数
2.2 方案
默认安装完Nagios Core和nagios-plugins之后,我们就可以直接使用脚本命令来监控本机资源,如果还希望直接使用Nagios监控平台实现动态监控本机资源的话,还需要修改Nagios的配置文件,重点配置文件如下:
监控命令配置文件(commands.cfg)
监控对象配置文件(localhost.cfg)
Nagios主配置文件(nagios.cfg)
配置页面登录配置文件(nagios.conf)
2.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:修改配置文件
1)修改监控命令配置文件
在该文件中使用define command指令定义监控命令,command_name定义监控命令的名称,command_line定义监控的功能。定义完成后就可以到特定的监控对象配置文件中调用该监控命令,实现监控的目的。
copytextpop-up
[[email protected] ~]# vim /usr/local/nagios/etc/objects/commands.cfg
define command {
command_name check_local_users
command_line $USER1$/check_users -w $ARG1$ -c $ARG2$
}
define command {
command_name check_http
command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
}
define command {
command_name check_ssh
command_line $USER1$/check_ssh -H $HOSTADDRESS$
}
[[email protected] ~]# vim /usr/local/nagios/etc/objects/commands.cfg define command { command_name check_local_users command_line $USER1$/check_users -w $ARG1$ -c $ARG2$ } define command { command_name check_http command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$ } define command { command_name check_ssh command_line $USER1$/check_ssh -H $HOSTADDRESS$ }
2)修改监控对象的配置文件(多个监控对象可以创建多个文件)
该文件中使用define host定义监控对象,包括被监控主机的主机名、IP地址、别名等。使用define service定义需要监控的服务,host name定义哪个主机实施监控,check_command定义调用哪个监控命令实现监控。
copytextpop-up
[[email protected] ~]# vim /usr/local/nagios/etc/objects/localhost.cfg
define host {
use linux-server
host_name localhost
alias localhost
address 127.0.0.1
}
define service{
use local-service
host_name localhost
service_description Current Users //监控登录用户数量
check_command check_local_users!20!50 //20为警告阈值,50为严重阈值
}
define service{
use local-service
host_name localhost
service_description http server //监控http服务
check_command check_http
}
define service{
use local-service
host_name localhost
service_description ssh server //监控ssh服务
check_command check_ssh
}
[[email protected] ~]# vim /usr/local/nagios/etc/objects/localhost.cfg define host { use linux-server host_name localhost alias localhost address 127.0.0.1 } define service{ use local-service host_name localhost service_description Current Users //监控登录用户数量 check_command check_local_users!20!50 //20为警告阈值,50为严重阈值 } define service{ use local-service host_name localhost service_description http server //监控http服务 check_command check_http } define service{ use local-service host_name localhost service_description ssh server //监控ssh服务 check_command check_ssh }
3)修改Nagios主配置文件nagios.cfg
通过该主配置文件,加载上面定义的配置文件内容。
copytextpop-up
[[email protected] ~]# vim /usr/local/nagios/etc/nagios.cfg
.. ..
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
.. ..
[[email protected] ~]# vim /usr/local/nagios/etc/nagios.cfg .. .. cfg_file=/usr/local/nagios/etc/objects/commands.cfg cfg_file=/usr/local/nagios/etc/objects/contacts.cfg cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg cfg_file=/usr/local/nagios/etc/objects/templates.cfg cfg_file=/usr/local/nagios/etc/objects/localhost.cfg .. ..
4)修改web页面认证配置文件
copytextpop-up
[[email protected] ~]# vim /etc/httpd/conf.d/nagios.conf
<Directory "/usr/local/nagios/sbin">
……
……
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
</Directory>
[[email protected] ~]# ]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password:
Re-type new password:
Adding password for user nagadmin
[[email protected] ~]# vim /etc/httpd/conf.d/nagios.conf <Directory "/usr/local/nagios/sbin"> …… …… AuthName "Nagios Access" AuthType Basic AuthUserFile /usr/local/nagios/etc/htpasswd.users Require valid-user </Directory> [[email protected] ~]# ]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin New password: Re-type new password: Adding password for user nagadmin
步骤二:启动服务,查看监控页面
1)启动Nagios服务
copytextpop-up
[[email protected] ~]# /etc/init.d/nagios start
Starting nagios:This account is currently not available.
done.
[[email protected] ~]# /etc/init.d/nagios start Starting nagios:This account is currently not available. done.
2)启动httpd服务:
copytextpop-up
[[email protected] ~]# service httpd restart
启动 httpd: [确定]
[[email protected] ~]# service httpd restart 启动 httpd: [确定]
3)登录监控Web页面,使用浏览器访问http://监控服务器IP地址/nagios
3 使用Nagios监控远端主机
3.1 问题
沿用练习二,通过调整Naiogs配置,实现以下目标:
监控服务器IP地址为192168.170.92
被监控主机ip地址是192.168.170.49
监控被监控主机上运行的vsftpd、httpd服务
监控被监控主机上运行的进程数、CPU负载、登录的用户数、根分区的使用量
3.2 方案
使用Nagios监控其他主机的资源需要使用nrpe扩展,监控拓扑结构如图-1所示。
图-1
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:配置被监控端主机
1)安装nagios-plugins
copytextpop-up
[[email protected] ~]# useradd nagios
[[email protected] ~]# groupadd nagcmd
[[email protected] ~]# usermod -G nagcmd nagios
[[email protected] ~]# tar -zxvf nagios-plugins-1.4.14.tar.gz
[[email protected] ~]# cd nagios-plugins-1.4.14
[[email protected]localhost nagios-plugins]# ./configure
[[email protected]localhost nagios-plugins]# make && make install
[[email protected] ~]# useradd nagios [[email protected] ~]# groupadd nagcmd [[email protected] ~]# usermod -G nagcmd nagios [[email protected] ~]# tar -zxvf nagios-plugins-1.4.14.tar.gz [[email protected] ~]# cd nagios-plugins-1.4.14 [ro[email protected] nagios-plugins]# ./configure [[email protected] nagios-plugins]# make && make install
2)安装nrpe扩展
Nagios监控远程主机资源是需要调用nrpe扩展,因此监控主机和被监控主机均需要安装nrpe。
copytextpop-up
[[email protected] ~]# tar -zxvf nrpe-2.12.tar.gz
[[email protected] ~]# cd nrpe-2.12
[[email protected]localhost nrpe-2.12]# ./configure