najios从入门到精通

(14) 2024-04-04 21:01:02

注:本环境默认安装在/usr/local/nagios中 nagios版本:nagios-4.2.4 插件版本:nagios-plugins-2.1.2
本环境默认防火墙关闭:systemctl stop firewalld

实验环境如下:


Host Name OS IP Software
Nagios-Server CentOS 7 192.168.32.140 Apache、Php、Nagios、nagios-plugins
Nagios-Client CentOS 7 192.168.32.139 nagios-plugins、nrpe

本文基于centos最小化安装
本文分为以下几个部分:
1. Nagios介绍和yum 安装web环境
2. Nagios4.2源码包编译
3. Nagios插件源码编译安装以及配置解读
4. Nagios实现邮件告警
5. Nagios 使用nrpe监控远程Linux
6. Nagios安装pnp4nagios实现绘图


一 Nagios介绍和yum安装web环境


1.1 安装apache+php环境

yum install -y httpd php  

1.2 设置apache和自动启动

systemctl restart httpd #启动httpd
systemclt enable httpd  #设置为开机默认启动

1.3 测试apache是否启动 /var/www/html/index.html

<html>
<head>test apache</head>
<body>
    <h1>test apache </h1>
</body>
</html>

1.4 测试apache+php是否启动成功/var/www/html/test.php

<?php echo "test php " ?>

1.5 ntpdata统一系统时间

yum install -y ntpdate
ntpdate cn.pool.ntp.org 

二 Nagios4.2源码包编译


2.1 安装nagios依赖包

yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel unzip perl

2.2 添加用户并授权

mkdir /usr/local/nagios 
useradd nagios -s /sbin/nologin -M 
chown nagios:nagios /usr/local/nagios/ 
ll -d /usr/local/nagios/

2.3 下载并安装nagios4.2.4

tar zxvf nagios-4.2.4.tar.gz
cd nagios-4.2.4
./configure --prefix=/usr/local/nagios  
make all #编译
make install #安装
make install-init  #管理nagios服务的脚本
make install-commandmode  #权限配置
make install-config #配置文件安装
make install-webconf #httpd支持nagios的配置(需要重启httpd)

2.4 检测nagios core是否安装成功

2.4.1 查看nagios core安装版本

[root@anan nagios-4.2.4]# /usr/local/nagios/bin/nagios --version

Nagios Core 4.2.4
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2017-08-24
License: GPL

Website: https://www.nagios.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

2.4.2 检查是否生成了对应httpd的web配置文件

ls -al /etc/httpd/conf.d/ 

