-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_ipmi_power.pl
executable file
·139 lines (119 loc) · 3.27 KB
/
check_ipmi_power.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
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use Pod::Text::Termcap;
use constant OK => 0;
use constant WARNING => 1;
use constant CRITICAL => 2;
use constant UNKNOWN => 3;
my $pkg_nagios_available = 0;
my $pkg_monitoring_available = 0;
my @g_long_message;
BEGIN {
eval {
require Monitoring::Plugin;
require Monitoring::Plugin::Functions;
$pkg_monitoring_available = 1;
};
if (!$pkg_monitoring_available) {
eval {
require Nagios::Plugin;
require Nagios::Plugin::Functions;
*Monitoring::Plugin:: = *Nagios::Plugin::;
$pkg_nagios_available = 1;
};
}
if (!$pkg_monitoring_available && !$pkg_nagios_available) {
print("UNKNOWN - Unable to find module Monitoring::Plugin or Nagios::Plugin\n");
exit UNKNOWN;
}
}
my $parser = Pod::Text::Termcap->new (sentence => 0, width => 78);
my $extra_doc = <<'END_MESSAGE';
END_MESSAGE
my $extra_doc_output;
$parser->output_string(\$extra_doc_output);
$parser->parse_string_document($extra_doc);
my $mp = Monitoring::Plugin->new(
shortname => "check_ipmi_power",
usage => "",
extra => $extra_doc_output
);
$mp->add_arg(
spec => 'hostname|H=s',
help => '',
required => 1,
);
$mp->add_arg(
spec => 'username=s',
help => 'The IPMI username',
required => 1,
);
$mp->add_arg(
spec => 'password=s',
help => 'The password of the IPMI user',
required => 1,
);
$mp->add_arg(
spec => 'driver-type=s',
help => 'Specify the driver type to use (Default: lanplus)',
default => 'lanplus',
);
$mp->getopts;
check();
my ($code, $message) = $mp->check_messages();
wrap_exit($code, $message . "\n" . join("\n", @g_long_message));
sub check
{
my @cmd;
push(@cmd, 'ipmi-power');
push(@cmd, ('--driver-type', $mp->opts->{'driver-type'}));
push(@cmd, ('--hostname', $mp->opts->hostname));
push(@cmd, ('--username', $mp->opts->username));
push(@cmd, ('--password', $mp->opts->password));
push(@cmd, ('--privilege-level', 'USER'));
push(@cmd, '--stat');
open(my $pipe,'-|',@cmd) or wrap_exit(UNKNOWN, "Can't start process: $!");
my @output=<$pipe>;
close($pipe) or wrap_exit(UNKNOWN, "Broken pipe: $!" . "\nOutput: \n" . join("\n", @output));
my $hop_count = 0;
my $hop_reachable = 1;
# Add default OK message
$mp->add_message(
OK,
'System is on'
);
foreach my $line (@output) {
if ($line =~ /^([^:]*?): (\w+)$/) {
my $hostname = $1;
my $state = $2;
if ($state ne 'on') {
$mp->add_message(
CRITICAL,
sprintf(
'System is not on: \'%s: %s\'',
$hostname,
$state
)
);
}
} else {
$line =~ s/^\s+|\s+$//g;
wrap_exit(
UNKNOWN,
sprintf(
'Unable to parse output: \'%s\'',
$line
)
);
}
}
}
sub wrap_exit
{
if($pkg_monitoring_available == 1) {
$mp->plugin_exit( @_ );
} else {
$mp->nagios_exit( @_ );
}
}