banbanbai 发表于 2019-1-12 11:27:38

Nagios监控安装之二

监控80端口 3306端口 passwd文件变化
  实验说明 :环境就是一台主机LinuxCentOS release 6.5 (Final) 192.168.0.22 开启httpd 80端口 3306端口
  一,开始安装前准备:
1,安装nagios服务端软件
a,nagios-3.5.1.tar.gz
b,nagios-plugins-1.4.16.tar.gz
c,nrpe-2.12.tar.gz
2,配置环境变量:

# echo 'export LC_ALL=C'>>/etc/profile
# tail -1 /etc/profile
export LC_ALL=C
# source /etc/profile
  3,关闭防火墙和selinux

# /etc/init.d/iptables stop
# /etc/init.d/iptables status
iptables: Firewall is not running.
# chkconfig iptables off
# chkconfig --list iptables
iptables      0:off   1:off   2:off   3:off   4:off   5:off   6:off
#sed –i ‘s/SELINUX=enforce/SELINUX=disabled/g’ /etc/sysconfig/selinux
  4,解决系统时间同步问题

# yum -y install ntpdate
# crontab -e         
#time sync by tony at 2018-01-29
*/5 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
  二、正式安装Ngios
需要一个LAMP环境

# yum -y install gcc glibc glibc-common
# yum -y install gd gd-devel
# yum -y install mysql-server
# yum -y install httpd php php-gd
  创建Nagios服务器端需要的用户及组

# /usr/sbin/useradd nagios
# /usr/sbin/useradd apache -M -s /sbin/nologin
# /usr/sbin/groupadd nagcmd
# /usr/sbin/usermod -a -G nagcmd nagios
# /usr/sbin/usermod -a -G nagcmd apache
  检查用户创建是否成功

# id -n -G nagios
# id -n -G apache
# groups nagios
# groups apache
  启动服务查看通讯端口

# /etc/init.d/httpd start
# lsof -i:80
# /etc/init.d/mysqld start
# lsof -i:3306
  配置hosts文件解析 :

# cat /etc/hosts   
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.22   Nagios-Server
  三、安装Nagios-Server服务端

# cd /soft/nagios
# tar xf nagios-3.5.1.tar.gz
# cd nagios
# ./configure --with-command-group=nagcmd
# make all
# make install
# make install-init
# make install-commandmode
  创建NagiosWeb监控界面后,登录时会需要输入用户名及密码,这里分别为tony和hwg123

# htpasswd -bc /usr/local/nagios/etc/htpasswd.users tony hwg123
# cat /usr/local/nagios/etc/htpasswd.users
tony:fr58UrsZgK.Tc
  查看一下授权文件

# grep AuthUserFile /etc/httpd/conf.d/nagios.conf
  重载一下httpd服务

# /etc/init.d/httpd reload
  修改联系人邮箱:

# cd/usr/local/nagios/etc/objects/
# vim contacts.cfg
修改35行:
email                           2077489243@qq.com
# /etc/init.d/postfix start
# lsof -i:25
# chkconfig postfix on
# chkconfig --list postfix
  在浏览器上输入http://IP/nagios ,然后输入tony,hwg123
进入web浏览器界面配置
安装Nagios-server服务器端插件包:

# yum -y install perl-devel openssl-devel
# cd /soft/nagios/
# tar xf nagios-plugins-1.4.16.tar.gz
# cd nagios-plugins-1.4.16
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules --with-mysql
# make
# make install   
# ls /usr/local/nagios/libexec/ |wc -l
  安装NRPE插件:

# cd /soft/nagios/
# tar xf nrpe-2.12.tar.gz
# cdnrpe-2.12
# ./configure
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
# ls /usr/local/nagios/libexec/ |wc -l
# chkconfig nagios on
# chkconfig --list nagios
# echo "/etc/init.d/nagios start">>/etc/rc.local
# tail -1 /etc/rc.local
# /etc/init.d/nagios checkconfig
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# /etc/init.d/nagios checkconfig
  四、配置服务(我此处就是本机监控自己)

