Nagios check http header
Материал из Ksimute
Коробанов Сергей
Август 15, 2010
Задача. Некоторое приложение имеет статус страницу и в http header возвращает некую пару Переменная:Значение надо проверять ее наличие и соответственно оформить это в виде плагина к нагиосу.
Стандартный nagios плагин check_http не может парсить хедер. Только статус отслеживает и может в request header значения передавать всякие. Вообщем задача специфическая.
Выложил на exchange.nagios.org http://exchange.nagios.org/directory/Plugins/Websites,-Forms-and-Transactions/check_http_header/details
Вот так примерно выглядит наш злой HTML
# /usr/lib/nagios/plugins/check_http -I server.net -p 80 -u '/URL' -v GET /URL HTTP/1.0 User-Agent: check_http/v1991 (nagios-plugins 1.4.12) Connection: close http://server.net:80/URL is 418 characters STATUS: HTTP/1.1 200 OK **** HEADER **** Server: nginx/0.7.63 Date: Wed, 11 Aug 2010 15:09:43 GMT Content-Type: text/plain;charset=UTF-8 Connection: close VALUE-1: 17 VALUE-2: 1 Content-Length: 43 Expires: Wed, 11 Aug 2010 15:10:43 GMT Cache-Control: max-age=60 **** CONTENT **** [{"Date":"2010-08-11","megavalue":48002051}] HTTP OK HTTP/1.1 200 OK - 418 bytes in 0.013 seconds |time=0.013146s;;;0.000000 size=418B;;;0 #
Ставим curl
ksi:~# aptitude install libwww-curl-perl
Пишем скрипт:
#!/usr/bin/perl -w
use strict;
use warnings;
use WWW::Curl::Easy;
use Getopt::Std;
my $plugin_name = 'check_http_header';
my $VERSION = '0.01';
# getopt module config
$Getopt::Std::STANDARD_HELP_VERSION = 1;
# nagios exit codes
use constant EXIT_OK => 0;
use constant EXIT_WARNING => 1;
use constant EXIT_CRITICAL => 2;
use constant EXIT_UNKNOWN => 3;
# parse cmd opts
my %opts;
getopts('vI:u:t:r:p:', \%opts);
$opts{t} = 60 unless (defined $opts{t});
$opts{u} = "/" unless (defined $opts{u});
if (not (defined $opts{I} and defined $opts{r})) {
print "ERROR: INVALID USAGE\n";
HELP_MESSAGE();
exit EXIT_CRITICAL;
}
# Setting the options
my $curl = new WWW::Curl::Easy;
$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_NOBODY,1);
$curl->setopt(CURLOPT_TIMEOUT,$opts{t});
$curl->setopt(CURLOPT_URL, "$opts{I}:$opts{p}$opts{u}");
my $response_body;
# NOTE - do not use a typeglob here. A reference to a typeglob is okay though.
open (my $fileb, ">", \$response_body);
$curl->setopt(CURLOPT_WRITEDATA,$fileb);
# Starts the actual request
my $retcode = $curl->perform;
# Looking at the results...
if ($retcode == 0) {
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
# judge result and next action based on $response_code
if ($response_body =~ m/$opts{r}/){print "OK REGEXP FOUND \n";exit EXIT_OK ;}
else {
print "REGEXP NOT FOUND \n"; exit EXIT_CRITICAL;
}
} else {
print("An error happened: ".$curl->strerror($retcode)." ($retcode)\n");
exit EXIT_UNKNOWN;
}
sub HELP_MESSAGE
{
print <<EOHELP
Retrieve an http/s URL and looks in its header (ACHTUNG HTTP header not content!) Output for a given text.
Returns CRITICAL is not found, OK if found, UNKNOWN otherwise.
--help shows this message
--version shows version information
-I IP address or name (use numeric address if possible to bypass DNS lookup).
-u URL to GET
-r <text> regexp to match in the output of http header
-t Timeout in seconds to wait for the URL to load. If the page fails to l
oad,
$plugin_name will exit with UNKNOWN state (default 60)
-p Port
EOHELP
;
}
sub VERSION_MESSAGE
{
print <<EOVM
$plugin_name v. $VERSION
Copyright 2010, KsI - http://ksimute.trancom.ru - Licensed under GPLv2
EOVM
;
}
Помошь по нему:
Retrieve an http/s URL and looks in its header (ACHTUNG HTTP header not content!) Output for a given text.
Returns CRITICAL is not found, OK if found, UNKNOWN otherwise.
--help shows this message
--version shows version information
-I IP address or name (use numeric address if possible to bypass DNS lookup).
-u URL to GET
-r <text> regexp to match in the output of http header
-t Timeout in seconds to wait for the URL to load. If the page fails to l
oad,
check_http_header will exit with UNKNOWN state (default 60)
-p Port
Пример использования:
$ curl -I ksimute.trancom.ru HTTP/1.1 301 Moved Permanently ..... Vary: Accept-Encoding,Cookie X-Vary-Options: Accept-Encoding; ..... ...... $ ./check_http_header.pl -I ksimute.trancom.ru -r Vary -p 80 -u / -t 20 OK REGEXP FOUND $ ./check_http_header.pl -I ksimute.trancom.ru -r Varyddd -p 80 -u / -t 20 REGEXP NOT FOUND
Hint:
Если скрипт не заработает с ошибкой: (Service check did not exit properly) это проблема встроенного perl в nagios (embedded perl) что-то он там криво интерпретирует. Это тема баян возникает часто с perl скриптами для нагиоса. Вызывайте его следующим образом:
define command{
command_name check_header
command_line /usr/bin/perl /usr/lib/nagios/plugins/my/check_http_header.pl -I $HOSTADDRESS$
-p 80 -u '/myurl' -r Regexp -t 20
}
i.e. Вызывайте внешний perl для интерпретации.