mondbd.in 26 KB
Newer Older
Gary Wong's avatar
Gary Wong committed
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
#!/usr/bin/perl -w
#
# Copyright (c) 2014 University of Utah and the Flux Group.
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
#
use strict;
use English;
Gary Wong's avatar
Gary Wong committed
32
use Getopt::Std;
Gary Wong's avatar
Gary Wong committed
33
34
35
36
37

#
# Populate the monitoring database.
# 

Gary Wong's avatar
Gary Wong committed
38
39
40
41
42
43
44
45
sub usage()
{
    print "Usage: mondbd [-d] [-i]\n";
    exit(1);
}
my $optlist   = "d";
my $debug     = 0;

Gary Wong's avatar
Gary Wong committed
46
47
48
49
50
51
52
53
54
55
#
# Configure variables
#
my $TB		 = "@prefix@";
my $TBOPS        = "@TBOPSEMAIL@";
my $DBNAME	 = "@TBDBNAME@";
my $DOMAIN       = "@OURDOMAIN@";
my $URL          = "https://www.$DOMAIN:5001";
my $PGENISUPPORT = @PROTOGENI_SUPPORT@;
my $NICKNAME	 = "@PROTOGENI_NICKNAME@";
56
my $LOGFILE      = "$TB/log/mondbd.log";
Gary Wong's avatar
Gary Wong committed
57
58
59
my $PORTSTATS    = "$TB/bin/portstats";

my $INTERVAL     = 120;
Gary Wong's avatar
Gary Wong committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

# Protos
sub fatal($);
	  
#
# Turn off line buffering on output
#
$| = 1; 

if ($UID != 0) {
    fatal("Must be root to run this script\n");
}

#
# Exit if not a protogeni site.
#
if (! $PGENISUPPORT) {
    exit(0);
}

Gary Wong's avatar
Gary Wong committed
84
85
86
87
88
89
90
91
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug++;
}

Gary Wong's avatar
Gary Wong committed
92
93
94
95
96
97
98
99
100
# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libaudit;
use libdb;
use libtestbed;
use emdbi;
use emutil;
use Experiment;
use Interface;
Gary Wong's avatar
Gary Wong committed
101
use Lan;
Gary Wong's avatar
Gary Wong committed
102
103
use Node;
use OSinfo;
Gary Wong's avatar
Gary Wong committed
104
use GeniHRN;
Gary Wong's avatar
Gary Wong committed
105
106
107
108
109
110
111
112

# Connect to the monitoring DB.
my $db = emdbi::NewTBDBHandle( "monitoring" );
my $tbdb = emdbi::NewTBDBHandle( $DBNAME );

if (CheckDaemonRunning("mondbd")) {
    fatal("Not starting another monitoring daemon!");
}
Gary Wong's avatar
Gary Wong committed
113
114
115
116
117
118

if( !$debug) {
    # Go to background.
    if (TBBackGround($LOGFILE)) {
	exit(0);
    }
Gary Wong's avatar
Gary Wong committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
}

if (MarkDaemonRunning("mondbd")) {
    fatal("Could not mark daemon as running!");
}

#
# Setup a signal handler for newsyslog.
#
sub handler()
{
    my $SAVEEUID = $EUID;
    
    $EUID = 0;
    ReOpenLog($LOGFILE);
    $EUID = $SAVEEUID;
}
$SIG{HUP} = \&handler;

138
139
140
my %allnodes = ();
my %allinterfaces = ();
    
