#!/usr/bin/perl -w
# Copyright (C) 2009  Glen Pitt-Pladdy
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#
#
# See: http://www.pitt-pladdy.com/blog/_20111116-201219_0000_OpenVz_User_Beancounters_UBC_on_Cacti_via_SNMP/
#
# Put Vz parameter ID on command line

$FILE = '/var/local/snmp/vz';

$param = shift @ARGV;
if ( $param !~ /^\w+(:\w+)?$/ ) {
	die "FATAL - need the numeric parameter to show\n";
}

# parse file for parameters

open ST, '<', $FILE
	or die "FATAL - can't read \"$FILE\": $!\n";
my %stat;
while ( defined ( $line = <ST> ) ) {
	chomp $line;
	if ( $line !~ /^(\d+):(\w.+)=(\w.*)$/ ) { next; }
	if ( $2 ne $param ) { next; }
	if ( $2 ne 'hostname' and $3 >= 2147483647 ) {
		$stat{$1} = 'U';	# no real limit
		next;
	}
	$stat{$1} = $3;
}
close ST;

for my $id (sort keys %stat) {
	print "$stat{$id}\n";
}


