nagios简介
`Nagios`是一款开源的电脑系统和网络监视工具,能有效监控`Windows`、`Linux`和`Unix`的主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。
`Nagios`原名为`NetSaint`,由`Ethan Galstad`开发并维护至今。`NAGIOS`是一个缩写形式: "`Nagios Ain't Gonna Insist On Sainthood`" `Sainthood` 翻译为圣徒,而"`Agios`"是"`saint`"的希腊表示方法。`Nagios`被开发在`Linux`下使用,但在`Unix`下也工作得非常好。
主要功能:
1.网络服务监控(SMTP、POP3、HTTP、NNTP、ICMP、SNMP、FTP、SSH,mysql
)
2.主机资源监控(CPU load、disk usage、system logs
),也包括Windows
主机(使用NSClient++ plugin
)可以指定自己编写的Plugin
通过网络收集数据来监控任何情况(温度、警告……)可以通过配置Nagios远程执行插件远程执行脚本
3.远程监控支持SSH
或SSL
加通道方式进行监控简单的plugin设计允许用户很容易的开发自己需要的检查服务,支持很多开发语言(shell scripts、C++、Perl、ruby、Python、PHP、C#
等)包含很多图形化数据Plugins(Nagiosgraph、Nagiosgrapher、PNP4Nagios
等)
4.可并行服务检查能够定义网络主机的层次,允许逐级检查,就是从父主机开始向下检查
当服务或主机出现问题时发出通告,可通过email, pager, sms
或任意用户自定义的plugin进行通知
5、能够自定义事件处理机制重新激活出问题的服务或主机
• 自动日志循环
• 支持冗余监控
• 包括Web界面可以查看当前网络状态,通知,问题历史,日志文件等
Nagios
工作原理
Nagios
的功能是监控服务和主机,但自身并没有该功能,所有的监控、检测功能都是通过插件来完成。
Nagios
周期性调用插件检测服务器状态,并维持一个队列,所有插件返回状态信息都进入队列,Nagios
每次从队首开始读取信息,并把状态通过web
显示。
Nagios
提供了许多插件,利用这些插件可以监控服务状态。安装完成后,在nagios主目录下/libexec放有nagios自带所有插件,如check_disk
检查磁盘空间插件,check_load
检查CPU
负载等等。每一个插件可运行./check_xxx –h
查看其使用方法和功能
工作原理
插件
[root@c7-41 ~]# ls /usr/local/nagios/libexec/
check_apt check_ide_smart check_ntp_peer check_tcp
check_breeze check_ifoperstatus check_ntp_time check_time
check_by_ssh check_ifstatus check_nwstat check_udp
check_clamd check_imap check_oracle check_ups
check_cluster check_ircd check_overcr check_uptime
check_dhcp check_load check_ping check_users
check_disk check_log check_pop check_wave
check_disk_smb check_mailq check_procs negate
check_dummy check_mrtg check_real urlize
check_file_age check_mrtgtraf check_rpc utils.pm
check_flexlm check_nagios check_sensors utils.sh
check_ftp check_nntp check_smtp
check_http check_nt check_ssh
check_icmp check_ntp check_swap
环境准备centos 7.7
1.安装基础编译环境软件
[root@c7-41 ~]# yum install php gcc glibc glibc-common unzip gd gd-devel –y
2.解决perl编译问题
[root@c7-41 ~]# echo "export LC_ALL=C" >> /etc/profile
3.解决时间同步问题
[root@c7-41 ~]# crontab -e
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
4.创建nagios用户及相关的组
[root@c7-41 ~]# useradd -m nagios
[root@c7-41 ~]# passwd nagios
Changing password for user nagios.
New password: #123456
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: #123456
passwd: all authentication tokens updated successfully.
[root@c7-41 ~]# groupadd nagcmd
[root@c7-41 ~]# usermod -a -G nagcmd nagios
[root@c7-41 ~]# usermod -a -G nagcmd apache
5.Nagios服务端安装
#下载nagios服务端软件及插件
[root@c7-41 ~]# wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.2.1.tar.gz
[root@c7-41 ~]# wget https://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz
6.源码编译安装nagios以及相关插件
[root@c7-41 ~]# tar zxf nagios-4.2.1.tar.gz
[root@c7-41 ~]# cd nagios-4.2.1/
[root@c7-41 nagios-4.2.1]# ./configure --with-command-group=nagcmd
[root@c7-41 nagios-4.2.1]# make all
make install
make install-init
make install-config
make install-webconf
make install-commandmode
7.安装并编译nagios插件
[root@c7-41 nagios-4.2.1]# cd
[root@c7-41 ~]# tar -zxvf nagios-plugins-2.1.2.tar.gz
[root@c7-41 ~]# cd nagios-plugins-2.1.2
[root@c7-41 nagios-plugins-2.1.2]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
[root@c7-41 nagios-plugins-2.1.2]# make
[root@c7-41 nagios-plugins-2.1.2]# make install
8.配置邮件联系人信息
配置文件在目录 /usr/local/nagios/etc
下,通过这些基础配置信息可以直接启动 Nagios
,现在唯一需要更改的是联系人信息,可以编辑 /usr/local/nagios/etc/objects/contacts.cfg
,将联系人邮件地址换成你自己的
[root@c7-41 ~]# cat /usr/local/nagios/etc/objects/contacts.cfg | grep email
email 154766385@qq.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
#1.安装sedmail来发送邮件
[root@nagios-server ~]#
yum install sendmail sendmail-cf
#2.安装mailx来发送邮件
[root@c7-41 nagios-plugins-2.1.2]# yum -y install mailx
[root@c7-41 nagios-plugins-2.1.2]# vim /etc/mail.rc
set from=154******@qq.com
set smtp=smtp.qq.com
set smtp-auth-user= 154******@qq.com
set smtp-auth-password=izuhxpmrnhprbgdc
set smtp-auth=login
9.为nagios创建登录web界面的密码
[root@c7-41 nagios-plugins-2.1.2]# htpasswd -c /usr/local/nagios/etc/htpasswd.users admin
New password: #123456
Re-type new password: #123456
Adding password for user admin
10.设置nagios服务开机自启并启动ngios
[root@c7-41 nagios-plugins-2.1.2]# systemctl start httpd
[root@c7-41 nagios-plugins-2.1.2]# systemctl start nagios
[root@c7-41 nagios-plugins-2.1.2]# systemctl enable nagios
[root@c7-41 nagios-plugins-2.1.2]# chkconfig --list |grep nagios
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
nagios 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@c7-41 nagios-plugins-2.1.2]# ps -ef |grep nagios
nagios 24404 1 0 11:01 ? 00:00:00 /usr/local/nagios/bin/nagio -d /usr/local/nagios/etc/nagios.cfg
nagios 24406 24404 0 11:01 ? 00:00:00 /usr/local/nagios/bin/nagio --worker /usr/local/nagios/var/rw/nagios.qh
nagios 24407 24404 0 11:01 ? 00:00:00 /usr/local/nagios/bin/nagio --worker /usr/local/nagios/var/rw/nagios.qh
nagios 24408 24404 0 11:01 ? 00:00:00 /usr/local/nagios/bin/nagio --worker /usr/local/nagios/var/rw/nagios.qh
nagios 24409 24404 0 11:01 ? 00:00:00 /usr/local/nagios/bin/nagio --worker /usr/local/nagios/var/rw/nagios.qh
nagios 24410 24404 0 11:01 ? 00:00:00 /usr/local/nagios/bin/nagio -d /usr/local/nagios/etc/nagios.cfg
root 24412 1740 0 11:01 pts/0 00:00:00 grep --color=auto nagios
11.检查nagios主配置文件语法是否正确
#我们只需关注以下两项参数即可,Total Errors 为0就说明语法正确。
#Total Warnings: 0
#Total Errors: 0
[root@c7-41 nagios-plugins-2.1.2]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 4.2.1
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 09-06-2016
License: GPL
Website: https://www.nagios.org
Reading configuration data...
Read main config file okay...
Read object config files okay...
Running pre-flight check on configuration data...
Checking objects...
Checked 8 services.
Checked 1 hosts.
Checked 1 host groups.
Checked 0 service groups.
Checked 1 contacts.
Checked 1 contact groups.
Checked 24 commands.
Checked 5 time periods.
Checked 0 host escalations.
Checked 0 service escalations.
Checking for circular paths...
Checked 1 hosts
Checked 0 service dependencies
Checked 0 host dependencies
Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...
Total Warnings: 0
Total Errors: 0
Things look okay - No serious problems were detected during the pre-flight check
12.浏览器访问web
错误
出现 找不到 文件 我怀疑是防火墙规则
[root@c7-41 ~]# systemctl start httpd
[root@c7-41 ~]# netstat -ntlp | grep 80
tcp6 0 0 :::80 :::* LISTEN 25027/httpd
[root@c7-41 ~]# firewall-cmd --zone=public --list-ports
[root@c7-41 ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@c7-41 ~]# firewall-cmd --reload
success
[root@c7-41 ~]# firewall-cmd --zone=public --list-port
80/tcp