Chinaunix首页 | 论坛 | 博客
  • 博客访问: 856325
  • 博文数量: 254
  • 博客积分: 5350
  • 博客等级: 大校
  • 技术积分: 2045
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 13:27
文章分类

全部博文(254)

文章存档

2015年(1)

2014年(9)

2013年(17)

2012年(30)

2011年(150)

2010年(17)

2009年(28)

2008年(2)

分类: LINUX

2011-04-21 20:47:33

Nagios 監控 squid 服務器

ethan225@163.com
04/21/2011

假設服務器與客戶端均已經架設好,現在使用網上下載的check_squid插件對squid服務監控。

squid客戶端操作:
1.安裝nrpe,nagios-plugin; 調整設定好,并啟動服務

監控端操作:
1.下載,安裝監控插件check_squid.文件內容如下

#!/usr/bin/perl -w
#
# check_squid - Nagios check plugin for testing a Squid proxy
#
# Christoph Haas
# Andre Osti
#
# License: GPL 2
#
# V0.2
#

require 5.004;
use POSIX;
use strict;
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_t $opt_u $opt_n $opt_s
                        $opt_p $opt_l $opt_o $opt_m $opt_e);
use vars qw($PROGNAME);
#use lib "/usr/lib/nagios/plugins";
use lib "/usr/local/nagios/libexec";
use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
use LWP::UserAgent;
use HTTP::Request::Common qw(POST GET);
use HTTP::Headers;
my ($url, $urluser, $urlpass, $proxy, $proxyport,
     $proxyuser, $proxypass, $expectstatus, $res, $req);

$PROGNAME = "check_squid_lw.pl";

sub print_help();
sub print_usage();

Getopt::Long::Configure('bundling');
GetOptions("V"   => \$opt_V, "version"    => \$opt_V,
        "h"   => \$opt_h, "help"         => \$opt_h,
        "t=s" => \$opt_t, "timeout=i"    => \$opt_t,
        "u=s" => \$opt_u, "url=s"        => \$opt_u,
        "n=s" => \$opt_n, "urluser=s"    => \$opt_n,
        "s=s" => \$opt_s, "urlpass=s"    => \$opt_s,
        "p=s" => \$opt_p, "proxy=s"      => \$opt_p,
        "l=s" => \$opt_l, "proxyport=s"  => \$opt_l,
        "o=s" => \$opt_o, "proxyuser=s"  => \$opt_o,
        "m=s" => \$opt_m, "proxypass=s"  => \$opt_m,
        "e=i" => \$opt_e, "status=i"     => \$opt_e);
        "n=s" => \$opt_n, "urluser=s"    => \$opt_n,
        "s=s" => \$opt_s, "urlpass=s"    => \$opt_s,
        "p=s" => \$opt_p, "proxy=s"      => \$opt_p,
        "l=s" => \$opt_l, "proxyport=s"  => \$opt_l,
        "o=s" => \$opt_o, "proxyuser=s"  => \$opt_o,
        "m=s" => \$opt_m, "proxypass=s"  => \$opt_m,
        "e=i" => \$opt_e, "status=i"     => \$opt_e);

if ($opt_V) {
    print_revision($PROGNAME,'$Revision: 0.1 $'); #'
    exit $ERRORS{'OK'};
}

if ($opt_h) {print_help(); exit $ERRORS{'OK'};}

($opt_u) || ($opt_u = shift) || usage("Use -h for more info\n");
$url = $opt_u;

($opt_p) || ($opt_p = shift) || usage("Use -h for more info\n");
$proxy = $opt_p;

($opt_l) || ($opt_l = shift) || usage("Use -h for more info\n");
$proxyport = $opt_l;

($opt_e) || ($opt_e = shift) || usage("Use -h for more info");
$expectstatus = $opt_e;

if(defined($opt_n)) { $urluser = $opt_n; }

if(defined($opt_s)) { $urlpass = $opt_s; }

if(defined($opt_o)) { $proxyuser = $opt_o; }

if(defined($opt_m)) { $proxypass = $opt_m; }
my $ua = new LWP::UserAgent;
my $h = HTTP::Headers->new();

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

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

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

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

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

if ($res->status_line =~ /^$expectstatus/)
{
        print "OK - Status: ".$res->status_line."\n";
                exit $ERRORS{"OK"};
}
else
{
        print "CRITICAL - Status: ".$res->status_line." (but expected $expectstatus...)\n";
                exit $ERRORS{"CRITICAL"};
}

sub print_usage () {
        print "Usage: $PROGNAME -u -p -l -e";
        print " \n";
}