# vim nagios.cfg +32
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/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
添加cfg_file=/usr/local/nagios/etc/objects/hosts.cfg和cfg_file=/usr/local/nagios/etc/objects/services.cfg
注释该行cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
# cat/usr/local/nagios/etc/objects/hosts.cfg
define host{
use                     linux-server
host_name         22-Nagios-Server
alias                   22-Nagios
address             192.168.0.22
}
define hostgroup{
hostgroup_name   linux-servers
alias                      Linux Servers
members            22-Nagios-Server
}
# cat /usr/local/nagios/etc/objects/services.cfg
define service{
use                                          generic-service
host_name                              22-Nagios-Server
service_description               HTTP
check_command                     check_http
notifications_enabled            0
}
define service{
use                                       generic-service
host_name                           22-Nagios-Server
service_description            bolg_port_80
check_command                  check_tcp!80
max_check_attempts         3
normal_check_interval         2
retry_check_interval             1
check_period                        24x7
notification_interval            30
notification_period                24x7
notification_options            w,u,c,r
contact_groups                  admins
}
define service{
use                                       generic-service
host_name                           22-Nagios-Server
service_description            bolg_port_3306
check_command               check_tcp!3306
max_check_attempts          3
normal_check_interval      2
retry_check_interval         1
check_period                      24x7
notification_interval            30
notification_period            24x7
notification_options            w,u,c,r
contact_groups                  admins
}
define service{
use                                     generic-service
host_name                         22-Nagios-Server
service_description            Passwd
check_command                check_passwd
notifications_enabled         0
}   
# cat/usr/local/nagios/etc/objects/ commands.cfg
#'check_passwd 'command definintion
define command{
command_name   check_passwd
command_line      $USER1$/check_passwd -H $HOSTADDRESS$ -c $ARG1$
}
#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#/etc/init.d/nagios reload
  具体操作是这样先添加主机、再添加服务、然后添加命令;检查配置最后重载Nagios服务。
  备注:可能会出现的故障(有些是关于NRPE错误)
1,登录web界面的时候提示错误:
解决:

vim/usr/local/nagios/etc/cgi.cfg
把所有关于nagiosadmin改成你自己添加的用户名,重启服务即可。
  2,command'check_Passwd' not define
解决:

cd /usr/local/nagios/etc/objects/ commands.cfg 把它定义好就可以了。
  3,Connction refurse host
解决:

cd /usr/local/nagios/libexec 下,使用 ./check_nrpe -H 192.168.0.150 -c check_ping 或者./check_nrpe -H 127.0.0.1 -c check_ping
如果还是提示Connction refurse host;接下来查看NRPE有没有启动:netstat -lntup |grep nrpe 如果没有启动执行
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d 然后再执行./check_nrpe -H 192.168.0.150 -c check_ping
  4,NRPE command'check_disk' not define
解决:

      先ps -ef |grep nrpe
pkill nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
ps -ef |grep nrpe
然后再获取./check_nrpe -H 192.168.0.150 -c check_disk
  5,CHECK_NRPE: Error - Could not complete SSL handshake.
解决:

vi /usr/local/nagios/etc/nrpe.cfg +79
allowed_hosts=127.0.0.1,192.168.0.22
ps -ef |grep nrpe
pkill nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
ps -ef |grep nrpe
  6,NRPE: Unable to read output
解决:

./check_nrpe -H 192.168.0.150 -c check_emm如果还提示NRPE: Unable to read output然后
直接到里面/usr/local/nagios/libexec/check_memory.pl -w 6% -c 3% 如果提示Permission denied
说明 /usr/local/nagios/libexec/check_memory.pl -w 6% -c 3% 没有执行的权限
chmod+x/usr/local/nagios/libexec/check_memory.pl -w 6% -c 3%
  7,Whoops!Error: Could not read host and service status information!错误
http://i2.运维网.com/images/blog/201803/08/d6898d4febf1a5c15d5fb6ed01a4c1ff.jpg
解决:

先检查配置文件,检查nagios进程,检查httpd进程;结果是开启nagios服务后即可
# /etc/init.d/nagios start


页: [1]
查看完整版本: Nagios监控安装之二