root/lib/Resmon/Module.pm

Revision cbbf0acaadfd823f2ccb4daa3e6e5e8f1bd724e0, 2.8 kB (checked in by Mark Harrison <mark@omniti.com>, 5 years ago)

Print a newline when printing the deprecation warning so it is readable

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

  • Property mode set to 100755
Line 
1 package Resmon::Module;
2
3 use strict;
4 use Data::Dumper;
5 use FileHandle;
6 use UNIVERSAL qw/isa/;
7 my %coderefs;
8
9 my $rmloading = "Registering";
10
11 sub fetch_monitor {
12   my $type = shift;
13   my $coderef = $coderefs{$type};
14   return $coderef if ($coderef);
15
16   # First if the monitor name is raw and looks right:
17   #   is a subclass of Resmon::Module and can 'handler'
18   # then we will promote it into the Resmon::Module namespace
19   # and use this one.
20   eval "use $type;";
21   if($type->isa(__PACKAGE__) && $type->can('handler')) {
22     eval "
23       package Resmon::Module::$type;
24       use vars qw/\@ISA/;
25       \@ISA = qw($type);
26       1;
27     ";
28     if($@) {
29       die "Could not repackage $type as Resmon::Module::$type\n";
30     }
31     return undef;
32   }
33   eval "use Resmon::Module::$type;";
34   return undef;
35 }
36
37 sub register_monitor {
38   my ($type, $ref) = @_;
39   if(ref $ref eq 'CODE') {
40     $coderefs{$type} = $ref;
41   }
42   print STDERR "$rmloading $type monitor\n";
43 }
44
45 sub fresh_status {
46   my $arg = shift;
47   print STDERR $arg->{type} . ": Warning: fresh_status() is deprecated, and no longer required.\n";
48   return undef;
49 }
50
51 sub fresh_status_msg {
52   # Deal with result caching if an 'interval' entry is placed in the config
53   # for that module
54   my $arg = shift;
55   return undef unless $arg->{interval};
56   my $now = time;
57   if(($arg->{lastupdate} + $arg->{interval}) >= $now) {
58     return $arg->{laststatus}, $arg->{lastmessage};
59   }
60   return undef;
61 }
62
63 sub set_status {
64   my $arg = shift;
65   $arg->{laststatus} = shift;
66   $arg->{lastmessage} = shift;
67   $arg->{lastupdate} = time;
68   if($arg->{laststatus} =~ /^([A-Z]+)\((.*)\)$/s) {
69     # This handles old-style modules that return just set status as
70     #     STATE(message)
71     $arg->{laststatus} = $1;
72     $arg->{lastmessage} = $2;
73   }
74   return ($arg->{laststatus}, $arg->{lastmessage});
75 }
76 sub config_as_hash {
77   my $self = shift;
78   my $conf = {};
79   while(my ($key, $value) = each %$self) {
80     if(! ref $value) {
81       # only stash scalars here.
82       $conf->{$key} = $value;
83     }
84   }
85   return $conf;
86 }
87
88 sub reload_module {
89     my $self = shift;
90     my $class = ref($self) || $self;
91     $class =~ s/::/\//g;
92     my $file = $INC{"$class.pm"};
93     # Deal with modules loaded from a LIB directory and not in
94     # lib/Resmon/Module: try MODNAME.pm instead of Resmon/Module/MODNAME.pm
95     unless ($file) {
96         $class =~ s/^.*\/([^\/]+)$/\1/;
97         $file = $INC{"$class.pm"};
98     }
99     print STDERR "Reloading module: $class\n";
100 #    my $fh = FileHandle->new($file);
101 #    local($/);
102     my $redef = 0;
103     local($SIG{__WARN__}) = sub {
104         if($_[0] =~ /[Ss]ubroutine ([\w:]+) redefined/ ) {
105             $redef++;
106             return;
107         }
108         warn @_;
109     };
110 #    eval <$fh>;
111     eval {do($file); die $@ if $@};
112     return $@ if $@;
113     return $redef;
114 }
115
116 $rmloading = "Demand loading";
117 1;
Note: See TracBrowser for help on using the browser.