使用Nagios监控本地服务状态

使用Nagios监控本地服务状态

环境

IP OS HOSTNAME packages
192.168.220.138 centos7 server nagios-4.3.1.tar.gz
nagios-plugins-2.2.1.tar.gz
nrpe-3.1.0.tar.gz
192.168.220.139 centos7 client mysql

大致流程

在这里插入图片描述

环境简易安装

[root@server ~]# yum install httpd php  gcc glibc gd gd-devel unzip mysql-devel -y
[root@server ~]# systemctl start httpd
[root@server ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

[root@server ~]# cat >> /var/www/html/index.php << eof
> <?php
> phpinfo();
> ?>
> eof

测试下环境
[root@server ~]# curl 192.168.220.138 -L		
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #ffffff; color: #000000;}
body, td, th, h1, h2 {font-family: sans-serif;}
pre {margin: 0px; font-family: monospace;}
......

安装nagios

创建用户
[root@server ~]# useradd nagios
[root@server ~]# groupadd nagcmd
[root@server ~]#  usermod -G nagcmd nagios
[root@server ~]#  usermod -G nagcmd apache
解压文件
[root@server ~]# tar -zxvf nagios-4.3.1.tar.gz
[root@server ~]# cd nagios-4.3.1/
[root@server nagios-4.3.1]# ls
base          daemon-init.in  install-sh    nagios.sysconfig  THANKS
cgi           docs            LEGAL         pkginfo.in        t-tap
Changelog     doxy.conf       lib           README            update-version
common        functions       LICENSE       README.asciidoc   UPGRADING
config.guess  html            Makefile.in   sample-config     worker
config.sub    include         make-tarball  subst.in          xdata
configure     indent-all.sh   mkpackage     t
configure.ac  indent.sh       module        tap
contrib       INSTALLING      nagios.spec   test
编译安装
[root@server nagios-4.3.1]# ./configure --with-command-group=nagcmd
......
*** Configuration summary for nagios 4.3.1 02-23-2017 ***:

 General Options:
 -------------------------
        Nagios executable:  nagios
        Nagios user/group:  nagios,nagios
       Command user/group:  nagios,nagcmd
             Event Broker:  yes
        Install ${prefix}:  /usr/local/nagios	#安装目录
    Install ${includedir}:  /usr/local/nagios/include/nagios
                Lock file:  ${prefix}/var/nagios.lock
   Check result directory:  ${prefix}/var/spool/checkresults
           Init directory:  /etc/rc.d/init.d
  Apache conf.d directory:  /etc/httpd/conf.d
             Mail program:  /bin/mail
                  Host OS:  linux-gnu
          IOBroker Method:  epoll

 Web Interface Options:
 ------------------------
                 HTML URL:  http://localhost/nagios/
                  CGI URL:  http://localhost/nagios/cgi-bin/
 Traceroute (used by WAP):  /usr/bin/traceroute


Review the options above for accuracy.  If they look okay,
type 'make all' to compile the main program and CGIs.

[root@server nagios-4.3.1]# make all && make install
生成目录
[root@server nagios-4.3.1]# ls /usr/local/nagios/
bin  libexec  sbin  share  var

说明:
bin:存放可执行文件,其中nagios命令,是服务器主程序,用于启动,检查nagios主配置文件等。
etc:配置文件目录。
libexec:次目录下存放所有用于监控的命令。
share:web接口访问的站点目录

生成启动脚本
[root@server nagios-4.3.1]# make install-init
/usr/bin/install -c -m 755 -d -o root -g root /etc/rc.d/init.d
/usr/bin/install -c -m 755 -o root -g root daemon-init /etc/rc.d/init.d/nagios

*** Init script installed ***

设定工作目录权限
[root@server nagios-4.3.1]# make install-commandmode
/usr/bin/install -c -m 775 -o nagios -g nagcmd -d /usr/local/nagios/var/rw
chmod g+s /usr/local/nagios/var/rw

*** External command directory configured ***

生成配置文件
[root@server nagios-4.3.1]# make install-config
[root@server nagios-4.3.1]# ls /usr/local/nagios/etc/
cgi.cfg  nagios.cfg  objects  resource.cfg

安装nagios的Web配置文件到apache /etc/httpd/conf.d/目录下
[root@server 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@server ~]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password: 
Re-type new password: 
Adding password for user nagiosadmin

[root@server ~]# cat /usr/local/nagios/etc/htpasswd.users
nagiosadmin:$apr1$oa2bzc/9$e5zj79xvsjqqvq3dRisif/

安装nagios插件

[root@server ~]# tar -zxvf nagios-plugins-2.2.1.tar.gz 
[root@server ~]#  cd nagios-plugins-2.2.1/

[root@server nagios-plugins-2.2.1]# ./configure  --with-nagios-user=nagios --with-nagios-group=nagcmd

[root@server nagios-plugins-2.2.1]# make && make install 
[root@server nagios-plugins-2.2.1]# ls /usr/local/nagios/libexec/
check_apt       check_file_age      check_log       check_oracle   check_tcp
check_breeze    check_flexlm        check_mailq     check_overcr   check_time
check_by_ssh    check_ftp           check_mrtg      check_ping     check_udp
check_clamd     check_http          check_mrtgtraf  check_pop      check_ups
check_cluster   check_icmp          check_nagios    check_procs    check_uptime
check_dhcp      check_ide_smart     check_nntp      check_real     check_users
check_dig       check_ifoperstatus  check_nt        check_rpc      check_wave
check_disk      check_ifstatus      check_ntp       check_sensors  negate
check_disk_smb  check_imap          check_ntp_peer  check_smtp     urlize
check_dns       check_ircd          check_ntp_time  check_ssh      utils.pm
check_dummy     check_load          check_nwstat    check_swap     utils.sh

登陆nagios:http://IP/nagios

[root@server nagios-plugins-2.2.1]# /etc/init.d/nagios start
Reloading systemd:                                         [  确定  ]
Starting nagios (via systemctl):                           [  确定  ]
通过http://192.168.220.138/nagios访问

监听本地主机

[root@server etc]# cd /usr/local/nagios/etc/objects/
先做下备份
[root@server objects]# cp localhost.cfg localhost.cfg.bak
[root@server objects]# > localhost.cfg
  1 # Define a host for the local machine
  2 
  3 define host{
  4     use         linux-server
  5     host_name           server	#定义主机名
  6     alias           HomeSwitch
  7         address                 192.168.220.138	#ip地址
  8         icon_image              switch.gif
  9         statusmap_image         switch.gd2
 10     2d_coords       100,200
 11     3d_coords       100,200,100
 12     }
 13 #
 14 # HOST GROUP DEFINITION
 15 
 16 define hostgroup{
 17         hostgroup_name  linux-servers ; The name of the hostgroup
 18         alias           Linux Servers ; Long name of the group
 19         members         server     ; Comma separated list of hosts that belong     to this group
 20         }
 21 define service{
 22         use                             local-service
 23         host_name                   server
 24         service_description             NFS
 25         check_command                   check_tcp!2049	#在commands.cfg有配置,检查2049端口
 26         }
 27 

[root@server objects]# yum install nfs-utils -y
[root@server objects]#vim /etc/exports
/tmp *(rw)
[root@server objects]# systemctl start nfs
检查下列表
[root@server objects]# showmount -e
Export list for server:
/tmp *
[root@server objects]# /etc/init.d/nagios restart
Restarting nagios (via systemctl):                         [  确定  ]

在这里插入图片描述

监控client mysql服务

[root@server objects]# cd /usr/local/nagios/etc/
[root@server etc]# ls
cgi.cfg  htpasswd.users  nagios.cfg  objects  resource.cfg
[root@server etc]# vim  nagios.cfg
添加一行
cfg_file=/usr/local/nagios/etc/objects/mysqlserver.cfg

[root@server etc]# cd objects/
[root@server objects]# vim mysqlserver.cfg 
define host{ 
        use                    linux-server 
        host_name             client
        alias                  mysql服务器 
        address                192.168.220.139
        icon_image              server.gif 
        statusmap_image        server.gd2 
        2d_coords              500,200
        3d_coords              500,200,100
        }
###################check_mysql############################# 
define service{ 
        use                    local-service 
        host_name             client
        service_groups          mysql服务组 
        service_description    mysql服务
        check_command          check_mysql 
        } 
##########################check_mysql_groups######################### 
define servicegroup{ 
        servicegroup_name      mysql服务组 
        alias                  mysql服务器 
        members client,mysql服务
        }


[root@server objects]# vim commands.cfg 
定义check_mysql
define command{
    command_name    check_mysql
    command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -u nagdb -d nagdb
    } 
-u 用户,-d要检查的数据库名  

client安装mysql(略):

https://blog.csdn.net/DoloresOOO/article/details/97392449

[root@client ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.60-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database nagdb;
Query OK, 1 row affected (0.00 sec)

mysql> grant select on nagdb.* to nagdb@'192.168.220.138';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

在这里插入图片描述

通过nrpe监控硬盘信息

[root@server ~]# yum install openssl openssl-devel xinetd  -y
[root@server ~]# tar zxvf nrpe-3.1.0.tar.gz 
[root@server ~]# cd nrpe-3.1.0/
[root@server nrpe-3.1.0]# ./configure

*** Configuration summary for nrpe 3.1.0-rc1 2017-04-06 ***:

 General Options:
 -------------------------
 NRPE port:    5666
 NRPE user:    nagios
 NRPE group:   nagios
 Nagios user:  nagios
 Nagios group: nagios
 

Review the options above for accuracy.  If they look okay,
type 'make all' to compile the NRPE daemon and client
or type 'make' to get a list of make options.

[root@server nrpe-3.1.0]# make all

cd ./src/; make
make[1]: 进入目录“/root/nrpe-3.1.0/src”
gcc -g -O2 -I/usr/include/krb5   -DHAVE_CONFIG_H -I ../include -I ./../include -o nrpe ./nrpe.c ./utils.c ./acl.c -lssl -lcrypto   -lnsl   
gcc -g -O2 -I/usr/include/krb5   -DHAVE_CONFIG_H -I ../include -I ./../include -o check_nrpe ./check_nrpe.c ./utils.c -lssl -lcrypto   -lnsl  
make[1]: 离开目录“/root/nrpe-3.1.0/src”

*** Compile finished ***

You can now continue with the installation or upgrade process.

Read the PDF documentation (docs/NRPE.pdf) for information on the next
steps you should take to complete the installation or upgrade.

[root@server nrpe-3.1.0]# make install

cd ./src/; make install
make[1]: 进入目录“/root/nrpe-3.1.0/src”
make install-plugin
make[2]: 进入目录“/root/nrpe-3.1.0/src”
/usr/bin/install -c -m 755 -d /usr/local/nagios/bin
/usr/bin/install -c -m 755 ../uninstall /usr/local/nagios/bin/nrpe-uninstall
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/libexec
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/libexec
/usr/bin/install -c -m 775 -o nagios -g nagios check_nrpe /usr/local/nagios/libexec
make[2]: 离开目录“/root/nrpe-3.1.0/src”
make install-daemon
make[2]: 进入目录“/root/nrpe-3.1.0/src”
/usr/bin/install -c -m 755 -d /usr/local/nagios/bin
/usr/bin/install -c -m 755 ../uninstall /usr/local/nagios/bin/nrpe-uninstall
/usr/bin/install -c -m 755 nrpe /usr/local/nagios/bin
/usr/bin/install -c -m 755 -d /usr/lib/tmpfiles.d
/usr/bin/install -c -m 644 ../startup/tmpfile.conf /usr/lib/tmpfiles.d/nrpe.conf
make[2]: 离开目录“/root/nrpe-3.1.0/src”
make[1]: 离开目录“/root/nrpe-3.1.0/src”

生成check_nrpe 
[root@server nrpe-3.1.0]# ls /usr/local/nagios/libexec/check_nrpe 
/usr/local/nagios/libexec/check_nrpe

[root@server ~]# cd /usr/local/nagios/etc/
[root@server etc]# vim nagios.cfg 
添加
cfg_file=/usr/local/nagios/etc/objects/clientdisk.cfg

[root@server etc]# cd objects/
[root@server objects]# touch clientdisk.cfg
[root@server objects]# vim clientdisk.cfg 
define host{
        use                    linux-server
        host_name             server1
        alias                  disk
        address                192.168.220.139
        icon_image              server.gif
        statusmap_image        server.gd2
        2d_coords              500,200
        3d_coords              500,200,100
        }

# SERVICE DEFINITIONS 

define service{ 
        use                             local-service    
        host_name                       client
        service_description              Root Partition 
        check_command                   check_nrpe!check_sda2  
        } 

define service{ 
        use                             local-service       
        host_name                       client
        service_description             Total Processes 
        check_command                   check_nrpe!check_total_procs 
        } 

define service{ 
        use                             local-service      
        host_name                       client
        service_description             Current Load 
        check_command                   check_nrpe!check_load 
        }

commands.cfg定义命令
[root@server objects]# vim commands.cfg 
添加
define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
        
[root@server objects]# systemctl restart nagios

配置要监控的服务器

[root@client ~]# useradd  nagios
[root@client ~]# groupadd nagcmd
[root@client ~]# usermod -G nagcmd nagios
[root@client ~]# tar zxvf nagios-plugins-2.2.1.tar.gz
[root@client nagios-plugins-2.2.1]# ./configure && make && make install

[root@client nagios-plugins-2.2.1]# cd 
[root@client ~]# tar zxvf nrpe-3.1.0.tar.gz 

[root@client ~]# cd nrpe-3.1.0/
[root@client nrpe-3.1.0]# ./configure
[root@client nrpe-3.1.0]# make all && make install
[root@client nrpe-3.1.0]# ls /usr/local/nagios/bin/nrpe
/usr/local/nagios/bin/nrpe
[root@client nrpe-3.1.0]# make install-config
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc
/usr/bin/install -c -m 644 -o nagios -g nagios sample-config/nrpe.cfg /usr/local/nagios/etc
[root@client nrpe-3.1.0]# vim /usr/local/nagios/etc/nrpe.cfg 
添加
allowed_hosts=192.168.220.138
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 38% -c 35% -p /dev/    sda1 
command[check_sda2]=/usr/local/nagios/libexec/check_disk -w 42% -c 43% -p /dev/    sda2 
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%

启动nrpe
-c 指定配置文件 -d 守护进程方式
[root@client nrpe-3.1.0]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@client nrpe-3.1.0]# netstat -antup | grep 5666
tcp        0      0 0.0.0.0:5666            0.0.0.0:*               LISTEN      34725/nrpe          
tcp6       0      0 :::5666                 :::*                    LISTEN      34725/nrpe

[root@server ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.220.139
NRPE v3.1.0-rc1

在这里插入图片描述

发布了65 篇原创文章 · 获赞 48 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/DoloresOOO/article/details/98961335