Centos 7.6 Install nagios 4.4.3

服务端安装安装依赖

yum -y install wget httpd httpd-devel openssl openssl-devel gcc glibc glibc-common gd gd-devel perl-devel perl-CPAN fcgi perl-FCGI perl-FCGI-ProcManager unizp libxml2 libxml2-devel curl-devel

下载插件

wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.3.tar.gz
wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
wget https://sourceforge.net/projects/nagios/files/nrpe-3.x/nrpe-3.1.0.tar.gz

创建ngaios用户

/usr/sbin/useradd -m nagios
passwd nagios

创建 nagcmd 用户组,将 nagios 和 apache(web 服务器的运行用户) 用户添加至该组

/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd apache

解压nagios

tar -zxvf nagios-4.4.3.tar.gz
cd nagios-4.4.3
./configure --with-command-group=nagcmd
/usr/bin/install -c -m 775 -o nagios -g nagcmd -d /usr/local/nagios/var/rw
chmod g+s /usr/local/nagios/var/rw
make all
make install-init
make install-commandmode
make install-config
make install

cp -rf  /root/nagios-4.4.3/contrib/eventhandlers /usr/local/nagios/libexec/
chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

生成apache配置文件

make install-webconf

生成nagios web界面的验证信息

htpasswd -c /usr/local/nagios/etc/htpasswd.users nagios

修改apache配置文件

vi /etc/httpd/conf/httpd.conf 修改
#42 Listen 80
#66  User nagios
#67 Group nagcmd
#95 ServerName 192.168.31.66:80
#164 DirectoryIndex index.php index.html
#285 AddHandler application/x-httpd-php .php
#308 LoadModule php7_module        modules/libphp7.so

安装php

wget https://www.php.net/distributions/php-7.1.4.tar.gz
tar -zxvf php-7.1.4.tar.gz
cd 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
make && make install

添加php环境变量

vi /etc/profile
export PATH=$PATH:/usr/local/php7/bin

刷新环境变量

source /etc/profile

查看php版本

php -v 或者 php -version

cd /etc/httpd/
ll /etc/httpd/modules/libphp7.so

启动apache

systemctl start httpd
systemctl enable httpd

启动nagiosh

 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

或者

systemctl start nagios
systemctl enable nagios

测试页面http://ip/nagios 输入创建的用户名密码

Centos 7.6 Install nagios 4.4.3_第1张图片

安装解压nagios-plugins

tar -zxvf nagios-plugins-2.2.1.tar.gz

cd nagios-plugins-2.2.1/
./configure --with-nagios-user=nagios --with-nagios-group=nagcmd --enable-perl-modules
make && make install

安装解压nrpe

tar zxvf nrpe-3.1.0.tar.gz
cd nrpe-3.1.0/
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
cp -rf sample-config/nrpe.cfg /usr/local/nagios/etc/nrpe.cfg

启动nrpe,并测试服务端本地是否可以连通

/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg
echo "/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg" >> /etc/rc.local
chmod +x /etc/rc.d/rc.local           # centos 7下需要这一步, 不然/etc/rc.local中的内容开机可能不执行  
/usr/local/nagios/libexec/check_nrpe -H localhost

nagios客户端配置

yum install gcc gcc-c++ wget perl-devel perl-CPAN openssl openssl-devel -y

创建用户

useradd nagios -M -s /sbin/nologin

wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
tar -zxvf nagios-plugins-2.2.1.tar.gz
cd nagios-plugins-2.2.1
 ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules
make && make install

wget https://sourceforge.net/projects/nagios/files/nrpe-3.x/nrpe-3.1.0.tar.gz
tar -zxvf nrpe-3.1.0.tar.gz
cd nrpe-3.1.0/
./configure --enable-command-args
make all
make install-plugin
make install-daemon
make install-config
mkdir -p /usr/local/nagios/etc/
cp -rf sample-config/nrpe.cfg /usr/local/nagios/etc/nrpe.cfg

启动nrpe,并测试服务端本地是否可以连通

/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg
echo "/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg" >> /etc/rc.local
chmod +x /etc/rc.d/rc.local           # centos 7下需要这一步, 不然/etc/rc.local中的内容开机可能不执行
/usr/local/nagios/libexec/check_nrpe -H localhost

配置nrpe

