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