Nagios 部署

Nagios 部署步骤

安装扩展源

yum install -y epel-release

安装nagios、插件、制图软件

yum install -y nagios nagios-plugins-all nagios-plugins-nt pnp4nagios

启动nagios

[root@review ~]# systemctl start nagios
[root@review ~]# ps aux|grep nagios
nagios     7500  5.7  0.2  51560  4560 ?        Ss   01:23   0:00 /usr/sbin/nagios -d /etc/nagios/nagios.cfg
nagios     7501  0.0  0.1  42044  1924 ?        S    01:23   0:00 /usr/sbin/nagios --worker /var/spool/nagios/cmd/nagios.qh
nagios     7502  0.0  0.1  42696  2188 ?        S    01:23   0:00 /usr/sbin/nagios --worker /var/spool/nagios/cmd/nagios.qh
nagios     7503  0.0  0.1  42044  1928 ?        S    01:23   0:00 /usr/sbin/nagios --worker /var/spool/nagios/cmd/nagios.qh
nagios     7504  0.0  0.1  42044  1928 ?        S    01:23   0:00 /usr/sbin/nagios --worker /var/spool/nagios/cmd/nagios.qh
nagios     7505  0.0  0.1  51560  1968 ?        S    01:23   0:00 /usr/sbin/nagios -d /etc/nagios/nagios.cfg
root       7509  0.0  0.0 112724   988 pts/0    S+   01:23   0:00 grep --color=autonagios

启动web端

[root@review ~]# systemctl start httpd
[root@review ~]# ps aux|grep httpd
root       7525  0.6  0.6 336628 12912 ?        Ss   01:26   0:00 /usr/sbin/httpd -DFOREGROUND
apache     7526  0.0  0.3 336764  6464 ?        S    01:26   0:00 /usr/sbin/httpd -DFOREGROUND
apache     7527  0.0  0.3 336764  7220 ?        S    01:26   0:00 /usr/sbin/httpd -DFOREGROUND
apache     7528  0.0  0.3 336764  6464 ?        S    01:26   0:00 /usr/sbin/httpd -DFOREGROUND
apache     7529  0.0  0.3 336764  6464 ?        S    01:26   0:00 /usr/sbin/httpd -DFOREGROUND
apache     7530  0.0  0.3 336764  6464 ?        S    01:26   0:00 /usr/sbin/httpd -DFOREGROUND
root       7535  0.0  0.0 112724   988 pts/0    S+   01:26   0:00 grep --color=autohttpd

浏览器登录

网址:http://IP地址/nagios/

默认用户和密码:nagiosadmin

pnp4nagios图形生成

选择 Bulk Mode with NPCD 模式。

进入/etc/pnp4nagios/nagios.cfg 将Bulk / NPCD mode模式下的内容复制出来:

[root@review pnp4nagios]# pwd
/etc/pnp4nagios
[root@review pnp4nagios]# ls
background.pdf  config.php        npcd.cfg            process_perfdata.cfg
check_commands  misccommands.cfg  pages               rra.cfg
config.d        nagios.cfg        pnp4nagios_release
[root@review pnp4nagios]# vim nagios.cfg
#
# Bulk / NPCD mode
# 

process_performance_data=1

# *** the template definition differs from the one in the original nagios.cfg
#
service_perfdata_file=/var/log/pnp4nagios/service-perfdata
service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::$SERVICEDESC$\tSERVICEPERFDATA::$SERVICEPERFDATA$\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$
service_perfdata_file_mode=a
service_perfdata_file_processing_interval=15
service_perfdata_file_processing_command=process-service-perfdata-file

# *** the template definition differs from the one in the original nagios.cfg
#
host_perfdata_file=/var/log/pnp4nagios/host-perfdata
host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$
host_perfdata_file_mode=a
host_perfdata_file_processing_interval=15
host_perfdata_file_processing_command=process-host-perfdata-file

添加到文件(/etc/nagios/nagios.cfg)末尾。

进入 /etc/pnp4nagios/misccommands.cfg 将下面内容复制出来:

#
# Bulk with NPCD mode
#
#define command {
#       command_name    process-service-perfdata-file
#       command_line    /bin/mv /var/log/pnp4nagios/service-perfdata /var/spool/pnp4nagios/service-perfdata.$TIMET$
#}

#define command {
#       command_name    process-host-perfdata-file
#       command_line    /bin/mv /var/log/pnp4nagios/host-perfdata /var/spool/pnp4nagios/host-perfdata.$TIMET$
#}

添加到文件(/etc/nagios/objects/commands.cfg)末尾,注意:要去掉注释

#
# Bulk with NPCD mode
#
define command {
       command_name    process-service-perfdata-file
       command_line    /bin/mv /var/log/pnp4nagios/service-perfdata /var/spool/pnp4nagios/service-perfdata.$TIMET$
}

define command {
       command_name    process-host-perfdata-file
       command_line    /bin/mv /var/log/pnp4nagios/host-perfdata /var/spool/pnp4nagios/host-perfdata.$TIMET$
}

