Nagios是一款自动化运维工具,可以协助运维人员监控服务器的运行状况,并且拥有报警功能。本文章将介绍其安装方法和详细的配置方法。
nagios监控服务应用指南
本地资源:负载,CPU,磁盘,内存.IO,RAID,温度,passwd文件变化,本地所有文件指纹识别
网络服务:端口,URL,丢包,进程,网络流量
其他设备:交换机,打印机,窗户等。
业务数据:用户登录失败次数,用户登录网站次数,输入验证码失败的次数,某个API接口流量并发,电商网站订单,支付交易的数量
nagios的成员:主程序的nagios,插件的nagios-插件,和一些可选的客户端NRPE,NSClient ++,NSCA和NDOUtils
- NRPE--被动half-模式
存放位置:安装在客户端
NRPE作用:在客户端执行相关的脚本插件来获取数据,实现对客户端主机资源的监控。
存在形式:守护进程(代理)模式,开启的端口5666
- NSClient ++:半被动模式,相当于Linux的系统的NRPE
- NDOUtils(不推荐用)
存在位置:端的服务
作用英文:用于将Nagios的的配置信息和各事件产生的数据存入数据库中,以实现对这些数据的检索和处理。
- NSCA:纯被动模式--->用在分布式监控环境中
位置:NSCA需要同时安装在nagios的服务器端和客户端
软件版本
nagios版本:4.3.1
nagios-plugins版本:2.2.1
nrpe版本:3.1.0
实现环境
nagios运行在LAMP环境下(如果还没有安装LAMP环境,可以参考下我的安装LNMP文章,因为我已经编译安装了php和Mysql,本文直接采取yum 方式安装apache,当然也可以采取编译方式安装apache。看个人喜好和公司需求)
Centos版本:CentOS Linux release 7.3.1611 (Core) 64位
Apache版本:Apache/2.4.6
Nginx版本: nginx 1.12.0
Mysql版本:mysql 5.7.18
PHP版本:php 7.1.4
下载nagios软件及插件包
下载慢的话可以去我的网盘下载
[root@nagios ~]# cd /software/
[root@nagios software]# wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.1.tar.gz
[root@nagios software]# wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
[root@nagios software]# wget https://sourceforge.net/projects/nagios/files/nrpe-3.x/nrpe-3.1.0.tar.gz
[root@nagios software]# ll
-rw-r--r-- 1 root root 11095797 4月 21 15:57 nagios-4.3.1.tar.gz
-rw-r--r-- 1 root root 2728818 4月 20 00:04 nagios-plugins-2.2.1.tar.gz
-rw-r--r-- 1 root root 501028 4月 17 22:36 nrpe-3.1.0.tar.gz
安装nagios-server服务端
安装依赖包
[root@nagios software]# yum -y install httpd httpd-devel gcc glibc glibc-common gd gd-devel perl-devel perl-CPAN fcgi perl-FCGI perl-FCGI-ProcManager
解压nagios源码包
[root@nagios software]# tar zxvf nagios-4.3.1.tar.gz
进入解压后的目录
[root@nagios software]# cd nagios-4.3.1/
创建ngaios用户和组(把nginx启动用户www加入到nagios相关组)
[root@nagios nagios-4.3.1]# useradd nagios -s /sbin/nologin
[root@nagios nagios-4.3.1]# id www
[root@nagios nagios-4.3.1]# groupadd nagcmd
[root@nagios nagios-4.3.1]# usermod -a -G nagcmd nagios
[root@nagios nagios-4.3.1]# usermod -a -G nagcmd www
[root@nagios nagios-4.3.1]# id -n -G nagios
[root@nagios nagios-4.3.1]# id -n -G www
配置nagios
[root@client1 nagios-4.3.1]# ./configure --with-command-group=nagcmd
编译和安装
[root@nagios nagios-4.3.1]# make all
[root@nagios nagios-4.3.1]# make install-init
[root@nagios nagios-4.3.1]# make install-commandmode
[root@nagios nagios-4.3.1]# make install-config
[root@nagios nagios-4.3.1]# make install
[root@nagios nagios-4.3.1]# cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/
[root@nagios nagios-4.3.1]# chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers
[root@nagios nagios-4.3.1]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
生成apache配置文件
[root@nagios nagios-4.3.1]# make install-webconf
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
if [ 0 -eq 1 ]; then \
ln -s /etc/httpd/conf.d/nagios.conf /etc/apache2/sites-enabled/nagios.conf; \
fi
*** Nagios/Apache conf file installed ***
生成nagios web界面的验证信息
[root@nagios nagios-4.3.1]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagios
New password: ==> 输入密码,这里我输入的密码是nagios,记住这个密码
Re-type new password: ==> 确认密码
Adding password for user nagios
修改apache配置文件
vi /etc/httpd/conf/httpd.conf
Listen 80 ===> 修改为 Listen 8080
User apache ===> 修改为 User www
Group apache ===> 修改为 Group www
DirectoryIndex index.html ===> 修改为 DirectoryIndex index.php index.html
AddType application/x-gzip .gz .tgz ===> 下面添加一行内容 AddHandler application/x-httpd-php .php
# LoadModule foo_module modules/mod_foo.so ===> 下面添加一行内容
LoadModule php7_module modules/libphp7.so
apache配置文件参考
[root@nagios httpd]# grep -v '^$' /etc/httpd/conf/httpd.conf|grep -v '#'
ServerRoot "/etc/httpd"
Listen 8080
LoadModule php7_module modules/libphp7.so
Include conf.modules.d/*.conf
User www
Group www
ServerAdmin root@localhost
AllowOverride none
Require all denied
DocumentRoot "/var/www/html"
AllowOverride None
Require all granted
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
DirectoryIndex index.php index.html
Require all denied
ErrorLog "logs/error_log"
LogLevel warn
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
CustomLog "logs/access_log" combined
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
AllowOverride None
Options None
Require all granted
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddHandler application/x-httpd-php .php
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
AddDefaultCharset UTF-8
MIMEMagicFile conf/magic
EnableSendfile on
IncludeOptional conf.d/*.conf
重新编译一下php,使其直接apache(也就是编译参数加上--with-apxs2)
[root@nagios nagios-4.3.1]# cd /software/php-7.1.4/
[root@nagios php-7.1.4]# ./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo -enable-tokenizer --enable-zip --with-apxs2
[root@nagios php-7.1.4]# make && make install
[root@nagios php-7.1.4]# cd /etc/httpd/
[root@nagios httpd]# ll /etc/httpd/modules/libphp7.so
-rwxr-xr-x 1 root root 38908880 4月 24 10:34 /etc/httpd/modules/libphp7.so ===> 可以看到这个模块已经生成
启动apache
[root@nagios httpd]# systemctl start httpd
[root@nagios httpd]# systemctl enable httpd
启动nagios
chkconfig nagios on
/etc/init.d/nagios start
在浏览器输入ip:8080/nagios测试nagios-web页面是否可以打开
安装nagios-plugins插件
解压nagios-plugins源码包
[root@nagios httpd]# cd /software/
[root@nagios software]# tar zxvf nagios-plugins-2.2.1.tar.gz
进入解压后的目录进行配置
[root@nagios software]# cd nagios-plugins-2.2.1/
[root@nagios nagios-plugins-2.2.1]# ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd --enable-perl-modules
编译及安装
[root@nagios nagios-plugins-2.2.1]# make && make install
安装nrpe
解压nrpe源码包
[root@nagios nagios-plugins-2.2.1]# cd /software/
[root@nagios software]# tar zxvf nrpe-3.1.0.tar.gz
进去解压后的目录进行配置
[root@nagios software]# cd nrpe-3.1.0/
[root@nagios nrpe-3.1.0]# ./configure
编译及安装
[root@nagios nrpe-3.1.0]# make all
[root@nagios nrpe-3.1.0]# make install-plugin
[root@nagios nrpe-3.1.0]# make install-daemon
[root@nagios nrpe-3.1.0]# make install-daemon-config
[root@nagios nrpe-3.1.0]# cp sample-config/nrpe.cfg /usr/local/nagios/etc/nrpe.cfg
安装完成后,查看下的libexec下面是否有插件
[root@nagios nrpe-3.1.0]# ls /usr/local/nagios/libexec/
check_apt check_flexlm check_log check_ntp_peer check_smtp disable_active_service_checks
check_breeze check_fping check_mailq check_ntp_time check_spop disable_notifications
check_by_ssh check_ftp check_mrtg check_nwstat check_ssh distributed-monitoring
check_clamd check_http check_mrtgtraf check_oracle check_ssmtp enable_active_service_checks
check_cluster check_icmp check_mysql check_overcr check_swap enable_notifications
check_dhcp check_ide_smart check_mysql_query check_ping check_tcp eventhandlers
check_dig check_ifoperstatus check_nagios check_pop check_time negate
check_disk check_ifstatus check_nntp check_procs check_udp redundancy-scenario1
check_disk_smb check_imap check_nntps check_real check_ups submit_check_result
check_dns check_ircd check_nrpe check_rpc check_uptime urlize
check_dummy check_jabber check_nt check_sensors check_users utils.pm
check_file_age check_load check_ntp check_simap check_wave utils.sh
启动NRPE,并测试服务端本地是否可以连通
[root@nagios nrpe-3.1.0]# /usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg
[root@nagios nrpe-3.1.0]# echo "/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg" >> /etc/rc.local
[root@nagios nrpe-3.1.0]# chmod +x /etc/rc.d/rc.local # centos 7下需要这一步, 不然/etc/rc.local中的内容开机可能不执行
[root@nagios nrpe-3.1.0]# netstat -lnput|grep 5666
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 67176/nrpe
tcp6 0 0 :::5666 :::* LISTEN 67176/nrpe
[root@nagios nrpe-3.1.0]# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v3.1.0-rc1
作者:君惜丶
链接:https://www.jianshu.com/p/3476d98a4703
來源:简书