File: storage.monitor

package info (click to toggle)
mon-contrib 1.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 896 kB
  • sloc: perl: 5,510; sh: 391; python: 118; makefile: 72
file content (187 lines) | stat: -rwxr-xr-x 4,774 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl
#
# storage.monitor
#
# Use SNMP to get free disk space from a machine that implements HOSTMIB.
#
#   Exits with value of 1 if free space on any host drops below
#   the configured value, or exits with the value of 2 if
#   there is a "soft" error (SNMP library error, or could not get a
#   response from the server).
#
# Requirements:
#
#   Requires the UCD SNMP library and G.S. Marzot's Perl SNMP module.
#   (Avoid UCSD SNMP 3.6.1, it will cause segfaults.  Use 3.6.2+)
#
# Written by Peter Holzleitner
# based heavily on netappfree.monitor by Jim Trocki
#
#    Copyright (C) 2000, Peter Holzleitner
#    Copyright (C) 1998, Jim Trocki
#
#    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
#
#    Version 1.0.0
#
#
use SNMP;
use Getopt::Long;

sub readcf;
sub toKB;

# load only the necessary MIBs:
$ENV{"MIBS"} = 'RFC1213-MIB:HOST-RESOURCES-MIB';

GetOptions (\%opt, "community=s", "timeout=i", "retries=i", "config=s", "list");

die "no host arguments\n" if (@ARGV == 0);

$RET = 0;
@ERRS = ();
@HOSTS = ();

$COMM    = $opt{"community"} || "public";
$TIMEOUT = $opt{"timeout"} * 1000 * 1000 || 5000000;
$RETRIES = $opt{"retries"} || 8;
$CONFIG  = $opt{"config"} || (-d "/etc/mon" ? "/etc/mon" : "/usr/lib/mon/etc")
	   . "/storage.cf";

$list = $opt{"list"};

($stDescr, $stUnit, $stSize, $stUsed) = (0..3);
$host = '';
$fsname = '';
$sizeK = 0;
$freeK = 0;
$OK = 0;

readcf ($CONFIG) || die "could not read config: $!\n";

foreach $host (@ARGV) {
    next if (!defined $FREE{$host});

    if (!defined($s = new SNMP::Session (DestHost => $host,
    		Timeout => $TIMEOUT, Community => $COMM,
		Retries => $RETRIES))) {
	$RET = ($RET == 1) ? 1 : 2;
	push (@HOSTS, $host);
	push (@ERRS, "could not create session to $host: " . $SNMP::Session::ErrorStr);
	next;
    }

    $v = new SNMP::VarList (
    	    ['hrStorageDescr'],  # 
    	    ['hrStorageAllocationUnits'],  # 
    	    ['hrStorageSize'],  #
    	    ['hrStorageUsed'],
    );

    while (defined $s->getnext($v)) {

	last if ($v->[$stDescr]->tag !~ /hrStorageDescr/);

#($stDescr, $stUnit, $stSize, $stUsed) = (0..3);

	$fsname =  $v->[$stDescr]->val;
	$unit   =  $v->[$stUnit]->val;
        $size   =  $v->[$stSize]->val;
	$used   =  $v->[$stUsed]->val;
        $freeK  = ($size - $used) * $unit / 1024;

	$OK = "(n/conf)";
	$needfreeK = $FREE{$host}{$fsname};
        if ( $needfreeK ) {
	    $OK = "OK";
	    if ( $freeK < $needfreeK ) {
		push (@HOSTS, $host);
		push (@ERRS, sprintf ("%s:%s: %1.1fMB free", $host, $fsname, $freeK));
		$OK = "LOW";
		$RET = 1;
	        }
	    }
        if($list)  {
            $sizeK  = $size * $unit / 1024;
	    write;
	    }
	} # while(getnext())

    if ($s->{ErrorNum}) {
	push (@HOSTS, $host);
	push (@ERRS, "could not get hrStorageDescr for $host: " . $s->{ErrorStr});
	$RET = ($RET == 1) ? 1 : 2;
	}
    }

if(!$list) {
    if ($RET) {
        print "@HOSTS\n";
	print "\n";
        print join("\n", @ERRS), "\n";
	}
    } # foreach(host)

exit $RET;


#
# read configuration file
#
sub readcf {
    my ($f) = @_;
    my ($l, $host, $filesys, $free);

    open (CF, $f) || return undef;
    while (<CF>) {
    	next if (/^\s*#/ || /^\s*$/);
	chomp;
	($host, $filesys, $free) = split;
	$filesys =~ tr/+/ /;    # + replaces space in config files
	if (!defined ($FREE{$host}{$filesys} = toKB ($free))) {
	    die "error free specification, config $f, line $.\n";
	}
    }
    close (CF);
}


sub toKB {
    my ($free) = @_;
    my ($n, $u);

    if ($free =~ /^(\d+\.\d+)(kb|mb|gb)$/i) {
        ($n, $u) = ($1, "\L$2");
    } elsif ($free =~ /^(\d+)(kb|mb|gb)$/i) {
        ($n, $u) = ($1, "\L$2");
    } else {
    	return undef;
    }

    return (int ($n * 1024))        if ($u eq "mb");
    return (int ($n * 1024 * 1024)) if ($u eq "gb");
    int ($n);
}


format STDOUT_TOP =
Host             File System                KB total     KB free   OK
----------------------------------------------------------------------------
.

format STDOUT =
@<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<<<<<   @>>>>>>>>>>  @>>>>>>>>>>  @<<<<<<<
$host, $fsname, $sizeK, $freeK, $OK
.