
package Monitor::metastat;

require 5.002;

use strict;
use Time::Local;

@Monitor::metastat::ISA = qw(Monitor::Monitor);

sub new
{
    my ($this, $cmd ) = @_;
    my  $class = ref($this) || $this;
    my  $self = {};

    bless($self, $class);

    $cmd = "/usr/opt/SUNWmd/sbin/metastat" unless( $cmd );
    $self->{"cmd"} = $cmd;
    $self->{"lastpoll"} = 0;
    $self->{"freq"} = 600;

    $self->{"cache"} = {};
    $self->{"htable"} = [];

    return($self);
}




sub add_check {
    my( $self, $item, $name, $okay, $yellow, $red ) = @_;
    my( $htable ) = $self->{"htable"};

    ($item = $uxmon::hostname.".md") unless( $item );
    push( @$htable, [ $item, $name, $okay, $yellow, $red ] );
}



sub check
{
    my ($self) = @_;
    my( $htable ) = $self->{"htable"};
    my( $now ) = time;
    my( @tab, $md );

    print "checking metastat ...\n" if( $BigSister::common::dl>2 );
    open( IN, $self->{"cmd"}."|" );
    while( <IN> ) {
	chomp;
	s/\s+$//;
	if( /^([a-z0-9]+): (.*)/ ) {
	    push( @tab, $md = {} );
	    $md->{"device"} = $1;
	    $md->{"type"} = $2;
	    $md->{"state"} = "Okay"; #default
	}
	elsif( /^\s{5,}/ ) {
	    next;
	}
	elsif( /^\s*State: (.*)/ ) {
	    $md->{"state"} = $1;
	}
	elsif( /^\s*Size: (\d+)/ ) {
	    $md->{"size"} = $1;
	}
	elsif( /^\s*Master Device: (.*)/ ) {
	    $md->{"master"} = $1;
	}
	elsif( /^\s*Logging Device: (.*)/ ) {
	    $md->{"log"} = $1;
	}
	elsif( /^\s*Submirror (\d): (.*)/ ) {
	    $md->{"sub$1"} = $2;
	}
	elsif( /^\s*Read option: (.*)/ ) {
	    $md->{"readopt"} = $1;
	}
	elsif( /^\s*Write option: (.*)/ ) {
	    $md->{"writeopt"} = $1;
	}
    }
    close IN;
    my $cache = $self->{"cache"} = \@tab;

    my( $item, $name, $okay, $yellow, $red );
    my( $check, $i, $j, $txt, $dev, $lev );
    my( $reported, $repitem );
    for( $i=0; $i<=$#$htable; $i++ ) {
	$check = $$htable[$i];
	( $item, $name, $okay, $yellow, $red ) = @$check;
	print "checking metastat( $item, $name, $okay, $yellow, $red )\n" if( $BigSister::common::dl>2 );
	$repitem = $item;
	for( $j=$#$cache; $j>=0; $j-- ) {
	    $md = $$cache[$j];
	    $txt = $md->{$name};
	    $dev = $md->{"device"};
	    $lev = $BigSister::common::status_codes{"green"};
	    if( $okay && ($txt =~ /$okay/) ) {
		$lev = $BigSister::common::status_codes{"green"};
	    }
	    elsif( $yellow && ($txt =~ /$yellow/) ) {
		$lev = $BigSister::common::status_codes{"yellow"};
	    }
	    elsif( $red && ($txt =~ /$red/) ) {
		$lev = $BigSister::common::status_codes{"red"};
	    }
	    elsif( ! $okay ) {
		$lev = $BigSister::common::status_codes{"green"};
	    }
	    elsif( ! $yellow ) {
		$lev = $BigSister::common::status_codes{"yellow"};
	    }
	    elsif( ! $red ) {
		$lev = $BigSister::common::status_codes{"red"};
	    }
	    if( $lev<$BigSister::common::status_codes{"green"} ) {
		$self->report( $item, $lev, $dev."(".($md->{"type"}).") ".$txt );
		$reported++;
	    }
	}
    }
    if( $repitem && ! $reported ) {
	$self->report( $repitem, $BigSister::common::status_codes{"green"}, "metadevices OK" );
    }
}

1;
