From 2178860394c5cc04592c25803422cb72dc95132c Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Tue, 22 Jul 2014 21:06:44 +0100 Subject: [PATCH] add node monitor email in netdisco-do --- Netdisco/bin/netdisco-do | 6 +++ .../Netdisco/DB/Result/Virtual/NodeMonitor.pm | 49 +++++++++++++++++ Netdisco/lib/App/Netdisco/Util/NodeMonitor.pm | 52 +++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 Netdisco/lib/App/Netdisco/DB/Result/Virtual/NodeMonitor.pm create mode 100644 Netdisco/lib/App/Netdisco/Util/NodeMonitor.pm diff --git a/Netdisco/bin/netdisco-do b/Netdisco/bin/netdisco-do index 34a1a52d..89adafea 100755 --- a/Netdisco/bin/netdisco-do +++ b/Netdisco/bin/netdisco-do @@ -87,6 +87,12 @@ if (!length $action) { App::Netdisco::Util::Graph::graph(); return ('done', 'Generated graph data.'); } + + use App::Netdisco::Util::NodeMonitor (); + sub monitor { + App::Netdisco::Util::NodeMonitor::monitor(); + return ('done', 'Generated monitor data.'); + } } my $worker = MyWorker->new(); diff --git a/Netdisco/lib/App/Netdisco/DB/Result/Virtual/NodeMonitor.pm b/Netdisco/lib/App/Netdisco/DB/Result/Virtual/NodeMonitor.pm new file mode 100644 index 00000000..40813bff --- /dev/null +++ b/Netdisco/lib/App/Netdisco/DB/Result/Virtual/NodeMonitor.pm @@ -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; diff --git a/Netdisco/lib/App/Netdisco/Util/NodeMonitor.pm b/Netdisco/lib/App/Netdisco/Util/NodeMonitor.pm new file mode 100644 index 00000000..6c47e67c --- /dev/null +++ b/Netdisco/lib/App/Netdisco/Util/NodeMonitor.pm @@ -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 \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;