nagios用NRPE监控Linux主机 服务端和客户端的安装配置

本文涉及的产品
RDS MySQL DuckDB 分析主实例,基础系列 4核8GB
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS AI 助手,专业版
简介:

安装配置nagios服务端

安装前的准备工作

1
2
3
4
5
yum -y install httpd gcc glibc glibc-common gd gd-devel php php-mysql mysql mysql-devel mysql-server
groupadd nagcmd
useradd -G nagcmd nagios
passwd nagios
usermod -a -G nagcmd apache


vim /etc/httpd/conf/httpd.conf

DirectoryIndex index.php index.html index.html.var  #找到这一行,添加index.php


编译安装nagios:

1
2
3
4
5
6
7
8
9
wget http: //prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.0.5.tar.gz
tar zxf nagios- 4.0 . 5 .tar.gz
cd nagios- 4.0 . 5
./configure -- with -command-group=nagcmd --enable-event-broker
make all
make install
make install-init
make install-commandmode
make install-config

附:在解压的时候如果提示下面的错误,是因为系统时间不对

tar: nagios-4.0.5/xdata/xsddefault.c: time stamp 2014-04-12 02:37:42 is 250653.223481153 s in the future

tar: nagios-4.0.5/xdata/xsddefault.h: time stamp 2014-04-12 02:37:42 is 250653.223419364 s in the future

tar: nagios-4.0.5/xdata: time stamp 2014-04-12 02:37:42 is 250653.223359922 s in the future

修改系统时间

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  

service ntpd stop

ntpdate asia.pool.ntp.org ; hwclock -w


需要在httpd的配置文件目录(conf.d)中生成Nagios的Web程序配置文件,继续在此编译安装目录输入一条命令:

# make install-webconf

创建一个登录nagios web程序的用户,这个用户帐号在以后通过web登录nagios认证时所用:

# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin #输入登陆nagios的密码

# service httpd restart

编译、安装nagios-plugins

nagios的所有监控工作都是通过插件完成的,因此,在启动nagios之前还需要为其安装官方提供的插件。http://exchange.nagios.org/directory/Plugins nagios插件链接

1
2
3
4
5
6
wget http: //nagios-plugins.org/download/nagios-plugins-2.0.tar.gz
tar zxf nagios-plugins- 2.0 .tar.gz
cd nagios-plugins- 2.0
./configure -- with -nagios-user=nagios -- with -nagios-group=nagios
make
make install

配置并启动Nagios

# chkconfig --add nagios

# chkconfig nagios on

检查其主配置文件

# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

# service nagios start


关闭selinux,

vim /etc/sysconfig/selinux  将其中的selinux后面的值“force”修改为“disable”或者

临时性的改动selinux也可以

1
2
3
# setenforce 0
# getenforce
Permissive

或者将nagios的CGI程序运行于SELinux/targeted模式而不用关闭selinux:

# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin

# chcon -R -t httpd_sys_content_t /usr/local/nagios/share

通过web界面查看nagios:

http://your_nagios_IP/nagios

输入帐号和密码

这时候可以看到本机的服务状态,如果没有出现页面,请检查iptables的80端口是否开放

如果出现下面提示,说明是selinux的问题,setenforce 0即可

1
2
3
4
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, root@localhost and inform them of the  time  the error occurred, and anything you might have  done  that may have caused the error.
More information about this error may be available  in  the server error log.


安装NRPE,服务端需要自己的check_nrpe,和被监控端联系

下载页面

1
http: //downloads .sourceforge.net /project/nagios/nrpe-2 .x /nrpe-2 .15 /nrpe-2 .15. tar .gz?r=&ts=1363788540&use_mirror=hivelocity
1
2
3
4
5
6
7
8
9
10
11
12
tar  -zxvf nrpe-2.15. tar .gz
cd  nrpe-2.15
. /configure  --with-nrpe-user=nagios \
--with-nrpe-group=nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
-- enable - command -args \
-- enable -ssl
make  all
make  install -plugin
make  install -daemon
make  install -daemon-config

