监控本机
配置步骤
- 1、定义监控命令->commands.cfg
- 2、定义监控对象->localhost.cfg
- 3、加载监控对象配置文件->nagios.cfg
- 4、配置登录页面认证用户->nagios.conf
- 5、启动nagios服务
- 6、启动httpd服务
- 7、登录监控页面
定义监控命令
- 修改commands.cfg
~]# vim /usr/local/nagios/etc/objects/commands.cfg
define command {
command_name //定义监控命令的名字(自定义)
command_line //定义命令具体实现的功能
}
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$
}
定义监控对象
- 修改localhost.cfg
~]# vim /usr/local/nagios/etc/objects/localhost.cfg
//指定被监控主机信息
define host{
use //监控使用的模板
host_name //被监控主机的主机名
alias //被监控主机别名
address //被监控主机IP地址
}
//定义监控对象
define service{
use //监控使用的模板
host_name //被监控主机的主机名
service_description //描述信息
check_command //使用的监控命令
}
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 #!号参数间隔符号
}
加载对象配置文件
- 修改nagios.cfg
~]# 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
//检查配置文件是否有语法错误
~]# cd /usr/local/nagios/bin/
bin]# ./nagios -v /usr/local/nagios/etc/nagios.cfg
...
Reading configuration data...
Read main config file okay...
...
Total Warnings: 0
Total Errors: 0
配置登录页面
- 修改httpd服务的nagios.conf
~]# vim /etc/httpd/conf.d/nagios.conf
...
<Directory "/usr/local/nagios/share">
...
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
</Directory>
~]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin
~]# cat /usr/local/nagios/etc/htpasswd.users
nagiosadmin:Aey1/M3bCP3fs
重启服务并测试
- 启动nagios服务
~]# /etc/init.d/nagios start
Starting nagios:This account is currently not available.
done.
- 启动httpd服务
~]# service httpd restart
停止 httpd: [确定]
启动 httpd: [确定]
- 登录监控页面 http://nagios服务器IP/nagios
监控远程服务器
NRPE工作原理
监控案例拓扑结构
- 案例分析:
- Nagios监控服务器的IP地址是192.168.170.92
- 监控IP地址是192.168.170.149服务器的状态
- 监控IP地址是192.168.170.149服务器上运行的NFS、vsftpd、httpd、postfix 服务
- 监控IP地址是192.168.170.149服务器上运行的进程数、CPU核数、登录的用户数、根分区的使用量、交换分区的使用量
监控远端主机公有数据
- Nagios服务器端配置步骤:
- 1、创建监控对象配置文件->xxx.cfg
- 2、加载监控对象配置文件->nagios.cfg
- 3、重启nagios服务
- 4、登录监控页面
- 远端主机配置:
- 能正常与监控服务器通信
- 1、创建监控对象配置文件->xxx.cfg
~]# cd /usr/local/nagios/etc/objects/
objects]# cp localhost.cfg 149-server.cfg #此文件名可自定义有标识性即可
objects]# vim 149-server.cfg
define host{
use linux-server
host_name 149server
alias mail.tarena.com
address 192.168.170.149
}
define service{
use local-service
host_name 149server
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service
host_name 149server
service_description SSH
check_command check_ssh
notifications_enabled 0
}
define service{
use local-service
host_name 149server
service_description Current Users
check_command check_local_users!2!3
}
- 2、加载监控对象配置文件->nagios.cfg
objects]# 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
cfg_file=/usr/local/nagios/etc/objects/149-server.cfg
- 3、重启nagios服务
objects]# cd /usr/local/nagios/bin/
bin]# ./nagios -v /usr/local/nagios/etc/nagios.cfg
bin]# /etc/init.d/nagios restart
bin]# ping -c 4 10.128.0.28
- 4、登录监控页面 http://192.168.170.92/nagios
监控远端主机私有数据
- 配置步骤:
- 被监控服务器配置
- 1、安装监控插件
- 2、安装nrpe
- 3、定义监控命令
- 4、启动nrpe服务
- Nagios服务器配置
- 1、安装nrpe插件
- 2、定义nrpe监控命令
- 3、调用nrpe命令监控私有数据
- 4、重启nagios服务
- 被监控服务器配置
被监控端配置
- 1、安装监控插件
~]# useradd nagios
~]# groupadd nagcmd
~]# usermod -G nagcmd nagios
~]# tar -zxvf nagios-plugins-1.4.14.tar.gz
~]# cd nagios-plugins-1.4.14
nagios-plugins]# ./configure
nagios-plugins]# make && make install
~]# cd /usr/local/nagios/ //安装目录
~]# cd /usr/local/nagios/libexec/ //监控插件目录
libexec]# ls
check_apt check_disk_smb check_ide_smart check_mrtg check_nwstat check_sensors check_users ...
- 2、安装nrpe
~]# tar -zxvf nrpe-2.12.tar.gz
~]# cd nrpe-2.12
nrpe-2.12]# ./configure
nrpe-2.12]# make && make install
nrpe-2.12]# make install-plugin
nrpe-2.12]# make install-daemon
nrpe-2.12]# make install-daemon-config
nrpe-2.12]# make install-xinetd
- 3、定义监控命令
~]# vim /usr/local/nagios/etc/nrpe.cfg
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
- 4、启动nrpe服务
~]# vim /etc/xinetd.d/nrpe
service nrpe
{
flags = REUSE
socket_type = stream
port = 5666
wait = no
user = nagios
group = nagios
server = /usr/local/nagios/bin/nrpe
server_args = -c /usr/local/nagios/etc/nrpe.cfg --inetd
log_on_failure += USERID
disable = no
only_from = 127.0.0.1 192.168.170.92 #Nagios服务器IP地址
}
~]# vim /etc/services //指定nrpe对应端口
nrpe 5666/tcp # TCP port service nrpe
~]# /etc/init.d/xinetd restart
停止 xinetd: [确定]
启动 xinetd: [确定]
~]# netstat -utnalp | grep :5666
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 29488/xinetd
- 5、测试nrpe
~]# cd /usr/local/nagios/libexec/
libexec]# ./check_nrpe -H localhost
NRPE v2.12
配置Nagios服务器
- 1、安装nrpe
~]# tar -zxvf nrpe-2.12.tar.gz
~]# cd nrpe-2.12
nrpe-2.12]# ./configure
nrpe-2.12]# make
nrpe-2.12]# make install
nrpe-2.12]# make install-plugin
nrpe-2.12]# cd /usr/local/nagios/libexec/
libexec]# ./check_nrpe -H 192.168.170.149
NRPE v2.12
- 2、定义nrpe监控命令
~]# cd /usr/local/nagios/etc/objects
objects]# vim commands.cfg
define command {
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
- 3、调用nrpe命令监控私有数据
~]# cd /usr/local/nagios/etc/objects/
objects]# vim 149-server.cfg
define service {
use local-service
host_name 149server
service_description 149-Users
check_command check_nrpe!check_users //被监控端定义的nrpe命令
}
- 4、重启nagios服务
objects]# cd /usr/local/nagios/bin/
bin]# ./nagios -v ../etc/nagios.cfg
bin]# /etc/init.d/nagios restart
- 5、登录监控页面 http://192.168.170.92/nagios
网友评论