sub print_help () {
        print_revision($PROGNAME,'$Revision: 0.1 $');
        print "Perl check squid proxy\n";

        print_usage();

        print "
-V, --version
        Version this script
-h, --help
        Help
-t, --timeout=INTEGER
        default 15s
-u, --url=
         The URL to check on the internet ()
-n, --urluser=username
        Username if the web site required authentication
-s, --urlpass=password
        Password if the web site required authentication
-p, --proxy=proxy
        Server that squid runs on (proxy.mydomain)
-l, --proxyport=INTEGER
        TCP port that Squid listens on (3128)
-o, --proxyuser=proxyuser
        Username if the web site required authentication
-m, --proxypass=proxypass
        Password if the web site required authentication
-e, --status=INTEGER
        HTTP code that should be returned

        ";

        support();
}

此文件關鍵是紅色部份,動態連接庫文件位置要按你安裝插件時的實際位置更改。將此文件保存為check_squid,并給執行權限。
chown nagios.nagios check_squid
chmod 755 check_squid

./check_squid -h 查看輸出幫助信息是否正常。

2.安装perl模块
如果缺少perl程序执行所依赖的模块,会出现以下的错误提示,请安装所需要的perl模块
# yum -y install perl-libwww-perl

check_squid命令参数
Usage: url urluser urlpass proxy proxyport proxyuser proxypass expectstatus
url squid dst站点域名 如:
urluser 目标站点需要认证登录的用户名, 符号“-”代表没有
urlpass 目标站点需要认证登录的用户密码, 符号“-”代表没有
proxy squid cache服务器的IP地址或者域名
proxyport Squid cache服务器监听的端口,默认3128
proxyuser squid cache服务器需要认证登录的用户名, 符号“-”代表没有
proxypass squid cache服务器需要认证登录的用户名, 符号“-”代表没有
expectstatus HTTP协议返回的HTTP code, 符号“2”代表从2开始

测试
目标主机,squid cache服务器192.168.1.200,监听端口80
#/usr/local/nagios/libexec/check_squid – - 192.168.1.200 80 – - 2
OK – Status: 200 OK
此方法我測試一直失敗,不知道什麽原因。
[root@ldapbak libexec]# ./check_squid - - 172.16.255.130 3128 - - 2
CRITICAL - Status: 500 Can't connect to -:-:80 (Bad hostname '-:-') (but expected 172.16.255.130...)

以下是前人的做法,我并未成功

配置nagios监控
# /usr/local/nagios/etc/objects/comands.cfg
define command {
command_name check_squid
command_line $USER1$/check_squid $ARG1$ $ARG2$ $ARG3$ $HOSTADDRESS$ $ARG4$ $ARG5$ $ARG6$ $ARG7$
}

# vi /usr/local/nagios/etc/objects/scomd.cfg
define host {
        use                  linux-server
        host_name    scomd.com
        alias                liunxhost
        address        192.168.1.200
}

define service {
host_name scomd.com            
use              generic-service                                                  使用已经定义好的模板(使用继承关系)
service_description squid 定义说明
check_command check_squid!!-!-!80!-!-!2
}

重启nagios服务,5分钟后测试状态成功!

3.我的做法。。
從運行幫助文件中,我知道加一些參數,可以正常運行此命令
[root@ldapbak libexec]# ./check_squid -u -p 172.16.255.130 -l 3128 -e 2
OK - Status: 200 OK

按此定議命令。
define command{
        command_name    check_squid
        command_line    $USER1$/check_squid -u -p 172.16.255.130 -l 3128 -e 2
}

定義服務
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       squid_server
        service_description             squid service online
#       check_command                   check_nrpe!check_squid_procs
        check_command                   check_squid
        max_check_attempts              4
        normal_check_interval           3
        retry_check_interval            2
        notification_interval           10
        notification_options            w,u,c,r
        }
這樣就可以正常測試。。了

4.在沒有想到上面這個方法之前,我還想到了從其服務器本機中監控其進程是否存在的方法,檢測squid服務,這是上面注釋的 check_command                   check_nrpe!check_squid_procs。
具體做法是在squid主機上的nrpe.cfg文件定義命令
command[check_squid_procs]=/usr/local/nrpe/libexec/check_procs -w 1:1 -c 2 -a 'squid -D' -u root
這條命令意思為:正常情況下服務器存在一個由root用戶啟動的squid服務,并有擁有者為squid的多個線程存在。只要檢測root用戶的服務存在且為一個時正常,有兩個是異常。。

5.還有一種方案是
使用nagios自带的check_http插件校验
check_http -H 目标主机域名 -p 80 -I 缓存服务器地址
這個代理服務器不能選定端口,或者我沒有發現如何選。。

阅读(1331) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~