分类:
2010-03-02 18:30:04
Nagios安装配置
确定系统已经安装了gd库
# rpm -qa |grep gd
gd-devel-2.0.33-9.4.el5_1.1
gd-2.0.33-9.4.el5_1.1
建立nagios账号
# groupadd nagcmd
# useradd -m nagios
# usermod -G nagcmd nagios
# usermod -G nagcmd apache
# tar zvxf nagios-3.2.0.tar.gz
# cd nagios-3.2.0
# ./configure --prefix=/usr/local/nagios --with-command-group=nagcmd
# make all
使用make install来安装主程序,CGI和HTML文件
# make install
使用make install-init在/etc/rc.d/init.d安装启动脚本
# make install-init
使用make install-cofig来安装示例配置文件,安装的路径是/usr/local/nagios/etc.
# make install-config
使用make install-commandmode来配置目录权限
# make install-commandmode
安装nagios插件
# tar zvxf nagios-plugins-1.4.14.tar.gz
# cd nagios-plugins-1.4.14
默认安装目录就是/usr/local/nagios
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --perfix=/usr/local/nagios
# make
# make install
确定安装了apache
# tar jvxf httpd-2.2.14.tar.bz2
# cd httpd-2.2.14/srclib/apr
# ./configure --prefix=/usr/local/apr
# make;make install
# cd ../apr-util/
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
# cd ../../
# ./configure --prefix=/usr/local/apache \
--enable-so \
--enable-ssl \
--enable-mods-shared=all \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util
# make
# make install
创建apache目录验证文件
# /usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password: (输入密码)
Re-type new password: (再输入一次密码)
Adding password for user nagiosadmin
修改Apache配置文件
# vi /usr/local/apache/conf/nagios.conf
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
# SSLRequireSSL
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
Alias /nagios "/usr/local/nagios/share"
# SSLRequireSSL
Options None
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
# vi /usr/local/apache/conf/httpd.conf
在最后增加下面的行
Include conf/nagios.conf
安装php
检查需要的软件,也可以使用源码包安装
# rpm -q libxml2 libxslt curl libgcrypt gd freetype libjpeg libpng zlib libgcrypt-devel libjpeg-devel libpng-devel freetype-devel libxslt-devel libmcrypt libmcrypt-devel
libxml2-2.6.26-2.1.2.8
libxslt-1.1.17-2.el5_2.2
curl-7.15.5-2.1.el5_3.5
libgcrypt-1.4.4-5.el5
gd-2.0.33-9.4.el5_1.1
freetype-2.2.1-21.el5_3
libjpeg-6b-37
libpng-1.2.10-7.1.el5_3.2
zlib-1.2.3-3
libgcrypt-devel-1.4.4-5.el5
libjpeg-devel-6b-37
libpng-devel-1.2.10-7.1.el5_3.2
freetype-devel-2.2.1-21.el5_3
libxslt-devel-1.1.17-2.el5_2.2
package libmcrypt is not installed
package libmcrypt-devel is not installed
# yum install libmcrypt libmcrypt-devel –y
# tar jvxf php-5.2.13.tar.bz2
# cd php-5.2.13
# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs \
--with-zlib-dir \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-pdo-mysql=/usr/local/mysql \
--with-curl --enable-ftp \
--with-libxml-dir=/usr/lib \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-xsl \
--with-gd --enable-mbstring --with-libexpat-dir=/usr/lib --enable-soap \
--enable-bcmath --with-mcrypt
# make
# make install
# cp php.ini-dist /usr/local/php/lib/php.ini
# vi /usr/local/apache/conf/php.conf
#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
LoadModule php5_module modules/libphp5.so
#
# Cause the PHP interpreter to handle files with a .php extension.
#
AddHandler php5-script .php
AddType text/html .php
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php
#
# Uncomment the following line to allow PHP to pretty-print .phps
# files as PHP source code:
#
#AddType application/x-httpd-php-source .phps
# vi /usr/local/apache/conf/httpd.conf
在最后加入下面的行,并删除LoadModule php的行
Include conf/php.conf
将里面的email地址改为自己的email地址。
#vi /usr/local/nagios/etc/objects/contacts.cfg
验证Nagios的配置文件是否正确
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
验证完后删除日志
# rm -r /usr/local/nagios/var/nagios.log
启动apache,nagios
# /usr/local/apache/bin/apachectl start
# /etc/init.d/nagios start
打开IE,访问url
输入用户名:nagiosadmin 密码:123456
安装nrpe插件,用来监控Linux机器
# tar zvxf nrpe-2.12.tar.gz
# cd nrpe-2.12
# ./configure
# make all
在Nagios服务器端只要安装nrpe监控插件就行
# make install-plugin
定义check_nrpe命令
# vi /usr/local/nagios/etc/objects/commands.cfg
# ‘check_nrpe ‘ command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
在被监控服务器(Linux/unix)上安装Nagios-plugins和nrpe
# tar zvxf nagios-plugins-1.4.14.tar.gz
# cd nagios-plugins-1.4.14
# ./configure
# make
# make install
# useradd -m nagios
# chown -R nagios.nagios /usr/local/nagios
# tar zvxf nrpe-2.12.tar.gz
# cd nrpe-2.12
# ./configure
# make all
安装nrpe插件,本监控端可以不装
# make install-plugin
安装nrpe守护进程
# make install-daemon
安张nrpe配置文件
# make install-daemon-config
修改nrpe配置文件,允许Nagios监控服务器(192.168.1.240)监控
# vi /usr/local/nagios/etc/nrpe.cfg
多台机器用逗号隔开
allowed_hosts=127.0.0.1,192.168.1.240
建立nrpe启动文件
# vi /etc/init.d/nrpe
#!/bin/sh
#
# Created 2000-01-03 by jaclu@grm.se
#
# nrpe This shell script takes care of starting and stopping
# nrpe.
#
# chkconfig: 2345 80 30
# description: nrpe is a daemon for a remote nagios server, \
# running nagios plugins on this host.
# processname: nrpe
# config: /usr/local/nagios/etc/nrpe.cfg
# Source function library
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
elif [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/functions ]; then
. /etc/rc.d/functions
fi
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
NrpeBin=/usr/local/nagios/bin/nrpe
NrpeCfg=/usr/local/nagios/etc/nrpe.cfg
LockFile=/var/lock/subsys/nrpe
# See how we were called.
case "$1" in
start)
# Start daemons.
echo -n "Starting nrpe: "
daemon $NrpeBin -c $NrpeCfg -d
echo
touch $LockFile
;;
stop)
# Stop daemons.
echo -n "Shutting down nrpe: "
killproc nrpe
echo
rm -f $LockFile
;;
restart)
$0 stop
$0 start
;;
status)
status nrpe
;;
*)
echo "Usage: nrpe {start|stop|restart|status}"
exit 1
esac
exit 0
# chmod +x /etc/init.d/nrpe
# /etc/init.d/nrpe start
# 检查nrpe是否安装正常
# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.12
如果有防火墙应该开放5666端口:
iptables -I eth0 -p tcp -m tcp -dport 5666 -j ACCEPT
或者
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5666 -j ACCEPT
# 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
#command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
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
下面的命令是自己定义的:
# 监控交换分区的使用情况,使用超过20%时为警告状态,超过10%时为严重状态
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
# 监控根分区磁盘使用情况
command[check_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
主机操作
自定义一个目录,里面放置自己的服务器的配置文件
# mkdir –p /usr/local/nagios/etc/servers
# chown nagios:nagios /usr/local/nagios/etc/servers/
在nagios.cfg配置文件中开启对/usr/local/nagios/etc/servers/中配置文件的引用。
# vi /usr/local/nagios/etc/nagios.cfg
cfg_dir=/usr/local/nagios/etc/servers
# vi /usr/local/nagios/etc/servers/241.cfg
###############################################################################
###############################################################################
#
# HOST DEFINITION
#
###############################################################################
###############################################################################
# Define a host for the local machine
define host{
use linux-server ; Name of host template to use
; This host definition will inherit all variables that are defined
; in (or inherited by) the linux-server host template definition.
host_name centos.241
alias 241
address 192.168.1.241
}
###############################################################################
###############################################################################
#
# SERVICE DEFINITIONS
#
###############################################################################
###############################################################################
# Define a service to "ping" the local machine
define service{
use local-service ; Name of service template to use
host_name centos.241
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
#检查远程Linux主机根分区使用情况,check_nrpe命令必须在/usr/local/nagios/etc/objects/commands.cfg中定义(默认未定义)
define service{
use generic-service ; Name of service template to use
host_name centos.241
service_description Root Partition
check_command check_nrpe!check_root
}
# 检查远程Linux主机的登录人数
define service{
use generic-service ; Name of service template to use
host_name centos.241
service_description Current Users
check_command check_nrpe!check_users
}
# 检查远程Linux的主机的负载
define service{
use generic-service ; Name of service template to use
host_name centos.241
service_description Current Load
check_command check_nrpe!check_load
}
# 检查远程Linux主机swap分区使用情况
define service{
use generic-service ; Name of service template to use
host_name centos.241
service_description Swap Usage
check_command check_nrpe!check_swap
}
# 检查远程Linux主机的SSH服务
define service{
use generic-service ; Name of service template to use
host_name centos.241
service_description SSH
check_command check_ssh
notifications_enabled 0
}
# 检查远程Linux主机的HTTP服务
define service{
use generic-service ; Name of service template to use
host_name centos.241
service_description HTTP
check_command check_http
notifications_enabled 0
}
# vi /usr/local/nagios/etc/servers/hostgroup.cfg
# 定义主机组(localhost.cfg中有类似的主机组设置,将其注释掉,否则可能会有冲突)
define hostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members localhost,wiki ; Comma separated list of hosts that belong to this group
}
#define hostgroup{
# hostgroup_name windows-servers ; The name of the hostgroup
# alias Windows Servers ; Long name of the group
# members print ; Comma separated list of hosts that belong to this group
# }
完成监控主机配置文件的配置后使用下面命令检查配置文件的正确性:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
确定无误后重启Nagios
# /etc/init.d/nagios restart