vi /usr/local/nagios/etc/nrpe.cfg 允许服务端IP和本机访问,192.168.31.66是nagios服务端IP地址
#106 allowed_hosts=0.0.0.0,::1,192.168.31.66
注释下面#302-306
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -r -w .15,.10,.05 -c .30,.25,.20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

增加#307
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -r -w .15,.10,.05 -c .30,.25,.20
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
command[check_mem]=/usr/local/nagios/libexec/check_mem.pl -w 90% -c 95%
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%

监控内存

vi /usr/local/nagios/libexec/check_mem.pl
! /usr/bin/perl -w
#
# $Id: check_mem.pl 8 2008-08-23 08:59:52Z rhomann $
#
# check_mem v1.7 plugin for nagios
#
# uses the output of `free` to find the percentage of memory used
#
# Copyright Notice: GPL
#
# History:
# v1.8 Rouven Homann - [email protected]
# + added findbin patch from Duane Toler
# + added backward compatibility patch from Timour Ezeev
#
# v1.7 Ingo Lantschner - ingo AT boxbe DOT com
# + adapted for systems with no swap (avoiding divison through 0)
#
# v1.6 Cedric Temple - cedric DOT temple AT cedrictemple DOT info
# + add swap monitoring
#       + if warning and critical threshold are 0, exit with OK
#       + add a directive to exclude/include buffers
#
# v1.5 Rouven Homann - [email protected]
# + perfomance tweak with free -mt (just one sub process started instead of 7)
# + more code cleanup
#
# v1.4 Garrett Honeycutt - [email protected]
# + Fixed PerfData output to adhere to standards and show crit/warn values
#
# v1.3 Rouven Homann - [email protected]
#   + Memory installed, used and free displayed in verbose mode
# + Bit Code Cleanup
#
# v1.2 Rouven Homann - [email protected]
# + Bug fixed where verbose output was required (nrpe2)
#       + Bug fixed where perfomance data was not displayed at verbose output
# + FindBin Module used for the nagios plugin path of the utils.pm
#
# v1.1 Rouven Homann - [email protected]
#     + Status Support (-c, -w)
# + Syntax Help Informations (-h)
#       + Version Informations Output (-V)
# + Verbose Output (-v)
#       + Better Error Code Output (as described in plugin guideline)
#
# v1.0 Garrett Honeycutt - [email protected]
#   + Initial Release
#
use strict;
use FindBin;
FindBin::again();
use lib $FindBin::Bin;
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use vars qw($PROGNAME $PROGVER);
use Getopt::Long;
use vars qw($opt_V $opt_h $verbose $opt_w $opt_c);

$PROGNAME = "check_mem";
$PROGVER = "1.8";

# add a directive to exclude buffers:
my $DONT_INCLUDE_BUFFERS = 0;

sub print_help ();
sub print_usage ();

Getopt::Long::Configure('bundling');
GetOptions ("V"   => \$opt_V, "version"    => \$opt_V,
  "h"   => \$opt_h, "help"       => \$opt_h,
        "v" => \$verbose, "verbose"  => \$verbose,
  "w=s" => \$opt_w, "warning=s"  => \$opt_w,
  "c=s" => \$opt_c, "critical=s" => \$opt_c);

if ($opt_V) {
  print_revision($PROGNAME,'$Revision: '.$PROGVER.' $');
  exit $ERRORS{'UNKNOWN'};
}

if ($opt_h) {
  print_help();
  exit $ERRORS{'UNKNOWN'};
}

print_usage() unless (($opt_c) && ($opt_w));

my ($mem_critical, $swap_critical);
my ($mem_warning, $swap_warning);
($mem_critical, $swap_critical) = ($1,$2) if ($opt_c =~ /([0-9]+)[%]?(?:,([0-9]+)[%]?)?/);
($mem_warning, $swap_warning)   = ($1,$2) if ($opt_w =~ /([0-9]+)[%]?(?:,([0-9]+)[%]?)?/);

# Check if swap params were supplied
$swap_critical ||= 100;
$swap_warning  ||= 100;

# print threshold in output message
my $mem_threshold_output = " (";
my $swap_threshold_output = " (";

if ( $mem_warning > 0 && $mem_critical > 0) {
  $mem_threshold_output .= "W> $mem_warning, C> $mem_critical";
}
elsif ( $mem_warning > 0 ) {
  $mem_threshold_output .= "W> $mem_warning";
}
elsif ( $mem_critical > 0 ) {
  $mem_threshold_output .= "C> $mem_critical";
}

