1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#! /usr/bin/perl
# vim:ts=8:sw=8
use CGI qw(fatalsToBrowser);
use DBI;
use lib '../../include';
use nms;
use strict;
use warnings;
use Data::Dumper;
my $cgi = CGI->new;
my %cc = ();
$cc{'stale-while-revalidate'} = "3600";
$cc{'max-age'} = "5";
# 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";
}
# 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;
}
# 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();
while (my $ref = $q->fetchrow_hashref()) {
my @fields = ('ifhighspeed','ifhcoutoctets','ifhcinoctets');
foreach my $val (@fields) {
$json{'switches'}{$ref->{'sysname'}}{'ports'}{$ref->{'ifname'}}{$val} = $ref->{$val};
}
$json{'switches'}{$ref->{'sysname'}}{'ports'}{$ref->{'ifname'}}{'time'} = $ref->{'time'};
}
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');
$q2->execute();
while (my $ref = $q2->fetchrow_hashref()) {
$ref->{'placement'} =~ /\((-?\d+),(-?\d+)\),\((-?\d+),(-?\d+)\)/;
my ($x1, $y1, $x2, $y2) = ($1, $2, $3, $4);
my $sysname = $ref->{'sysname'};
$json{'switches'}{$ref->{'sysname'}}{'switchtype'} = $ref->{'switchtype'};
$json{'switches'}{$ref->{'sysname'}}{'management'}{'ip'} = $ref->{'ip'};
$json{'switches'}{$ref->{'sysname'}}{'management'}{'poll_frequency'} = $ref->{'poll_frequency'};
$json{'switches'}{$ref->{'sysname'}}{'management'}{'community'} = $ref->{'community'};
$json{'switches'}{$ref->{'sysname'}}{'management'}{'last_updated'} = $ref->{'last_updated'};
$json{'switches'}{$ref->{'sysname'}}{'placement'}{'x'} = $x2;
$json{'switches'}{$ref->{'sysname'}}{'placement'}{'y'} = $y2;
$json{'switches'}{$ref->{'sysname'}}{'placement'}{'width'} = $x1 - $x2;
$json{'switches'}{$ref->{'sysname'}}{'placement'}{'height'} = $y1 - $y2;
}
$q3->execute();
while (my $ref = $q3->fetchrow_hashref()) {
my $sysname = $ref->{'sysname'};
$json{'switches'}{$ref->{'sysname'}}{'temp'} = $ref->{'temp'};
$json{'switches'}{$ref->{'sysname'}}{'temp_time'} = $ref->{'time'};
}
my $q4 = $dbh->prepare(' select linknet, (select sysname from switches where switch = switch1) as sysname1, addr1, (select sysname from switches where switch = switch2) as sysname2,addr2 from linknets');
$q4->execute();
while (my $ref = $q4->fetchrow_hashref()) {
$json{'linknets'}{$ref->{'linknet'}} = $ref;
# push @{$json{'linknets'}}, $ref;
}
my $q5;
$q5 = $dbh->prepare ('select ' . $now . ' as time;');
$q5->execute();
$json{'time'} = $q5->fetchrow_hashref()->{'time'};
my $q6 = $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');
$q6->execute();
while (my $ref = $q6->fetchrow_hashref()) {
push @{$json{'switches'}{$ref->{'sysname'}}{'comments'}},$ref;
}
$json{'username'} = $cgi->remote_user();
printcc;
print $cgi->header(-type=>'text/json; charset=utf-8');
print JSON::XS::encode_json(\%json);
|