aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorKristian Lyngstol <kly@kly.no>2016-02-21 18:59:40 +0000
committerKristian Lyngstol <kly@kly.no>2016-02-21 18:59:40 +0000
commit1a448d0d7c53d25c87d31bd3b175f5b75644f618 (patch)
treeac91a62ff0928ede012c0c114eeacdc2f43dbbc8 /include
parent47e4039f8fae192f268a13b08e64424d835edb83 (diff)
nms: Refactoring work
Moving SNMP into a separate module reduces startup time for all the stuff that doesn't need it. Currently comments are broken because the js hasn't been updated.
Diffstat (limited to 'include')
-rwxr-xr-xinclude/nms.pm80
-rw-r--r--include/nms/snmp.pm89
-rwxr-xr-xinclude/nms/web.pm23
3 files changed, 109 insertions, 83 deletions
diff --git a/include/nms.pm b/include/nms.pm
index 4012cc6..6a9598e 100755
--- a/include/nms.pm
+++ b/include/nms.pm
@@ -6,7 +6,6 @@ use Net::OpenSSH;
use Net::Telnet;
use Data::Dumper;
use FileHandle;
-use SNMP;
use JSON;
package nms;
@@ -18,22 +17,9 @@ BEGIN {
eval {
require "config.local.pm";
};
-
- # $SNMP::debugging = 1;
-
- # sudo mkdir /usr/share/mibs/site
- # cd /usr/share/mibs/site
- # wget -O- ftp://ftp.cisco.com/pub/mibs/v2/v2.tar.gz | sudo tar --strip-components=3 -zxvvf -
- SNMP::initMib();
- SNMP::addMibDirs("../mibs");
- SNMP::loadModules('SNMPv2-MIB');
- SNMP::loadModules('ENTITY-MIB');
- SNMP::loadModules('IF-MIB');
- SNMP::loadModules('LLDP-MIB');
- SNMP::loadModules('IP-MIB');
- SNMP::loadModules('IP-FORWARD-MIB');
}
+
sub db_connect {
my $connstr = "dbi:Pg:dbname=" . $nms::config::db_name;
$connstr .= ";host=" . $nms::config::db_host unless (!defined($nms::config::db_host));
@@ -157,70 +143,6 @@ sub switch_disconnect($) {
waitpid($struct->{pid}, 0);
}
}
-
-sub snmp_open_session {
- my ($ip, $community, $async) = @_;
-
- $async //= 0;
-
- my %options = (UseEnums => 1);
- if ($ip =~ /:/) {
- $options{'DestHost'} = "udp6:$ip";
- } else {
- $options{'DestHost'} = "udp:$ip";
- }
-
- if ($community =~ /^snmpv3:(.*)$/) {
- my ($username, $authprotocol, $authpassword, $privprotocol, $privpassword) = split /\//, $1;
-
- $options{'SecName'} = $username;
- $options{'SecLevel'} = 'authNoPriv';
- $options{'AuthProto'} = $authprotocol;
- $options{'AuthPass'} = $authpassword;
-
- if (defined($privprotocol) && defined($privpassword)) {
- $options{'SecLevel'} = 'authPriv';
- $options{'PrivProto'} = $privprotocol;
- $options{'PrivPass'} = $privpassword;
- }
-
- $options{'Version'} = 3;
- } else {
- $options{'Community'} = $community;
- $options{'Version'} = 2;
- }
-
- my $session = SNMP::Session->new(%options);
- if (defined($session) && ($async || defined($session->getnext('sysDescr')))) {
- return $session;
- } else {
- die 'Could not open SNMP session to ' . $ip;
- }
-}
-
-# Not currently in use; kept around for reference.
-sub fetch_multi_snmp {
- my ($session, @oids) = @_;
-
- my %results = ();
-
- # Do bulk reads of 40 and 40; seems to be about the right size for 1500-byte packets.
- for (my $i = 0; $i < scalar @oids; $i += 40) {
- my $end = $i + 39;
- $end = $#oids if ($end > $#oids);
- my @oid_slice = @oids[$i..$end];
-
- my $localresults = $session->get_request(-varbindlist => \@oid_slice);
- return undef if (!defined($localresults));
-
- while (my ($key, $value) = each %$localresults) {
- $results{$key} = $value;
- }
- }
-
- return \%results;
-}
-
# A few utilities to convert from SNMP binary address format to human-readable.
sub convert_mac {
diff --git a/include/nms/snmp.pm b/include/nms/snmp.pm
new file mode 100644
index 0000000..b1354ae
--- /dev/null
+++ b/include/nms/snmp.pm
@@ -0,0 +1,89 @@
+#! /usr/bin/perl
+use strict;
+use warnings;
+use SNMP;
+use nms;
+package nms::snmp;
+
+use base 'Exporter';
+our @EXPORT = qw();
+
+BEGIN {
+ # $SNMP::debugging = 1;
+
+ # sudo mkdir /usr/share/mibs/site
+ # cd /usr/share/mibs/site
+ # wget -O- ftp://ftp.cisco.com/pub/mibs/v2/v2.tar.gz | sudo tar --strip-components=3 -zxvvf -
+ SNMP::initMib();
+ SNMP::addMibDirs("../mibs");
+ SNMP::loadModules('SNMPv2-MIB');
+ SNMP::loadModules('ENTITY-MIB');
+ SNMP::loadModules('IF-MIB');
+ SNMP::loadModules('LLDP-MIB');
+ SNMP::loadModules('IP-MIB');
+ SNMP::loadModules('IP-FORWARD-MIB');
+}
+
+sub snmp_open_session {
+ my ($ip, $community, $async) = @_;
+
+ $async //= 0;
+
+ my %options = (UseEnums => 1);
+ if ($ip =~ /:/) {
+ $options{'DestHost'} = "udp6:$ip";
+ } else {
+ $options{'DestHost'} = "udp:$ip";
+ }
+
+ if ($community =~ /^snmpv3:(.*)$/) {
+ my ($username, $authprotocol, $authpassword, $privprotocol, $privpassword) = split /\//, $1;
+
+ $options{'SecName'} = $username;
+ $options{'SecLevel'} = 'authNoPriv';
+ $options{'AuthProto'} = $authprotocol;
+ $options{'AuthPass'} = $authpassword;
+
+ if (defined($privprotocol) && defined($privpassword)) {
+ $options{'SecLevel'} = 'authPriv';
+ $options{'PrivProto'} = $privprotocol;
+ $options{'PrivPass'} = $privpassword;
+ }
+
+ $options{'Version'} = 3;
+ } else {
+ $options{'Community'} = $community;
+ $options{'Version'} = 2;
+ }
+
+ my $session = SNMP::Session->new(%options);
+ if (defined($session) && ($async || defined($session->getnext('sysDescr')))) {
+ return $session;
+ } else {
+ die 'Could not open SNMP session to ' . $ip;
+ }
+}
+
+# Not currently in use; kept around for reference.
+sub fetch_multi_snmp {
+ my ($session, @oids) = @_;
+
+ my %results = ();
+
+ # Do bulk reads of 40 and 40; seems to be about the right size for 1500-byte packets.
+ for (my $i = 0; $i < scalar @oids; $i += 40) {
+ my $end = $i + 39;
+ $end = $#oids if ($end > $#oids);
+ my @oid_slice = @oids[$i..$end];
+
+ my $localresults = $session->get_request(-varbindlist => \@oid_slice);
+ return undef if (!defined($localresults));
+
+ while (my ($key, $value) = each %$localresults) {
+ $results{$key} = $value;
+ }
+ }
+
+ return \%results;
+}
+
diff --git a/include/nms/web.pm b/include/nms/web.pm
index b768104..b13fa9a 100755
--- a/include/nms/web.pm
+++ b/include/nms/web.pm
@@ -1,6 +1,8 @@
#! /usr/bin/perl
+# vim:ts=8:sw=8
use strict;
use warnings;
+use utf8;
use CGI qw(fatalsToBrowser);
use DBI;
use Data::Dumper;
@@ -9,14 +11,14 @@ use nms;
package nms::web;
use base 'Exporter';
-our @EXPORT = qw(finalize_output json cgi dbh);
+our @EXPORT = qw(finalize_output json cgi dbh db_safe_quote);
our $cgi;
-our %json = ();
+our %json;
our $dbh;
our $now;
our $when;
our $ifname;
-our %cc = ();
+our %cc;
# Print cache-control from %cc
sub printcc {
@@ -29,13 +31,26 @@ sub printcc {
print 'Cache-Control: ' . $line . "\n";
}
+sub db_safe_quote {
+ my $word = $_[0];
+ my $term = $cgi->param($word);
+ if (!defined($term)) {
+ if(defined($_[1])) {
+ $term = $_[1];
+ } else {
+ die "Missing CGI param $word";
+ }
+ }
+ return $dbh->quote($term) || die;
+}
+
# returns a valid $when statement
# Also sets cache-control headers if time is overridden
sub setwhen {
my $when;
$now = "now()";
if (defined($cgi->param('now'))) {
- $now = "'" . $cgi->param('now') . "'::timestamp ";
+ $now = db_safe_quote('now') . "::timestamp ";
$cc{'max-age'} = "3600";
}
$when = " time > " . $now . " - '5m'::interval and time < " . $now . " ";