测试与质量的关系 测试有助于提高软件的质量,但是提高软件的质量不能依赖于测试。测试与质量的关系很象在考试中“检查”与“成绩”的关系。 学习好的学生,在考试时通过认真检查能减少因疏忽而造成的答题错误,从而“提高”了考试成绩(取得他本来就该得的好成绩)。 而学习差的学生,他原本就不会做题目,无论检查多么细心,也不能提高成绩。 所以说,软件的高质量是设计出来的,而不是靠测试修补出来的。 I love U software testing

nagios监控系统搭建

上一篇 / 下一篇  2007-05-31 17:31:20

近段时间一直在研究nagios监控系统,借鉴了不少高手的文章,费了不少功夫总算把它搞定了,以下就是具体的安装过程,更深层的监控项目还有 待进一步研究,希望大家给点建设!!
#===========================安装 Nagios    ========================================
cd /opt
groupadd nagios
groupadd nagcmd
useradd nagios -g nagcmd -d /usr/local/nagios
chown nagios.nagios /usr/local/nagios/
chmod 755 /usr/local/nagios
wgethttp://osdn.dl.sourceforge.net/sourceforge/nagios/nagios-3.0.2.tar.gz
tar -zxvf nagios-3.0.2.tar.gz
cd nagios-3.0.2
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
#编辑httpd.conf配置文件
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/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
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/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
</Directory>
#重启apache
killall httpd
#============================安装nagios plugins=========================
cd /opt
wgethttp://osdn.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.11.tar.gz
tar -zxvf nagios-plugins-1.4.11.tar.gz
cd nagios-plugins-1.4.11
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-gourp=nagios --with-mysql=/usr/local/mysql5/ --enable-perl-modules
make
make install
#配置和启动nagios
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#如果提示“Whoops!   Error: Could not read object configuration data! ”,这是因为没有启动nagios后台进程,执行以下命令
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
#=============================安装nrpe 插件==============================
#对远程一台linux主机进行监控
#监控服务器上安装设置
cd /opt
wgethttp://www.mirrors.wiretapped.net/security/network-monitoring/nagios/nrpe-2.8.1.tar.gz
#监控主机上安装方法
tar -zxvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure
make all
make install
make install-plugin    ---监控机需要安装check_nrpe这个插件,被监控机并不需要
##########################################################################
#被监控主机上安装方法
cd /opt
useradd nagios -d /usr/local/nagios
chown nagios.nagios /usr/local/nagios/
tar -zxvf nagios-plugins-1.4.11.tar.gz
cd nagios-plugins-1.4.11
./configure --enable-perl-modules --with-ping-command=ping
make
make install
cd /opt
tar -zxvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure
make all
make install
make install-plugin
make install-daemon
make install-daemon-config

vi /usr/local/nagios/etc/nrpe.conf
allowed_hosts=192.168.8.150
#为了监控swap在nrpe.cfg中添加
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda1
#sdb1可根椐实际情况更改,我这里的硬盘类型是scsi
vi /etc/services
#增加nrpe
nrpe            5666/tcp                        # nrpe
#启动nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
netstat -at|grep nrpe
netstat -an|grep 5666
#############################################################################
#再对监控服务器进行设置
#添加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$
        }
vi /usr/local/nagios/etc/cgi.cfg
把use_authentication=1修改为use_authentication=0如果出现页面无法显示之类的
authorized_for_system_commands=nagiosadmin,kerry    --kerry为http访问授权用户
authorized_for_all_services=nagiosadmin,kerry
authorized_for_all_hosts=nagiosadmin,kerry
authorized_for_all_service_commands=nagiosadmin,kerry
authorized_for_all_host_commands=nagiosadmin,kerry
#测试NRPE是否则正常工作
/usr/local/nagios/libexec/check_nrpe -H localhost
killall nagios
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d" >> /etc/rc.local
ehco "/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg" >>/etc/rc.local
#问题:notifications for this service have been disabled
#解决办法:enable notifications for this service
#问题:Status Map页面无法显示
#解决方法:ln -s /usr/local/lib/libgd.so.2 /usr/lib/libgd.so.2
vi /usr/local/nagios/etc/nagios.cfg
#添加
cfg_file=/usr/local/nagios/etc/objects/emos-mailserver.cfg
#emos-mailserver.cfg这个文件名可以自定义
#对刚定义的emos-mailserver.cfg文件进行配置
vi /usr/local/nagios/etc/objects/emos-mailserver.cfg
define host{
           use          linux-server
          host_name     emos-mailserver
          alias         emos-mailserver
          address       192.168.8.151
        }
