blib/lib/App/Netdisco/Util/NodeMonitor.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 64 CC 2 SCAR 10.0
CC
2
Cov
0%
_email
6.0
_header_value
2.0
_headers
2.0
CRAP 2.7
1 package App::Netdisco::Util::NodeMonitor;
2  
3 use App::Netdisco;
4  
5 use Dancer qw/:syntax :script/;
6 use Dancer::Plugin::DBIC 'schema';
7  
8 use Net::Domain 'hostfqdn';
9 use App::Netdisco::Util::DNS qw/hostname_from_ip ipv4_from_hostname/;
10  
11 use base 'Exporter';
12 our @EXPORT_OK = qw/
13 monitor
14 /;
15 our %EXPORT_TAGS = (all => \@EXPORT_OK);
16  
17 sub _header_value {
18 (my $value = shift) =~ s/[\r\n]+/ /g;
19 return $value;
20 }
21  
22 sub _headers {
23 my ($to, $subject, $domain) = @_;
24 return (
25 "To: @{[_header_value($to)]}\n",
26 "From: Netdisco <netdisco\@@{[_header_value($domain)]}>\n",
27 "Subject: @{[_header_value($subject)]}\n\n",
28 );
29 }
30  
31 sub _email {
32 my ($to, $subject, $body) = @_;
33 return unless $to;
34 my $domain = (hostfqdn || 'fqdn-undefined');
35  
36 my $SENDMAIL = '/usr/sbin/sendmail';
37 open (SENDMAIL, "| $SENDMAIL -t") or die "Can't open sendmail at $SENDMAIL.\n";
38 print SENDMAIL _headers($to, $subject, $domain);
39 print SENDMAIL $body;
40 close (SENDMAIL) or die "Can't send letter. $!\n";
41 }
42  
43 sub monitor {
44 my $monitor = schema(vars->{'tenant'})->resultset('Virtual::NodeMonitor');
45  
46 while (my $entry = $monitor->next) {
47 my $body = <<"end_body";
48 ........ n e t d i s c o .........
49 Node : @{[$entry->mac]} (@{[$entry->why]})
50 When : @{[$entry->date]}
51 Switch : @{[$entry->name]} (@{[$entry->switch]})
52 Port : @{[$entry->port]} (@{[$entry->portname]})
53 Location: @{[$entry->location]}
54  
55 end_body
56  
57 debug sprintf ' monitor - reporting on %s at %s:%s',
58 $entry->mac, $entry->name, $entry->port;
59 _email(
60 $entry->cc,
61 "Saw mac @{[$entry->mac]} (@{[$entry->why]}) on @{[$entry->name]} @{[$entry->port]}",
62 $body
63 );
64 }
65 }
66  
67 1;