重启

[root@review objects]# systemctl restart nagios
[root@review objects]# systemctl restart httpd
[root@review objects]# systemctl restart npcd

查询下面目录中是否有对应文件

[root@review ~]# ll /var/lib/pnp4nagios/localhost/
总用量 5300
-rw-rw-r--. 1 nagios nagios 1151496 10月 24 02:20 Current_Load.rrd
-rw-rw-r--. 1 nagios nagios    3353 10月 24 02:20 Current_Load.xml
-rw-rw-r--. 1 nagios nagios  384952 10月 24 02:20 Current_Users.rrd
-rw-rw-r--. 1 nagios nagios    1931 10月 24 02:20 Current_Users.xml
-rw-rw-r--. 1 nagios nagios  768224 10月 24 02:21 _HOST_.rrd
-rw-rw-r--. 1 nagios nagios    2489 10月 24 02:21 _HOST_.xml
-rw-rw-r--. 1 nagios nagios  768224 10月 24 02:20 HTTP.rrd
-rw-rw-r--. 1 nagios nagios    2505 10月 24 02:20 HTTP.xml
-rw-rw-r--. 1 nagios nagios  768224 10月 24 02:21 PING.rrd
-rw-rw-r--. 1 nagios nagios    2595 10月 24 02:21 PING.xml
-rw-rw-r--. 1 nagios nagios  384952 10月 24 02:21 Root_Partition.rrd
-rw-rw-r--. 1 nagios nagios    1979 10月 24 02:21 Root_Partition.xml
-rw-rw-r--. 1 nagios nagios  384952 10月 24 02:22 SSH.rrd
-rw-rw-r--. 1 nagios nagios    1893 10月 24 02:22 SSH.xml
-rw-rw-r--. 1 nagios nagios  384952 10月 24 02:20 Swap_Usage.rrd
-rw-rw-r--. 1 nagios nagios    1945 10月 24 02:20 Swap_Usage.xml
-rw-rw-r--. 1 nagios nagios  384952 10月 24 02:20 Total_Processes.rrd
-rw-rw-r--. 1 nagios nagios    1973 10月 24 02:20 Total_Processes.xml

浏览器访问:http://192.168.1.91/pnp4nagios

 默认用户密码都是nagiosadmin

pnp4nagios主页图形生成

vim /etc/nagios/objects/templates.cfg

在末尾添加:

define host {
   name       host-pnp
   action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
   register   0
}
 
define service {
   name       service-pnp
   action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
   register   0
}

在templates.cfg文件中找到 generic-host 末尾添加一行内容

define host {

    name                            generic-host            ; The name of this host template
    notifications_enabled           1                       ; Host notifications are enabled
    event_handler_enabled           1                       ; Host event handler is enabled
    flap_detection_enabled          1                       ; Flap detection is enabled
    process_perf_data               1                       ; Process performance data
    retain_status_information       1                       ; Retain status information across program restarts
    retain_nonstatus_information    1                       ; Retain non-status information across program restarts
    notification_period             24x7                    ; Send host notifications at any time
    register                        0                       ; DON'T REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
    use                             host-pnp                ; add context(添加这行)
}

找到 generic-service 末尾添加一行内容

define service {

    name                            generic-service         ; The 'name' of this service template
    active_checks_enabled           1                       ; Active service checks are enabled
    passive_checks_enabled          1                       ; Passive service checks are enabled/accepted
    parallelize_check               1                       ; Active service checks should be parallelized (disabling this can lead to major performance problems)
    obsess_over_service             1                       ; We should obsess over this service (if necessary)
    check_freshness                 0                       ; Default is to NOT check service 'freshness'
    notifications_enabled           1                       ; Service notifications are enabled
    event_handler_enabled           1                       ; Service event handler is enabled
    flap_detection_enabled          1                       ; Flap detection is enabled
    process_perf_data               1                       ; Process performance data
    retain_status_information       1                       ; Retain status information across program restarts
    retain_nonstatus_information    1                       ; Retain non-status information across program restarts
    is_volatile                     0                       ; The service is not volatile
    check_period                    24x7                    ; The service can be checked at any time of the day
    max_check_attempts              3                       ; Re-check the service up to 3 times in order to determine its final (hard) state
    check_interval                  10                      ; Check the service every 10 minutes under normal conditions
    retry_interval                  2                       ; Re-check the service every two minutes until a hard state can be determined
    contact_groups                  admins                  ; Notifications get sent out to everyone in the 'admins' group
    notification_options            w,u,c,r                 ; Send notifications about warning, unknown, critical, and recovery events
    notification_interval           60                      ; Re-notify about service problems every hour
    notification_period             24x7                    ; Notifications can be sent out at any time
    register                        0                       ; DON'T REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
    use                             service-pnp             ; add context(添加这行)
}

重启

systemctl restart nagios
systemctl restart httpd
systemctl restart npcd

浏览器登录 http://192.168.1.91/nagios/

监控Linux

服务端配置

yum install nagios-plugins-nrpe -y
yum install nrpe  -y

