1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
package PINGDOMFETCH::Notify;
use strict;
use warnings;
use PINGDOMFETCH::Config;
use PINGDOMFETCH::DateHelper;
use PINGDOMFETCH::Display;
use PINGDOMFETCH::Utils;
use MIME::Lite;
our @ISA = ('PINGDOMFETCH::Display');
sub new {
my ( $class, %vals ) = @_;
my $self = bless \%vals, $class;
my $config = $self->{config};
$self->{message} = [];
$self->{warnings} = 0;
$self->{criticals} = 0;
return $self;
}
sub message_push {
my ( $self, $message ) = @_;
push @{ $self->{message} }, $message;
return undef;
}
sub message_unshift {
my ( $self, $message ) = @_;
my $config = $self->{config};
unshift @{ $self->{message} }, $message;
return undef;
}
sub has_messages {
my ($self) = @_;
return @{ $self->{message} } > 0 ? 1 : 0;
}
sub has_warnings {
my ($self) = @_;
return $self->{warnings} > 0 ? 1 : 0;
}
sub has_criticals {
my ($self) = @_;
return $self->{criticals} > 0 ? 1 : 0;
}
sub info_notification_send {
my ($self) = @_;
my $config = $self->{config};
$self->notification_send_to( $config->array('notify.info.email.to') );
return undef;
}
sub notification_send {
my ($self) = @_;
my $config = $self->{config};
$self->notification_send_to( $config->array('notify.email.to') );
return undef;
}
sub notification_send_to {
my ( $self, @email_to ) = @_;
return if !$self->has_messages();
my $config = $self->{config};
my $from = $config->get('notify.email.sender');
my $warning_less = $config->get('warning.if.avail.is.less');
my $critical_less = $config->get('critical.if.avail.is.less');
my ( $dh_from, $dh_to ) = ( $config->{'dh_from'}, $config->{'dh_to'} );
my $message = join '', @{ $self->{message} };
my $subject = do {
if ( $self->has_criticals() ) {
'!! ';
}
elsif ( $self->has_warnings() ) {
'! ';
}
else {
' ';
}
};
$subject .= 'Availability stats for ';
if ( $dh_from->is_begin_of_a_day()
and $dh_to->is_begin_of_a_day()
and 1 == $dh_from->days_until($dh_to) )
{
$subject .= $dh_from->day_str();
}
else {
$subject .= "'$dh_from' - '$dh_to'";
}
$message .= "Legend:\n";
$message .=
"'!' means: TLS or Service Availability is less than $warning_less% (Exception: Threshold is non-standard)\n";
$message .= "'!!' means: TLS Availability is less than $critical_less%\n\n";
$message .=
"Response times are not reasonable (collected from all over the world)!\n";
$message .= "\n" . get_version_full();
unless ( $config->bool('arg.notify-dummy') ) {
$self->send_mail( $from, $_, $subject, $message ) for @email_to;
}
else {
$self->info("Dummy-Email to stdout");
say $subject;
say "";
say $message;
}
$self->{messages} = [];
return undef;
}
sub send_mail {
my ( $self, $from, $to, $subject, $message ) = @_;
my $email = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Type => 'TEXT',
Data => $message,
);
$self->info("Sending email '$subject' to '$to'");
$email->send();
return undef;
}
1;
|