1#!/usr/bin/perl -w
2#
3# mrtg-storage: return storage item usage
4# Version 0.1: Steve Shipway 2008
5#
6# usage: mrtg-storage -H hostname -C community -t timeout -d -v dtorage identifier
7#
8
9use strict;
10use Net::SNMP;
11use Getopt::Std;
12use vars qw/$opt_h $opt_H $opt_C $opt_t $opt_v $opt_d/;
13
14my($hrStorage) = "1.3.6.1.2.1.25.2";
15my($hrStorageDescr) = "$hrStorage.3.1.3";
16my($hrStorageUsed ) = "$hrStorage.3.1.4";
17my($hrStorageSize ) = "$hrStorage.3.1.5";
18my($hrStorageAllocationUnits) = "$hrStorage.3.1.6";
19
20my($HOSTNAME,$COMMUNITY,$MODULE) = ('localhost','public','');
21my($TIMEOUT) = 10;
22my($DEBUG) = 0;
23my($snmp,$snmperr,$resp);
24my($instance) = -1;
25my($oid);
26
27sub dohelp {
28	print "Usage: mrtg-storage -H hostname -C community [-t timeout][-d][-v item]\n";
29	exit 0;
30}
31
32# Process the arguments
33getopts('H:C:t:v:dh');
34if($opt_h) { dohelp(); exit(0); }
35$TIMEOUT   = $opt_t if($opt_t);
36$COMMUNITY = $opt_C if($opt_C);
37$HOSTNAME  = $opt_H if($opt_H);
38$MODULE    = $opt_v if($opt_v);
39$DEBUG = 1 if($opt_d);
40
41# Open the SNMP connection
42($snmp,$snmperr) = Net::SNMP->session( -hostname=>$HOSTNAME,
43        -community=>$COMMUNITY, -timeout=>$TIMEOUT );
44if($snmperr) { print "UNKNOWN\nUNKNOWN\n\nError: $snmperr\n"; exit 0; }
45
46# Search for matching storage entry
47$resp = $snmp->get_table( -baseoid=>$hrStorageDescr );
48if(!$resp) { print "UNKNOWN\nUNKNOWN\n\nNo SNMP data available\n"; exit 0; }
49foreach $oid ( keys %$resp ) {
50	print "Storage ".$resp->{$oid}." found...\n" if($DEBUG);
51	if(!$MODULE) { print $resp->{$oid}."\n"; next; }
52	if($resp->{$oid} eq $MODULE) {
53		$instance=$1 if( $oid =~ /\.(\d+)$/ );
54		last;
55	}
56}
57exit 0 if(!$MODULE); # testing mode
58if($instance<1) {
59	print "UNKNOWN\nUNKNOWN\n\n";
60	print "The $MODULE storage item is not present\n";
61	exit 0;
62}
63
64# Now we know the sequence number, we can get the items
65$resp = $snmp->get_request( -varbindlist=>[ "$hrStorageAllocationUnits.$instance", "$hrStorageSize.$instance", "$hrStorageUsed.$instance" ] );
66if(!$resp) {
67	print "UNKNOWN\nUNKNOWN\n\n";
68	print "Plugin error: ".$snmp->errmsg()."\n";
69	exit 0;
70}
71
72print "".($resp->{"$hrStorageUsed.$instance"}*$resp->{"$hrStorageAllocationUnits.$instance"})."\n";
73print "".($resp->{"$hrStorageSize.$instance"}*$resp->{"$hrStorageAllocationUnits.$instance"})."\n";
74print "\n";
75print "Storage Used/Size for $MODULE\n";
76exit 0;
77