Gary Wong's avatar
Gary Wong committed
141
sub AddInterfaces($$$) {
Gary Wong's avatar
Gary Wong committed
142

143
    my ($n, $node, $ts) = @_;
Gary Wong's avatar
Gary Wong committed
144
145
146
147
148
149
150
    my @interfaces;

    Interface->LookupAll( $node, \@interfaces );

    foreach my $interface( @interfaces ) {
	my $i = $interface->iface();
	my $addr = $interface->IP();
Gary Wong's avatar
Gary Wong committed
151
	my $role = $interface->role() eq "ctrl" ? "control" : "experimental";
Gary Wong's avatar
Gary Wong committed
152
153
154
155
156
157
158
	my $speed;
	my $packets;

	if( $interface->TypeCapability( "ethernet_defspeed", \$speed ) < 0 ) {
	    $speed = 1000000;
	}

159
160
	$speed *= 1000;
	$packets = $speed / 800;
Gary Wong's avatar
Gary Wong committed
161
162

	emdbi::DBQueryWarnN( $db, "INSERT INTO ops_interface SET " .
163
			     "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/interface#', " .
Gary Wong's avatar
Gary Wong committed
164
165
166
167
168
169
170
171
			     "id='${DOMAIN}_interface_$n:$i'," .
			     "selfRef='$URL/info/interface/${DOMAIN}_interface_$n:$i'," .
			     "urn='urn:publicid:IDN+$DOMAIN+interface+$n:$i'," .
			     "ts='$ts'," .
			     "properties\$role='$role'," .
			     "properties\$max_bps='$speed'," .
			     "properties\$max_pps='$packets'" );

172
173
	emdbi::DBQueryWarnN( $db, "INSERT INTO ops_interface_addresses SET " .
			     "interface_id='${DOMAIN}_interface_$n:$i'," .
174
			     "addrtype='IPv4'," .
Gary Wong's avatar
Gary Wong committed
175
176
			     "address='$addr'" )
	    if( $addr =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ );
177

Gary Wong's avatar
Gary Wong committed
178
179
180
181
182
	emdbi::DBQueryWarnN( $db, "INSERT INTO ops_node_interface SET " .
			     "id='${DOMAIN}_interface_$n:$i'," .
			     "node_id='${DOMAIN}_node_$n'," .
			     "urn='urn:publicid:IDN+$DOMAIN+interface+$n:$i'," .
			     "selfRef='$URL/info/interface/${DOMAIN}_interface_$n:$i'" );
183
184

	$allinterfaces{"$n:$i"} = 1;
Gary Wong's avatar
Gary Wong committed
185
186
187
    }
}

Gary Wong's avatar
Gary Wong committed
188
189
190
191
sub AddLink($$) {

    my ($name, $ts) = @_;

192
193
194
    # FIXME would be nice to set layer, too
    emdbi::DBQueryWarnN( $db, "REPLACE INTO ops_link SET " .
			 "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/link#', " .
Gary Wong's avatar
Gary Wong committed
195
196
197
198
199
			 "id='${DOMAIN}_link_$name', " .
			 "selfRef='$URL/info/link/${DOMAIN}_link_$name'," .
			 "urn='urn:publicid:IDN+$DOMAIN+link+$name'," .
			 "ts='$ts'" );

200
    emdbi::DBQueryWarnN( $db, "REPLACE INTO ops_aggregate_resource SET " .
Gary Wong's avatar
Gary Wong committed
201
202
203
204
205
206
207
208
209
210
			 "id='${DOMAIN}_link_$name'," .
			 "aggregate_id='$NICKNAME'," .
			 "urn='urn:publicid:IDN+$DOMAIN+link+$name'," .
			 "selfRef='$URL/info/link/${DOMAIN}_link_$name'" );
}

sub AddInterfaceVlan($$$$$) {

    my ($linkid,$idomain,$iface,$tag,$ts) = @_;
    my $url;
211
212
    my $interfaceurl;
    
213
    $url = "$URL/info/interfacevlan/${idomain}_interface_${iface}:${tag}";
214
    $interfaceurl = "$URL/info/interface/${idomain}_interface_${iface}";
Gary Wong's avatar
Gary Wong committed
215

216
    emdbi::DBQueryWarnN( $db, "REPLACE INTO ops_interfacevlan SET " .
217
			 "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/interfacevlan#', " .
Gary Wong's avatar
Gary Wong committed
218
219
			 "id='${idomain}_interface_${iface}:${tag}', " .
			 "selfRef='$url', " .
220
			 "urn='urn:publicid:IDN+$idomain+interfacevlan+$iface:$tag', " .
Gary Wong's avatar
Gary Wong committed
221
			 "ts='$ts', " .
222
			 "tag=$tag," .
223
224
			 "interface_urn='urn:publicid:IDN+$idomain+interface+$iface'," .
			 "interface_href='$interfaceurl'" );
Gary Wong's avatar
Gary Wong committed
225

226
227
228
229
230
231
232
233
234
    if( $idomain ne $DOMAIN ) {
	# Need to add a fake interface entry for the foreign interface.
	emdbi::DBQueryWarnN( $db, "REPLACE INTO ops_interface SET " .
			     "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/interface#', " .
			     "id='${idomain}_interface_${iface}'," .
			     "selfRef='${interfaceurl}'," .
			     "urn='urn:publicid:IDN+$idomain+interface+$iface'," .
			     "ts='$ts'," .
			     "properties\$role='stub'" );
235
236
237
238
239
240
241
242
243
244
    } elsif( !exists( $allinterfaces{$iface} ) ) {
	emdbi::DBQueryWarnN( $db, "REPLACE INTO ops_interface SET " .
			     "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/interface#', " .
			     "id='${DOMAIN}_interface_${iface}'," .
			     "selfRef='$URL/info/interface/${DOMAIN}_interface_${iface}'," .
			     "urn='urn:publicid:IDN+$DOMAIN+interface+${iface}'," .
			     "ts='$ts'," .
			     "properties\$role='experimental'" );
	    
	$allinterfaces{$iface} = 1;
245
246
    }
    
Gary Wong's avatar
Gary Wong committed
247
248
    emdbi::DBQueryWarnN( $db, "INSERT INTO ops_link_interfacevlan SET " .
			 "id='${idomain}_interface_${iface}:${tag}', " .
249
			 "link_id='${DOMAIN}_link_$linkid'" );
Gary Wong's avatar
Gary Wong committed
250
251
}