vim /etc/nagios/objects/commands.cfg
添加command
define command{
    command_name check_nrpe
    command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

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

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

被监控端配置

yum install nrpe  -y
添加监控项
vim /etc/nagios/nrpe.cfg


command[check_mem]=/usr/lib64/nagios/plugins/check_mem.sh -w 80 -c 90
command[check_cpu]=/usr/lib64/nagios/plugins/check_cpu.sh -w 80 -c 90
编写脚本监控内存
vim /usr/lib64/nagios/plugins/check_memory.sh

#!/bin/bash

# Get total memory and free memory
TOTAL=`free -m | grep Mem | awk '{print $2}'`
FREE=`free -m | grep Mem | awk '{print $4}'`

# Calculate memory usage percentage
USED=`echo "scale=2; 100 - (($FREE/$TOTAL) * 100)" | bc`

# Check memory usage against warning and critical thresholds
if [ $USED -ge $1 ] && [ $USED -lt $2 ]; then
    echo "WARNING - Memory usage is at ${USED}%"
    exit 1
elif [ $USED -ge $2 ]; then
    echo "CRITICAL - Memory usage is at ${USED}%"
    exit 2
else
    echo "OK - Memory usage is at ${USED}%"
    exit 0
fi
编写脚本监控cpu
vim /usr/lib64/nagios/plugins/check_cpu.sh

#!/bin/bash

# Get CPU usage percentage
CPU=`top -bn1 | grep load | awk '{printf "%.2f%%\n", $(NF-2)}'`

# Remove '%' sign from CPU usage
CPU=${CPU%?}

# Check CPU usage against warning and critical thresholds
if [ ${CPU%.*} -ge $1 ] && [ ${CPU%.*} -lt $2 ]; then
    echo "WARNING - CPU usage is at ${CPU}"
    exit 1
elif [ ${CPU%.*} -ge $2 ]; then
    echo "CRITICAL - CPU usage is at ${CPU}"
    exit 2
else
    echo "OK - CPU usage is at ${CPU}"
    exit 0
fi
给脚本可执行权限
chmod +x /usr/lib64/nagios/plugins/check_memory.sh

chmod +x /usr/lib64/nagios/plugins/check_cpu.sh
启动 nrpe
systemctl start nrpe && systemctl enable nrpe

监控windows

安装插件

vim /etc/nagios/objects/windows.cfg

# 添加主机
define host {

    use                     windows-server         
    host_name               winserver            
    alias                   My Windows Server       
    address                 192.168.1.100             
}
# 加入组
define hostgroup {

    hostgroup_name          windows-servers        
    alias                   Windows Servers         
}
#需要监控的服务
define service {

    use                     generic-service
    host_name               winserver
    service_description     NSClient++ Version
    check_command           check_nt!CLIENTVERSION
}

vim /etc/nagios/nagios.cfg

# cfg_file=/etc/nagios/objects/windows.cfg
# 修改为
cfg_file=/etc/nagios/objects/windows.cfg

重启nagios、httpd、npcd

systemctl restart nagios
systemctl restart httpd
systemctl restart npcd
### Nagios 部署教程 #### 准备工作 为了成功安装和配置Nagios监控系统,需要准备一台运行Linux操作系统的服务器。推荐使用Ubuntu或CentOS作为操作系统环境[^1]。 #### 安装依赖包 在开始之前,确保更新软件源并安装必要的依赖项。对于基于Debian的系统,可以执行如下命令来完成这一步骤: ```bash sudo apt-y sudo apt-get install build-essential libgd-dev openssl libssl-dev unzip wget -y ``` 对于RedHat系发行版,则应采用yum工具来进行相同的操作: ```bash sudo yum update -y sudo yum groupinstall "Development Tools" sudo yum install gd gd-y ``` #### 下载与编译Nagios Core 前往[Nagios官方网站](https://www.nagios.org/downloads/)下载最新版本的核心程序压缩文件,并解压到指定目录下进行编译安装: ```bash cd /tmp/ wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.6.tar.gz tar zxf nagios-4.4.6.tar.gz cd nagios-4.4.6/ ./configure --with-httpd-conf=/etc/httpd/conf.d make all sudo make install-groups-users newgrp nagios sudo make install-daemoninit sudo make install-commandmode sudo make install-config sudo make install-webconf ``` #### 插件安装 同样地,在线获取官方提供的插件集合,并按照指示完成其设置过程: ```bash cd /tmp/ wget http://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz tar zxf nagios-plugins-2.2.1.tar.gz cd nagios-plugins-2.2.1/ ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd make sudo make install ``` #### 启动服务 最后启动相关联的服务组件,并将其加入开机自启列表中去: ```bash sudo systemctl start httpd.service sudo systemctl enable httpd.service sudo systemctl start nagios.service sudo systemctl enable nagios.service ``` 此时应该可以通过浏览器访问`http://<your_server_ip>/nagios`页面查看默认界面了。登录用户名为'nagiosadmin',密码是在前面创建web接口时设定的那个。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩未零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值