Nagios分布式监控系统
2016-06-20 00:00
375 查看
Nagios分布式监控系统
Nagios是一个监视系统运行状态和网络信息的监视系统。Nagios能监视所指定的本地或远程主机以及服务,同时提供异常通知功能等Nagios可运行在Linux/Unix平台之上,同时提供一个可选的基于浏览器的WEB界面以方便系统管理人员查看网络状态,各种系统问题,以及日志等等。一、Nagios的主要功能特点:
监视网络服务 (SMTP, POP3, HTTP, NNTP, PING等)监视主机资源 (进程, 磁盘等)
简单的插件设计可以轻松扩展Nagios的监视功能
服务等监视的并发处理
准备软件包:
Nagios Core nagios-3.2.2.tar.gz
Nagios Plugins nagios-plugins-1.4.15.tar.gz
Nagios Addons nrpe-2.12.tar.gz
二、服务器端安装
查看安装服务器环境(LAMP)#rpm -qa | grep httpd
#rpm -qa | grep php
没有的话安装
# yum -y install gcc glibc glibc-common gd gd-devel php openssl-devel httpd
创建用户:
# useradd -m -s /bin/bash nagios
# groupadd nagios
# usermod -G nagios nagios
# vi /etc/passwd
nagios:x:500:500::/home/nagios:/sbin/nologin
改成:
nagios:x:500:500::/home/nagios:/bin/bash
创建一个用户组名为nagcmd 用于从Web接口执行外部命令。将nagios用户和apache用户都加到这个组中。
因为要用到 CGI 的 Web 监控面板,所以这里我们还要添加一个 nagcmd 组,用于 CGI 执行相关指令。
# /usr/sbin/groupadd nagcmd
# /usr/sbin/usermod -G nagcmd nagios
# /usr/sbin/usermod -a -G nagcmd daemon (因为是编译方式安装的apache,默认是以daemon用户运行)
2.1下载相关的软件包,服务器端需要安装以下三个包,客户端只需要安装后两个插件包:
[root@server ~]#cd /usr/local/src/tarbag/
[root@server tarbag]#wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.2.tar.gz
[root@server tarbag]#wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
[root@server tarbag]#wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
2.2解压并编译安装Nagios:
# tar xvzf nagios-3.2.2.tar.gz
# cd nagios-3.2.2
运行Nagios配置脚本并使用先前开设的用户及用户组:
# ./configure --prefix=/usr/local/nagios --with-command-group=nagcmd
编译Nagios程序包源码:
# make all
安装二进制运行程序、初始化脚本、配置文件样本并设置运行目录权限:
# make install
# make install-init //在/etc/rc.d/init.d安装启动脚本
# make install-config //安装示例配置文件,安装的路径是/usr/local/nagios/etc
# make install-commandmode //配置目录权限
#ls /usr/local/nagios/
bin etc libexec sbin share var
三、安装与配置Apache和Php(服务端)
Apache 和Php 不是安装nagios 所必须的,但是nagios提供了web监控界面,通过web监控界面可以清晰的看到被监控主机、资源的运行状态,因此,安装一个web服务是很必要的。需要注意的是,nagios在nagios3.1.x版本以后,配置web监控界面时需要php的支持。这里我们下载的nagios版本为nagios-3.4.3,因此在编译安装完成apache后,还需要编译php模块,这里选取的php版本为php5.4.10。
A. 安装Apache
# wget http://archive.apache.org/dist/httpd/httpd-2.2.23.tar.gz
# tar zxvf httpd-2.2.23.tar.gz
# cd httpd-2.2.23
# ./configure --prefix=/usr/local/apache2
# make && make install
若出现错误:
则在编译时入加 --with-included-apr 即可解决。
#cp /usr/local/apache2/bin/apachectl /sbin/
启动apache:
#apachectl start
#netstat -tnl | grep 80
#vim /etc/rc.loacl //添加开机启动项
追加/usr/local/apache2/bin/apachectl start
b. 安装Php
# wget http://cn2.php.net/distributions/php-5.4.10.tar.gz
# tar zxvf php-5.4.10.tar.gz
# cd php-5.4.10
# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs
# make && make install
c. 配置apache
找到apache 的配置文件/usr/local/apache2/conf/httpd.conf
找到:
User daemon
Group daemon
修改为
User nagios
Group nagios
然后找到
<IfModule dir_module>
DirectoryIndex index.html </IfModule>
修改为
<IfModule dir_module>
DirectoryIndex index.html index.php </IfModule>
接着增加如下内容:
AddType application/x-httpd-php .php
四、对nagios进行配置
样例配置文件默认安装在这个目录下/usr/local/nagios/etc,这些样例文件可以配置Nagios使之正常运行,只需要做一个简单的修改...用你擅长的编辑器软件来编辑这个/usr/local/nagios/etc/objects/contacts.cfg配置文件,更改email地址nagiosadmin的联系人定义信息中的EMail信息为你的EMail信息以接收报警内容。
每个文件或目录含义如下表所示:
文件名或目录名 用途
cgi.cfg 控制CGI访问的配置文件
nagios.cfg Nagios 主配置文件
resource.cfg 变量定义文件,又称为资源文件,在此文件中定义变量,以便由其他配置文件引用,如$USER1$
objects objects 是一个目录,在此目录下有很多配置文件模板,用于定义Nagios 对象
objects/commands.cfg 命令定义配置文件,其中定义的命令可以被其他配置文件引用
objects/contacts.cfg 定义联系人和联系人组的配置文件
objects/localhost.cfg 定义监控本地主机的配置文件
objects/printer.cfg 定义监控打印机的一个配置文件模板,默认没有启用此文件
objects/switch.cfg 定义监控路由器的一个配置文件模板,默认没有启用此文件
objects/templates.cfg 定义主机和服务的一个模板配置文件,可以在其他配置文件中引用
objects/timeperiods.cfg 定义Nagios 监控时间段的配置文件
objects/windows.cfg 监控Windows 主机的一个配置文件模板,默认没有启用此文件
vi /usr/local/nagios/etc/objects/contacts.cfg
4.1安装nagios插件
#cd ../#tar zxvf nagios-plugins-1.4.16.tar.gz
#cd nagios-plugins-1.4.16
#./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/usr/local/nagios/ //指定安装目录及用户和组
#make;make install
配置httpd ,生成Nagios的Apache配置文件
# cd nagios-3.2.2
# make install-webconf
若这里报错:
请创建文件夹
#mkdir -pv /etc/httpd/conf.d
# /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
# cd sample-config
参考sample-config/httpd.conf配置内容添加到Apache的httpd.conf配置文件中
创建一个nagiosadmin的用户用于Nagios的Apache接口登录。记下你所设置的登录口令,一会儿你会用到它。
切换到/usr/local/apache2/bin既apache2的安装目录下bin文件夹下。
# ./htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
密码:nagiosmonitor
重启Apache服务以使设置生效。
安装NRPE插件,想获取客户机上更为详细的信息,还必须在服务器及客户端上安装NRPE插件。#cd ..
#tar zxvf nrpe-2.14.tar.gz
#cd nrpe-2.14
#./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/usr/local/nagios/
Ubuntu系统上面这句需要添加--with-ssl-lib=/usr/lib/x86_64-linux-gnu
# make all
# make install-plugin;make install-daemon;make install-daemon-config
# ls /usr/local/nagios/libexec/
check_apt check_ftp check_mailq check_overcr check_tcp .......
验证Nagios的样例配置文件
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg如果没有报错,可以启动Nagios服务
启动httpd及nagios服务并验证
#chkconfig --add nagios //设置nagios及http开机自启动
#chkconfig nagios on
#chkconfig httpd on
#service nagios start
#service httpd start
五、客户端安装
#useradd -s /sbin/nologin nagios //添加nagios用户安装nagios-plugins
# tar -zxvf nagios-plugins-1.4.15.tar.gz
# cd nagios-plugins-1.4.15
# ./configure --prefix=/usr/local/nagios
# make
# make install
# chown nagios.nagios /usr/local/nagios/
# chown -R nagios.nagios /usr/local/nagios/libexec/
安装nrpe插件
# tar -zxvf nrpe-2.12.tar.gz
# cd nrpe-2.12
# ./configure --prefix=/usr/local/nagios/
# make all
# make install-plugin 安装check_nrpe这个插件
# make install-daemon 安装daemon
# make install-daemon-config 安装配置文件
如果安装时报错:
checking for SSL headers... configure: error: Cannot find ssl headers
# rpm -qa|grep openssl
openssl-devel-0.9.8e-12.el5_4.6
openssl-0.9.8e-12.el5_4.6
yum install openssl-devel
或者下载:http://www.openssl.org/source/
tar zxvf openssl-1.0.0a.tar.gz
cd openssl-1.0.0a
./config
make
make test
make install
修改客户端配置文件
vi /usr/local/nagios/etc/nrpe.cfg
server_port:5666
allowed_hosts=127.0.0.1,192.168.1.95 //添加服务器端的IP地址
指定nagios监控主机ip,多个ip用逗号分隔,后面的IP地址,是nagios服务端的ip地址,也就是说只允许指定的ip通过nrpe开的端口5666取得本机的信息。
然后修改nrpe.cfg中的command部分。
启动NRPE守护进程:(可以将此命令加入/etc/rc.local,以便开机自动启动)
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
可以将此命令加入/etc/rc.local,以便开机自动启动
echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local
#netstat -utpln |grep nrpe //查看nrpe进程是否已正常启动
#/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
NRPE v2.13 //nrpe测试结果,此结果为nrpe已经正常工作了
然后在nagios监控服务器上测试
#/usr/local/nagios/libexec/check_nrpe -H 192.168.1.77//被监控主机ip
返回信息被监控服务器上安装的NRPE版本:NRPE v2.13 说明可以监控
六、定义监控内容
# vi /usr/local/nagios/etc/nrpe.cfg //定义监控服务器内容command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10 #监控登陆的用户数量
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20 #监控CPU的负载
command[check_sda2]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda2 #监控磁盘利用率,这里的sda2必须是实际的硬盘分区,可使用fdisk –l查
command[check_swap]=/usr/local/nagios//libexec/check_swap -w 20 -c 10 #监控交换空间
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z #监控进程中的僵尸进程
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200 #监控所有进程
注意:command后面括号中的内容就是定义的变量,变量名可以任意指定,只需和服务器配置文件中的一致即可
至此结束
七、其他问题
7.1客户端如果执行语句nagios_client.sh时报错则vim进去看下,输入:set ff看下文件类型是否是unix,如果不是则:set ff=unix,然后执行脚本。
7.2后期添加监控主机的方式:
所需软件nagios-plugins、nrpe
被监控端安装nagios-plugins
# tar xf nagios-plugins-1.5.tar.gz
# useradd -s /sbin/nologin nagios
# cd nagios-plugins-1.5
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make all
被监控端安装nrpe
# tar xf nrpe-2.15.tar.gz
# cd nrpe-2.15
# ./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-command-args --enable-ssl
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d //启动nrpe服务
或者 # vim /etc/init.d/nrped
生成启动脚本nrpe
#!/bin/bash
# chkconfig: 2345 88 12
# description: NRPE DAEMON
NRPE=/usr/local/nagios/bin/nrpe
NRPECONF=/usr/local/nagios/etc/nrpe.cfg
case "$1" in
start)
echo -n "Starting NRPE daemon..."
$NRPE -c $NRPECONF -d
echo " done."
;;
stop)
echo -n "Stopping NRPE daemon..."
pkill -u nagios nrpe
echo " done."
;;
restart)
$0 stop
sleep 2
$0 start
;;
*)
echo "Usage: $0 start|stop|restart"
;;
esac
exit 0
# /etc/init.d/nrped start
在主监控添加nrpe的定义
7.3增加主机监控配置项
方案一、
# vim /etc/nagios/objects/131.cfg
define host{
use linux-server
host_name 192.168.235.131
alias 北师大WEB
address 192.168.235.131
}
define service{
use generic-service
host_name 192.168.235.131
service_description load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name 192.168.235.131
service_description PING
check_command check_ping!100.0,20%!200.0,50%
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 192.168.235.131
service_description FTP
check_command check_ftp!21
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 192.168.235.131
service_description SSH
check_command check_ssh
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 192.168.235.131
service_description HTTP
check_command check_http
max_check_attempts 5
normal_check_interval 1
}
# vim /etc/nagios/nagios.cfg
添加一行cfg_file=/etc/nagios/objects/131.cfg
# service nagios reload 重载刷新nagios-web界面可以看到添加的被监控主机
好了,此处nagios已经被安装好了,而且被监控节点也已经上线了!
方案二、
#vim hosts.cfg //添加
define host{
host_name download
alias 下载服务器
address 123.56.159.121
contact_groups admins
#use host-pnp
check_command check-host-alive
max_check_attempts 2 #检测到问题后立即报警,不重试。
notification_interval 5 #notification_interval定义为5,报警发送5次
notification_period 24x7
notification_options d,u,r
}
define host{
host_name fb_gridfs_4
alias 发布_文件系统_4
address 123.56.226.226
contact_groups admins
#use host-pnp
check_command check-host-alive
max_check_attempts 2 #检测到问题后立即报警,不重试。
notification_interval 5 #notification_interval定义为5,报警发送5次
notification_period 24x7
notification_options d,u,r
}
#vim hostsgroup.cfg //添加
define hostgroup{
hostgroup_name fb_gridfs ; The name of the hostgroup
alias 发布环境文件系统 ; Long name of the group
members download,fb_gridfs_4, ; Comma separated list of hosts that belong to this group
}
#vim service.cfg //添加
define service{
use service ; Name of service template to use
host_name fb_gridfs_4
service_description 根分区
check_command check_nrpe!check_/
}
define service{
use service ; Name of service template to use
host_name fb_gridfs_4
service_description data分区
check_command check_nrpe!check_/data
}
define service{
use service ; Name of service template to use
host_name fb_gridfs_4
service_description nginx_80端口
check_command check_port!123.56.226.226!80
}
define service{
use service ; Name of service template to use
host_name fb_gridfs_4
service_description mongo_6080端口
check_command check_port!10.171.112.13!6080
}
重启nagios和httpd
相关链接: CentOS搭建Nagios监控http://www.opstool.com/article/236
下载程序最新版本的nagios-cn源程序(当前是3.0.3)和nagios插件(当前是1.4.11版本)源程序下载
wget http://osdn.dl.sourceforge.net/sourceforge/nagios-cn/nagios-cn-3.0.3.tar.gz wget http://osdn.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.11.tar.gz
相关文章推荐
- ios混合音频、设置渐变音量、画外音音量
- IOS视图控制对象生命周期
- iOS开发质量提高速成篇
- CocoadPods的详解之二:使用CocoaPods管理iOS的第三方类库
- iOS利用touchesMoved事件来实现view的拖拽
- iOS事件的传递过程及其基本原理简单的解释
- iOS事件传递的应用(两个例子)
- iOS文件上传遇到的坑
- 并发模型互斥和内存模型
- 【转】iOS中16进制转10进制
- ios osx 多线程编程指南
- iOS学习之事件处理的原理
- iOS新闻客户端开发教程8-加载更多和新闻详情
- iOS开发,javascript直接调用oc代码而非通过改变url回调方式
- iOS屏幕旋转学习笔记
- iOS开发之Socket
- Nagios 4.0.1安装
- 个人简历
- iOS是怎么被越狱的(UAF漏洞详解)
- iOS打包ipa步骤