add node monitor email in netdisco-do

This commit is contained in:
Oliver Gorwits
2014-07-22 21:06:44 +01:00
parent 888b522c7f
commit 2178860394
3 changed files with 107 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
package App::Netdisco::DB::Result::Virtual::NodeMonitor;
use strict;
use warnings;
use utf8;
use base 'DBIx::Class::Core';
__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
__PACKAGE__->table('node_monitor_virtual');
__PACKAGE__->result_source_instance->is_virtual(1);
__PACKAGE__->result_source_instance->view_definition(<<'ENDSQL');
SELECT nm.why, nm.cc, trim(trailing '.' from trim(trailing '0123456789' from date::text)) as date,
n.mac, n.switch, n.port,
d.name, d.location,
dp.name AS portname
FROM node_monitor nm, node n, device d, device_port dp
WHERE nm.mac = n.mac
AND nm.active
AND nm.cc IS NOT NULL
AND d.ip = n.switch
AND dp.ip = n.switch
AND dp.port = n.port
AND d.last_macsuck = n.time_last
ENDSQL
__PACKAGE__->add_columns(
"why",
{ data_type => "text", is_nullable => 1 },
"cc",
{ data_type => "text", is_nullable => 0 },
"date",
{ data_type => "timestamp", is_nullable => 0 },
"mac",
{ data_type => "macaddr", is_nullable => 0 },
"switch",
{ data_type => "inet", is_nullable => 0 },
"port",
{ data_type => "text", is_nullable => 0 },
"name",
{ data_type => "text", is_nullable => 0 },
"location",
{ data_type => "text", is_nullable => 1 },
"portname",
{ data_type => "text", is_nullable => 0 },
);
1;

View File

@@ -0,0 +1,52 @@
package App::Netdisco::Util::NodeMonitor;
use App::Netdisco;
use Dancer qw/:syntax :script/;
use Dancer::Plugin::DBIC 'schema';
use App::Netdisco::Util::DNS qw/hostname_from_ip ipv4_from_hostname/;
use base 'Exporter';
our @EXPORT_OK = qw/
monitor
/;
our %EXPORT_TAGS = (all => \@EXPORT_OK);
sub _email {
my ($to, $subject, $body) = @_;
my $domain = setting('domain_suffix') || 'localhost';
$domain =~ s/^\.//;
my $SENDMAIL = '/usr/sbin/sendmail';
open (SENDMAIL, "| $SENDMAIL -t") or die "Can't open sendmail at $SENDMAIL.\n";
print SENDMAIL "To: $to\n";
print SENDMAIL "From: Netdisco <netdisco\@$domain>\n";
print SENDMAIL "Subject: $subject\n\n";
print SENDMAIL $body;
close (SENDMAIL) or die "Can't send letter. $!\n";
}
sub monitor {
my $monitor = schema('netdisco')->resultset('Virtual::NodeMonitor');
while (my $entry = $monitor->next) {
my $body = <<"end_body";
........ n e t d i s c o .........
Node : @{[$entry->mac]} (@{[$entry->why]})
When : @{[$entry->date]}
Switch : @{[$entry->name]} (@{[$entry->switch]})
Port : @{[$entry->port]} (@{[$entry->portname]})
Location: @{[$entry->location]}
end_body
_email(
$entry->cc,
"Saw mac @{[$entry->mac]} (@{[$entry->why]}) on @{[$entry->name]} @{[$entry->port]}",
$body
);
}
}
1;