-
Notifications
You must be signed in to change notification settings - Fork 8
/
check_memory_solaris
executable file
·219 lines (176 loc) · 6.09 KB
/
check_memory_solaris
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/opt/csw/bin/perl
#
# check_memory_solaris - Check kstat(3) data against given tresholds
#
# Copyright (C) 2007 Thomas Guyot-Sionnest <tguyot@gmail.com>
# Copyright (C) 2015 Amadeus Germany GmbH <opensource@amadeus.com>
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# This plugin is based on the check_memory plugin by Thomas Guyot-Sionnest.
# It has been adapted to run on Solaris.
use strict;
use warnings;
use vars qw($PROGNAME $VERSION $KSTATCMD $PAGESIZECMD $UNIT);
$PROGNAME = "check_memory_solaris";
$VERSION = '1.0.1-amadeus';
$KSTATCMD = '/usr/bin/kstat';
$PAGESIZECMD = '/usr/bin/pagesize';
$UNIT = 'M';
### START PLUGIN LOADER
sub load_module {
my $module;
eval "require $_" and $module = $_ and last for @_;
$module->import();
return $module;
}
my $plugin_module;
BEGIN {
$plugin_module = load_module('Monitoring::Plugin', 'Nagios::Plugin', 'Nagios::Monitoring::Plugin');
}
### END PLUGIN LOADER
my $np = $plugin_module->new(
usage => "Usage: %s [ -w <warning_threshold> ] [ -c <critical_threshold> ]\n"
. ' [ -u <unit> ]',
version => $VERSION,
plugin => $PROGNAME,
blurb => 'Check kstat(3) data against given tresholds',
timeout => 30,
license => 'GPL-2.0-or-later',
);
$np->add_arg(
spec => 'warning|w=s',
help => "-w, --warning=THRESHOLD[%]\n"
. " Warning threshold (in bytes or percent) for free memory. See\n"
. " http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT\n"
. " for the threshold format. Alternatively this can be defined as a percentage\n"
. ' of minimum free memory (warning and critical must be in the same format).',
required => 0,
);
$np->add_arg(
spec => 'critical|c=s',
help => "-c, --critical=THRESHOLD[%]\n"
. " Critical threshold (in bytes or percent) for free memory. See\n"
. " http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT\n"
. " for the threshold format. Alternatively this can be defined as a percentage\n"
. ' of minimum free memory (warning and critical must be in the same format).',
required => 0,
);
$np->add_arg(
spec => 'unit|u=s',
help => "-u, --unit=UNIT\n"
. " Unit to use for human-redeable output. Can be 'b', 'K' 'M' or 'G' for\n"
. " bytes, KiB, MiB or GiB respectively (default: '$UNIT').",
default => $UNIT,
required => 0,
);
$np->getopts;
# Assign, then check args
my $multiple;
my $unit = $np->opts->unit;
if ($unit eq 'M') {
$multiple = 1024 * 1024;
} elsif ( $unit eq 'K') {
$multiple = 1024;
} elsif ( $unit eq 'b') {
$multiple = 1;
} elsif ( $unit eq 'G') {
$multiple = 1024 * 1024 * 1024;
} else {
$np->nagios_exit('UNKNOWN', "Unit must be one of 'b', 'K', 'M' or 'G', case-sensitive.");
}
my $verbose = $np->opts->verbose;
# Would better fit later but doing it here validates thresholds
my $warning = $np->opts->warning;
my $critical = $np->opts->critical;
$np->set_thresholds(
warning => ((defined($warning) && $warning !~ /^\d+%$/) ? $warning : undef),
critical => ((defined($critical) && $critical !~ /^\d+%$/) ? $critical : undef),
);
# Better safe than sorry
alarm $np->opts->timeout;
my ($used, $free);
if (-x $KSTATCMD) {
warn("Running: '$PAGESIZECMD'\n") if ($verbose);
# Opening a command as a safe no-shell run
open(RESULT, '-|', $PAGESIZECMD)
or $np->nagios_exit(CRITICAL, 'Could not determine pagesize');
my $pagesize = readline(RESULT);
close(RESULT);
chomp($pagesize);
$pagesize += 0;
my $cmd = "$KSTATCMD -p unix::system_pages:availrmem unix::system_pages:freemem";
warn("Running: '$cmd'\n") if ($verbose);
# Opening a command as a safe no-shell run
open(RESULT, '-|', $cmd)
or $np->nagios_exit('CRITICAL', "Could not run $KSTATCMD");
my @kstat_out = <RESULT>;
close(RESULT);
# KStat should provide an output like this:
#
# unix:0:system_pages:availrmem 3472830
# unix:0:system_pages:freemem 3430897
#
# We extract the value out of the line
my @kstat_vals = map{ ( split( /\s+/, $_ ) )[1] } @kstat_out;
warn("Output from $cmd: ".join("\n", @kstat_out)."\n") if ($verbose > 1);
$used = ( $kstat_vals[0] + 0 ) * $pagesize;
$free = ( $kstat_vals[1] + 0 ) * $pagesize;
} else {
$np->nagios_exit(CRITICAL, 'No method to get free memory found');
}
alarm(0);
$np->nagios_exit('CRITICAL', "Unable to interpret $KSTATCMD output") if (!defined($free));
my $total = $used + $free;
my §percentage_use = 0;
if (defined($warning) && $warning =~ /^\d+%$/) {
$percentage_use += 1;
$warning =~ s/%//;
$warning = $total / 100 * $warning;
warn("Calculated threshold (from percentage): warn=>$warning\n") if ($verbose);
}
if (defined($critical) && $critical =~ /^\d+%$/) {
$percentage_use += 1;
$critical =~ s/%//;
$critical = $total / 100 * $critical;
warn("Calculated threshold (from percentage): crit=>$critical\n") if ($verbose);
}
# We make sure that we store the % values only if both have been provided as %
if( $percentage_use == 2 ) {
$np->set_thresholds(
warning => $warning . ':',
critical => $critical . ':',
);
}
$np->add_perfdata(
label => "free",
value => $free,
uom => 'b',
min => 0,
max => $total,
warning => $warning,
critical => $critical,
);
$np->add_perfdata(
label => "used",
value => $used,
uom => 'b',
min => 0,
max => $total,
);
my $freeprint = int($free/$multiple);
$np->nagios_exit($np->check_threshold($free), "$freeprint$unit free");