252
253
254
255
256
257
258
259
260
sub AddSliceUser($$$) {

    my ($slice_id,$user_urn,$user_role) = @_;
    my $udomain;
    my $utype;
    my $uname;

    ( $udomain, $utype, $uname ) = GeniHRN::Parse( $user_urn );

261
    emdbi::DBQueryWarnN( $db, "INSERT IGNORE INTO ops_slice_user SET " .
262
263
264
265
266
267
268
			 "id='${udomain}_user_${uname}', " .
			 "slice_id='$slice_id', " .
			 "urn='$user_urn', " .
			 "role='$user_role', " .
			 "selfRef='$URL/info/user/${udomain}_user_${uname}';" );
}

Gary Wong's avatar
Gary Wong committed
269
270
my %portcounters;

Gary Wong's avatar
Gary Wong committed
271
while (1) {
272
273
274
    my $ts = time() . "000000";
    my $expire = ( time() - 24 * 60 * 60 ) . "000000";

Gary Wong's avatar
Gary Wong committed
275
    # Add the local CM.
276
277
278
    # FIXME would be nice to add routable_ip_poolsize
    emdbi::DBQueryWarnN( $db, "LOCK TABLES ops_aggregate WRITE" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_aggregate" );
Gary Wong's avatar
Gary Wong committed
279
    emdbi::DBQueryWarnN( $db, "INSERT INTO ops_aggregate SET " .
280
			 "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/aggregate#', " .
Gary Wong's avatar
Gary Wong committed
281
282
283
284
			 "id='$NICKNAME'," .
			 "selfRef='$URL/info/aggregate/$NICKNAME'," .
			 "urn='urn:publicid:IDN+$DOMAIN+authority+cm'," .
			 "ts='$ts'," .
285
286
287
			 "measRef='$URL/data/'," .
			 "operational_status='development'" );
    emdbi::DBQueryWarnN( $db, "UNLOCK TABLES" );
Gary Wong's avatar
Gary Wong committed
288
289

    # Add local XEN nodes and interfaces from the shared pool.
290
291
292
293
294
295
296
297
    # NB: it would be nice to delete only very old entries from timestamped
    # tables (to retain some historical data), but ts is not part of the
    # primary key for them so we must discard the old state before we're
    # permitted to update with the current.
#    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_node WHERE ts < $expire" );
#    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_interface WHERE ts < $expire" );
#    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_link WHERE ts < $expire" );
#    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_interfacevlan WHERE ts < $expire" );
Gary Wong's avatar
Gary Wong committed
298
299
    emdbi::DBQueryWarnN( $db, "LOCK TABLES ops_node WRITE, " .
			 "ops_interface WRITE, " .
300
301
302
			 "ops_interfacevlan WRITE, " .
			 "ops_interface_addresses WRITE, " .
			 "ops_link WRITE, " .
Gary Wong's avatar
Gary Wong committed
303
304
			 "ops_aggregate_resource WRITE, " .
			 "ops_node_interface WRITE" );
305
306
307
308
309
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_node" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_interface" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_link" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_interfacevlan" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_interface_addresses" );
Gary Wong's avatar
Gary Wong committed
310
311
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_aggregate_resource" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_node_interface" );
312
313
314
315

    %allnodes = ();
    %allinterfaces = ();
    
Gary Wong's avatar
Gary Wong committed
316
317
318
319
320
321
322
323
324
    # The shared pool experiment isn't named consistently across sites.
    # Rather than fixing it, let's just grab everything we can find and
    # hope for the best.
    my @nodes = ExpNodes( "emulab-ops", "shared-node", 1, 1 );
    my @morenodes = ExpNodes( "emulab-ops", "shared-nodes", 1, 1 );
    push( @nodes, @morenodes );

    foreach my $n ( @nodes ) {
	my $node = Node->Lookup( $n );
325
326
	my $os = OSinfo->Lookup( $node->def_boot_osid(),
				 $node->def_boot_osid_vers() );
Gary Wong's avatar
Gary Wong committed
327
328
329
330
331
332

	next unless $os->osname =~ /XEN/;

	my $mem = $node->memory() * 1024;

	emdbi::DBQueryWarnN( $db, "INSERT INTO ops_node SET " .
333
			     "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/node#', " .
Gary Wong's avatar
Gary Wong committed
334
335
336
337
			     "id='${DOMAIN}_node_$n'," .
			     "selfRef='$URL/info/node/${DOMAIN}_node_$n'," .
			     "urn='urn:publicid:IDN+$DOMAIN+node+$n'," .
			     "ts='$ts'," .
338
			     "node_type='server'," .
Gary Wong's avatar
Gary Wong committed
339
			     "properties\$mem_total_kb='$mem'," .
340
			     "virtualization_type='xen'" );
Gary Wong's avatar
Gary Wong committed
341
342
343
344
345
346
347

	emdbi::DBQueryWarnN( $db, "INSERT INTO ops_aggregate_resource SET " .
			     "id='${DOMAIN}_node_$n'," .
			     "aggregate_id='$NICKNAME'," .
			     "urn='urn:publicid:IDN+$DOMAIN+node+$n'," .
			     "selfRef='$URL/info/node/${DOMAIN}_node_$n'" );

348
349
	$allnodes{$n} = 1;
	
350
	AddInterfaces( $n, $node, $ts );
Gary Wong's avatar
Gary Wong committed
351
352
    }

Gary Wong's avatar
Gary Wong committed
353
    my @fakenodenames = ();
354
355
356
357
358
359
360
    my @switchnodenames = ();
    my %fakenodemap = ();
    my $query_result = emdbi::DBQueryWarnN( $tbdb, "SELECT e.node_id, w.node_id1, w.card1, w.port1, w.node_id2, w.card2, w.port2 " .
					    "FROM external_networks as e, " .
					    "wires as w WHERE " .
					    "e.node_id=w.node_id1 OR " .
					    "e.node_id=w.node_id2" );
Gary Wong's avatar
Gary Wong committed
361
362
363
364
    if( $query_result && $query_result->numrows) {
	my @fakenodes;

	while( @fakenodes = $query_result->fetchrow_array() ) {
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
	    my ($external, $node1, $card1, $port1, $node2, $card2, $port2) = @fakenodes;
	    my $extnode = $external eq $node1 ? $node1 : $node2;
	    my $extcard = $external eq $node1 ? $card1 : $card2;
	    my $extport = $external eq $node1 ? $port1 : $port2;
	    my $switchnode = $external eq $node1 ? $node2 : $node1;
	    my $switchcard = $external eq $node1 ? $card2 : $card1;
	    my $switchport = $external eq $node1 ? $port2 : $port1;

	    my $iface = Interface->Lookup( $extnode, $extcard, $extport );
	    my $ifacename = $iface->iface();
	    
	    $fakenodemap{ "$extnode:$ifacename" } =
		"$switchnode:$switchcard:$switchport";
	    
	    next if grep { $_ eq $switchnode } @switchnodenames;
	    
	    push( @switchnodenames, ($switchnode) );

	    push( @fakenodenames, ($external) )
		unless grep { $_ eq $external } @fakenodenames;
	    
	    my $node = Node->Lookup( $switchnode );
Gary Wong's avatar
Gary Wong committed
387
388

	    emdbi::DBQueryWarnN( $db, "INSERT INTO ops_node SET " .
389
				 "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/node#', " .
390
391
392
				 "id='${DOMAIN}_node_$switchnode'," .
				 "selfRef='$URL/info/node/${DOMAIN}_node_$switchnode'," .
				 "urn='urn:publicid:IDN+$DOMAIN+node+$switchnode'," .
393
394
				 "ts='$ts'," .
				 "node_type='switch'" );
Gary Wong's avatar
Gary Wong committed
395

396
397
	    $allnodes{$switchnode} = 1;
	    
Gary Wong's avatar
Gary Wong committed
398
	    emdbi::DBQueryWarnN( $db, "INSERT INTO ops_aggregate_resource SET " .
399
				 "id='${DOMAIN}_node_$switchnode'," .
Gary Wong's avatar
Gary Wong committed
400
				 "aggregate_id='$NICKNAME'," .
401
402
				 "urn='urn:publicid:IDN+$DOMAIN+node+$switchnode'," .
				 "selfRef='$URL/info/node/${DOMAIN}_node_$switchnode'" );
403
404
405

	    # Switches generally don't have interfaces in the database,
	    # but it doesn't hurt to try.
406
	    AddInterfaces( $switchnode, $node, $ts );
Gary Wong's avatar
Gary Wong committed
407
408
409
410
411
	}
    }

    emdbi::DBQueryWarnN( $db, "UNLOCK TABLES" );

Gary Wong's avatar
Gary Wong committed
412
    emdbi::DBQueryWarnN( $db, "LOCK TABLES ops_link WRITE, " .
413
			 "ops_interface WRITE, " .
Gary Wong's avatar
Gary Wong committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
			 "ops_interfacevlan WRITE, " .
			 "ops_link_interfacevlan WRITE, " .
			 "ops_aggregate_resource WRITE" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_link_interfacevlan" );

    $query_result = emdbi::DBQueryWarnN( $tbdb, "SELECT DISTINCT lanid " .
					    "FROM lan_members AS l, " .
					    "external_networks AS e WHERE " .
					    "l.node_id=e.node_id" );
    if( $query_result && $query_result->numrows ) {
	my @lanids;

	while( @lanids = $query_result->fetchrow_array() ) {
	    my ($lanid) = @lanids;
	    
	    my $lan = VLan->Lookup( $lanid );

	    next if( !defined( $lan ) );

	    my $tag = $lan->GetTag();

435
436
	    next if( !defined( $tag ) || $tag <= 0 );

Gary Wong's avatar
Gary Wong committed
437
438
439
440
441
442
443
	    my @members;

	    next if( $lan->MemberList( \@members ) != 0 );

	    next if( ( scalar @members ) < 2 );

	    my $edomain;
444
445
	    my $enode = undef;
	    my $inode = undef;
Gary Wong's avatar
Gary Wong committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
	    my $snode;

	    foreach my $member ( @members ) {
		my $noderef;
		my $nodeid;
		my $iface;

		next if( $member->GetNodeIface( \$noderef, \$iface ) != 0 );
		$nodeid = $noderef->node_id();		

		if( !defined( $enode ) &&
		    grep { $_ eq $nodeid } @fakenodenames ) {

		    my $peer;
		    # Figuring out the other end of an external interface of
		    # a fake node seems to be inconsistent.  Sometimes it's
		    # in the external_interface column of the wires table.
		    # Sometimes it's in the external_interface of the
		    # external_networks table.  So we try both...
		    my $pquery_result = emdbi::DBQueryWarnN(
			$tbdb, "SELECT external_interface FROM " .
			"external_networks WHERE node_id='$nodeid' AND " .
			"external_interface IS NOT NULL" );
		    if( $pquery_result && $pquery_result->numrows ) {
			($peer) = $pquery_result->fetchrow_array();
		    } else {
			$pquery_result = emdbi::DBQueryWarnN(
			    $tbdb, "SELECT external_interface FROM " .
			    "wires AS w, interfaces AS i WHERE " .
			    "( ( w.node_id1='$nodeid' AND w.card1=i.card " .
			    "AND w.port1=i.port ) OR " .
			    "( w.node_id2='$nodeid' AND w.card2=i.card " .
			    "AND w.port2=i.port ) ) AND " .
			    "i.node_id='$nodeid' AND i.iface='$iface'" );

			if( $pquery_result && $pquery_result->numrows ) {
			    ($peer) = $pquery_result->fetchrow_array();
			} else {
			    next;
			}
		    }

		    my $type;
		    ( $edomain, $type, $enode ) = GeniHRN::Parse( $peer );
		    $enode = GeniHRN::Transcribe( $enode );
491
		    $snode = $fakenodemap{ "$nodeid:$iface" };
Gary Wong's avatar
Gary Wong committed
492
493
494
495
496
497
498
		} else {
		    $inode = "$nodeid:$iface";
		}
	    }

	    next if( !defined( $enode ) || !defined( $inode ) );

499
	    emdbi::DBQueryWarnN( $db, "INSERT INTO ops_interface SET " .
500
501
502
503
504
				 "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/interface#', " .
				 "id='${DOMAIN}_interface_${snode}'," .
				 "selfRef='$URL/info/interface/${DOMAIN}_interface_${snode}'," .
				 "urn='urn:publicid:IDN+$DOMAIN+interface+${snode}'," .
				 "ts='$ts'," .
505
506
				 "properties\$role='experimental'" )
		unless( exists( $allinterfaces{$snode} ) );
507
	    
Gary Wong's avatar
Gary Wong committed
508
509
510
511
512
513
514
515
516
517
518
519
520
	    AddLink( "${lanid}e", $ts );
	    AddLink( "${lanid}i", $ts );

	    AddInterfaceVlan( "${lanid}e", $DOMAIN, $snode, $tag, $ts );
	    AddInterfaceVlan( "${lanid}e", $edomain, $enode, $tag, $ts );

	    AddInterfaceVlan( "${lanid}i", $DOMAIN, $snode, $tag, $ts );
	    AddInterfaceVlan( "${lanid}i", $DOMAIN, $inode, $tag, $ts );
	}
    }

    emdbi::DBQueryWarnN( $db, "UNLOCK TABLES" );

Gary Wong's avatar
Gary Wong committed
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
    # Add traffic counters for the (fake) switch nodes.  Do this after
    # unlocking the database, because we invoke portstats, which is slow.
    # We lose atomicity, but since we're only doing appends, it doesn't
    # really matter.
    foreach my $n ( @fakenodenames ) {
	open( P, "$PORTSTATS -s $n:eth0|" );
	# ignore 3 lines of headers
	<P>;
	<P>;
	<P>;
	# Do any fake nodes we care about have more than one interface?  Let's
	# assume not.
	if( <P> =~ /(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/ ) {
	    my @counts = ( $2, $3 + $4, $5, $6 + $7 );

	    if( exists( $portcounters{$n} ) ) {
		my $oldcounts = $portcounters{$n};

		my $rx_b = ( $counts[ 0 ] - $$oldcounts[ 0 ] ) / $INTERVAL;
		my $rx_p = ( $counts[ 1 ] - $$oldcounts[ 1 ] ) / $INTERVAL;
		my $tx_b = ( $counts[ 2 ] - $$oldcounts[ 2 ] ) / $INTERVAL;
		my $tx_p = ( $counts[ 3 ] - $$oldcounts[ 3 ] ) / $INTERVAL;

544
545
546
		$rx_b *= 8;
		$tx_b *= 8;
		
Gary Wong's avatar
Gary Wong committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
		if( ( $rx_b >= 0 ) && ( $rx_p >= 0 ) &&
		    ( $rx_b >= 0 ) && ( $rx_p >= 0 ) ) {
		    emdbi::DBQueryWarnN( $db, "INSERT INTO " .
					 "ops_interface_rx_bps SET " .
					 "id='${DOMAIN}_interface_$n:eth0'," .
					 "ts='$ts'," .
					 "v=$rx_b;" );
		    emdbi::DBQueryWarnN( $db, "INSERT INTO " .
					 "ops_interface_rx_pps SET " .
					 "id='${DOMAIN}_interface_$n:eth0'," .
					 "ts='$ts'," .
					 "v=$rx_p;" );
		    emdbi::DBQueryWarnN( $db, "INSERT INTO " .
					 "ops_interface_tx_bps SET " .
					 "id='${DOMAIN}_interface_$n:eth0'," .
					 "ts='$ts'," .
					 "v=$tx_b;" );
		    emdbi::DBQueryWarnN( $db, "INSERT INTO " .
					 "ops_interface_tx_pps SET " .
					 "id='${DOMAIN}_interface_$n:eth0'," .
					 "ts='$ts'," .
					 "v=$tx_p;" );
		    # We don't count errors.  Does anybody care?  If they
		    # do, we could insert rows of zeroes.
		}
	    }

	    $portcounters{$n} = \@counts;
	}
	close( P );
    }

579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
    my %slices;
    my %users;
    
    emdbi::DBQueryWarnN( $db, "LOCK TABLES ops_aggregate_resource WRITE, " .
			 "ops_aggregate_sliver WRITE, " .
			 "ops_node WRITE, " .
			 "ops_slice WRITE, " .
			 "ops_slice_user WRITE, " .
			 "ops_sliver WRITE, " .
			 "ops_user WRITE" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_aggregate_sliver" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_slice" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_slice_user" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_sliver" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_user" );

    $query_result = emdbi::DBQueryWarnN( $tbdb, "SELECT s.idx, s.uuid, s.slice_uuid, unix_timestamp(s.created)*1000000, unix_timestamp(slices.expires)*1000000, c1.urn, c2.urn, s.resource_id FROM `geni-cm`.geni_slivers AS s, `geni-cm`.geni_certificates AS c1, `geni-cm`.geni_certificates AS c2, `geni-cm`.geni_slices AS slices WHERE s.slice_uuid=c1.uuid AND s.creator_uuid=c2.uuid AND s.slice_uuid=slices.uuid AND s.resource_type='Node'" );
    if( $query_result && $query_result->numrows) {
	my @result;

	while( @result = $query_result->fetchrow_array() ) {
	    my ($idx, $uuid, $slice_uuid, $created, $expires, $slice_urn, $creator_urn, $node_id) = @result;

	    emdbi::DBQueryWarnN( $db, "INSERT INTO ops_sliver SET " .
				 "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/sliver#', " .
				 "id='${DOMAIN}_sliver_$idx'," .
				 "selfRef='$URL/info/sliver/${DOMAIN}_sliver_$idx'," .
				 "urn='urn:publicid:IDN+$DOMAIN+sliver+$idx'," .
				 "uuid='$uuid'," .
				 "ts='$ts'," .
				 "aggregate_urn='urn:publicid:IDN+$DOMAIN+authority+cm'," .
				 "aggregate_href='$URL/info/aggregate/$NICKNAME'," .
				 "slice_urn='$slice_urn'," .
				 "slice_uuid='$slice_uuid'," .
				 "creator='$creator_urn'," .
				 "created='$created'," .
				 "expires='$expires'," .
616
				 "node_id='${DOMAIN}_node_${node_id}';" );
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670

	    emdbi::DBQueryWarnN( $db, "INSERT INTO ops_aggregate_sliver SET " .
				 "id='${DOMAIN}_sliver_${idx}'," .
				 "aggregate_id='$NICKNAME'," .
				 "urn='urn:publicid:IDN+$DOMAIN+sliver+$idx'," .
				 "selfRef='$URL/info/sliver/${DOMAIN}_sliver_$idx'" );
	    
	    emdbi::DBQueryWarnN( $db, "INSERT INTO ops_node SET " .
				 "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/node#', " .
				 "id='${DOMAIN}_node_${node_id}'," .
				 "selfRef='$URL/info/node/${DOMAIN}_node_${node_id}'," .
				 "urn='urn:publicid:IDN+$DOMAIN+node+$node_id'," .
				 "ts='$ts'," .
				 "node_type='server'" );

	    emdbi::DBQueryWarnN( $db, "INSERT INTO ops_aggregate_resource SET " .
				 "id='${DOMAIN}_node_${node_id}'," .
				 "aggregate_id='$NICKNAME'," .
				 "urn='urn:publicid:IDN+$DOMAIN+node+$node_id'," .
				 "selfRef='$URL/info/node/${DOMAIN}_node_${node_id}'" );
	    
	    $slices{$slice_uuid} = 1;
	}
    }
    
    $query_result = emdbi::DBQueryWarnN( $tbdb, "SELECT s.idx, s.uuid, s.slice_uuid, unix_timestamp(s.created)*1000000, unix_timestamp(slices.expires)*1000000, c1.urn, c2.urn, s.resource_id FROM `geni-cm`.geni_slivers AS s, `geni-cm`.geni_certificates AS c1, `geni-cm`.geni_certificates AS c2, `geni-cm`.geni_slices AS slices WHERE s.slice_uuid=c1.uuid AND s.creator_uuid=c2.uuid AND s.slice_uuid=slices.uuid AND s.resource_type='Interface'" );
    if( $query_result && $query_result->numrows) {
	my @result;

	while( @result = $query_result->fetchrow_array() ) {
	    my ($idx, $uuid, $slice_uuid, $created, $expires, $slice_urn, $creator_urn, $if_id) = @result;

	    $if_id =~ s{//}{:};
	    
	    my $pquery_result = emdbi::DBQueryWarnN(
		$tbdb, "SELECT id FROM vlans WHERE members LIKE '%$if_id%'" );
	    if( $pquery_result && $pquery_result->numrows ) {
		my $lanid;
		($lanid) = $pquery_result->fetchrow_array();
		
		emdbi::DBQueryWarnN( $db, "INSERT INTO ops_sliver SET " .
				     "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/sliver#', " .
				     "id='${DOMAIN}_sliver_$idx'," .
				     "selfRef='$URL/info/sliver/${DOMAIN}_sliver_$idx'," .
				     "urn='urn:publicid:IDN+$DOMAIN+sliver+$idx'," .
				     "uuid='$uuid'," .
				     "ts='$ts'," .
				     "aggregate_urn='urn:publicid:IDN+$DOMAIN+authority+cm'," .
				     "aggregate_href='$URL/info/aggregate/$NICKNAME'," .
				     "slice_urn='$slice_urn'," .
				     "slice_uuid='$slice_uuid'," .
				     "creator='$creator_urn'," .
				     "created='$created'," .
				     "expires='$expires'," .
671
				     "link_id='${DOMAIN}_link_${lanid}i';" );
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
	    
		emdbi::DBQueryWarnN( $db, "INSERT INTO ops_aggregate_sliver SET " .
				     "id='${DOMAIN}_sliver_${idx}'," .
				     "aggregate_id='$NICKNAME'," .
				     "urn='urn:publicid:IDN+$DOMAIN+sliver+$idx'," .
				     "selfRef='$URL/info/sliver/${DOMAIN}_sliver_$idx'" );
		$slices{$slice_uuid} = 1;
	    }
	}
    }

    foreach my $slice( keys( %slices ) ) {
	$query_result = emdbi::DBQueryWarnN( $tbdb, "SELECT s.uuid, unix_timestamp(s.created)*1000000, unix_timestamp(s.expires)*1000000, c1.urn, s.creator_uuid, c2.urn FROM `geni-cm`.geni_slices AS s, `geni-cm`.geni_certificates AS c1, `geni-cm`.geni_certificates AS c2 WHERE s.uuid='$slice' AND c1.uuid=s.uuid AND c2.uuid=s.creator_uuid;" );

	if( $query_result && $query_result->numrows) {
	    my @result;
	    my $uquery_result;
	    
	    if( @result = $query_result->fetchrow_array() ) {
		my ($slice_uuid, $created, $expires, $slice_urn, $creator_uuid, $creator_urn) = @result;

		my $sdomain;
		my $stype;
		my $sname;
		( $sdomain, $stype, $sname ) = GeniHRN::Parse( $slice_urn );

		my $slice_id = "${sdomain}_${stype}_${sname}";
		
		emdbi::DBQueryWarnN( $db, "INSERT INTO ops_slice SET " .
				     "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/slice#', " .
				     "id='$slice_id'," .
				     "selfRef='$URL/info/slice/$slice_id'," .
				     "urn='$slice_urn'," .
				     "uuid='$slice_uuid'," .
				     "ts='$ts'," .
				     "authority_urn='urn:publicid:IDN+$sdomain+authority+sa'," .
				     "authority_href='$URL/info/authority/$sdomain'," .
				     "created='$created'," .
				     "expires='$expires';" );

		AddSliceUser( $slice_id, $creator_urn, "creator" );
		
		$users{$creator_uuid} = 1;
		
		$uquery_result = emdbi::DBQueryWarnN( $tbdb, "SELECT b.user_uuid, c.urn FROM `geni-cm`.geni_bindings AS b, `geni-cm`.geni_certificates AS c WHERE b.user_uuid=c.uuid AND b.slice_uuid='$slice'" );
		
		if( $uquery_result && $uquery_result->numrows) {
		    my @result;

		    while( @result = $uquery_result->fetchrow_array() ) {
			my ($user_uuid, $user_urn) = @result;

			AddSliceUser( $slice_id, $user_urn, "user" );
			
			$users{$user_uuid} = 1;
		    }
		}
	    }
	}	
    }

    foreach my $user( keys( %users ) ) {
	$query_result = emdbi::DBQueryWarnN( $tbdb, "SELECT u.name, u.email, c.urn FROM `geni-cm`.geni_users AS u, `geni-cm`.geni_certificates AS c WHERE c.uuid=u.uuid AND u.uuid='$user';" );

	if( $query_result && $query_result->numrows) {
	    my @result;

	    if( @result = $query_result->fetchrow_array() ) {
		my ($user_name, $user_email, $user_urn) = @result;
		my $udomain;
		my $utype;
		my $uname;

		( $udomain, $utype, $uname ) = GeniHRN::Parse( $user_urn );

		emdbi::DBQueryWarnN( $db, "INSERT INTO ops_user SET " .
				     "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140828/user#', " .
				     "id='${udomain}_user_${uname}', " .
				     "selfRef='$URL/info/user/${udomain}_user_${uname}', " .
				     "urn='$user_urn', " .
				     "ts='$ts', " .
				     "authority_urn='urn:publicid:IDN+$udomain+authority+sa', " .
				     "authority_href='$URL/info/authority/$udomain'," .
				     "fullname='$user_name', " .
				     "email='$user_email';" );
	    }
	}
    }			     

    emdbi::DBQueryWarnN( $db, "UNLOCK TABLES" );
    
763
764
    emdbi::DBQueryWarnN( $db, "FLUSH TABLES" );
    
Gary Wong's avatar
Gary Wong committed
765
    sleep( $INTERVAL );
Gary Wong's avatar
Gary Wong committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
}

sub fatal($)
{
    my ($msg) = @_;

    #
    # Send a message to the testbed list. 
    #
    SENDMAIL($TBOPS,
	     "ProtoGENI monitoring daemon died",
	     $msg,
	     $TBOPS);
    MarkDaemonStopped("mondbd");
    die("*** $0:\n".
	"    $msg\n");
}