najios从入门到精通 (https://mushiming.com/)  第1张

2.4.2 cd /usr/local/nagios/ #查看是否有以下几个目录

bin:Nagios #可执行程序所在目录
etc:Nagios #配置文件所在目录
sbin:nagios #CGI文件所在目录,也就是执行外部命令所需要文件的所在的目录
share:   #nagios网页文件所在的目录
libexec:    #nagios外部插件 所在目录
var:   #nagios日志文件,lock等文件所在的目录

2.5 为nagios添加默认登陆名nagiosadmin 并设置密码

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

2.6 配置页面检测是否正常并添加别名放入.bashrc中

alias nagioscheck='/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg'

2.7 nagios服务器管理

chkconfig --add nagios #nagios加入服务管理
systemctl enable nagios #设置开机启动
systemctl restart nagios #启动nagios
systemctl restart httpd #重启httpd 

2.8 登陆网页localhost/nagios输入用户名和密码进入网站

najios从入门到精通 (https://mushiming.com/)  第2张

三 Nagios插件源码编译安装以及配置解读

3.1 下载并安装

wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
tar zxvf nagios-plugins-2.2.1.tar.gz
cd zxvf nagios-plugins-2.2.1
./configure --prefix=/usr/local/nagios/ --with-nagios-user=nagios --with-nagios-group=nagios
make && make install

3.2 检查nagios插件是否安装成功

ll -h /usr/local/nagios/libexec/

najios从入门到精通 (https://mushiming.com/)  第3张

3.3 修改主机配置文件 添加或者减少需要监控的内容

vi  /usr/local/nagios/etc/objects/localhost.cfg

3.4 简化nagios配置

cat /usr/local/nagios/etc/nagios.cfg | egrep -v '^#|^$'

3.5 nagios配置简要说明

log_file=/usr/local/nagios/var/nagios.log
cfg_file=/usr/local/nagios/etc/objects/commands.cfg #命令配置文件
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg  #联系人配置文件
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg #检测时间配置
cfg_file=/usr/local/nagios/etc/objects/templates.cfg #模板配置
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg #主机配置
cfg_file=/usr/local/nagios/etc/objects/resource.cf #宏定义配置

3.6 添加主机监控目录/usr/local/nagios/etc/nagios.cfg

cfg_dir=/usr/local/nagios/etc/objects/hosts #需要创建文件夹 该目录下的所有配置文件都会被加载

3.7 自定义添加监控主机 监控主机组以及监控对象 vi /usr/local/nagios/etc/objects/hosts/140.cfg

#################################定义主机######################### define host{ use linux-server host_name nagios_192.168.32.140 address 192.168.32.140 } #################################定义主机组################################# define hostgroup{ hostgroup_name localgroup alias localgroup members nagios_192.168.32.140 } ###########################定义监控服务################################### define service{ use generic-service host_name nagios_192.168.32.140 service_description ping_www check_command check_ping!30.0,10%!100.0,40% } define service { use generic-service host_name nagios_192.168.32.140 service_description check_Port 80 check_command check_tcp!80 } define service { use generic-service host_name nagios_192.168.32.140 service_description check_Port82 check_command check_tcp!80 } 

四 Nagios实现邮件告警

4.1 编写邮件脚本 /usr/local/nagios/libexec/nagios_email.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

from email.mime.text import MIMEText
from email.header import Header
import smtplib
import sys

smtpaddr = 'smtp.163.com'
#smtpaddr = 'smtp.qq.com'
myemail='18725883755@163.com'
#password='password' #输入你的密码
#f = open('/usr/local/nagios/libexec/password','r')
#password = f.readline()

recvmail=sys.argv[1]
subject=sys.argv[2]
content=sys.argv[3]

msg = MIMEText("""%s"""%(content), "html", "utf-8")

msg['Subject'] = Header(subject, 'utf-8').encode()
msg['From'] = myemail
msg['To'] =  recvmail

try:
        server = smtplib.SMTP()
        server.connect(smtpaddr, "25")
        server.starttls()
        server.login(myemail, password)
        server.sendmail(myemail, recvmail.split(','), msg.as_string())
        server.quit()
        print("success")
except Exception, e:
        print("fail: "+str(e))

4.2 测试邮件接收是否正常

chmod a+x /usr/local/nagios/libexec/nagios_email.py
/usr/local/nagios/libexec/nagios_email.py "18725883755@163.com" "disk not enough" "disk is <20"

4.3 定义邮件接收人:contacts.cfg

define contact{
    contact_name    nagiosadmin
    use             generic.contact
    alias           Nagios Admin

    email            nagios@localhost  #这里改为需要接收信息的邮箱
}

4.4 更改默认的告警配置,使用python脚本发送
/usr/local/nagios/etc/objects/commands.cfg

define command{
        command_name    notify-service-by-email
        command_line    python /usr/local/nagios/libexec/nagios_email.py $CONTACTEMAIL$ "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" "***** Nagios *****<br /><br />Notification Type: $NOTIFICATIONTYPE$<br /><br />Service: $SERVICEDESC$<br />Host: $HOSTALIAS$<br />Address: $HOSTADDRESS$<br />State: $SERVICESTATE$<br /><br />Date/Time: $LONGDATETIME$<br /><br />Additional Info:<br /><br />$SERVICEOUTPUT$<br />"
        }

五 Nagios 使用nrpe监控远程Linux

注:nagios需要安装nrpe和nagios-plugins

5.1 安装nrpe插件 :

 yum install gcc gcc-c++ openssl-devel openssl make #安装基本依赖
wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz
tar zxvf nrpe-3.2.1.tar.gz
cd nrpe-3.2.1
./configure --prefix=/usr/local/nagios
make all
make install
make install-config

5.2 nrpe是否安装成功

 ll -h /usr/local/nagios/libexec/check_nrpe /usr/local/nagios/bin/nrpe

najios从入门到精通 (https://mushiming.com/)  第4张

5.3 简化 nrpe配置并添加nagios_server可以监控nagios_client

cp /usr/local/nagios/etc/nrpe.cfg /usr/local/nagios/etc/nrpe.cfg_bak
cat /usr/local/nagios/etc/nrpe.cfg |egrep -v '^#|^$'

nagios_client修改/usr/local/nagios/etc/nrpe.cfg

allowed_hosts=127.0.0.1,::1,192.168.32.140 #添加nagios_server服务器ip

5.4 启动nrpe

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

5.5 查看5666端口是否启动

netstat -tulnp |grep nrpe

najios从入门到精通 (https://mushiming.com/)  第5张

5.6 nrpe启动测试

/usr/local/nagios/libexec/check_nrpe -H 192.168.32.139 #远程服务器ip

5.7 nagios_service添加被监控主机服务 (参考主机监控)

#################################定义主机######################### define host{ use linux-server host_name nagios_192.168.32.139 address 192.168.32.139 } #################################定义主机组################################# define hostgroup{ hostgroup_name 192.168.32.139 alias localgroup members nagios_192.168.32.139 } ###########################定义监控服务################################### define service{ use generic-service host_name nagios_192.168.32.139 service_description ping_www check_command check_ping!30.0,10%!100.0,40% } define service { use generic-service host_name nagios_192.168.32.139 service_description check_Port 80 check_command check_tcp!80 } define service { use generic-service host_name nagios_192.168.32.139 service_description check_Port82 check_command check_tcp!80 } 

najios从入门到精通 (https://mushiming.com/)  第6张

使用NRPE监控Linux cpu信息

5.8 使用NRPE监控Linux cpu信息
添加自定义命令 /usr/local/nagios/etc/objects/commands.cfg

define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ $ARG1$     }

添加主机远程监控
/usr/local/nagios/etc/objects/hosts/139.cfg中添加service定义

define service{
        use generic-service
        host_name nagios_192.168.32.139
        service_description check_Load
        check_command check_nrpe!check_load
}


六:Nagios安装pnp4nagios实现绘图


6.1 pnp4nagios的安装

yum -y install rrdtool rrdtool-devel perl-rrdtool perl-Time-HiRes php-gd
cd /usr/local/src/
wget 'https://jaist.dl.sourceforge.net/project/pnp4nagios/PNP-0.6/pnp4nagios-0.6.26.tar.gz'
tar -zxvf pnp4nagios-0.6.26.tar.gz
cd pnp4nagios-0.6.26
./configure --prefix=/usr/local/pnp4nagios --with-nagios-user=nagios --with-nagios-group=nagios
make all
make install            #安装pnp4nagios的主程序和web文件
make install-config     #安装配置文件
make install-init       #安装启动脚本
make install-webconf    #安装网页配置(重启httpd)

6.2 验证pnp4nagios的安装

/usr/local/pnp4nagios/bin/npcd --version

6.3 更新pnp4nagios的配置

cd  /usr/local/pnp4nagios/etc/
cp misccommands.cfg-sample misccommands.cfg
cp nagios.cfg-sample.nagios.cfg 
cp rra.cfg-sample rra.cfg

未完待续……


错误记录:

问题一:连接失败

[root@anan var]# /usr/local/nagios/libexec/check_nrpe -H 192.168.32.139
connect to address 192.168.32.139 port 5666: No route to host
connect to host 192.168.32.139 port 5666: No route to host

原因:防火墙未关闭,关闭防火墙后正常


问题二:没有/usr/local/nagios/etc/nrpe.cfg该文件

原因:未在安装nrpe时没有执行make install-config命令

问题三:安装pnp4nagios不要放在/usr/local/nagios下 否则会覆盖nagios页面 导致页面失败


THE END

发表回复