mondbd.in 9.5 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
56
#
# 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@";
my $LOGFILE      = "$TB/log/expire_daemon.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
101
102
103
104
105
106
107
108
109
110
# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libaudit;
use libdb;
use libtestbed;
use emdbi;
use emutil;
use Experiment;
use Interface;
use Node;
use OSinfo;

# 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
111
112
113
114
115
116

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

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;

Gary Wong's avatar
Gary Wong committed
136
sub AddInterfaces($$$) {
Gary Wong's avatar
Gary Wong committed
137

138
    my ($n, $node, $ts) = @_;
Gary Wong's avatar
Gary Wong committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    my @interfaces;

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

    foreach my $interface( @interfaces ) {
	my $i = $interface->iface();
	my $addr = $interface->IP();
	my $role = $interface->role();
	my $speed;
	my $packets;

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

	$speed *= 100;
	$packets = $speed / 80;

	emdbi::DBQueryWarnN( $db, "INSERT INTO ops_interface SET " .
Gary Wong's avatar
Gary Wong committed
158
			     "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140501/port#', " .
Gary Wong's avatar
Gary Wong committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
			     "id='${DOMAIN}_interface_$n:$i'," .
			     "selfRef='$URL/info/interface/${DOMAIN}_interface_$n:$i'," .
			     "urn='urn:publicid:IDN+$DOMAIN+interface+$n:$i'," .
			     "ts='$ts'," .
			     "address_type='ipv4'," .
			     "address_address='$addr'," .
			     "properties\$role='$role'," .
			     "properties\$max_bps='$speed'," .
			     "properties\$max_pps='$packets'" );

	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'" );
    }
}

Gary Wong's avatar
Gary Wong committed
177
178
my %portcounters;

Gary Wong's avatar
Gary Wong committed
179
while (1) {
180
181
182
    my $ts = time() . "000000";
    my $expire = ( time() - 24 * 60 * 60 ) . "000000";

Gary Wong's avatar
Gary Wong committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    # Add the local CM.
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_aggregate WHERE ts < $expire" );
    emdbi::DBQueryWarnN( $db, "INSERT INTO ops_aggregate SET " .
			 "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140501/aggregate#', " .
			 "id='$NICKNAME'," .
			 "selfRef='$URL/info/aggregate/$NICKNAME'," .
			 "urn='urn:publicid:IDN+$DOMAIN+authority+cm'," .
			 "ts='$ts'," .
			 "measRef='$URL/data/'" );

    # Add local XEN nodes and interfaces from the shared pool.
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_node WHERE ts < $expire" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_interface WHERE ts < $expire" );
    emdbi::DBQueryWarnN( $db, "LOCK TABLES ops_node WRITE, " .
			 "ops_interface WRITE, " .
			 "ops_aggregate_resource WRITE, " .
			 "ops_node_interface WRITE" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_aggregate_resource" );
    emdbi::DBQueryWarnN( $db, "DELETE FROM ops_node_interface" );
    # 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 );
211
212
	my $os = OSinfo->Lookup( $node->def_boot_osid(),
				 $node->def_boot_osid_vers() );
Gary Wong's avatar
Gary Wong committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

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

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

	emdbi::DBQueryWarnN( $db, "INSERT INTO ops_node SET " .
			     "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140501/node#', " .
			     "id='${DOMAIN}_node_$n'," .
			     "selfRef='$URL/info/node/${DOMAIN}_node_$n'," .
			     "urn='urn:publicid:IDN+$DOMAIN+node+$n'," .
			     "ts='$ts'," .
			     "properties\$mem_total_kb='$mem'," .
			     "properties\$vm_server_type='xen'" );

	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'" );

233
	AddInterfaces( $n, $node, $ts );
Gary Wong's avatar
Gary Wong committed
234
235
    }

Gary Wong's avatar
Gary Wong committed
236
    my @fakenodenames = ();
Gary Wong's avatar
Gary Wong committed
237
238
239
240
241
242
243
    my $query_result = emdbi::DBQueryWarnN( $tbdb, "SELECT node_id FROM nodes " .
					    "WHERE type='interconnect'" );
    if( $query_result && $query_result->numrows) {
	my @fakenodes;

	while( @fakenodes = $query_result->fetchrow_array() ) {
	    my ($n) = @fakenodes;
Gary Wong's avatar
Gary Wong committed
244
	    push( @fakenodenames, @fakenodes );
Gary Wong's avatar
Gary Wong committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
	    my $node = Node->Lookup( $n );

	    emdbi::DBQueryWarnN( $db, "INSERT INTO ops_node SET " .
				 "\$schema='http://www.gpolab.bbn.com/monitoring/schema/20140501/node#', " .
				 "id='${DOMAIN}_node_$n'," .
				 "selfRef='$URL/info/node/${DOMAIN}_node_$n'," .
				 "urn='urn:publicid:IDN+$DOMAIN+node+$n'," .
				 "ts='$ts'" );

	    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'" );
	    
260
	    AddInterfaces( $n, $node, $ts );
Gary Wong's avatar
Gary Wong committed
261
262
263
264
265
	}
    }

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

Gary Wong's avatar
Gary Wong committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
    # 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;

		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 );
    }

    sleep( $INTERVAL );
Gary Wong's avatar
Gary Wong committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340

    # FIXME add slice, sliver, user, vlan information
}

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");
}