如果./configure时候出现下面错误

1
2
checking  for  SSL headers... SSL headers found  in  /usr/local/ssl
checking  for  SSL libraries... configure: error: Cannot  find  ssl libraries

1
2
3
# find /usr/ -name libssl.so
/usr/local/ssl/lib/libssl .so
# ./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios  --with-nagios-group=nagios --enable-command-args --enable-ssl --with-ssl-lib=/usr/local/ssl/lib


安装配置被监控端

yum -y install gcc glibc glibc-common gd gd-devel 

或者

yum grouplist  #检查  yum -y groupinstall "Development Tools" "Development Libraries"


添加nagios用户

1
2
3
4
5
6
useradd -s /sbin/nologin nagios
tar zxf nagios-plugins- 2.0 .tar.gz
cd nagios-plugins- 2.0
./configure -- with -nagios-user=nagios -- with -nagios-group=nagios -- with -mysql=/usr/local/mysql
make all
make install

安装NRPE

下载页面

1
http: //downloads.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz?r=&ts=1363788540&use_mirror=hivelocity


1
2
3
4
5
6
7
8
9
10
11
12
tar -zxvf nrpe- 2.15 .tar.gz
cd nrpe- 2.15
./configure -- with -nrpe-user=nagios \
      -- with -nrpe-group=nagios \
      -- with -nagios-user=nagios \
      -- with -nagios-group=nagios \
      --enable-command-args \
      --enable-ssl
make all
make install-plugin
make install-daemon
make install-daemon-config

 注:如果./configure有下面提示,请 yum install openssl-devel
checking for SSL headers... SSL headers found in /usr/local/ssl
checking for SSL libraries... configure: error: Cannot find ssl libraries


配置NRPE

# vim /usr/local/nagios/etc/nrpe.cfg  找到相应的做修改

1
2
3
4
5
6
7
8
9
10
log_facility=daemon
pid_file=/ var /run/nrpe.pid
server_address= 172.16 . 100.11   #本机ip,本机提供nrpe服务,所以自己为服务端,此行需手动添加
server_port= 5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts= 172.16 . 100.1   #监控端的ip
command_timeout= 60
connection_timeout= 300
debug= 0

启动NRPE

# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

为了便于NRPE服务的启动,可以将如下内容定义为/etc/init.d/nrped脚本,用service nrped start 启动:vim /etc/init.d/nrped

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
# chkconfig:  2345  88  12
# description: NRPE DAEMON
NRPE=/usr/local/nagios/bin/nrpe
NRPECONF=/usr/local/nagios/etc/nrpe.cfg
case  "$1"  in
  start)
   echo -n  "Starting NRPE daemon..."
   $NRPE -c $NRPECONF -d
   echo  " done."
   ;;
  stop)
   echo -n  "Stopping NRPE daemon..."
   pkill -u nagios nrpe
   echo  " done."
  ;;
  restart)
   $ 0  stop
   sleep  2
   $ 0  start
   ;;
  *)
   echo  "Usage: $0 start|stop|restart"
   ;;
  esac
exit  0

chmod +x /etc/init.d/nrped


基于NRPE的监控

配置允许远程主机监控的对象

vim /usr/local/nagios/etc/nrpe.cfg

command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10

command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -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

上面是系统自带的,下面是添加的

1
2
3
4
5
6
7
8
9
10
11
12
command[check_rootdisk]=/usr/local/nagios/libexec/check_disk -w  20 % -c  10 % -p /
command[check_swap]=/usr/local/nagios/libexec/check_disk -w  40 % -c  20 %
command[check_sensors]=/usr/local/nagios/libexec/check_sensors
command[check_zombies]=/usr/local/nagios/libexec/check_procs -w  5  -c  10  -s Z
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w  20 % -c  10 % -p /dev/sda1
command[check_sda3]=/usr/local/nagios/libexec/check_disk -w  20 % -c  10 % -p /dev/sda3
#对mysql监控:
command[check_mysql]=/usr/local/nagios/libexec/check_mysql -utest -ptest -s / var /lib/mysql/mysql.sock -H localhost
#为了监控mysql需要在被监控的mysql加授权
#create database test;
#grant select on test.* to test@localhost identified by  "test" ;
#flush privileges;

