-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulkcheck
executable file
·199 lines (182 loc) · 5.16 KB
/
bulkcheck
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
use Term::ANSIColor qw(:constants);
use Net::SNMP;
use Scalar::Util qw(looks_like_number);
$Term::ANSIColor::AUTORESET = 1;
use constant SERVERFORMAT => '%-24s';
my ( $filename, $mode, $warning, $critical );
my $modes = {
load => [
{
header => '1min',
oids => '1.3.6.1.4.1.2021.10.1.3.1',
format => '%8s',
warning => 3,
critical => 5,
},
{
header => '5min',
oids => '1.3.6.1.4.1.2021.10.1.3.2',
format => '%8s',
warning => 3,
critical => 5,
},
{
header => '10min',
oids => '1.3.6.1.4.1.2021.10.1.3.3',
format => '%8s',
warning => 3,
critical => 5,
},
],
mem => [
{
header => 'total',
oids => '1.3.6.1.4.1.2021.4.5.0',
format => '%12s',
},
{
header => 'used',
oids => '1.3.6.1.4.1.2021.4.6.0',
format => '%12s',
},
{
header => 'free',
oids => '1.3.6.1.4.1.2021.4.11.0',
format => '%12s',
warning => 20 * 1024 * 1024,
critical => 10 * 1024 * 1024,
below => 'true',
},
{
header => 'avail_swap',
oids => '1.3.6.1.4.1.2021.4.4.0',
format => '%12s',
},
],
disk => [
{
header => 'total',
oids => '1.3.6.1.4.1.2021.9.1.6.1',
format => '%12s',
},
{
header => 'used',
oids => '1.3.6.1.4.1.2021.9.1.8.1',
format => '%12s',
},
{
header => 'free',
oids => '1.3.6.1.4.1.2021.9.1.7.1',
format => '%12s',
},
{
header => 'used(%)',
oids => '1.3.6.1.4.1.2021.9.1.9.1',
format => '%8s',
warning => 80,
critical => 90,
},
{
header => 'inode(%)',
oids => '1.3.6.1.4.1.2021.9.1.10.1',
format => '%8s',
},
],
};
sub get_array {
my ( $array_ref, $key ) = @_;
my @results;
for my $hash (@$array_ref) {
my $value = (exists $hash->{$key}) ? $hash->{$key} : '';
push @results, $hash->{$key};
}
return \@results;
}
GetOptions(
'--file|f=s' => \$filename,
'--mode|m=s' => \$mode,
);
$mode ||= 'load';
my @lines;
if ($filename) {
open my $FH, '<', $filename;
@lines = <$FH>;
close $FH;
} elsif (scalar @ARGV >= 1) {
@lines = @ARGV;
} else {
die 'no target selected. abort.'
}
my $results = [];
if ($lines[0] !~ /^\-/) {
unshift @lines, '-';
}
my $headers = get_array($modes->{$mode}, 'header');
my $oids = get_array($modes->{$mode}, 'oids');
my $formats = get_array($modes->{$mode}, 'format');
my $warnings = get_array($modes->{$mode}, 'warning');
my $criticals = get_array($modes->{$mode}, 'critical');
my $belows = get_array($modes->{$mode}, 'below');
for my $line (@lines) {
chomp $line;
if ( $line !~ /^#/ ) {
if ( $line !~ /^\-/ ) {
my $target_name = sprintf SERVERFORMAT, $line;
my $result = ["$target_name"];
my $values = connect_snmp($line, $oids);
for my $i (0 .. (scalar(@{$oids}) - 1) ) {
my $value = '';
my $style;
if ( $criticals->[$i] && (( !$belows->[$i] && $values->{$oids->[$i]} >= $criticals->[$i] ) || ($belows->[$i] && $values->{$oids->[$i]} <= $criticals->[$i])) ) {
$style = RED.BOLD;
} elsif ( $warnings->[$i] && (( !$belows->[$i] && $values->{$oids->[$i]} >= $warnings->[$i] ) || ($belows->[$i] && $values->{$oids->[$i]} <= $warnings->[$i])) ) {
$style = YELLOW.BOLD;
}
$value .= $style if $style;
$value .= (sprintf $formats->[$i], commify($values->{$oids->[$i]}));
$value .= RESET if $style;
push @$result, $value;
}
push @$results, $result;
} else {
$line =~ s/^\-+\s?//g;
my $header = (sprintf SERVERFORMAT, $line);
for my $i (0 .. (scalar(@{$headers}) - 1)) {
$header .= sprintf $formats->[$i], $headers->[$i];
}
push @$results, $header;
}
}
}
for my $printline (@$results) {
if (ref($printline) eq 'ARRAY') {
print @$printline;
} else {
print BOLD.$printline.RESET;
}
print "\n";
}
exit;
sub connect_snmp {
my $host = shift;
my $oids = shift;
my ($session, $error);
($session, $error) = Net::SNMP->session(
-hostname => $host,
-community => 'public',
-port => '161' ) or return $error;
my $res;
if (!defined($res = $session->get_request(@$oids))) {
printf("ERROR: %s.\n", $session->error());
}
return $res;
}
sub commify{
my $num = shift;
while ($num =~ s/^([-+]?\d+)(\d{3})/$1,$2/) { }
return $num;
}