CentOS 7下Nagios的安装及配置

简介

Nagios 是一款自动化运维工具,可以协助运维人员监控服务器的运行状况,并且拥有报警功能。本文章将介绍其安装方法和详细的配置方法。

nagios 监控服务应用指南
本地资源:负载,CPU,磁盘,内存。IO,RAID,温度,passwd文件变化,本地所有文件指纹识别
网络服务:端口,URL,丢包,进程,网络流量
其他设备:交换机,打印机,windows等。
业务数据:用户登录失败次数,用户登录网站次数,输入验证码失败的次数,某个API接口流量并发,电商网站订单,支付交易的数量

nagios成员:主程序nagios,插件nagios-plugins,和一些可选的客户端nrpe,NSClient++,NSCA和NDOUtils

  • NRPE--半被动模式
    存放位置:安装在客户端
    NRPE作用:在客户端执行相关的脚本插件来获取数据,实现对客户端主机资源的监控。
    存在形式:守护进程(agent)模式,开启的端口5666

  • NSClient++:半被动模式,相当于linux系统的nrpe

  • NDOUtils(不推荐用)
    存在位置:服务端
    作用:用于将nagios的配置信息和各event产生的数据存入数据库中,以实现对这些数据的检索和处理。

  • NSCA:纯被动模式--->用在分布式监控环境中
    位置:NSCA需要同时安装在nagios的服务器端和客户端

软件版本

nagios版本:4.3.1
nagios-plugins版本:2.2.1
nrpe版本:3.1.0

实现环境

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
<Directory />
    AllowOverride none
    Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule mime_module>
    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
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
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页面是否可以打开

CentOS 7下Nagios的安装及配置

 

CentOS 7下Nagios的安装及配置

 

CentOS 7下Nagios的安装及配置

 



安装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,并测试服务端本地是否可以连通

相关推荐