-
Notifications
You must be signed in to change notification settings - Fork 0
/
chk_zadarma_balance
executable file
·71 lines (53 loc) · 2.1 KB
/
chk_zadarma_balance
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
#!/usr/bin/perl
use strict;
use Nagios::Plugin;
use LWP::UserAgent;
use Mojo::DOM;
use Switch;
use Config::IniFiles;
my $np = Nagios::Plugin->new(
usage => "Usage: %s "
. "[ -c|--critical=<threshold> ] [ -w|--warning=<threshold> ] -l|--login <login> -f|--conf <path-to-ini>",
);
$np->add_arg(
spec => 'critical|c=s',
help => '-c, --critical=INTEGER:INTEGER . See '
. 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT '
. 'for the threshold format. ',
);
$np->add_arg(
spec => 'warning|w=s',
help => '-w, --warning=INTEGER:INTEGER . See '
. 'http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT '
. 'for the threshold format. ',
);
$np->add_arg(
spec => 'login|l=s',
help => '-l, --login=LOGIN . '
. 'youmagic login '
. 'for the threshold format. ',
);
$np->add_arg(
spec => 'conf|f=s',
help => '-f, --conf=PATH Path to ini-file with passwords '
);
$np->getopts;
my $cfg = new Config::IniFiles( -file => $np->opts->conf ) || $np->nagios_exit( UNKNOWN, "Unable to read config at ".$np->opts->conf) ;
my $login = $np->opts->login;
my $password = $cfg->val( 'zadarma', "password_$login" );
my $ua = LWP::UserAgent->new(cookie_jar => { file => "/tmp/.cookies.$$.txt" });
push @{ $ua->requests_redirectable }, 'POST';
my $response = $ua->post( 'https://ss.zadarma.com/auth/login/', {email => $login, password => $password} );
my $dom = Mojo::DOM->new($response->decoded_content);
my $errormsg = $dom->find("p.error")->[0];
$np->nagios_exit( UNKNOWN, "Login failed: ".$errormsg->text) if $errormsg;
my $response = $ua->get( 'https://ss.zadarma.com/' );
$dom = Mojo::DOM->new($response->decoded_content);
my $balancespan = $dom->find("span.balance")->[0]->text;
my ($balance) = ( $balancespan =~ /\$(\d+\.\d+)/);
$np -> add_perfdata( label =>"balance", value => $balance , threshold => 0 );
$np->nagios_exit(
return_code => $np->check_threshold($balance),
message => "Balance is $balance"
);
END { unlink("/tmp/.cookies.$$.txt"); }