diff options
author | Kristian Lyngstol <kristian@bohemians.org> | 2016-05-20 16:48:03 +0200 |
---|---|---|
committer | Kristian Lyngstol <kristian@bohemians.org> | 2016-05-20 16:48:03 +0200 |
commit | 746e73a9ea27dc90047ac26950c21f963bf99af5 (patch) | |
tree | 62c2b3c642dfa08a12b9b6fbbb76da62e9074ad4 | |
parent | 212753aa48e60ccc9cbd74a16f313cebc6d3cda6 (diff) |
Add system-generated op logs
First up: Add a switch, get a log entry.
This might require some filtering in the GUI eventually. This is why the
user is set to 'system', even when we have an actual user that triggered
the event.
-rwxr-xr-x | include/nms.pm | 2 | ||||
-rw-r--r-- | include/nms/oplog.pm | 34 | ||||
-rw-r--r-- | include/nms/util.pm | 1 | ||||
-rwxr-xr-x | web/api/write/switch-add | 4 |
4 files changed, 38 insertions, 3 deletions
diff --git a/include/nms.pm b/include/nms.pm index 273d65d..df4cc3a 100755 --- a/include/nms.pm +++ b/include/nms.pm @@ -8,7 +8,7 @@ use JSON; package nms; use base 'Exporter'; -our @EXPORT = qw(switch_disconnect switch_connect_ssh switch_connect_dlink switch_exec switch_exec_json switch_timeout db_connect); +our @EXPORT = qw(db_connect); BEGIN { require "config.pm"; diff --git a/include/nms/oplog.pm b/include/nms/oplog.pm new file mode 100644 index 0000000..c5194cf --- /dev/null +++ b/include/nms/oplog.pm @@ -0,0 +1,34 @@ +# vim:ts=8:sw=8 +use strict; +use warnings; +use utf8; +use DBI; +use Data::Dumper; +use JSON; +use nms; +package nms::oplog; + +use base 'Exporter'; +our @EXPORT = qw(oplog); +my $dbh; +my $query; +my $user; + +use Data::Dumper; + + +sub oplog { + $query->execute($_[0], "[$user]" . $_[1]); + $dbh->commit; +} + +BEGIN { + $user = $ENV{'REMOTE_USER'} || "internal"; + $dbh = nms::db_connect(); + $query = $dbh->prepare("INSERT INTO oplog (username, systems, log) VALUES('system',?,?)"); +} + +END { + $dbh->disconnect(); +} +1; diff --git a/include/nms/util.pm b/include/nms/util.pm index d2382f9..898aa9a 100644 --- a/include/nms/util.pm +++ b/include/nms/util.pm @@ -10,7 +10,6 @@ our @EXPORT = qw(guess_placement parse_switches_txt parse_switches parse_switch) # Parse a single switches.txt-formatted switch sub parse_switch { my ($switch, $subnet4, $subnet6, $mgtmt4, $mgtmt6, $lolid, $distro) = split(/ /); - my %foo = guess_placement($switch); my %ret = ( 'sysname' => "$switch", 'subnet4' => "$subnet4", diff --git a/web/api/write/switch-add b/web/api/write/switch-add index 56e5cb2..d210567 100755 --- a/web/api/write/switch-add +++ b/web/api/write/switch-add @@ -11,6 +11,7 @@ use strict; use warnings; use JSON; use Data::Dumper; +use nms::oplog qw(oplog); $nms::web::cc{'max-age'} = "0"; @@ -83,10 +84,11 @@ foreach my $tmp2 (@tmp) { $nms::web::dbh->do("INSERT INTO SWITCHES (mgmt_v4_addr, sysname, poll_frequency, community, lldp_chassis_id, mgmt_v6_addr, placement,subnet4,subnet6,distro_name) VALUES ($template{'mgmt_v4_addr'}, $template{'sysname'}, $template{'poll_frequency'}, $template{'community'}, $template{'lldp_chassis_id'}, $template{'mgmt_v6_addr'}, $template{'placement'},$template{'subnet4'},$template{'subnet6'},$template{'distro_name'});"); push @added, $switch{'sysname'}; + oplog($switch{'sysname'}, "Switch added: " . $switch{'sysname'}); } } $json{'switches_addded'} = \@added; -print "X-ban: /api/.*switches.*\n"; +print "X-ban: /api/.*\n"; finalize_output(); |