Python 自动化运维(3)--Nagios安装

Nagios

一款免费的开源IT基础设施监控系统,其功能强大,灵活性强,能够监控windows、linux、VMware和Unix主机状态,交换机、路由器等网络设置等。

结构上分为两个部分:

  1. 核心功能 轻量化
  2. 插件

Nagios特性

  1. 监控网络服务(HTTP,)
  2. 监控主机资源(CPU负载、CPU使用率、进程状态等)
  3. 主动通知
  4. web页面(发现异常报警功能)
  5. 可扩展

Nagios优点

  1. 轻量级,架构简单
  2. 容易部署
  3. 文档健全
  4. 灵活、全面(插件安装方便,可随时安装卸载,即插即用)

Nagios 缺点

  1. 修改配置麻烦(只能改配置文件,不能再web页面改)
  2. 太灵活、学习成本高
  3. 监控报警缺乏历史数据(可以通过插件解决 )
  4. 严重依赖外部插件

Nagios 原理

简而言之,主机给客户机发了指令,让客户机收集相关信息,客户机把信息收集后发送给主机。

nagios

通信过程:

  1. Nagios执行安装在它里面的check_nrpe插件,并告诉check_nrpe去检测哪些服务。
  2. Nagios执行安装在它里面的check_nrpe插件,并告诉check_nrpe去检测哪些服务
  3. NRPE运行本地的各种插件区检测本地的服务和状态(check_disk,..etc)
  4. 最后,NRPE把检测的结果传给主机端的check_nrpe,check_nrpe再把结果送到Nagios状态队列中。
  5. Nagios依次读取队列中的信息,再把结果显示出来。

Nagios 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
cd /etc/yum.repos.d/
mv CentOS-Base.repo CentOS-Base.repo.bak
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
yum makecache
yum -y update

# Install the required packages
yum install gcc glibc glibc-common wget gd gd-devel perl postfix unzip zip httpd php # php安装后可访问web页面
# Download and Install Nagios Core
cd /tmp
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.3.tar.gz
tar xzf nagios-4.4.3.tar.gz
./configure

nagiosinstalled

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd apache # 得安装httpd才有

./configure --with-nagios-group=nagios --with-command-group=nagcmd
make all
make install
make install-init
# Next, run the following command to install the Nagios sample configuration files
make install-config
# To, install the initialization script which can be used to manage your Nagios service, run the following command
make install-daemoninit
# Run the following command to install and configure the external command file to make Nagios Core to work from the command line:
make install-commandmode
# The following command will install the Apache web server configuration files
make install-webconf
# After all the installations are complete, restart your apache service with:
systemctl restart httpd

nagiosconfigpath

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
# Create nagiosadmin User Account
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin #password 1234

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

wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
tar -zxvf nagios-plugins-2.2.1.tar.gz
cd /tmp/nagios-plugins-2.2.1/
# Compile and install the Nagios plugins.
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install

# 最后将 Nagios 和 Apache 设置为开机启动,并重启 Nagios 服务以使配置修改内容生效,最后打开防火墙,放行HTTP服务:
systemctl enable nagios
systemctl enable httpd
systemctl restart nagios
firewall-cmd --add-service=http --zone=public --permanent
firewall-cmd --reload

# Accessing Nagios Core
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
systemctl start nagios

http://119.255.249.177/nagios/

参考:

https://linuxhostsupport.com/blog/how-to-install-nagios-core-on-centos-7/

https://www.itzgeek.com/how-tos/linux/centos-how-tos/monitor-centos-7-rhel-7-using-nagios-4-0-7.html