Nagios

 

【服务端安装】
ntpdate time.nist.gov #该命令同步网络当前的时间
如果提示没有ntpdate命令,则安装ntp,并且配置系统自动更新时间
crontab -e
*/10 * * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn
【mail】
mailx sendmail

【加用户加组】
#!/bin/bash
useradd -m nagios
passwd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd apache
--------------------------------------------------------------------------------
【nagios】
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf

#【检查】
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

#【改通知,作练习可不改】
vi /usr/local/nagios/etc/objects/contacts.cfg

#【设登录账户密码】
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

#【若新用户配权限】
cd /usr/local/nagios/etc
sed -i 's#nagiosadmin#xxx#g' cgi.cfg
默认权限为nagiosadmin
检查权限grep xxx cgi.cfg

#【检查】
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
--------------------------------------------------------------------------------
【nagios-plugins】
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make && make install

#【查看插件数量】
ls /usr/local/nagios/libexec/ | wc -l

#【firewalld】
systemctl start firewalld
firewall-cmd --add-service=http --permanent
firewall-cmd --add-port=5666/tcp --permanent
firewall-cmd --query-port=5666/tcp --permanent
firewall-cmd --reload

systemctl enable nagios&&systemctl start nagios
systemctl enable httpd&&systemctl start httpd

#【测试】
IP/nagios
nagiosadmin
【htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin,nagiosadmin或自定义web登录账户以及密码】

--------------------------------------------------------------------------------
【NRPE】
./configure --with-nrpe-user=nagios \
--with-nrpe-group=nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--enable-command-args \
--enable-ssl
make all
make install-plugin
make install-daemon
make install-daemon-config

#【检查NRPE】
ll /usr/local/nagios/libexec/ | grep check_nrpe

--------------------------------------------------------------------------------
【监控端】
【nagios-plugins】
useradd -s /sbin/nologin nagios
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make all
make install
--------------------------------------------------------------------------------
【NRPE】
./configure -with-nrpe-user=nagios \
--with-nrpe-group=nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--enable-command-args \
--enable-ssl
make all
make install-plugin
make install-daemon
make install-daemon-config

ls /usr/local/nagios/libexec/ | wc -l
ll /usr/local/nagios/libexec/ | grep check_nrpe

#【启动NRPE】
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
or
/usr/local/nagios/bin/nrpe

#【加入启动NRPE】
vi /etc/init.d/nrpe
#!/bin/bash
# chkconfig: 2345 88 12
# description: NRPE DAEMON
NRPE=/usr/local/nagios/bin/nrpe
NRPECONF=/usr/local/nagios/etc/nrpe.cfg
case "$1" in
start)
echo -n "Starting NRPE daemon..."
$NRPE -c $NRPECONF -d
echo " done."
;;
stop)
echo -n "Stopping NRPE daemon..."
pkill -u nagios nrpe
echo " done."
;;
restart)
$0 stop
sleep
$0 start
;;
*)
echo "Usage: $0 start|stop|restart"
;;
esac
exit 0

chmod +x /etc/init.d/nrpe
service nrpe start

#【检查5666端口】
firewall-cmd --query-port=5666/tcp

#【添加服务端IP】
vi /usr/local/nagios/etc/nrpe.cfg +81
allowed_hosts=127.0.0.1,192.168.x.x(两个地址之间只有一个逗号,不能有空格)

#【重启NRPE】
pkill nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

#【检查NRPE】
ll /usr/local/nagios/libexec/ | grep check_nrpe
--------------------------------------------------------------------------------
【服务端检测监控端】
cd /usr/local/nagios/libexec/
./check_nrpe -H 监控端IP
出现版本号如:NRPE v2.15 则OK!
--------------------------------------------------------------------------------
【pnp4nagios】
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make all
make install
make install-webconf
make install-config
make install-init
make install-config
cd ./sample-config
make install-webconf

cd /usr/local/nagios/pnp4nagios/etc
mv misccommands.cfg-sample misccommands.cfg
mv rra.cfg-sample rra.cfg
mv nagios.cfg-sample nagios.cfg
cd pages/
mv web_traffic.cfg-sample web_traffic.cfg
cd check_commands/
mv check_all_local_disks.cfg-sample check_all_local_disks.cfg
mv check_nrpe.cfg-sample check_nrpe.cfg
mv check_nwstat.cfg-sample check_nwstat.cfg

/etc/init.d/npcd start&&chkconfig npcd on

vi /usr/local/nagios/etc/nagios.cfg
process_performance_data=1 #默认为0,修改为1
#并添加如下
# service performance data
#
service_perfdata_file=/usr/local/pnp4nagios/var/service-perfdata
service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$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
#
# host performance data starting with Nagios 3.0
#
host_perfdata_file=/usr/local/pnp4nagios/var/host-perfdata
host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$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

vi /usr/local/nagios/etc/objects/commands.cfg
#添加如下
define command{
command_name process-service-perfdata-file
command_line /usr/local/pnp4nagios/libexec/process_perfdata.pl --bulk=/usr/local/pnp4nagios/var/service-perfdata
}

define command{
command_name process-host-perfdata-file
command_line /usr/local/pnp4nagios/libexec/process_perfdata.pl --bulk=/usr/local/pnp4nagios/var/host-perfdata
}

vi /usr/local/nagios/etc/objects/templates.cfg
#添加如下
define host {
name host-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
register 0
}

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

vi /usr/local/nagios/etc/objects/localhost.cfg
#修改如下
define host{
use linux-server,host-pnp
host_name localhost
alias localhost
address 127.0.0.1
}

define service{
use local-service,service-pnp
host_name localhost
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}

define service{
use local-service,service-pnp
host_name localhost
service_description Root Partition
check_command check_local_disk!20%!10%!/
}

systemctl restart nagios&&systemctl restart httpd

点击图表小图标查看
绿色代表配置正常,如果不是全绿,要逐个解决错误
rm -rf /usr/local/pnp4nagios/share/install.php
再次点击图表小图标查看


--------------------------------------------------------------------------------
【添加监控机】
【若添加新监控命令】
vi /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

【添加监控端】
vi /usr/local/nagios/etc/objects/xxx.cfg
define host{
use linux-server
host_name 192.168.140.134
alias 192.168.140.134
address 192.168.140.134
}
define service{
use generic-service
host_name 192.168.140.134
service_description check_ping
check_command check_ping!100.0,20%!200.0,50%
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 192.168.140.134
service_description check_ssh
check_command check_ssh
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 192.168.140.134
service_description check_http
check_command check_http
max_check_attempts 5
normal_check_interval 1
}

#【添加监控主机xxx.cfg到nagios.cfg】
vi /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/xxx.cfg
#【若添加新监控服务到nrpe.cfg】
vi /usr/local/nagios/etc/nrpe.cfg

#【检查】
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

#【httpd错误】
touch /var/www/html/index.html
服务端systemctl restart httpd &&systemctl restart nagios
监控端systemctl restart httpd

posted @ 2019-02-18 13:31  UnlimitedBlade  阅读(217)  评论(0)    收藏  举报