因为我这里是sda1和sda3,磁盘检测请添加自己的

wKiom1Nd-R2TjoZNAACw3fjViGA932.jpg

# service nrped restart



再回到nagios服务端  定义配置

cd /usr/local/nagios/libexec

./check_nrpe -H 172.16.100.11 #测试一下

如果提示CHECK_NRPE: Socket timeout after 10 seconds. 那就把iptables的5666端口打开


/usr/local/nagios/libexec/check_nrpe -H 172.16.100.11 -c check_load

OK - load average: 1.55, 1.31, 1.30|load1=1.550;15.000;30.000;0; load5=1.310;10.000;25.000;0; load15=1.300;5.000;20.000;0; 


配置服务端

先定义命令  commands.cfg  

vim /usr/local/nagios/etc/objects/commands.cfg

在最后添加

define command{

 command_name check_nrpe

 command_line $USER1$/check_nrpe -H "$HOSTADDRESS$" -c $ARG1$

       }

再定义主机和服务

vim /usr/local/nagios/etc/objects/linuxhost.cfg  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
define host{
         use                      linux-server
         host_name               my linux server
         alias                   mylinux server
         address                  172.16 . 100.11  #被监控端的ip
         }
define service{
         use                              generic-service
         host_name                       my linux server
         service_description             check users
         check_command                   check_nrpe!check_users
         }
define service{
         use                              generic-service
         host_name                       my linux server
         service_description             check load
         check_command                   check_nrpe!check_load
         }
define service{
         use                              generic-service
         host_name                       my linux server
         service_description             check zombie proce
         check_command                   check_nrpe!check_zombie_procs
         }
define service{
         use                              generic-service
         host_name                       my linux server
         service_description             check total proce
         check_command                   check_nrpe!check_total_procs
         }
define service{
         use                              generic-service
         host_name                       my linux server
         service_description             check rootdisk
         check_command                   check_nrpe!check_rootdisk
         }
define service{
         use                              generic-service
         host_name                       my linux server
         service_description             check swap
         check_command                   check_nrpe!check_swap
         }
define service{
         use                              generic-service
         host_name                       my linux server
         service_description             check sda1
         check_command                   check_nrpe!check_sda1
         }
define service{
         use                              generic-service
         host_name                       my linux server
         service_description             check sda3
         check_command                   check_nrpe!check_sda3
         }
define service{
         use                              generic-service
         host_name                       my linux server
         service_description             check mysql
         check_command                   check_nrpe!check_mysql
         }

vim /usr/local/nagios/etc/nagios.cfg  #添加刚才的配置文件

cfg_file=/usr/local/nagios/etc/objects/linuxhost.cfg


/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg #检查配置是否正确

service nagios restart

到网页查看nagios

wKioL1Nd-TSAgbZdAAYEmSefDcw860.jpg


如果出现下面的提示,是说明iptables开启了,不过开启的是udp 5666端口,而不是tcp,这里需要开启tcp的5666

[1416991726] Warning: Return code of 255 for check of service 'check total proce' on  host 'my linux server' was out of bounds










