-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_yml.pl
executable file
·144 lines (121 loc) · 4.45 KB
/
check_yml.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/perl -w
use strict;
use Nagios::Plugin;
use LWP::UserAgent;
use XML::LibXML;
use XML::LibXML::XPathContext;
use Date::Parse;
use Date::Format;
my $np = Nagios::Plugin->new(
usage => 'Usage: %s [--user HTTP USERNAME] [--password HTTP PASSWORD] [--max-age SECONDS] [--strict] [--offers] --url url [ url ..]',
);
$np->add_arg(
spec => 'url=s@',
help => 'Specify URL to check',
required => 1,
);
$np->add_arg(
spec => 'user|u=s',
help => 'Specify HTTP username',
);
$np->add_arg(
spec => 'password|p=s',
help => 'Specify HTTP password',
);
$np->add_arg(
spec => 'strict',
help => 'Strict check for 200 HTTP responce code',
default => 1,
);
$np->add_arg(
spec => 'max-age|a=s',
help => 'Check age of yml file',
);
$np->add_arg(
spec => 'offers|o',
help => 'Check for count of offers in yml',
default => 1,
);
$np->getopts();
sub verbose {
my $message = shift || return 1;
if ( $np->opts->verbose ) {
$|++;
print "$message";
$|--;
}
}
my $xc = XML::LibXML::XPathContext->new();
my $now = time();
# Create a user agent object
my $ua = LWP::UserAgent->new;
$ua->agent( 'check yml/0.1' );
foreach my $url (@{$np->opts->url}, @ARGV) {
# Create a request
verbose "Requesting $url : ";
my $req = HTTP::Request->new( GET => $url );
# Autorization
$req->authorization_basic( $np->opts->user, $np->opts->password );
# Handle response
my $res = $ua->request( $req );
verbose $res->code."\n";
if ( $res->is_success ) {
if ( $np->opts->strict ) {
# Check for not 200 return code
if ( $res->code != 200 ) {
$np->add_message( CRITICAL, 'Requesting '.$req->uri.' is not 200 OK' );
}
}
if ( length( $res->content ) ) {
my $dom = XML::LibXML->load_xml(
string => $res->content
);
verbose "Content length: ".length( $res->content )."\n";
if ( $dom->indexElements() ) {
verbose "Number of elements: ".$dom->indexElements."\n";
# create XPath object
$xc->setContextNode( $dom );
# Check last offers list update
if ( $np->opts->get('max-age') ) {
my $date = $xc->findvalue('/yml_catalog/@date');
verbose "price date: $date\n";
# check date format
if (! $date =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/ ) {
$np->add_message( CRITICAL, $req->uri.' has wrong date format: '.$date.';' );
} else {
$date = str2time($date);
# format pretty date
my $pretty_date = time2str("%d %h %R", $date);
my $diff = $now - $date;
verbose "current time: $now; change time: $date; diff: $diff\n";
if ( $diff > $np->opts->get('max-age') ) {
$np->add_message( CRITICAL, $req->uri." last update ". $pretty_date.';' );
} else {
$np->add_message( OK, $req->uri.' OK;' );
}
}
} else {
$np->add_message( OK, $req->uri.': GET OK;' );
}
# Check count of offers
if ( $np->opts->get('offers') ) {
my $offers_count = $xc->findvalue('count(/yml_catalog/shop/offers/offer)');
verbose "Number of offers: ".$offers_count."\n";
if ( $offers_count > 0 ) {
$np->add_message( OK, $offers_count.' offers;' );
} else {
$np->add_message( CRITICAL, $req->uri.' has '.$offers_count.' offers;' );
}
}
} else {
$np->add_message( CRITICAL, $req->uri.' has no XML elements;' );
}
} else{
$np->add_message( CRITICAL, $req->uri.' content length is 0;' );
}
} else {
$np->add_message( CRITICAL, 'Requesting '.$req->uri.' failed. Code '.$res->code.';' );
}
}
my ( $code, $message ) = $np->check_messages();
$np->nagios_exit( $code, $message );