package Monitor::Monitor;

require 5.001;

use strict;
use CWorker;
require Schedule;


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

sub new
{
   print STDERR "new Monitor::Monitor object????\n";
}


sub add_check {
}



sub discover {
}


sub check
{
}


sub late_check
{
}


sub cycle_done {
}


sub unconfigure {
}


sub log
{
}


sub run
{
    my( $self, $mode, @args ) = @_;

    $self->{"monitor_mode"} = $mode;
    if( defined $self->{"monitor_reports_$mode"} ) {
	my $id;

	foreach $id (@{$self->{"monitor_reports_$mode"}}) {
	    uxmon::unreport( $id->[0], $id );
	}
	foreach $id (@{$self->{"monitor_variables_$mode"}}) {
	    uxmon::unset_variable( $id->[0], $id->[1] );
	}
    }
    $self->{"monitor_reports_$mode"} = $self->{"monitor_variables_$mode"} = [];
    if( $mode eq "check" ) {
	$self->check(@args);
    }
    elsif( $mode eq "discover" ) {
        $self->discover( @args );
    }
    elsif( $mode eq "late" ) {
	$self->late_check( @args );
    }
    elsif( $mode eq "cycle_done" ) {
	$self->cycle_done( @args );
    }
    elsif( $mode eq "unconfigure" ) {
	$self->unconfigure( @args );
    }
}


sub perfreport {
    my( $self, $alias, $variable, $time, $value ) = @_;

    uxmon::perfreport( $alias, $variable, $time, $value );
}


sub report {
    my( $self, $item, $value, $comment ) = @_;
    my $id;

    push( @{$self->{"monitor_reports_".$self->{"monitor_mode"}}},
	  $id = [ $item, $value ] );
    uxmon::report( $item, $value, $comment, $id );
}


sub event {
    my( $self, $item, $time, $priority, $comment ) = @_;

    BigSister::common::log( $priority, "$item: $comment" );
    uxmon::postevent( $item, $time, $priority, $comment );
}


sub set_variable {
    my( $self, $host, $variable, $value ) = @_;

    $host =~ s/ /_/g;
    $variable =~ s/ /_/g;
    unless( $host) { $host = $BigSister::common::hostname; }
    push( @{$self->{"monitor_variables_".$self->{"monitor_mode"}}},
	  [ $host, $variable ] );
    uxmon::set_variable( $host, $variable, $value );
}


sub execute_on {
    my( $self, $host, $command ) = @_;

    if( $host eq $BigSister::common::hostname || ! $host ) {
        return $command;
    }
    my $cmd = "$command";
    if( $self->{"rexec_command"} ) {
        $cmd = $self->{"rexec_command"};
    }
    $cmd =~ s/\$host/$host/;
    $cmd =~ s/\$command/$command/;
    return $cmd;
}


sub set_execute_on {
    my( $self, $cmd ) = @_;

    $self->{"rexec_command"} = $cmd;
}
      

sub timeout_execute {
    my( $self, $command, $timeout ) = @_;
    my $pid;
    
    my $text = "";
    if( ! defined $timeout ) { $timeout = 10; }
    eval {
        $SIG{"ALRM"} = sub { die ""; };
	Platform::bsalarm( $timeout );
        $pid = open( _CMD, "$command|" );
	while( <_CMD> ) {
	    $text .= $_;
 	}
	close( _CMD );
	undef $pid;
	Platform::bsalarm(0);
    };
    if( $pid ) {
        kill 15, $pid;
	eval {
	    $SIG{"ALRM"} = sub { kill 9, $pid; die ""; };
	    Platform::bsalarm(1);
	    waitpid $pid, 0;
	}
    }
    $SIG{"ALRM"} = "IGNORE";
    return( $text );
}


# make a string safe for use in HTML
my %htmlent = ('<' => 'lt',
              '>' => 'gt',
              '&' => 'amp');
foreach (0x00..0x08, 0x0c, 0x0e..0x1f, 0x7f..0xff) {
  $htmlent{pack('C', $_)} = sprintf('<%-2.2X>', $_);
}
my $htmlent = join('', keys %htmlent);

sub html_protect {
  my ($self, $str) = @_;
  if ($str eq '') {
    $str = '&nbsp;'
  } else {
    $str =~ s/([$htmlent])/&$htmlent{$1};/g;
  }
  $str;
}


1;