if ( $swap_warning > 0 && $swap_critical > 0) {
  $swap_threshold_output .= "W> $swap_warning, C> $swap_critical";
}
elsif ( $swap_warning > 0 ) {
  $swap_threshold_output .= "W> $swap_warning";
}
elsif ( $swap_critical > 0 )  {
  $swap_threshold_output .= "C> $swap_critical";
}

$mem_threshold_output .= ")";
$swap_threshold_output .= ")";

my $verbose = $verbose;

my ($mem_percent, $mem_total, $mem_used, $swap_percent, $swap_total, $swap_used) = &sys_stats();
my $free_mem = $mem_total - $mem_used;
my $free_swap = $swap_total - $swap_used;

# set output message
my $output = "Memory Usage".$mem_threshold_output.": ". $mem_percent.'%
';
$output .= "Swap Usage".$swap_threshold_output.": ". $swap_percent.'%';

# set verbose output message
my $verbose_output = "Memory Usage:".$mem_threshold_output.": ". $mem_percent.'% '."- Total: $mem_total MB, used: $mem_used MB, free: $free_mem MB
";
$verbose_output .= "Swap Usage:".$swap_threshold_output.": ". $swap_percent.'% '."- Total: $swap_total MB, used: $swap_used MB, free: $free_swap MB
";

# set perfdata message
my $perfdata_output = "MemUsed=$mem_percent\%;$mem_warning;$mem_critical";
$perfdata_output .= " SwapUsed=$swap_percent\%;$swap_warning;$swap_critical";


# if threshold are 0, exit with OK
if ( $mem_warning == 0 ) { $mem_warning = 101 };
if ( $swap_warning == 0 ) { $swap_warning = 101 };
if ( $mem_critical == 0 ) { $mem_critical = 101 };
if ( $swap_critical == 0 ) { $swap_critical = 101 };


if ($mem_percent>$mem_critical || $swap_percent>$swap_critical) {
    if ($verbose) { print "CRITICAL: ".$verbose_output."|".$perfdata_output."\n";}
    else { print "CRITICAL: ".$output."|".$perfdata_output."\n";}
    exit $ERRORS{'CRITICAL'};
} elsif ($mem_percent>$mem_warning || $swap_percent>$swap_warning) {
    if ($verbose) { print "WARNING: ".$verbose_output."|".$perfdata_output."\n";}
    else { print "WARNING: ".$output."|".$perfdata_output."\n";}
    exit $ERRORS{'WARNING'};
} else {
    if ($verbose) { print "OK: ".$verbose_output."|".$perfdata_output."\n";}
    else { print "OK: ".$output."|".$perfdata_output."\n";}
    exit $ERRORS{'OK'};
}

sub sys_stats {
    my @memory = split(" ", `free -mt`);
    my $mem_total = $memory[7];
    my $mem_used;
    if ( $DONT_INCLUDE_BUFFERS) { $mem_used = $memory[15]; }
    else { $mem_used = $memory[8];}
    my $swap_total = $memory[18];
    my $swap_used = $memory[19];
    my $mem_percent = ($mem_used / $mem_total) * 100;
    my $swap_percent;
    if ($swap_total == 0) {
  $swap_percent = 0;
    } else {
  $swap_percent = ($swap_used / $swap_total) * 100;
    }
    return (sprintf("%.0f",$mem_percent),$mem_total,$mem_used, sprintf("%.0f",$swap_percent),$swap_total,$swap_used);
}

sub print_usage () {
    print "Usage: $PROGNAME -w -c [-v] [-h]\n";
    exit $ERRORS{'UNKNOWN'} unless ($opt_h);
}

sub print_help () {
    print_revision($PROGNAME,'$Revision: '.$PROGVER.' $');
    print "Copyright (c) 2005 Garrett Honeycutt/Rouven Homann/Cedric Temple\n";
    print "\n";
    print_usage();
    print "\n";
    print "-w , = Memory and Swap usage to activate a warning message (eg: -w 90,25 ) .\n";
    print "-c , = Memory and Swap usage to activate a critical message (eg: -c 95,50 ).\n";
    print "-v = Verbose Output.\n";
    print "-h = This screen.\n\n";
    support();
}

赋予脚步权限

chmod 755 /usr/local/nagios/libexec/check_mem.pl

nrpe服务

