root/resmon

Revision 627a96772d62dfc3c4d65c35f81a7f266d5a4719, 2.3 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 8 years ago)

make this more extensible and make Resmon auto 'use lib' based off \-tcsh

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

  • Property mode set to 100755
Line 
1 #!/usr/bin/perl
2
3 BEGIN {
4   (my $dir = $0) =~ s/\/?[^\/]+$//;
5   print "use lib '$dir/lib';\n";
6   eval "use lib '$dir/lib';";
7   die $@ if($@);
8 };
9
10 use strict;
11 use Time::HiRes qw( gettimeofday tv_interval sleep );
12 use POSIX qw( setsid );
13 use Getopt::Long;
14 use Data::Dumper;
15 use vars qw($config_file $debug $status_file $interface $port $config);
16
17 use Resmon::Config;
18 use Resmon::ExtComm;
19 use Resmon::Status;
20 use Resmon::Module;
21
22 GetOptions(
23   "i=s" => \$interface,
24   "p=i" => \$port,
25   "c=s" => \$config_file,
26   "d"   => \$debug,
27   "f=s" => \$status_file,
28 );
29
30 $config_file ||= "$0.conf";
31 die "Cannot open configuration file: $config_file" unless (-r $config_file);
32
33 sub configure {
34   $config = Resmon::Config->new($config_file);
35   $config->{statusfile} = $status_file if($status_file);
36 }
37
38 $SIG{'HUP'} = \&configure;
39 configure();
40
41 my $sigint = 0;
42 sub sigint_handler { $sigint = 1; }
43 $SIG{'INT'} = \&sigint_handler;
44
45 my $rmlast = undef;
46 sub wait_interval {
47   $rmlast = [gettimeofday] unless defined($rmlast);
48   my $elapsed = $config->{interval} - tv_interval($rmlast);
49   if($elapsed > 0) {
50     sleep($elapsed);
51   }
52   $rmlast = [gettimeofday];
53 }
54
55 my $list = [];
56 my $status = Resmon::Status->new($config->{statusfile});
57 $status->open();
58 $status->serve_http_on($interface, $port) if($port);
59
60 unless($debug) {
61   fork && exit;
62   setsid;
63   close(STDIN);
64   close(STDOUT);
65   close(STDERR);
66   fork && exit;
67 }
68
69 while(1) {
70   while(my($module_name, $mod_configs) = each %{$config->{Module}}) {
71     my $coderef = Resmon::Module::fetch_monitor($module_name);
72     foreach my $monobj (@$mod_configs) {
73       my $check_rv = 'BAD',
74       my $check_mess = 'no data';
75       my $starttime = [gettimeofday];
76       if($coderef) {
77         eval { ($check_rv, $check_mess) = $coderef->($monobj); };
78       } else {
79         eval { ($check_rv, $check_mess) = $monobj->handler(); };
80       }
81       my $results = { last_runtime_seconds => tv_interval($starttime) };
82       if($@) {
83         $results->{state} = 'BAD';
84         $results->{message} = $@;
85       } else {
86         $results->{state} = $check_rv;
87         $results->{message} = $check_mess;
88       }
89       $status->store($module_name,$monobj->{'object'}, $results);
90     }
91   }
92   $status->close();
93   die "Exiting.\n" if($sigint);
94   wait_interval();
95   die "Exiting.\n" if($sigint);
96   print "\n---- ".localtime(time)."----------\n"
97    unless $status->open();
98 }
99
Note: See TracBrowser for help on using the browser.