File Coverage

blib/lib/Bio/GMOD/Admin/Monitor.pm
Criterion Covered Total %
statement 12 62 19.3
branch 0 18 0.0
condition 0 16 0.0
subroutine 4 15 26.6
pod 9 11 81.8
total 25 122 20.4


line stmt bran cond sub pod time code
1             package Bio::GMOD::Admin::Monitor;
2              
3 3     3   21 use strict;
  3         8  
  3         112  
4 3     3   17 use vars qw/@ISA/;
  3         5  
  3         117  
5 3     3   3083 use Bio::GMOD;
  3         9  
  3         84  
6             #use Bio::GMOD::Util::Email;
7 3     3   19 use Bio::GMOD::Util::Rearrange;
  3         6  
  3         3026  
8              
9             @ISA = qw/Bio::GMOD Bio::GMOD::Util::Email/;
10              
11             # A simple generic new method - no need to reload a MOD adaptor
12             # since most monitoring options are site-specific
13             sub new {
14 0     0 1   my ($class,@p) = @_;
15 0           my ($mod) = rearrange([qw/MOD/],@p);
16 0 0         if ($mod) {
17 0           my $gmod = Bio::GMOD->new(-mod=>$mod,-class=>$class);
18 0           return $gmod;
19             } else {
20 0           my $this = bless {},$class;
21 0           return $this;
22             }
23             }
24              
25             sub generate_report {
26 0     0 1   my ($self,@p) = @_;
27 0           my ($email_report,$log_report,$components,$email_to_ok,$email_to_warn,$email_from,$email_subject)
28             = rearrange([qw/EMAIL_REPORT LOG_REPORT COMPONENTS EMAIL_TO_OK EMAIL_TO_WARN EMAIL_FROM EMAIL_SUBJECT/],@p);
29              
30 0   0       $email_report ||= 'none';
31 0   0       $log_report ||= 'all';
32              
33 0           my $date = $self->fetch_date;
34 0           my $msg = <
35             $email_subject
36             DATE: $date
37              
38             END
39              
40 0           my $failed_flag; # Track whether any of the tests failed
41 0           foreach my $component (@$components) {
42 0           $msg .= $component->initial_status_string. "\n";
43 0 0         $msg .= $component->final_status_string . "\n" if ($component->final_status_string);
44 0           $msg .= "\n";
45 0 0         $failed_flag++ if $component->final_status_string;
46             }
47              
48 0 0         unless ($email_report eq 'none') {
49 0           my @to;
50 0 0         push (@to,$email_to_ok) if $email_to_ok;
51 0 0 0       push (@to,$email_to_warn) if $email_to_warn && $email_to_ok && ($email_to_warn ne $email_to_ok);
      0        
52              
53 0 0 0       unless ($email_report eq 'failures' && !$failed_flag) {
54             # Bio::GMOD::Util::Email->send_email(-to => \@to,
55             # -from => $email_from,
56             # -subject => $email_subject,
57             # -content => $msg
58             # );
59             }
60             }
61 0           print $msg;
62             }
63              
64              
65             # Generic accessors
66 0     0 1   sub status { return shift->{status}; }
67 0     0 1   sub tested_at { return shift->{tested_at}; }
68 0     0 1   sub testing { return shift->{testing}; }
69              
70 0     0 1   sub is_up { return shift->{is_up}; }
71 0     0 1   sub is_down { return shift->{is_down} }
72              
73 0     0 1   sub initial_status_string { return shift->{initial_status_string}; }
74 0     0 1   sub final_status_string { return shift->{final_status_string}; }
75              
76              
77             # Create a formatted string - useful for emails and such
78             sub build_status_string {
79 0     0 0   my ($self,@p) = @_;
80 0           my ($timing,$msg) = rearrange([qw/TIMING MSG/],@p);
81 0           my $status = $self->{$timing}->{status};
82              
83 0           my $MAX_LENGTH = 60;
84              
85 0           my $date = $self->fetch_date;
86             # Pad the string with '.' up to MAX_LENGTH in length;
87 0           my $string = sprintf("%-*s %*s [%s]",
88             (length $msg) + 1,$msg,
89             $MAX_LENGTH - ((length $msg) + 2),
90             ("." x ($MAX_LENGTH - ((length $msg) + 2))),
91             $status);
92 0           my $full_string = "[$date] $string";
93 0           $self->{$timing . "_status_string"} = $full_string;
94 0           return $full_string;
95             }
96              
97              
98             # Status flags are used for testing various services like acedb,
99             # mysqld, httpd or whatever
100             sub set_status {
101 0     0 0   my ($self,@p) = @_;
102 0           my ($timing,$msg,$status) = rearrange([qw/TIMING MSG STATUS/],@p);
103             # Timing is one of initial or final
104             # Status is true or false if the service is available
105              
106 0           $self->{$timing}->{status} = $status;
107 0           $self->{$timing}->{msg} = $msg;
108              
109             # Set up some redundant flags, provided as a convenience
110 0 0 0       if ($status eq 'up' || $status eq 'succeeded') {
111 0           $self->{is_up}++;
112 0           $self->{is_down} = undef;
113             }
114              
115 0           my $string = $self->build_status_string(-timing=>$timing,-msg=>$msg);
116              
117             # Return a boolean for status for easy testing
118 0 0         return ($string,$self->is_up ? 1 : 0);
119             }
120              
121              
122             __END__