ITPub博客

首页 > 自动化运维 > 应用服务器 > CentOS7安装部署Nagios及监控主机python一键部署脚本

CentOS7安装部署Nagios及监控主机python一键部署脚本

投稿 应用服务器 作者:噯我莂辵 时间:2025-02-28 10:29:20 0 删除 编辑

1. 准备工作

确保你的CentOS 7系统已经安装了 wget、 gcc、 make等工具。

yum install -y wget gcc glibc glibc-common make gettext automake autoconf openssl-devel net-snmp net-snmp-utils epel-release perl-Net-SNMP

2. 使用 subprocess 模块来执行部署操作命令,以下是一个简单的Python脚本:

import subprocess


def run_command(command):

    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    stdout, stderr = process.communicate()

    if process.returncode != 0:

        print(f"Error: {stderr.decode('utf-8')}")

    else:

        print(stdout.decode('utf-8'))


# 安装依赖

run_command("yum install -y wget gcc glibc glibc-common make gettext automake autoconf openssl-devel net-snmp net-snmp-utils epel-release perl-Net-SNMP")


# 下载并安装Nagios Core

run_command("wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.6.tar.gz")

run_command("tar -zxvf nagios-4.4.6.tar.gz")

run_command("cd nagios-4.4.6 && ./configure --with-command-group=nagcmd")

run_command("make all")

run_command("make install")

run_command("make install-init")

run_command("make install-config")

run_command("make install-commandmode")

run_command("make install-webconf")

run_command("useradd nagios")

run_command("usermod -a -G nagcmd nagios")

run_command("usermod -a -G nagcmd apache")

run_command("systemctl start nagios")

run_command("systemctl enable nagios")


# 下载并安装Nagios Plugins

run_command("wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz")

run_command("tar -zxvf nagios-plugins-2.3.3.tar.gz")

run_command("cd nagios-plugins-2.3.3 && ./configure --with-nagios-user=nagios --with-nagios-group=nagios")

run_command("make")

run_command("make install")


# 下载并安装NRPE

run_command("wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.0.3/nrpe-4.0.3.tar.gz")

run_command("tar -zxvf nrpe-4.0.3.tar.gz")

run_command("cd nrpe-4.0.3 && ./configure")

run_command("make all")

run_command("make install-plugin")

run_command("make install-daemon")

run_command("make install-daemon-config")

run_command("make install-xinetd")

run_command("echo 'nrpe 5666/tcp # NRPE' >> /etc/services")

run_command("systemctl restart xinetd")


# 配置Windows主机监控

with open("/usr/local/nagios/etc/objects/windows.cfg", "a") as f:

    f.write("""

define host {

    use                     windows-server

    host_name               windows-host

    alias                   Windows Host

    address                 192.168.1.100

}


define service {

    use                     generic-service

    host_name               windows-host

    service_description     CPU Load

    check_command           check_nt!CPULOAD!-l 5,80,90

}


define service {

    use                     generic-service

    host_name               windows-host

    service_description     Memory Usage

    check_command           check_nt!MEMUSE!-w 80 -c 90

}


define service {

    use                     generic-service

    host_name               windows-host

    service_description     Disk Usage

    check_command           check_nt!USEDDISKSPACE!-l C -w 80 -c 90

}

""")


# 配置Linux主机监控

with open("/usr/local/nagios/etc/objects/linux.cfg", "a") as f:

    f.write("""

define host {

    use                     linux-server

    host_name               linux-host

    alias                   Linux Host

    address                 192.168.1.101

}


define service {

    use                     generic-service

    host_name               linux-host

    service_description     CPU Load

    check_command           check_nrpe!check_load

}


define service {

    use                     generic-service

    host_name               linux-host

    service_description     Memory Usage

    check_command           check_nrpe!check_mem

}


define service {

    use                     generic-service

    host_name               linux-host

    service_description     Disk Usage

    check_command           check_nrpe!check_disk

}

""")


# 重启Nagios服务

run_command("systemctl restart nagios")


print("Nagios installation and configuration completed!")

3. 执行Python脚本

将上述Python脚本保存为 install_nagios.py,并执行:

python3 install_nagios.py

通过上述步骤,就可以在CentOS 7上成功安装和配置Nagios,并监控Windows和Linux主机的各项性能指标。大家可以使用以上Python脚本来自动化一键部署整个过程。




来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29726860/viewspace-3074067/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2014-06-26

  • 博文量
    10
  • 访问量
    11146