File: emailreports.pm

package info (click to toggle)
monitorix 3.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,036 kB
  • sloc: perl: 57,375; makefile: 220; sh: 183
file content (220 lines) | stat: -rw-r--r-- 5,644 bytes parent folder | download | duplicates (2)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#
# Monitorix - A lightweight system monitoring tool.
#
# Copyright (C) 2005-2022 by Jordi Sanfeliu <jordi@fibranet.cat>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

package emailreports;

use strict;
use warnings;
use Monitorix;
use MIME::Lite;
use LWP::UserAgent;
use Exporter 'import';
our @EXPORT = qw(emailreports_send);

sub emailreports_send {
	my $myself = (caller(0))[3];
	my ($config, $report, $when, $debug) = @_;
	my $emailreports = $config->{emailreports};
	my $imgfmt_lc = lc($config->{image_format});

	my $base_cgi = $config->{base_cgi};
	my $imgs_dir = $config->{imgs_dir};
	my $images;
	my $mime;
	my $subject;

	logger("$myself: sending $report reports.");

	my $uri = URI->new($emailreports->{url_prefix});
	my $scheme = $uri->scheme || "";
	my $userinfo = $uri->userinfo || "";
	$userinfo .= "@" unless !$userinfo;
	my $hostname = $uri->host || "";
	my $port = $uri->port || "";

	my $prefix = "$scheme://$userinfo$hostname:$port";

	$mime = "image/png";
	$mime = "image/svg+xml" if uc($config->{image_format}) eq "SVG";

	my $html = <<"EOF";
<html>
  <head>
  <style>
EOF

	open(IN, "$config->{base_dir}/css/white.css");
	while(<IN>) { $html .= $_; }
	close(IN);

	$html .= <<"EOF";
  </style>
  </head>
  <table class='cgi-header-table'>
  <tr>
     <th class='td-title'><b>&nbsp;&nbsp;Host:&nbsp;</b></th>
     <td class='td-title-host'>
       <b>&nbsp;&nbsp;$hostname&nbsp;&nbsp;</b>
     </td>
     <td class='td-title' ><b>&nbsp;&nbsp;$report&nbsp;&nbsp;</b></td>
  </tr>
  </table>
  <br>
EOF

	my $html_footer = <<EOF;
  <p class='text-copyright'>
  <a href='https://www.monitorix.org'><img src='cid:image_logo' border='0'></a>
  <br>
  Copyright &copy; 2005-2022 Jordi Sanfeliu
  </body>
</html>
EOF

	foreach (split(',', $emailreports->{$report}->{graphs})) {
		my $g = trim($_);
		my $n;
		my $e;
		my $ssl = "";

		$ssl = "ssl_opts => {verify_hostname => 0}"
			if lc($config->{accept_selfsigned_certs}) eq "y";

		# generate the graphs and get the html source
		my $url = $prefix . $base_cgi . "/monitorix.cgi?mode=localhost&graph=_$g&when=$when&color=white";
		my $ua = LWP::UserAgent->new(timeout => 120, $ssl);
		$ua->agent($config->{user_agent_id}) if $config->{user_agent_id} || "";

		my $response = $ua->request(HTTP::Request->new('GET', $url));

		if(!$response->is_success) {
			logger("$myself: ERROR: Unable to connect to '$url'.");
			logger("$myself: " . $response->status_line);
			$html .= "<pre>\n";
			$html .= $response->status_line;
			$html .= "</pre>\n";
		}

		my $data = $response->content;
		$e = 0;
		foreach ($data =~ /<!-- graph table begins -->/gi) {
			$data =~ s/\n/@@@/g;
			(my $graph) = $data =~ m/<!-- graph table begins -->@@@(.*?)<!-- graph table ends -->/;

			if(!$graph) {
				logger("$myself: unable to retrieve graphs from '$g'. It's enabled?");
				next;
			}

			$graph =~ s/@@@/\n/g;

			$graph =~ s/<a href=.*?>//g;
			$graph =~ s/><\/a>/>/g;

			# get the images
			my @tmp = ();
			$n = 1;
			foreach (split('\n', $graph)) {
				if(/<img src=/) {
					push(@tmp, "<img src='cid:image_$g$e$n.$imgfmt_lc' border='0'>");
					($url) = $_ =~ m/<img src=['"](.*?)['"] /;
					my $uri = URI->new($url);
					my $path = $uri->path || "";
					$response = $ua->request(HTTP::Request->new('GET', "$prefix$path"));
					$images->{"image_$g$e$n.$imgfmt_lc"} = $response->content;
					$n++;
				} else {
					push(@tmp, $_);
				}
			}

			$html .= join("\n", @tmp);
			$html .= "<br>";

			$data =~ s/<!-- graph table begins -->.*?<!-- graph table ends -->//;
			$e++;
		}
	}

	# addendum data included in report
	if($emailreports->{$report}->{addendum_script}) {
		$html .= <<"EOF";
  <table class='table-module' width='1'>
  <tr>
     <td class='td-title'><b>&nbsp;&nbsp;Addendum data&nbsp;</b></td>
  </tr>
  <tr>
     <td class='td-addendum'>
     <pre>
EOF

		$html .= `$emailreports->{$report}->{addendum_script}`;
		$html .= "\n";
		$html .= "  </pre>\n";
		$html .= "  </td>\n";
		$html .= " </tr>\n";
		$html .= "</table>\n";
	}

	$html .= $html_footer;

	$subject = $emailreports->{subject_prefix} || "Monitorix:";

	# create the multipart container and add attachments
	foreach (split(',', $emailreports->{$report}->{to})) {
		my $to = trim($_);

		my $msg = new MIME::Lite(
			From		=> $emailreports->{from_address},
			To		=> $to,
			Subject		=> "$subject '$report' Report",
			Type		=> "multipart/related",
			Organization	=> "Monitorix",
		);

		$msg->attach(
			Type		=> 'text/html',
			Data		=> $html,
		);
		$msg->attach(
			Type		=> 'image/png',
			Id		=> 'image_logo',
			Path		=> $config->{base_dir} . $config->{logo_bottom},
		);
		while (my ($key, $val) = each(%{$images})) {
			$msg->attach(
				Type		=> "$mime; name=\"$key\"",
				Id		=> $key,
				Data		=> $val,
			);
		}

		if(lc($emailreports->{method} || "") eq "relay") {
			$msg->send();

		} else {
			$msg->send('smtp', $emailreports->{smtp_hostname}, Timeout => 60);
		}

		logger("\t$myself: to: $to") if $debug;
	}
}

1;