root/resmon

Revision 5e9f5b0db78af749a3639226470497cca26caf9e, 2.3 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

fix print and move conf file to sample name

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

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