diff options
-rwxr-xr-x | include/nms/web.pm | 83 | ||||
-rwxr-xr-x | web/nms.gathering.org/comment.pl | 16 | ||||
-rwxr-xr-x | web/nms.gathering.org/port-state.pl | 67 |
3 files changed, 148 insertions, 18 deletions
diff --git a/include/nms/web.pm b/include/nms/web.pm new file mode 100755 index 0000000..b768104 --- /dev/null +++ b/include/nms/web.pm @@ -0,0 +1,83 @@ +#! /usr/bin/perl +use strict; +use warnings; +use CGI qw(fatalsToBrowser); +use DBI; +use Data::Dumper; +use JSON; +use nms; +package nms::web; + +use base 'Exporter'; +our @EXPORT = qw(finalize_output json cgi dbh); +our $cgi; +our %json = (); +our $dbh; +our $now; +our $when; +our $ifname; +our %cc = (); + +# Print cache-control from %cc +sub printcc { + my $line = ""; + my $first = ""; + foreach my $tmp (keys(%cc)) { + $line .= $first . $tmp . "=" . $cc{$tmp}; + $first = ", "; + } + print 'Cache-Control: ' . $line . "\n"; +} + +# 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 "; + $cc{'max-age'} = "3600"; + } + $when = " time > " . $now . " - '5m'::interval and time < " . $now . " "; + return $when; +} + +# Sets the ifname. If we are logged in, it's simply set to "ifname", otherwise +# it's hashed for anonymization. +sub obfuscateifname { + my $ifname = "ifname"; + if (defined($cgi->param('public'))) { + $ifname = "regexp_replace(ifname, 'ge-0/0/(([0-3][0-9])|(4[0-3])|([0-9]))\$',concat('ge-participant',sha1_hmac(ifname::bytea,'".$nms::config::nms_hash."'::bytea))) as ifname"; + } + return $ifname; +} + +sub finalize_output { + my $query; + $query = $dbh->prepare ('select ' . $now . ' as time;'); + $query->execute(); + + $json{'time'} = $query->fetchrow_hashref()->{'time'}; + $json{'username'} = $cgi->remote_user(); + printcc; + + print $cgi->header(-type=>'text/json; charset=utf-8'); + print JSON::XS::encode_json(\%json); + print "\n"; +} + +BEGIN { + $cgi = CGI->new; + + $cc{'stale-while-revalidate'} = "3600"; + $cc{'max-age'} = "5"; + + $dbh = nms::db_connect(); + # FIXME: Shouldn't be magic. + # Only used for setting time in result from DB time. + # FIXME: Clarification, this _has_ to be set before setwhen is run, + # since it secretly overrides it. + $when = setwhen(); + $ifname = obfuscateifname(); +} +1; diff --git a/web/nms.gathering.org/comment.pl b/web/nms.gathering.org/comment.pl new file mode 100755 index 0000000..d22dcdc --- /dev/null +++ b/web/nms.gathering.org/comment.pl @@ -0,0 +1,16 @@ +#! /usr/bin/perl +# vim:ts=8:sw=8 + +use lib '../../include'; +use nms::web; +use strict; +use warnings; +use Data::Dumper; + +my $query = $nms::web::dbh->prepare('select sysname,extract(epoch from date_trunc(\'second\',time)) as time,state,username,id,comment from switch_comments natural join switches where state != \'delete\' order by time desc limit 1'); +$query->execute(); +while (my $ref = $query->fetchrow_hashref()) { + push @{$nms::web::json{'comments'}{$ref->{'sysname'}}{'comments'}},$ref; +} + +nms::web::finalize_output(); diff --git a/web/nms.gathering.org/port-state.pl b/web/nms.gathering.org/port-state.pl index 81ce025..e3dc24a 100755 --- a/web/nms.gathering.org/port-state.pl +++ b/web/nms.gathering.org/port-state.pl @@ -1,4 +1,6 @@ #! /usr/bin/perl +# vim:ts=8:sw=8 + use CGI qw(fatalsToBrowser); use DBI; use lib '../../include'; @@ -9,24 +11,56 @@ use Data::Dumper; my $cgi = CGI->new; +my %cc = (); +$cc{'stale-while-revalidate'} = "3600"; +$cc{'max-age'} = "5"; -my $dbh = nms::db_connect(); -my $cin = $cgi->param('time'); -my $now = "now()"; -if (defined($cgi->param('now'))) { - $now = "'" . $cgi->param('now') . "'::timestamp "; +# Print cache-control from %cc +sub printcc { + my $line = ""; + my $first = ""; + foreach my $tmp (keys(%cc)) { + $line .= $first . $tmp . "=" . $cc{$tmp}; + $first = ", "; + } + print 'Cache-Control: ' . $line . "\n"; } -my $when =" time > " . $now . " - '5m'::interval and time < " . $now . " "; -my %json = (); -my $ifname = "ifname"; -if (defined($cgi->param('public'))) { - $ifname = "regexp_replace(ifname, 'ge-0/0/(([0-3][0-9])|(4[0-3])|([0-9]))\$',concat('ge-participant',sha1_hmac(ifname::bytea,'".$nms::config::nms_hash."'::bytea))) as ifname"; +# FIXME: Shouldn't be magic. +# Only used for setting time in result from DB time. +# FIXME: Clarification, this _has_ to be set before setwhen is run, +# since it secretly overrides it. +my $now; + +# 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 "; + $cc{'max-age'} = "3600"; + } + $when = " time > " . $now . " - '5m'::interval and time < " . $now . " "; + return $when; } -if (defined($cin)) { - $when = " time < " . $now . " - '$cin'::interval and time > ". $now . " - ('$cin'::interval + '5m'::interval) "; + +# Sets the ifname. If we are logged in, it's simply set to "ifname", otherwise +# it's hashed for anonymization. +sub obfuscateifname { + my $ifname = "ifname"; + if (defined($cgi->param('public'))) { + $ifname = "regexp_replace(ifname, 'ge-0/0/(([0-3][0-9])|(4[0-3])|([0-9]))\$',concat('ge-participant',sha1_hmac(ifname::bytea,'".$nms::config::nms_hash."'::bytea))) as ifname"; + } + return $ifname; } + +my %json = (); +my $dbh = nms::db_connect(); +my $when = setwhen; +my $ifname = obfuscateifname; + my $query = 'select sysname,extract(epoch from date_trunc(\'second\',time)) as time, '.$ifname.',ifhighspeed,ifhcinoctets,ifhcoutoctets from polls natural join switches where time in (select max(time) from polls where ' . $when . ' group by switch,ifname);'; my $q = $dbh->prepare($query); $q->execute(); @@ -38,7 +72,6 @@ while (my $ref = $q->fetchrow_hashref()) { } $json{'switches'}{$ref->{'sysname'}}{'ports'}{$ref->{'ifname'}}{'time'} = $ref->{'time'}; } -#print Dumper(%json); my $q2 = $dbh->prepare('select switch,sysname,placement,ip,switchtype,poll_frequency,community,last_updated from switches natural join placements'); my $q3 = $dbh->prepare('select distinct on (switch) switch,temp,time,sysname from switch_temp natural join switches where ' . $when . ' order by switch,time desc'); @@ -73,11 +106,7 @@ while (my $ref = $q4->fetchrow_hashref()) { } my $q5; -if (defined($cin)) { - $q5 = $dbh->prepare ('select (' . $now . ' - \'' . $cin . '\'::interval) as time;'); -} else { - $q5 = $dbh->prepare ('select ' . $now . ' as time;'); -} +$q5 = $dbh->prepare ('select ' . $now . ' as time;'); $q5->execute(); $json{'time'} = $q5->fetchrow_hashref()->{'time'}; @@ -88,5 +117,7 @@ while (my $ref = $q6->fetchrow_hashref()) { } $json{'username'} = $cgi->remote_user(); +printcc; + print $cgi->header(-type=>'text/json; charset=utf-8'); print JSON::XS::encode_json(\%json); |