测试环境 主监控机:CentOS 7.5 192.168.131.10 被监控机:CentOS 6.2 192.168.131.20 安装包:nagios-3.5.1.tar.gz nrpe-2.15.tar.gz nagios-plugins-2.1.1.tar.gz 主机:192.168.131.10配置 1.安装依赖包 yum install -y gcc glibc glibc-common php gd gd-devel libpng libmng libjpeg zlib systemctl start httpd 2.添加nagios用户 useradd nagios groupadd nagcmd usermod -G nagcmd apache 3.二进制包安装nagios mkdir /opt/nagios tar zxvf nagios-3.5.1.tar.gz -C /opt/nagios cd /opt/nagios ./configure --with-command-group=nagcmd make all make install && make install-init && make install-commandmode && make install-config && make install-webconf
//make install #安装生成/usr/local/nagios/,其中/usr/local/nagios/share即nagiosWEB 访问界面的站点目录 //make install-init #安装生成/etc/rc.d/init.d/nagios 启动脚本 //make install-config #安装生成/usr/local/nagios/etc下的nagios相关配置文件 //make install-commandmode #设定相应nagios工作目录的权限 //make install-webconf #安装Nagios的WEB配置文件到Apache的conf.d目录下 认识一下nagios的配置文件 ** vim /usr/local/nagios/etc/nagios.cfg** logfile=/var/log/nagios/nagios.log #日志位置 cfgfile=/etc/nagios/objects/commands.cfg #这个文件定义了很多命令 cfgfile=/etc/nagios/objects/contacts.cfg #定义联系人,怎么联系 cfgfile=/etc/nagios/objects/timeperiods.cfg #定义了时间段 cfgfile=/etc/nagios/objects/templates.cfg #模板(联系人,主机,时间) cfgfile=/etc/nagios/objects/localhost.cfg #监控本机相关配置文件 #cfgfile=/etc/nagios/objects/windows.cfg #windows,默认不监控 #cfgfile=/etc/nagios/objects/switch.cfg #交换机路由器监控,默认不监控 #cfgfile=/etc/nagios/objects/printer.cfg #打印机监控,默认不监控 #cfgdir=/etc/nagios/servers #定义了服务合集(多个使用) #cfgdir=/etc/nagios/printers #定义了打印机合集(多个使用) #cfgdir=/etc/nagios/switches #定义了交换合集(多个使用) #cfgdir=/etc/nagios/routers #定义了路由合集(多个使用) resourcefile=/etc/nagios/private/resource.cfg 资源变量配置文件,包括$USER1$变量(一个路径)等 statusupdateinterval=10 #状态更新时间,单位s logrotationmethod=d #日志滚动,默认天 servicechecktimeout=60 #服务检查超时时间 hostchecktimeout=30 #主机检查超时时间 eventhandlertimeout=30 notificationtimeout=30 ocsptimeout=5 perfdatatimeout=5*
vim /usr/local/nagios/etc/cgi.cfg //此配置文件比nagios.cfg优先级高 main_config_file= /usr/local/nagios/etc/nagios.cfg #主配置文件 physical_html_path= /usr/local/nagios/share #物理路径 url_html_path=/nagios #在URL后面加上/nagios才能访问 use_authentication=1 #使用认证 use_ssl_authentication=0 #不使用ssl authorized_for_system_information=nagiosadmin #认证用户
Nagios访问测试 systemctl restart httpd 访问地址 http://192.168.131.10/nagios 提示需要账户和密码 vi /etc/httpd/conf.d/nagios.conf
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin systemctl restart httpd 通过-v参数检测配置文件是否有问题 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 两者参数都为0,表示没有错误,以守护进程方式启动 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg 安装nagios-plugins插件 为了能够监控mysql,我们要在主监控机上安装mysql客户端 yum install mysql mysql-devel tar xvf nagios-plugins-2.1.1.tar.gz -C /opt/nagios cd /opt/nagios/nagios-plugins-2.1.1 ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd make && make install 重启nagios systemctl restart httpd; /etc/init.d/nagios restart 浏览器测试:登陆之后,点击hosts,可以看到,默认监控的是本机,说明环境 nagios监控本机服务器 监控本地nfs状态 yum -y install nfs-utils cd /usr/local/nagios/etc/objects vi localhost.cfg
*define service{ use local-service hostname localhost servicedescription NFS checkcommand checktcp!2049 }
#在使用check命令之前,要确保下/usr/local/nagios/libexec/ 目录下有没有你需要的check命令,如果有,直接调用,如果没有,检测端口来代替,检测格式: checktcp!端口号*
重启nagios /etc/init.d/nagios restart
修改主题的话 把相应主题包解压到 /usr/local/nagios/share/目录下,重启nagios就可以生效 unzip vautour_style.zip -d /usr/local/nagios/share/ ** nagios监控远程mysql** 在主监控机192.168.131.10上配置nagios服务 vi /usr/local/nagios/etc/nagios.cfg
cfgfile=/usr/local/nagios/etc/objects/localhost.cfg #在这两行下面添加下面两行 cfgfile=/usr/local/nagios/etc/objects/hosts.cfg cfgfile=/usr/local/nagios/etc/objects/service.cfg
vi /usr/local/nagios/etc/objects/hosts.cfg
*define host{ use linux-server #使用的模板名字 hostname sep-mysql #主机名 address 192.168.131.20 #IP地址 } *
vi /usr/local/nagios/etc/objects/service.cfg
*define service{ use local-service hostname sep-mysql servicedescription MySqlSev checkcommand checkmysql } *
vi /usr/local/nagios/etc/objects/commands.cfg * define command{ commandname checkmysql commandline $USER1$/checkmysql -H $HOSTADDRESS$ -u nagios –p123456 -d nagios }* 本机通过该命令测试 /usr/local/nagios/libexec/check_mysql -H192.168.131.20 -unagios -p123456 -d nagios
** 通过nrpe监控外部主机私有信息** 客户端的NRPE安装 1:安装nagios- plugins插件 2:安装NRPE( make install-daemon-config make install-xinetd) 3:配置我们xinted服务,允许谁来拿数据 4:启动xinetd 5:查看端口是否正常启动 解决依赖 监控机和被监控机都安装 yum install -y openssl openssl-devel
服务端安装NRPE tar xvf nrpe-2.15.tar.gz -C /opt/nagios cd /opt/nagios/nrpe-2.15/ ./configure && make && make install make install-plugin && make install-daemon #前者安装插件,后者以守护经常来运行 ls /usr/local/nagios/libexec/check_nrpe #这个命令需要安装nrpe之后才有 客户端配置 useradd -s /sbin/nologin nagios groupadd nagcmd usermod -G nagcmd nagios yum install -y xinetd tar xvf nagios-plugins-2.1.1.tar.gz -C /opt/nagios tar xvf nrpe-2.15.tar.gz -C /opt/nagios cd /opt/nagios/nagios-plugins-2.1.1/ ./configure && make && make install cd ../nrpe-2.15/ ./configure && make && make install make install-daemon-config //这里是客户端,不用make-install-plugin make install-xinetd 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.131.10 } echo "nrpe 5666/tcp # NRPE" >> /etc/services //端口注册 netstat -antup | grep 5666 tcp 0 0:::5666 :::* LISTEN 68667/xinetd 客户端nrpe命令 vim /usr/local/nagios/etc/nrpe.cfg command[check_sdc1]=/usr/local/nagios/libexec/check_disk -w 38% -c 35% -p /dev/sdc1 #-w为警告 -C为告急 systemctl restart xinetd
服务端手动测试
/usr/local/nagios/libexec/check_nrpe -H192.168.131.20 NRPE v2.15 [root@xuegod63 ~]# /usr/local/nagios/libexec/check_nrpe -H192.168.131.20 -c check_sdc1
定义被监控主机 cd /usr/local/nagios/etc/objects/ vim hosts.cfg define host{ use linux-server host_name sep-mysql address 192.168.131.20 }
定义需要监控的服务
[root@xuegod63 objects]# vim service.cfg
define service{
use local-service
host_name sep-mysql
service_description sdac_Partition
check_command check_nrpe!check_sdc1
}
定义NRPE监控命令
vim commands.cfg define command{ command_name check_nrpe command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ } $ARG1$ 表示调用后面的参数。
通过http://www.onealert.com/设置报警 点击配置有相应的操作方式 这边配置微信绑定和设置QQ邮箱 就可以收到报警通知