Nagios
-------------------------------------------------Nagios----------------------------------------------
Nagios是一款开源的免费网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等;
环境:
server:ck01.yht.com 192.168.131.147
client:ck02.yht.com 192.168.131.142
具有DNS解析
NTP配置成功且同步成功
Nagios 建立
1.安装Apache
#yum install httpd -y
#rm -f /etc/httpd/conf.d/welcome.conf
2.配置Apache
#vi /etc/httpd/conf/httpd.conf
#定义86行
ServerAdmin root@yht.com
#定义95行
ServerName ck01.yht.com:80
#定义151: change
AllowOverride All
#定义164
DirectoryIndex index.html index.cgi index.php
//*于文件末尾增加如下内容
ServerTokens Prod
KeepAlive On
3.配置PHP
# yum install php php-mbstring php-pear -y
#vi /etc/php.ini
//*定义878行,时间区域
date.timezone=”Asia/Shanghai”
4.建立index.php
#vi /var/www/html/index.php
<?php phpinfo() ?>
5.启动Apache
#systemctl start httpd
#systemctl enable httpd
6.测试Apache与PHP
浏览器 http://192.168.131.147
7.删除测试页
#rm -rf /var/www/html/index.php
8.安装Nagios
#yum install nagios nagiosplugins-{ping,disk,users,procs,load,swap,ssh,http} -y →需要特定的YUM源
9. 配置Nagios
#vi /etc/httpd/conf.d/nagios.conf
//*注释22,23行
#Require all granted
#Require local
在下面追加
Require ip 127.0.0.1 192.168.131.0/24
//*注释53,54行
#Require all granted
#Require local
在下面追加
Require ip 127.0.0.1 192.168.131.0/24
//*生成Nagios管理员账号及密码(于/etc/httpd/conf.d/nagios中定义的口令文件)(于/etc/nagios/中定义管理员为nagiosadmin)
#htpasswd /etc/nagios/passwd nagiosadmin[nagiosadmin是账号]
10. 启动Nagios
#systemctl start nagios
#systemctl enable nagios
#systemctl restart httpd
#systemctl status httpd nagios
11. 修正Nagios-bug
//*如出现如下错误
qh: Failed to init socket'/var/log/nagios/rw/nagios.qh'. bind() failed: Nosuch file or directory
请创建socket所需目录
#mkdir -v /var/log/nagios/rw
#chown nagios. /var/log/nagios/rw
12. 访问Nagios WEB
浏览器->http://192.168.131.147/nagios
→输入账号nagiosadmin 密码123456
Nagios 通知设定
1.安装SMTP
#yum install postfix -y
2. 配置SMTP
#vi /etc/postfix/main.cf
//*定义75行,myhostname
myhostname = mail.yht.com
//*定义116行
inte_interfaces = all
//*定义164行,追加$mydomain
mydestination = $myhostname,localhost.$mydomain,localhost,$mydomain
//*定义264,定义自己的网络段
mynetworks = 168.168.131.0/24, 127.0.0.0/8
//*定义419行,
home_mailbox = Maildir/
//*574行,追加
smtpd_banner = $myhostname ESMTP
//*在文件最底下追加如下内容
//*设置MAIL大小为10M一封
message_size_limit = 10485760
//*设置MailBox大小为1G
mailbox_size_limit = 1073741824
//*设置SMTP认证
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = permit_mynetworks,permit_auth_destination,permit_sasl_authenticated,reject
3.启动Postfix
#systemctl start postfix
#systemctl enable postfix
4. 在DNS服务器的数据库中设定MX记录
#vi /var/named/yht.db
…………
@ IN MX 10 mail.yht.com.
…………
4.启动dns
#systemctl restart named
#systemctl enable named
5.在Nagios服务器中设定Nagios收件人
#yum install mailx -y
#vi /etc/nagios/objects/contacts.cfg
//*设定34行,nagios管理员邮件地址
email root@localhost
#systemctl restart nagios
6.在Nagios WEB中开启通知
Service->找到右侧带有红色”\”标记->点击-
>找到右侧->Enable notifications for this service->Commit
Nagios 监控设定
1.监控本地磁盘使用量
# vi /etc/nagios/objects/localhost.cfg
//*在本地主机上监控磁盘的使用量;如果使用率低于20%空间将给予警告,如果空间使用率低于10%将提出严重警告通知.可根据基线自己修改
# vi /etc/nagios/objects/localhost.cfg
//*76行
define service{
use local-service
host_name localhost
service_description Root Partition
check_command check_local_disk!20%!10%!/
}
2.使用nagios插件检测指定分区
1)显示插件所能提供的选项
# /usr/lib64/nagios/plugins/check_disk -w 80% -c 90% -p /dev/sda1
2)指定插件的命令配置文件定义插件的调用
# vi /etc/nagios/objects/commands.cfg
define command{→在文件空白处添加即可
command_name check_sda1
command_line $USER1$/check_disk -w$ARG1$ -c $ARG2$ -p $ARG3$
}
3)调用插件编辑检测内容
#vi /etc/nagios/objects/localhost.cfg
define service{→在文件空白处添加即可
use local-service
host_name localhost
service_description Boot Partition
check_command check_sda1!80%!90%!/dev/sda1
}
#systemctl restart nagios
刷新Nagios WEB页面
为Nagios 添加一个监控项目
1.# yum search nagios-plugins[为Nagios 添加一个监控项目
2.添加Nagios监控插件
# yum install nagios-pluginsntp -y
3.查看Nagios监控插件使用方法
#/usr/lib64/nagios/plugins/check_ntp_time -h
4.配置commands.cfg定义插件调用方法
#vi /etc/nagios/objects/commands.cfg
define command{→在文件空白处添加即可
command_name check_ntp_srv
command_line $USER1$/check_ntp_time -H $ARG1$ -w $ARG2$ -c $ARG3$
}
5.配置localhost.cfg调用插件检测
#vi /etc/nagios/objects/localhost.cfg
define service{→在文件空白处添加即可
use local-service
host_name localhost
service_description NTP_TIME
check_command check_ntp_srv!192.168.131.147!1!2→检测192.168.188.49,如果延迟1秒提出警告,延迟2秒提出严重警告
notifications_enabled 1→开启通知
}
6.启动Nagios
#systemctl restart nagios
7.Nagios WEB页检测
增加一个监控Linux 主机
1. 设定Nagios服务器
1)设定服务器监控配置文件所在目录
#vi /etc/nagios/nagios.cfg
//*取消52行注释
cfg_dir=/etc/nagios/servers 或 cfg_dir=/etc/nagios/conf.d←打开哪个目录就在哪个目录下修改
2)创建目录
#mkdir -v /etc/nagios/servers
3)修改相关权限
#chgrp nagios /etc/nagios/servers
#chmod 750 /etc/nagios/servers
4)配置目标监控主机(所要监控的服务器)
#vi /etc/nagios/conf.d/ck02.cfg
define host{
use linux-server
host_name ck02
alias ck02
address 192.168.136.142
}
define service{
use generic-service
host_name ck02
service_description PING
check_command check_ping!100.0,10%!300.0,40%
} ↓
//*检测ping,如果ping包平均往返超过100ms且丢包率超过10%给予警告;如果ping包平均往返超过300ms且丢包率超过40%给予严重警告;
5)启动Nagios
#systemctl restart nagios
2.配置目标服务器(在客户端192.168.131.142操作)
1)为目标服务器添加Nagios插件
#yum install nrpe nagiosplugins-{ping,disk,users,procs,load,swap,ssh}
//*NRPE(Nagios Remote Plugin Execution)插件一起安装,NRPE插件可以使用类似代理的方式在远程系统上做检查;
2)配置nrpe.cfg
#vi /etc/nagios/nrpe.cfg
//*定义81行,允许Nagios服务器访问
allowed_hosts=127.0.0.1,192.168.131.147
//*定义97行,允许由Nagios服务传递监控参数
dont_blame_nrpe=1
//*注释210-214行本地检测
#command[check_users]=/usr/lib64/nagios/plugins/check_users -w 5 -c 10
#command[check_load]=/usr/lib64/nagios/plugins/check_load -w 15,10,5 -c 30,25,20
#command[check_hda1]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -p /dev/hda1
#command[check_zombie_procs]=/usr/lib64/nagios/plugins/check_procs -w 5 -c 10 -sZ
#command[check_total_procs]=/usr/lib64/nagios/plugins/check_procs -w 150 -c 200
//*开启223-226行,由服务器传递检测参数
command[check_users]=/usr/lib64/nagios/plugins/check_users -w $ARG1$ -c $ARG2$
command[check_load]=/usr/lib64/nagios/plugins/check_load -w $ARG1$ -c $ARG2$
command[check_disk]=/usr/lib64/nagios/plugins/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
command[check_procs]=/usr/lib64/nagios/plugins/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$
//*开启nrpe
#systemctl start nrpe
#systemctl enable nrpe
3.配置Nagios服务器(在主服务器192.168.131.147操作)
1)安装nrpe插件
#yum install nagios-plugins-nrpe -y
#vi /etc/nagios/objects/commands.cfg
//*于文件最后追加
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
查看插件→如果此目录里没有要监控的对应插件就需要安装插件
#cd /usr/lib64/nagios/plugins
#ls
配置所需监控信息
#vi /etc/nagios/conf.d/ck02.cfg
define service{
use generic-service
host_name ck02
service_description ROOT
check_command check_nrpe!check_disk\!20%\!10%\!/
} ↓
//*监控远程磁盘使用量 注意此处加/
define service{
use generic-service
host_name ck02
service_description user
check_command check_nrpe!check_users\!20\!50
}
//*检测远程当前登陆账户数量
define service{
use generic-service
host_name ck02
service_description Boot
check_command check_nrpe!check_disk\!80%\!90%\!/dev/sda1
}
define service{
use generic-service
host_name ck02
service_description Total Processes
check_command check_nrpe!check_procs\!250\!400\!RSZDT
}
//*监控远程服务器进程及类型
define service{
use local-service
host_name ck02
service_description Swap Usage
check_command check_nrpe!check_swap\!20\!10
}
//*需要在ck02设备上 /etc/nagios/nrpe.cfg下添加command[check_swap]=/usr/lib64/nagios/plugins/check_swap -w $ARG1$ -c $ARG2$
define service{
use generic-service
host_name ck02
service_description Current Load
check_command check_nrpe!check_load\!5.0,4.0,3.0\!10.0,6.0,4.0
}
//*监控远程服务器当前负载
define service{
use generic-service
host_name ck03
service_description HTTP
check_command check_nrpe!check_http\!192.168.131.143\!80
notifications_enabled 0
}
//*远程监控HTTP服务
//*需要在ck02设备上 /etc/nagios/nrpe.cfg下添加command[check_http]=/usr/lib64/nagios/plugins/check_http -H $ARG1$ -p $ARG2$
5)启动Nagios
#systemctl restart nagios