Nagios是一个监视系统运行状态和网络信息的监视系统。Nagios能监视所指定的本地或远程主机以及服务,同时提供异常通知功能等。 Nagios可运行在Linux/Unix平台之上,同时提供一个可选的基于浏览器的WEB界面以方便系统管理人员查看网络状态,各种系统问题,以及日志等等。
Nagios的主要功能特点:
- 监视网络服务 (SMTP, POP3, HTTP, NNTP, PING等)
- 监视主机资源 (进程, 磁盘等)
- 简单的插件设计可以轻松扩展Nagios的监视功能
- 服务等监视的并发处理
- 错误通知功能 (通过email, pager, 或其他用户自定义方法)
- 可指定自定义的事件处理控制器
- 可选的基于浏览器的WEB界面以方便系统管理人员查看网络状态,各种系统问题,以及日志等等
- 可以通过手机查看系统监控信息
官网: http://www.nagios.org/
截图: http://www.nagios.org/about/screenshots
Nagios在线帮助中文版: http://nagios-cn.sourceforge.net/nagios-cn/index.html
1.软件安装
1.1. 环境
以下安装所需软件都可以从各自的官网进行下载:
Nagios&Nagios Plugins: http://www.nagios.org/download
Apache: http://www.apache.org/dyn/closer.cgi
php: http://www.php.net/downloads.php
1.2. 安装Nagios
shell>useradd nagios ##用户nagios安装和运行
shell>tar -xzvf nagios-3.4.1.tar.gz
shell>cd nagios
shell>./configure –prefix=/usr/local/nagios
shell>make all
shell>make install ##安装主要的程序、CGI及HTML文件
shell>make install-commandmode ##给外部命令访问nagios配置文件的权限
shell>make install-config ##把配置文件的例子复制到nagios的安装目录,这里是/usr/local/nagios/etc
shell>make install-init ##[可选]把nagios做成一个运行脚本,放到/etc/[rc.d/]init.d目录,方便启停和查看状态。
验证是否正常完成,切换至/usr/local/nagios目录下,看到如下目录基本可以确定正常完成安装:
各目录功能如下:
bin | Nagios执行程序所在目录,这个目录只有一个文件nagios |
etc | Nagios配置文件位置,初始安装完后,只有几个*.cfg-sample文件 |
sbin | Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录 |
share | Nagios网页文件所在的目录 |
var | Nagios日志文件、spid、lock等文件所在的目录 |
var/archives | Nagios日志自动归档目录 |
var/rw | 用来存放外部命令文件的目录 |
libexec | Nagios插件目录,未安装插件时为空 |
ps:
安装Nagios中文化插件(建议直接使用英文版本)
中文插件下载地址:http://sourceforge.net/projects/nagios-cn/files/
下载对应nagios版本的中文插件,然后开始安装:
shell>tar -xvfz nagios-cn-3.2.0.tar.bz2
shell>cd nagios-cn-3.2.0
shell>./configure
shell>make all
shell>make install
1.3. 安装nagios的插件
没有插件,nagios将什么作用也没有,插件也是nagios扩展功能的强大武器,除了下载常用的插件外,我们还可以根据实际要求编写自己的插件。
插件与nagios之间的版本关联不大。
shell>tar -xzvf nagios-plugins-1.4.16.tar.gz
shell>cd nagios-plugins-1.4.16
shell>./configure –prefix=/usr/local/nagios ##注意路径,会将插件生成到nagios/libexec目录下
shell>make
shell>make install
1.4. 安装web服务器apache
Web服务不是nagios所必须的,但是如果nagios没有web,查看监控对象的对象将是非常费事和没有趣味的事情(只有通过查看nagios的日志来判断状态)。
shell>tar -xzvf httpd-2.2.9.tar.gz
shell>cd httpd-2.2.9
shell>./configure –prefix=/usr/local/apache
shell>make
shell>make install
shell>./usr/local/apache/bin/apachectl –t ##检查一下apache是否正确安装
通过web界面进行配置项的操作,需要web服务器进程能够访问nagios目录下的一些文件,web服务器的默认运行用户为apache,而nagios目录的权限拥有者为nagios,默认两者没有任何关系,所以往往会报如下的错误:
Error: Could not open command file ‘/usr/local/nagios/var/rw/nagios.cmd’ for update!
The permissions on the external command file and/or directory may be incorrect. Read the FAQs on how to setup proper permissions.
An error occurred while attempting to commit your command for processing.
处理的办法是:将web服务器的运行用户apache加入到nagios组中,由于nagios中相关目录都是同组用户可以操作的,就可以解决这个问题:
shell>usermod -G nagios apache
如果还出现如:
Error: Could not stat() command file ‘/var/lib/nagios2/rw/nagios.cmd’!
The external command file may be missing, Nagios may not be running, and/or Nagios may not be checking external commands.
如上报错信息,尝试将针对rw目录执行chmod o+w rw,添加权限。如果发现nagios.cmd不存在,尝试/etc/init.d/nagios restart。
1.5. 安装php
因为nagios的web的后台源码是php,所以需要搭建php运行环境。
shell>tar -xzvf php-5.3.15.tar.gz
shell>cd php-5.3.15
shell>./configure –with-gd –with-zlib –prefix=/usr/local/php –with-apxs2=/usr/local/apache/bin/apxs
shell>make
shell>make install
shell>ls -lrt /usr/local/apache/modules/libphp5.so ##检查是否已产生此文件
2.配置
2.1. 配置前准备
shell>useradd nagios ##添加系统用户nagios,来运行nagios系统。
shell>chown –R nagios.nagios /usr/local/nagios ##更改目录属主、属组
2.2. 配置详解
* 步骤1 修改apache的配置文件httpd.conf,所在路径:/usr/local/apache/conf/httpd.conf
指定web访问路径以及对nagios的目录进行用户验证,只有合法的授权用户才可以访问nagios的页面文件。
在最后添加如下内容:
ScriptAlias /nagios/cgi-bin/ "/usr/local/nagios/sbin/" <Directory "/usr/local/nagios/sbin/"> AuthType Basic Options ExecCGI AllowOverride None Order allow,deny Allow from all AuthName "Nagios Access" AuthUserFile /usr/local/nagios/etc/htpasswd Require valid-user </Directory> Alias /nagios/ "/usr/local/nagios/share/" <Directory "/usr/local/nagios/share/"> AuthType Basic Options None AllowOverride None Order allow,deny Allow from all AuthName "nagios Access" AuthUserFile /usr/local/nagios/etc/htpasswd Require valid-user </Directory>
另外,修改配置文件使监听8081端口:”Listen 8081″
* 步骤2 生成用户验证文件
即步骤1中的指定用户验证的信息
shell>/usr/local/apache/bin/htpasswd –c /usr/local/nagios/etc/htpasswd nagiosadmin
以上命令会在文件/usr/local/nagios/etc/htpasswd写入一行,第一个字段是刚生成的用户名,第二个是加密后的密码。
如果还要添加更多的用户,执行命令 htpasswd 就不需要选项 “-c”,否则就会覆盖所有已经生成的行。
生成合法用户nagiosadmin,会确认两次密码,这里设置密码为abc。
* 步骤3 web服务中配置php
修改apache的配置文件httpd.conf,路径依然是:/usr/local/apache/conf/httpd.conf
在最后添加:
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
* 步骤4 启动apache
shell>/usr/local/apache/bin/apachectl –t ##检查apache配置文件是否有语法错误,无误后处理下一步
shell>/usr/local/apache/bin/apachectl start ##启动apache
* 步骤5 web访问