#!perl

=head1 NAME

hv_monitor - LibreNMS style JSON SNMP extend for hypervisor monitoring.

=head1 SYNOPSIS

hv_monitor [B<-c>] [B<-b> <backend>]

=head1 DESCRIPTION

For cron...

    */5 * * * * /usr/local/bin/hv_monitor > /var/cache/hv_monitor.json -c 2> /dev/null

For snmpd...

    extend hv-monitor /bin/cat /var/cache/hv_monitor.json

=head1 FLAGS

=head2 -b <backend>

The backend to use.

Defaults are as below.

    FreeBSD: CBSD
    Linux: Libvirt

=head2 -c

Compress the output using gzip and base64 encoded so it
can be transmitted via SNMP with out issue.

=cut

use JSON;
use strict;
use warnings;
use Getopt::Long;
use JSON;
use HV::Monitor;
use MIME::Base64;
use IO::Compress::Gzip qw(gzip $GzipError);
use Pod::Usage;

sub version {
	print 'hv_monitor v. ' . $HV::Monitor::VERSION . "\n";
}


my $backend;
if ( $^O eq 'freebsd' ) {
	$backend = 'CBSD';
} elsif ( $^O eq 'linux' ) {
	$backend = 'Libvirt';
}

# get the commandline options
my $help     = 0;
my $version  = 0;
my $compress = 0;
Getopt::Long::Configure('no_ignore_case');
Getopt::Long::Configure('bundling');
GetOptions(
	'version' => \$version,
	'v'       => \$version,
	'help'    => \$help,
	'h'       => \$help,
	'b=s'     => \$backend,
	'c'       => \$compress,
);

if ($version) {
		print 'hv_monitor v. ' . $HV::Monitor::VERSION . "\n";
}

if ($help) {
        pod2usage( -exitval => 255, -verbose => 2, -output => \*STDOUT, );
}

if ($version) {
	exit 255;
}

my $hm = HV::Monitor->new( { backend => $backend } );

eval { $hm->load; };
if ($@) {
	print encode_json( { version => 1, data => {}, error => 1, errorString => 'load failed... ' . $@ } ) . "\n";
	exit 1;
}

my $data = encode_json( $hm->run );

if ( !$compress ) {
	print $data. "\n";
	exit;
}

# compress and write to the cache file for it
my $compressed_string;
gzip \$data => \$compressed_string;
my $compressed = encode_base64($compressed_string);
$compressed =~ s/\n//g;
$compressed = $compressed . "\n";
print $compressed;