方法一
killall nrpe               
/usr/local/nagios/bin/nrpe -d -c /usr/local/nagios/etc/nrpe.cfg

方法二
kill -HUP `ps -ef|grep nrpe|awk 'NR==1{print $2}'`

服务器端测试

/usr/local/nagios/libexec/check_nrpe -H 192.168.31.60

/usr/local/nagios/libexec/check_nrpe -H 192.168.31.60 -c check_disk

nagios默认把全部的权限给nagiosadmin,所以可以通过修改cgi.cfg文件赋予nagios权限,切换到/usr/local/nagios/etc目录下
sed -i 's#nagiosadmin#nagiosadmin,nagios#g' /usr/local/nagios/etc/cgi.cfg
grep nagiosadmin /usr/local/nagios/etc/cgi.cfg

vi /usr/local/nagios/etc/nagios.cfg
#注释掉这行
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
#添加下面两行内容
cfg_file=/usr/local/nagios/etc/objects/services.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg

赋予权限

chown -R nagios.nagios /usr/local/nagios/etc/*

vi /usr/local/nagios/etc/objects/commands.cfg进入后按shift+g切到结尾加入下面内容。
# 'check_nrpe' command definition
define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }

# 'check_ping' command definition
define command{
        command_name    check-ping
        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 100.0,20% -c 200.0,50% -p 3 -t 2
        }

# 'check_http' command definition
define command{
        command_name    check-weburl
        command_line    $USER1$/check_http -H $HOSTADDRESS$ $ARG1$ -w 5 -c 10
        }

# 'check_tcp' command definition
define command{
        command_name    check-tcp
        command_line    $USER1$/check_tcp -H $HOSTADDRESS$ -p $ARG1$ -w 0.02 -c 0.1
        }


vi /usr/local/nagios/etc/objects/hosts.cfg
# Define some hosts
define host {
        use                      linux-server
        host_name                nagios-server
        alias                    nagios-server
        address                  192.168.31.66
        check_command            check-host-alive
        max_check_attempts        3
        normal_check_interval     2
        retry_check_interval      2
        check_period              24x7
        notification_interval     300
        notification_period       24x7
        notification_options      d,u,r
        contact_groups            admins
        process_perf_data         1
}

define host {
        use                      linux-server
        host_name                nagios-client
        alias                    nagios-client
        address                  192.168.31.60
        check_command            check-host-alive
        max_check_attempts        3
        normal_check_interval     2
        retry_check_interval      2
        check_period              24x7
        notification_interval     300
        notification_period       24x7
        notification_options      d,u,r
        contact_groups            admins
        process_perf_data         1
}

define hostgroup{
        hostgroup_name  linux-servers ; The name of the hostgroup
        alias           Linux Servers ; Long name of the group
        members         nagios,nagios-client     ; Comma separated list of hosts that belong to this group
        }


vi /usr/local/nagios/etc/objects/services.cfg
define service{
        use                     generic-service
        host_name               nagios-server
        service_description     Load
        check_command           check_nrpe!check_load       #这里的check_nrpe不是服务端/usr/local/nagios/libexec/check_nrpe,而是command.cfg里定义的命令
        }

define service{
        use                     generic-service
        host_name               nagios-server
        service_description     Disk
        check_command           check_nrpe!check_disk
        }

define service{
        use                     generic-service
        host_name               nagios-server
        service_description     memory
        check_command           check_nrpe!check_mem
        }
define service{
        use                     generic-service
        host_name               nagios-server
        service_description     Ping
        check_command           check-ping!192.168.31.66
}

define service{
        use                     generic-service
        host_name               nagios-server
        service_description     port_3306
        check_command           check-tcp!3306
}

define service{
        use                     generic-service
        host_name               nagios-client
        service_description     Load
        check_command           check_nrpe!check_load
        }

define service{
        use                     generic-service
        host_name               nagios-client
        service_description     Disk
        check_command           check_nrpe!check_disk
        }

define service{
        use                     generic-service
        host_name               nagios-client
        service_description     memory
        check_command           check_nrpe!check_mem
        }
define service{
        use             generic-service
        host_name               nagios-client
        service_description     Ping
        check_command           check-ping!192.168.31.60
}
define service{
        use                     generic-service
        host_name               nagios-client
        service_description     port_3306
        check_command           check-tcp!3306
}

重启服务

systemctl restart nagios

你可能感兴趣的:(Nagios,Centos)