Changeset b3c640775d36906eb2e9f8a2b1cbf1bbb0ba8fd4

Show
Ignore:
Timestamp:
03/22/07 02:34:40 (8 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1174530880 +0000
git-parent:

[1ba116e5b141130d922830f5316992432db538d7]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1174530880 +0000
Message:

make this stuff object oriented.. but Resmon::Modules should really by Resmon::Module

git-svn-id: https://labs.omniti.com/resmon/trunk@20 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • resmon

    r9adf5e8 rb3c6407  
    11#!/usr/bin/perl 
    22 
    3 push @INC, '/opt/resmon'; 
     3use lib '/opt/resmon/lib'; 
    44 
    55use strict; 
    66use Time::HiRes qw( gettimeofday tv_interval sleep ); 
     7use Data::Dumper; 
    78use POSIX qw( setsid ); 
    8 use vars qw($opt_c $opt_d $opt_f $list); 
     9use Getopt::Long; 
     10use vars qw($config_file $debug $status_file $config); 
    911 
    10 require 'getopts.pl'; 
    11 require 'resmon_conf.pl'; 
    12 require 'resmon_code.pl'; 
     12use Resmon::Config; 
     13use Resmon::ExtComm; 
     14use Resmon::Status; 
     15use Resmon::Modules; 
    1316 
    14 Getopts('c:df:'); 
     17GetOptions( 
     18  "c=s" => \$config_file, 
     19  "d"   => \$debug, 
     20  "f=s" => \$status_file, 
     21); 
    1522 
    16 $opt_c ||= 'resmon.conf'; 
    17 die "Cannot open configuration file: $opt_c" unless (-r $opt_c); 
     23$config_file ||= 'resmon.conf'; 
     24die "Cannot open configuration file: $config_file" unless (-r $config_file); 
    1825 
    1926sub configure { 
    20   $list = parse_config($opt_c); 
    21   set_statusfile($opt_f) if($opt_f); 
     27  $config = Resmon::Config->new($config_file); 
     28  $config->{statusfile} = $status_file if($status_file); 
     29  print Dumper($config) if($debug); 
    2230} 
    23 unless($opt_d) { 
     31unless($debug) { 
    2432  fork && exit; 
    2533  setsid; 
     
    2735configure(); 
    2836$SIG{'HUP'} = \&configure; 
     37 
     38use Time::HiRes qw( gettimeofday tv_interval sleep ); 
     39 
     40my $rmlast = undef; 
     41sub wait_interval { 
     42  $rmlast = [gettimeofday] unless defined($rmlast); 
     43  my $elapsed = $config->{interval} - tv_interval($rmlast); 
     44  if($elapsed > 0) { 
     45    sleep($elapsed); 
     46  } 
     47  $rmlast = [gettimeofday]; 
     48} 
     49 
     50my $list = []; 
     51my $status = Resmon::Status->new($config->{statusfile}); 
    2952while(1) { 
    30   foreach my $monobj (@$list) { 
    31     my $coderef = fetch_monitor($monobj->{'type'}); 
    32     unless($coderef) { 
    33       $coderef = fetch_monitor($monobj->{'type'}); 
    34     } 
    35     if($coderef) { 
    36       print_statusfile($monobj->{'object'}."(".$monobj->{'type'} 
    37                         .") :: ".$coderef->($monobj)."\n"); 
    38     } else { 
    39       print_statusfile($monobj->{'object'}."(".$monobj->{'type'} 
    40                         .") :: BAD(no monitor available)\n"); 
     53  while(my($module_name, $mod_configs) = each %{$config->{Modules}}) { 
     54    my $coderef = Resmon::Modules::fetch_monitor($module_name); 
     55    foreach my $monobj (@$mod_configs) { 
     56      my $check_rv = 'BAD', 
     57      my $check_mess = 'no data'; 
     58      if($coderef) { 
     59        eval { ($check_rv, $check_mess) = $coderef->($monobj); }; 
     60      } else { 
     61        eval { ($check_rv, $check_mess) = $monobj->handler(); }; 
     62      } 
     63      if($@) { 
     64        $status->store($module_name,$monobj->{'object'},'BAD',$@); 
     65      } else { 
     66        $status->store($module_name,$monobj->{'object'},$check_rv,$check_mess); 
     67      } 
    4168    } 
    4269  } 
    43   close_statusfile(); 
     70  $status->close(); 
    4471  wait_interval(); 
    4572  print "\n---- ".localtime(time)."----------\n" 
    46     unless open_statusfile(); 
     73    unless $status->open(); 
    4774} 
    4875