【Web 集群实战】22_Nagios
标签(空格分隔): Web集群实战
文章目录
1. Nagios 监控工具及原理
1.1 Nagios 特点
- 监控网络服务(HTTP、TCP、PING、SMTP、POP3等)
- 监控主机资源(CPU、负载、I/O 状况、虚拟及正式内存及磁盘利用率等)
- 简单的插件设计模式使得用户可以很方便地定制符合自己服务的检测方法
- 并行服务检查机制
- 具备定义网络分层结构的能力,用 “parent” 主机定义来表达网络间的关系,这种关系可被用来发现和明晰主机宕机或不可达的状态
- 当服务或主机问题产生与解决后将及时通报联系人(mail/im/sms/sound)
- 具备定义事件句柄功能,它可以在主机或服务的事件发生时获取更多问题定位
- 自动的日志回滚
- 可以支持并实现对主机的冗余监控(支持分布式监控)
- 可选的 Web 界面用于查看当前的网络状态、通知和故障历史、日志文件等
1.2 Nagios 监控构成
- Nagios 监控一般由一个主程序(Nagios)、一个插件程序(Nagios-plugins)和一些可选的附加程序(NRPE、NSClient++、NSCA 和 NDOUtils)等组成。
2. Nagios 服务器端安装
2.1 Nagios 安装准备
(1)准备 3 台服务器或 VM 虚拟机
HOSTNAME | IP | 说明 |
---|---|---|
nagios-server | 192.168.2.151 | Nagios 服务器端 |
web001 | 192.168.2.152 | 被监控的客户端服务器 |
web002 | 192.168.2.144 | 被监控的客户端服务器 |
(2)设置 yum 安装源
[root@nagios-server ~]# cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
[root@nagios-server ~]# wget /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
(3) 解决 Perl 软件编译问题
[root@nagios-server ~]# echo 'export LC_ALL=C'>> /etc/profile
[root@nagios-server ~]# tail -1 /etc/profile
export LC_ALL=C
[root@nagios-server ~]# source /etc/profile
[root@nagios-server ~]# echo $LC_ALL
C
(4)关闭 Nagios Server 端防火墙及 SELinux
[root@nagios-server ~]# systemctl disable firewalld.service
[root@nagios-server ~]# systemctl stop firewalld.service
[root@nagios-server ~]# systemctl status firewalld.service
* firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@nagios-server ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# 修改配置文件可使配置永久生效,需重启系统
[root@nagios-server ~]# cat /etc/selinux/config|grep SELINUX=disabled
SELINUX=disabled
[root@nagios-server ~]# getenforce
Disabled
(5)解决系统时间同步问题
[root@nagios-server ~]# echo '#time sync by nagios-server at 2018-09-16' >>/var/spool/cron/root
[root@nagios-server ~]# echo '*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2&1' >> /var/spool/cron/root
[root@nagios-server ~]# crontab -l
#time sync by nagios-server at 2018-10-13
*/2 * * * * /usr/sbin/ntpdate ntp1.aliyun.com
>/dev/null 2>&1
(6) 安装 Nagios 服务器端所需软件包(LAMP 环境)
[root@nagios-server ~]# yum install gcc glibc glibc-common -y
[root@nagios-server ~]# yum install gd gd-devel -y
[root@nagios-server ~]# yum install httpd php php-gd -y
[root@nagios-server ~]# rpm -qa httpd php
httpd-2.4.6-80.el7.centos.1.x86_64
php-5.4.16-45.el7.x86_64
MySQL 安装参见【Web 集群实战】12_LNMP 之 MySQL 的安装与配置
(7)创建 Nagios 服务器端需要的用户及组
[root@nagios-server ~]# /usr/sbin/useradd nagios
[root@nagios-server ~]# /usr/sbin/useradd apache -M -s /sbin/nologin
useradd: user 'apache' already exists
[root@nagios-server ~]# /usr/sbin/groupadd nagcmd
[root@nagios-server ~]# /usr/sbin/usermod -a -G nagcmd nagios
[root@nagios-server ~]# /usr/sbin/usermod -a -G nagcmd apache
[root@nagios-server ~]# id -n -G nagios
nagios nagcmd
[root@nagios-server ~]# id -n -G apache
apache nagcmd
[root@nagios-server ~]# groups nagios
nagios : nagios nagcmd
[root@nagios-server ~]# groups apache
apache : apache nagcmd
(8)下载所需软件包
[root@nagios-server ~]# cd /home/ylt/tools/
[root@nagios-server tools]# mkdir nagios -p
[root@nagios-server tools]# cd nagios/
[root@nagios-server nagios]# wget https://sourceforge.net/projects/nagios/files/nagios-3.x/nagios-3.5.1/nagios-3.5.1.tar.gz/download
[root@nagios-server nagios]# ll
total 1724
-rw-r--r-- 1 root root 1763584 Aug 31 2013 nagios-3.5.1.tar.gz
[root@nagios-server nagios]# wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz#_ga=2.27512634.762344303.1539496511-137884230.1539496511
[root@nagios-server nagios]# ll nagios-plugins-2.2.1.tar.gz
-rw-r--r-- 1 root root 2728818 Apr 20 2017 nagios-plugins-2.2.1.tar.gz
[root@nagios-server nagios]# wget https://sourceforge.net/projects/nagios/files/nrpe-2.x/nrpe-2.12/nrpe-2.12.tar.gz/download
[root@nagios-server nagios]# ll nrpe-2.12.tar.gz
-rw-r--r-- 1 root root 405725 Mar 11 2008 nrpe-2.12.tar.gz
(9)启动 LAMP 环境的 HTTP 服务
[root@nagios-server tools]# systemctl start httpd
[root@nagios-server tools]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 1352 root 4u IPv6 21968 0t0 TCP *:http (LISTEN)
httpd 1353 apache 4u IPv6 21968 0t0 TCP *:http (LISTEN)
httpd 1354 apache 4u IPv6 21968 0t0 TCP *:http (LISTEN)
httpd 1355 apache 4u IPv6 21968 0t0 TCP *:http (LISTEN)
httpd 1356 apache 4u IPv6 21968 0t0 TCP *:http (LISTEN)
httpd 1357 apache 4u IPv6 21968 0t0 TCP *:http (LISTEN)
2.2 安装 Nagios 服务器端
[root@nagios-server nagios]# tar xf nagios-3.5.1.tar.gz
[root@nagios-server nagios]# ll
total 1728
drwxrwxr-x 15 root root 4096 Aug 31 2013 nagios
-rw-r--r-- 1 root root 1763584 Aug 31 2013 nagios-3.5.1.tar.gz
[root@nagios-server nagios]# cd nagios/
[root@nagios-server nagios]# ./configure --with-command-group=nagcmd
Review the options above for accuracy. If they look okay,
type 'make all' to compile the main program and CGIs.
[root@nagios-server nagios]# make all
Enjoy.
[root@nagios-server nagios]# make install
make install-init
- This installs the init script in /etc/rc.d/init.d
make install-commandmode
- This installs and configures permissions on the
directory for holding the external command file
make install-config
- This installs sample config files in /usr/local/nagios/etc
make[1]: Leaving directory `/home/ylt/tools/nagios/nagios'
[root@nagios-server nagios]# make install-init
*** Init script installed ***
[root@nagios-server nagios]# make install-commandmode
*** External command directory configured ***
[root@nagios-server nagios]# make install-config
*** Config files installed ***
(1) 安装 Nagios Web 配置文件及创建登录用户
- 安装 Nagios Web 配置文件
[root@nagios-server nagios]# make install-webconf
*** Nagios/Apache conf file installed ***
- 创建登录用户
[root@nagios-server nagios]# cd ..
[root@nagios-server nagios]# htpasswd -bc /usr/local/nagios/etc/htpasswd.users nagios nagios
Adding password for user ylt
[root@nagios-server nagios]# cat /usr/local/nagios/etc/htpasswd.users
nagios:$apr1$l7AGreUZ$LUP7tkFCcLoJ21cACkOvU/
- 重新加载 Apache 服务
[root@nagios-server nagios]# systemctl reload httpd
(2)添加监控报警信息接收的 Email 地址
[root@nagios-server nagios]# sed -i 's#nagios@localhost#yanglt7@163.com#g' /usr/local/nagios/etc/objects/contacts.cfg
[root@nagios-server nagios]# sed -n '35p' /usr/local/nagios/etc/objects/contacts.cfg
email yanglt7@163.com ;
- 使用第三方邮件服务商提供的服务发送邮件
[root@nagios-server nagios]# tail -2 /etc/mail.rc
set from=1622320046@qq.com
smtp=smtp.qq.com smtp-auth-user=1622320046 smtp-auth-password=password smtp-auto=login
(3)解决 Web 端用户 nagios 没有被许可查看服务资源的问题,将 nagiosadmin 改