define service{
        use             generic-service
        host_name       emos-mailserver
        service_description     HTTP
        check_command     check_http
       }
define service{
        use             generic-service
        host_name       emos-mailserver
        service_description     SSH
        check_command   check_ssh
       }
define service{
        use             generic-service
        host_name       emos-mailserver
        service_description     SMTP
        check_command   check_smtp
       }
define service{
        use             generic-service
        host_name       emos-mailserver
        service_description     POP3
        check_command   check_pop
       }
#define service{
#        use             generic-service
#        host_name       emos-mailserver
#        service_description     mysql
#        check_command   check_mysql
#       }
define service{
        use             generic-service
        host_name       emos-mailserver
        service_description     check-swap
        check_command           check_nrpe!check_swap
        }
define service{
        use             generic-service
        host_name       emos-mailserver
        service_description     check-load
        check_command           check_nrpe!check_load
        }
define service{
        use             generic-service
        host_name       emos-mailserver
        service_description     check-disk
        check_command           check_nrpe!check_sda1
}
define service{
        use             generic-service
        host_name       emos-mailserver
        service_description     zombie_procs
        check_command           check_nrpe!check_zombie_procs
        }
define service{
        use             generic-service
        host_name       emos-mailserver
        service_description     check-users
        check_command           check_nrpe!check_users
        }
define service{
        use             generic-service
        host_name       emos-mailserver
        service_description     total_procs
        check_command           check_nrpe!check_total_procs
#配置完后,重启nagios
killall nagios
service nagios start
pstree |grep nagios

#=============================安装 pnp===============================================
cd /opt
tar zxvf pnp-0.4.12.tar.gz
cd pnp-0.4.12
./configure --with-nagios-user=nagios \
--with-nagios-group-nagios \
--with-rrdtool=/usr/local/rrdtool/bin/rrdtool \
--with-perfdata-dir=/usr/local/nagios/share/perfdata
make
make all
make install
make install-config
make install-init
#错误提示:RRDs Perl Modules:                *** NOT FOUND ***
#解决方法:ln -sv /usr/local/rrdtool/lib/perl/5.8.8/i386-linux-thread-multi/auto/RRDs/RRDs.so /usr/lib/perl5/5.8.8/i386-linux-thread-multi/
#要产生图形数据还需在commands.cfg中重定义命令:
define command{
command_name process-service-perfdata
command_line /usr/local/nagios/libexec/process_perfdata.pl
}
#产生图形数据还要设定nagios.cfg文件:
process_performance_data=1
service_perfdata_command=process-service-perfdata
#==========================监控远程windows主 机==================================
#监控windows服务器
wgethttp://nchc.dl.sourceforge.net/sourceforge/nscplus/NSClient++-Win32-0.3.5.zip
解压nsclient++0.3.3.zip到c盘根目录
解压为C:\NSClient++
#在nagios监控服务器上
vi /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/windows.cfg 去掉这句话的注释
#到win服务器上,打开命令窗口,cd到刚才解压的目录
#在命令行界面执行 nsclient++ /install
#然后 nsclient++ SysTray 如果出错不用管!
#此时在“服务”里面已经有了nsclient的服务
双击打开,点"登录"标签,在"允许服务与桌面交互"前打勾
#编辑NES.ini,在 [modules] 选项里,去掉所有的注释符号; 除了
CheckWMI.dll和RemoteConfiguration.dll
#the [Settings] 选项里
修改allowd_host=192.168.8.150(nagios服务器的ip)
#[NSClient] 里面,去掉port=12489的注释!他靠端口12489侦听,所以防火墙要打开这个端口!
在[Settings]部分设置'password'选项 来设置密码,作用是在nagios连接过来时要求提供密码.这一步是可选的,我这里方便起见跳过它,不要密码.
#然后启动nsclient
nsclient++ /start
#接下来我们开始配置nagios服务器里面的内容
vi /usr/local/nagios/etc/objects/windows.cfg
define host{
    use windows-server
    host_name winserver alias
    My Windows Server
    address 192.168.8.151   --windows服务器的IP地址
    }
#修改hostname和address,很重要!!
#重新启动监控服务器上的nagios
killall nagios
service nagios start
#==============================安装 sengEmail ==================================
#使用sendEmail发送报警邮件
cd /opt
wgethttp://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.55.tar.gz
tar -zxvf sendEmail-v1.55.tar.gz
cd sendEmail-v1.55
cp sendEmail /usr/local/bin
chmod +x /usr/local/bin/sendEmail
#sendEmail使用方法
/usr/local/bin/sendEmail –fkerry.hu@3aaa.com–tkerry.hu@3aaa.com–s mail.3aaa.com –u “from nagios” –xukerry.hu@3aaa.com–xp 11111 –m happy
#解释:-f 表示发送者的邮箱
     -t 表示接收者的邮箱
     -s 表示SMTP服务器的域名或者ip
     -u 表示邮件的主题
     -xu 表示SMTP验证的用户名
     -xp 表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确识别)
     -m 表示邮件的内容如果你不带-m参数的话,就会提示你自行输入