本文转自 bbotte 51CTO博客,原文链接:http://blog.51cto.com/bbotte/1403997,如需转载请自行联系原作者
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
7月前
|
Ubuntu Linux 网络安全
Linux服务器之Ubuntu的安装与配置
Ubuntu Desktop是目前最成功、最流行的图形界面的Linux发行版;而Ubuntu Server也在服务器端市场占据了较大的份额。今天为大家详细介绍了Ubuntu Server的安装与配置,希望对你能有所帮助。关于VMware、VirtualBox等虚拟化软件的使用,朱哥还会在后续的文章中为大家详细介绍,敬请关注!
|
5月前
|
存储 Linux 开发工具
Linux环境下使用Buildroot配置软件包
使用Buildroot可以大大简化嵌入式Linux系统的开发和维护工作,但它需要对Linux系统和交叉编译有深入的理解。通过上述步骤,可以有效地配置和定制软件包,为特定的嵌入式应用构建高效、稳定的系统。
628 11
|
12月前
|
安全 Linux 开发工具
【Linux】vim使用与配置教程
Vim是一款功能强大的文本编辑器,广泛应用于Linux环境,是开发者和系统管理员的必备工具。本文介绍了Vim的基本操作与简单配置,涵盖命令模式、插入模式和底行模式的使用方法,以及光标定位、复制粘贴、搜索替换等常用技巧。同时,文章还提供了实用的分屏操作和代码注释方法,并分享了通过`.vimrc`文件进行个性化配置(如显示行号、语法高亮、自动缩进等)的技巧,帮助用户提升文本编辑效率。掌握这些内容,能让Vim更好地服务于日常工作与开发需求。
910 3
|
8月前
|
Kubernetes Linux 网络安全
Rocky Linux 8.9配置Kubernetes集群详解,适用于CentOS环境
初始化成功后,记录下显示的 `kubeadm join`命令。
521 0
|
8月前
|
存储 安全 Linux
Linux服务器上安装配置GitLab的步骤。
按照以上步骤,一个基础的GitLab服务应该运行并可以使用。记得定期检查GitLab官方文档,因为GitLab的安装和配置步骤可能随着新版本而变化。
811 0
|
10月前
|
关系型数据库 MySQL Java
安装和配置JDK、Tomcat、MySQL环境,以及如何在Linux下更改后端端口。
遵循这些步骤,你可以顺利完成JDK、Tomcat、MySQL环境的安装和配置,并在Linux下更改后端端口。祝你顺利!
581 11
|
10月前
|
运维 监控 中间件
Linux运维笔记 - 如何使用WGCLOUD监控交换机的流量
WGCLOUD是一款开源免费的通用主机监控工具,安装使用都非常简单,它可以监控主机、服务器的cpu、内存、磁盘、流量等数据,也可以监控数据库、中间件、网络设备
|
9月前
|
Linux 网络安全 开发工具
在Linux下配置gitee与Github的远程仓库
注意,git push后,是输入你的账号与密码。这个步骤可以通过特殊设置省去,但是一开始还是不要太省。
464 0
|
11月前
|
Ubuntu Linux 网络安全
如何在Linux中更改主机名?修改主机名最新方法
本期教程将指导您如何在Linux系统中更改主机名。主机名是Linux系统的常用功能,用于识别服务器,帮助区分不同服务器,并与网络进程和其他应用程序协同工作。教程涵盖显示当前主机名的方法(通过`hostname`命令),以及在CentOS 7、Debian 9和Ubuntu 16.04及以上版本中更改主机名的步骤(使用`hostnamectl set-hostname`命令)。对于其他Linux版本,可编辑`/etc/hostname`文件实现更改。记得重启相关服务或服务器以使更改生效!
2707 12
|
12月前
|
Java Linux 应用服务中间件
在Rocky Linux 9上安装JDK并配置环境变量!
本教程介绍在Rocky Linux 9上安装JDK并配置环境变量的完整步骤。首先更新系统,清理旧版本JDK相关包及残留文件,确保环境干净。接着搜索并安装所需版本的JDK(如OpenJDK 17),验证安装是否成功。然后查找JDK安装路径,配置全局环境变量`JAVA_HOME`和`PATH`,最后验证环境变量设置。按照此流程操作,可顺利完成Java开发环境搭建,支持多版本切换(如JDK 8/11/17)。生产环境请谨慎操作,避免影响现有服务。
1748 21