#!/usr/bin/perl -w
#
# cricket module which parses output from HP OmniBack omnistat command
# line utility (you need to have User Interface installed on machine
# on which you plan to run omni_parse.pl) and draws nice graphs about
# concurrent jobs (can be used to plan licenses)
#
# Dobrica Pavlinusic <dpavlin@rot13.org>
# http://www.rot13.org/~dpavlin/sysadm.html
# 
# Usage:
#
# 	parse_omni.pl [/path/to/omnistat]
#
# Warning: I am not sure that I can catch all types which are known to
# omnistat. So far, I check and report events which are in @stat.
# All other (if there are any) will be reported as other (I really need to
# re-read OmniBack documentation!)

use strict;

my $omnistat= shift @ARGV || "/usr/omni/bin/omnistat";

my @stat = (
#	'------------------' output width
	'In Progress',
	'In Progress/Failur',
	'In Progress/Errors',
	'Queuing',
	'Aborted',
	'Failed',
	'Completed',
	'Completed/Errors',
	'Completed/Failure',
	);

my $debug = 0;

my %backup;
my %restore;

open(O,"$omnistat |") || die "can't start $omnistat !";

my $header = <O>;
chomp $header;
# SessionID Type Status User
$header =~ m/^(\w+\s+)(\w+\s+)(\w+\s+)(\w+\s+)/;

my $tf = length($1);	# Type from pos
my $tl = length($2);	# Type len
my $sf = $tf+$tl;	# Status from pos
my $sl = length($3);	# Status len

my $media = 0;

while(<O>) {
	chomp;
	my $type = substr($_,$tf,$tl);
	$type =~ s/ +$//;
	my $status = substr($_,$sf,$sl);
	$status =~ s/ +$//;

	if ($type =~ m/Backup/i) {
		$backup{$status}++;
	} elsif ($type =~ m/Restore/i) {
		$restore{$status}++;
	} elsif ($type =~ m/Media/i) {
		$media++;
	} elsif ($type !~ m/=+/) {
		# don't report this on header!
		print STDERR "unknown type: '$type'\n";
	}
}
close(O);

# dump data

foreach (@stat) {
	print STDERR "$_: " if ($debug);
	if (defined $backup{$_}) {
		print $backup{$_},"\n";
		delete $backup{$_};
	} else {
		print "0\n";
	}
	if (defined $restore{$_}) {
		print $restore{$_},"\n";
		delete $restore{$_};
	} else {
		print "0\n";
	}
}

# dump backup other

my $other;
foreach (keys %backup) {
	print STDERR "unknown status backup: '$_'\n";
	$other += $backup{$_};
}
print $other || "0\n";

# dump restore other

undef $other;
foreach (keys %restore) {
	print STDERR "unkwown status restore: '$_'\n";
	$other += $restore{$_};
}
print $other || "0\n";

print "$media\n";