#编辑配置文件,nagios使用sendEmail来发警告邮件
vi /usr/local/nagios/etc/objects/commands.cfg
# 'notify-host-by-email' command definition
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/sendEmail –fkerry.hu@3aaa.com–t $CONTACTEMAIL$ –s mail.3aaa.com –u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" –xukerry.hu@3aaa.com–xp 11111
}
# 'notify-service-by-email' command definition
define command{
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /usr/local/bin/sendEmail –fkerry.hu@3aaa.com–t $CONTACTEMAIL$ –s mail.3aaa.com –u "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" –xukerry.hu@3aaa.com–xp 111111
}
#注:在使用sendEmail的过程中无法发送报警邮件,不知是什么原因,只好改用系统自带的sendmail发邮 件!!

http://workaround.org/squid/nagios-plugin/check_squid的脚本用来检测squid很好用,但有时 会报错:

Parsing of undecoded UTF-8 will give garbage when decoding entities at /usr/lib/perl5/vendor_perl/5.8.5/LWP/Protocol.pm line 114.

原因是:
HTML::HeadParser模块在使用parse()方法时,对没有编码的UTF-8会弄混,要保证在传值之前进行适当的编码。
参考:http://www.xinjiezuo.com/blog/?p=43

解决方式是加入一行:

$ua->parse_head(0);

跳过去就好了。

下面是改过后的check_squid.


#!/usr/bin/perl -w
#
# check_squid - Nagios check plugin for testing a Squid proxy
#
# Christoph Haas (email@christoph-haas.de)
# License: GPL 2
#
# V0.2
#

use LWP::UserAgent;
use HTTP::Request::Common qw(POST GET);
use HTTP::Headers;
use strict;

my ($url, $urluser, $urlpass, $proxy, $proxyport,
        $proxyuser, $proxypass, $expectstatus) = @ARGV;

unless ($url && $proxy && $expectstatus)
{
        print "Usage: url urluser urlpass proxy proxyport proxyuser proxypass expectstatus\n";
                                print " url       -> The URL to check on the internet (http://www.google.com)\n";
                                print " urluser   -> Username if the web site required authentication (- = none)\n";
                                print " urlpass   -> Password if the web site required authentication (- = none)\n";
                                print " proxy     -> Server that squid runs on (proxy.mydomain)\n";
                                print " proxyport -> TCP port that Squid listens on (3128)\n";
                                print " proxyuser -> Username if the web site required authentication (- = none)\n";
                                print " proxypass -> Password if the web site required authentication (- = none)\n";
                                print " expectstatus -> HTTP code that should be returned\n";
                                print "                  (2 = anything that begins with 2)\n";
        exit -1;
}

$urluser='' if $urluser eq '-';
$urlpass='' if $urlpass eq '-';
$proxyuser='' if $proxyuser eq '-';
$proxypass='' if $proxypass eq '-';

my $ua = new LWP::UserAgent;
$ua->parse_head(0); //加入这行就好了

my $h = HTTP::Headers->new();

if ($proxy)
{
        $ua->proxy(['http', 'ftp'], "http://$proxy:$proxyport");

        if ($proxyuser)
        {
                $h->proxy_authorization_basic($proxyuser,$proxypass);
        }
}

if ($urluser)
{
        $h->authorization_basic($urluser, $urlpass);
}

my $req = HTTP::Request->new('GET', $url, $h);

my $res = $ua->request($req);

if ($res->status_line =~ /^$expectstatus/)
{
        print "OK - Status: ".$res->status_line."\n";
        exit 0;
}
else
{
        print "WARNING - Status: ".$res->status_line." (but expected $expectstatus...)\n";
        exit 1;
}
http://www.nagios.org/download 查考

TAG:

 

评分:0

我来说两句

日历

« 2024-04-24  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 95182
  • 日志数: 112
  • 图片数: 1
  • 文件数: 1
  • 书签数: 1
  • 建立时间: 2007-01-16
  • 更新时间: 2010-06-28

RSS订阅

Open Toolbar