-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_passive_by_ssh.pl
91 lines (73 loc) · 1.7 KB
/
check_passive_by_ssh.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
#!/usr/bin/perl -w
# 0 host
# 1 username
# 2 public key file
# 3 private key file
# 4 command file
# 5 label
# 6 warning level
# 7 critical level
# 8 parameter
use strict;
use lib "/monitor/icinga/libexec/lib/perl";
use lib "/monitor/icinga/libexec";
use range;
use Net::SSH2;
# $ARGV[0] = "host";
# $ARGV[1] = "username";
# $ARGV[2] = "/monitor/icinga/etc/id_rsa.pub";
# $ARGV[3] = "/monitor/icinga/etc/id_rsa";
# $ARGV[4] = "/monitor/icinga/etc/sh/test.sh";
# $ARGV[5] = "LABEL";
# $ARGV[6] = "0";
# $ARGV[7] = "0";
# $ARGV[8] = "?";
if (!defined($ARGV[8])) {
$ARGV[8] = "?";
}
open(INFILE,"<$ARGV[4]");
my @cmd = <INFILE>;
close(INFILE);
my $cmdstr = join('',@cmd);
$cmdstr =~ s/\?/$ARGV[8]/g;
#print "$cmdstr\n";
#exit(0);
my $ssh = Net::SSH2->new();
#$ssh->debug(1);
if (!$ssh->connect($ARGV[0])) {
print "Can't connect: $!\n";
exit(3);
}
if (!$ssh->auth_publickey($ARGV[1],$ARGV[2],$ARGV[3])) {
print "Can't authenticate: $!\n";
exit(3);
}
my $chan = $ssh->channel();
if (!$chan->exec($cmdstr)) {
print "Can't exec: $!\n";
exit(3);
}
my @poll = {handle=>$chan,events=>['in']};
$ssh->poll(10000,\@poll);
#$poll[0]->{revents}->{in};
#while (<$chan>) {print}
while (my $line = <$chan>) { chomp $line; print "$line\n"; }
my $ret = <$chan>;
if (!$ret) {
print "Timeout\n";
exit(3);
}
chomp($ret);
$chan->close();
$ssh->disconnect();
$ret =~ s/,/./;
if (range->check_range($ARGV[7],$ret)) {
print "CRITICAL: $ARGV[5]=$ret/$ARGV[6]/$ARGV[7]|$ARGV[5]=$ret/$ARGV[6]/$ARGV[7]\n";
exit(2);
}
if (range->check_range($ARGV[6],$ret)) {
print "WARNING: $ARGV[5]=$ret/$ARGV[6]/$ARGV[7]|$ARGV[5]=$ret/$ARGV[6]/$ARGV[7]\n";
exit(1);
}
print "OK: $ARGV[5]=$ret/$ARGV[6]/$ARGV[7]|$ARGV[5]=$ret/$ARGV[6]/$ARGV[7]\n";
exit(0);