get-map-info.php 35 KB
Newer Older
Eric - kg6wxc's avatar
Eric - kg6wxc committed
1
#!/usr/bin/env php
2
3
4
5
6
7
8
9
10
<?php
if (PHP_SAPI !== 'cli') {
    $file = basename($_SERVER['PHP_SELF']);
    exit("<style>html{text-align: center;}p{display: inline;}</style>
        <br><strong>This script ($file) should only be run from the 
        <p style='color: red;'>command line</p>!</strong>
        <br>exiting...");
}
$mtimeStart = microtime(true);
Eric - kg6wxc's avatar
Eric - kg6wxc committed
11
/*************************************************************************************
12
* get-map-info script v3 by kg6wxc\eric satterlee kg6wxc@gmail.com
13
* Licensed under GPLv3 or later
Eric - kg6wxc's avatar
Eric - kg6wxc committed
14
15
16
17
* This script is the heart of kg6wxcs' mesh map system.
* bug fixes, improvements and corrections are welcomed!
*                                                                                               
* One Script to rule them all!!
18
* 
19
* see CHANGELOG.md for notes
Eric - kg6wxc's avatar
Eric - kg6wxc committed
20
21
22
**************************************************************************************/

/******
23
24
25
26
27
28
29
30
31
32
33
34
35
* This file is part of the Mesh Mapping System.
* The Mesh Mapping System is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Mesh Mapping System is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with The Mesh Mapping System.  If not, see <http://www.gnu.org/licenses/>.
Eric - kg6wxc's avatar
Eric - kg6wxc committed
36
37
******/

38
//Increase PHP memory limit to 128M (you may need more if you are connected to a "Mega Mesh" :) )
Eric - kg6wxc's avatar
Eric - kg6wxc committed
39
//this should be moved to the ini file maybe
40
41
42
43
44
ini_set('memory_limit', '128M');

/***********************************************************************
 *DO NOT CHANGE ANYTHING BELOW HERE UNLESS YOU KNOW WHAT YOU ARE DOING!!!!
 ************************************************************************/
Eric - kg6wxc's avatar
Eric - kg6wxc committed
45
$INCLUDE_DIR = "..";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
46

47
//check for users user-settings.ini file and use it if it exists
Eric - kg6wxc's avatar
Eric - kg6wxc committed
48
//inform the user and exit if the file does not exist
49
global $USER_SETTINGS;
50
if (file_exists($INCLUDE_DIR . "/scripts/user-settings.ini")) {
51
52
    $USER_SETTINGS = parse_ini_file($INCLUDE_DIR . "/scripts/user-settings.ini");
}else {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
53
    exit("\n\nYou **must** copy the user-settings.ini-default file to user-settings.ini and edit it!!\n\n");
54
}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
55
56
57

//kg6wxc's functions. (ALWAYS REQUIRED!, change path if you moved it!)
require $INCLUDE_DIR . "/scripts/wxc_functions.inc";
58
require $INCLUDE_DIR . "/scripts/checkDB.inc";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
59

60
61
62
63
//the custom include file
//the "@" just suppresses any errors if the file is not found, the file is optional
@include $INCLUDE_DIR . "/custom.inc";

64
65
66
67
68
$script_arg = "";
if (isset($argv[1])) {
    $script_arg = $argv[1];
}
if ($script_arg == "--help" || $script_arg == "--h" || $script_arg == "-help" || $script_arg == "-h" || $script_arg == "/?" || $script_arg == "?") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
69
    echo $argv[0] . " Usage:\n\n";
70
    echo $argv[1] . "\tThis help message\n\n";
71
    echo "--test-mode-no-sql\tDO NOT access database only output to screen\n";
72
    echo "(useful to make sure everything is working)\n\n";
73
    echo "--test-mode-with-sql\tDO access the database AND output to screen\n";
74
75
    echo "(useful to see if everything is working and there are no errors reading/writing to the database)\n\n";
    echo "No arguments to this script will run it in \"silent\" mode, good for cron jobs! :)\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
76
    echo "\n";
77
78
    exit();
}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
79
$TEST_MODE_NO_SQL = 0;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
80
$TEST_MODE_WITH_SQL = 0;
81
if ($script_arg == "--test-mode-no-sql") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
82
83
84
    //output only to console, nothing saved. (great to just see what it does)
    $TEST_MODE_NO_SQL = 1;
}
85
if ($script_arg == "--test-mode-with-sql") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
86
87
88
    //output to console, but *with* calls to the database. (see what it's doing while saving data)
    $TEST_MODE_WITH_SQL = 1;
}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
89
90
91

//are we in either test mode?
if ($TEST_MODE_NO_SQL) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
92
93
94
95
96
97
98
    $showRuntime = 1;
    $testLinkInfo = 1;
    $testNodePolling = 1;
    $do_sql = 0;
    $getLinkInfo = 1;
    $getNodeInfo = 1;
    echo "TEST MODE (NO SQL) ENABLED!\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
99
}elseif ($TEST_MODE_WITH_SQL) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
100
101
102
103
104
105
106
    $showRuntime = 1;
    $testLinkInfo = 1;
    $testNodePolling = 1;
    $do_sql = 1;
    $getLinkInfo = 0;
    $getNodeInfo = 0;
    echo "TEST MODE (WITH SQL) ENABLED!\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
107
}else {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
108
109
110
111
112
113
    $showRuntime = 0;
    $testLinkInfo = 0;
    $testNodePolling = 0;
    $do_sql = 1;
    $getLinkInfo = 0;
    $getNodeInfo = 0;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
114
115
116
117
118
119
120
121
122
}

//(WiP)checks for some things we need to run
//(currently only really checks for the mysqli php extension
wxc_checkConfigs();

//what time is it now? returns a DateTime Object with current time.
date_default_timezone_set($USER_SETTINGS['localTimeZone']);
$currentTime = wxc_getCurrentDateTime();
123
124
125
//setting our global TimeZone
global $localTimeZone;
$localTimeZone = new DateTimeZone($USER_SETTINGS['localTimeZone']);
Eric - kg6wxc's avatar
Eric - kg6wxc committed
126

Eric - kg6wxc's avatar
Eric - kg6wxc committed
127
//what do we want to use in the sql server
Eric - kg6wxc's avatar
Eric - kg6wxc committed
128
129
130
131
132
133
$sql_db_tbl = $USER_SETTINGS['sql_db_tbl'];
$sql_db_tbl_node = $USER_SETTINGS['sql_db_tbl_node'];
$sql_db_tbl_topo = $USER_SETTINGS['sql_db_tbl_topo'];

if ($do_sql) {
	//an sql connection that we can reuse...
Eric - kg6wxc's avatar
Eric - kg6wxc committed
134
135
136
137
	//$sql_connection	=	wxc_connectToMySQL();
	wxc_connectToMySQL();
	//check for new or changed database items (tables, columns,etc)
	wxc_checkDB();
Eric - kg6wxc's avatar
Eric - kg6wxc committed
138
139
140
141
142
143
}else {
	if ($TEST_MODE_NO_SQL) {
		wxc_echoWithColor("SQL Server access disabled!", "red");
		echo "\n";
	}	
}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
144

Eric - kg6wxc's avatar
Eric - kg6wxc committed
145
146
//This controls when certain parts of the script run.
//We check the DB for the time we last checked and only run if we need to.
Eric - kg6wxc's avatar
Eric - kg6wxc committed
147
//intervals are now controled via the ini file.
Eric - kg6wxc's avatar
Eric - kg6wxc committed
148
149
//thanks K6GSE!
if ($do_sql) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
150
	//if $do_sql is set to 1, check when we last polled all the known nodes, if it was more than the interval set the variable to 1
Eric - kg6wxc's avatar
Eric - kg6wxc committed
151
152
153
154
155
156
157
158
159
160
	$lastRunGetNodeInfo = wxc_scriptGetLastDateTime("NODEINFO", "node_info");
	if ($lastRunGetNodeInfo) {
		if ($USER_SETTINGS['node_polling_interval'] > 0) {
			$intervalNODE = date_diff($lastRunGetNodeInfo, $currentTime);
			$intervalNodeInMinutes = $intervalNODE->days * 24 * 60;
			$intervalNodeInMinutes += $intervalNODE->h * 60;
			$intervalNodeInMinutes += $intervalNODE->i;
			if ($intervalNodeInMinutes >= intval($USER_SETTINGS['node_polling_interval'])) {
				if ($TEST_MODE_WITH_SQL) {
					echo "It has been " . $USER_SETTINGS['node_polling_interval'] . " or more minutes since this script polled all the nodes\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
161
					echo "Set to poll nodes.\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
162
163
164
165
166
167
				}
				$getNodeInfo = 1;
			}
		}
	}else {
		//probably never run before, lets get some data!!
Eric - kg6wxc's avatar
Eric - kg6wxc committed
168
		echo "Set to poll nodes.\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
169
170
		$getNodeInfo = 1;
	}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
	
	//if $do_sql is set to 1, check when we last got link info, if it was more than the interval set the variable to 1
	$lastRunGetLinkInfo = wxc_scriptGetLastDateTime("LINKINFO", "topology");
	if($lastRunGetLinkInfo) {
		if ($USER_SETTINGS['link_update_interval'] > 0) {
			$intervalLINK = date_diff($lastRunGetLinkInfo, $currentTime);
			if ($intervalLINK->i >= intval($USER_SETTINGS['link_update_interval'])) {
				if ($TEST_MODE_WITH_SQL) {
					echo "It has been " . $USER_SETTINGS['link_update_interval'] . " or more minutes since this script got the link info\n";
					echo "Set to get network linking info.\n\n";
				}
				$getLinkInfo = 1;
			}
		}
	}else {
		//probably never run before, let's get some data!
		echo "Set to get network linking info.\n\n";
		$getLinkInfo = 1;
	}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
190
191
}
//check the database to see if we are already polling nodes
Eric - kg6wxc's avatar
Eric - kg6wxc committed
192
193
//this trys to prevent 2 polling runs at once
//it does nothing in "--test-mode-no-sql"
Eric - kg6wxc's avatar
Eric - kg6wxc committed
194
195
196
197
198
199
200
201
if ($do_sql) {
	$currently_polling_nodes = wxc_getMySql("SELECT script_last_run, currently_running from map_info WHERE id = 'NODEINFO'");
	if (is_null($currently_polling_nodes['currently_running'])) {
		$currently_polling_nodes['currently_running'] = 0;
		$getNodeInfo = 1;
	}elseif ($currently_polling_nodes['currently_running'] == 1) {
		$getNodeInfo = 0;
	}
202
203
204
205
206
	//hopefully catch a stalled polling run after 3 * node_polling_interval has expired.
	//something may have gone wonky and the node polling run never completed and never had a chance
	//to unset the "currently_running" bit in the DB,
	//this *should* catch that and and just run the node polling again
	if ($currently_polling_nodes['currently_running'] == 1 && $intervalNodeInMinutes >= intval($USER_SETTINGS['node_polling_interval']) * 3) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
207
208
	    $currently_polling_nodes['currently_running'] = 0;
		$getNodeInfo = 1;
209
	}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
210
211
212
}

//check for old outdated node info (intervals will be set in the ini file)
Eric - kg6wxc's avatar
Eric - kg6wxc committed
213
214
$do_expire = $USER_SETTINGS['expire_old_nodes'];
if ($do_sql && $do_expire) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
215
216
	wxc_checkOldNodes();
}
217
218
219
if ($do_sql) {
	wxc_removeIgnoredNodes();
}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
220
221
222
223
224
225
226
227
228
229
230

$node = "";
if ($getNodeInfo) {
	//this section is what goes out to each node on the mesh and asks for it's info
	//this is really the heart of the mapping system, without this (and the sysinfo.json file),
	//none of this would even be possible.
	
	//tell the database we are actively polling nodes
	if ($do_sql && $currently_polling_nodes['currently_running'] == 0) {
		wxc_putMySql("INSERT INTO map_info (id, table_or_script_name, script_last_run, currently_running) VALUES ('NODEINFO', 'node_info', NOW(), '1') ON DUPLICATE KEY UPDATE table_or_script_name = 'node_info', script_last_run = NOW(), currently_running = '1'");
	}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
231
232
	//get a new list of IP's to poll
	$meshNodes = wxc_netcat($USER_SETTINGS['localnode'], "2004", null, "ipOnly");
Eric - kg6wxc's avatar
Eric - kg6wxc committed
233
234
	if ($meshNodes) {
		
Eric - kg6wxc's avatar
Eric - kg6wxc committed
235
236
		/* TESTING IDEA */
		/*
Eric - kg6wxc's avatar
Eric - kg6wxc committed
237
238
239
240
241
242
243
244
245
246
		$ipAddrArray = explode("\n", $meshNodes);
		$ipAddrArrayChunks = array_chunk($ipAddrArray, $USER_SETTINGS['numParallelThreads']);
		foreach($ipAddrArrayChunks as $chunk => $ipList) {
			foreach($ipList as $ipAddr) {
				//echo "";
			}
		}
		foreach($ipAddrArray as $ipAddr) {
			//echo "";
		}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
247
		*/
Eric - kg6wxc's avatar
Eric - kg6wxc committed
248
		/* END TESTING */
Eric - kg6wxc's avatar
Eric - kg6wxc committed
249
250
		
		
Eric - kg6wxc's avatar
Eric - kg6wxc committed
251
252
253
254
255
		foreach (preg_split("/((\r?\n)|(\r\r?))/", $meshNodes) as $line) {
		list ($ipAddr) = explode("\n", $line);
		
		//check for nodes that we know will not have the info we are going to request and skip them
		if ($do_sql) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
256
			if (wxc_getMySql("SELECT ip FROM hosts_ignore WHERE ip = '$ipAddr'")) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
257
258
259
260
261
262
263
				continue;
			}
		}
		
		//copy to new var name (I dont feel like editing it all right now)
		$nodeName = $ipAddr;
		if ($USER_SETTINGS['node_polling_parallel']) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
264
			shell_exec("php $INCLUDE_DIR/scripts/parallel_node_polling.php $ipAddr $do_sql 0 > /dev/null 2>/dev/null &");
Eric - kg6wxc's avatar
Eric - kg6wxc committed
265
266
267
			//for($count = 1; $count <= 20; $count++) {
			//	$ipAddrList .= $ipAddr . "\n";
			//}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
268
//			$parallel_pids = array();
Eric - kg6wxc's avatar
Eric - kg6wxc committed
269
			//$parallel_pids[] = trim(shell_exec("php $INCLUDE_DIR/scripts/parallel_node_polling.php $ipAddr $do_sql 0 > /dev/null 2>/dev/null & echo $!"));
Eric - kg6wxc's avatar
Eric - kg6wxc committed
270
//			$parallel_pids[] = trim(shell_exec("php $INCLUDE_DIR/scripts/parallel_node_polling.php $ipAddr $do_sql 0 > /dev/null 2>/dev/null & echo $!"));
Eric - kg6wxc's avatar
Eric - kg6wxc committed
271
272
273
274
275
276
			//$parallel_pids[] = shell_exec("php $INCLUDE_DIR/scripts/parallel_node_polling.php $ipAddr $do_sql 0 > /dev/null & echo $!");
			
		      //var_dump($parallel_pids);
			 
		}else {
			//get the sysinfo.json file from the node being polled.
Eric - kg6wxc's avatar
Eric - kg6wxc committed
277
			$sysinfoJson = @file_get_contents("http://$ipAddr:8080/cgi-bin/sysinfo.json?services_local=1"); //get the .json file
Eric - kg6wxc's avatar
Eric - kg6wxc committed
278
			
Eric - kg6wxc's avatar
Eric - kg6wxc committed
279
280
281
282
			//check if we got anything back, if not, try to tell why.
			if($sysinfoJson === FALSE) {
				$error = error_get_last();
				wxc_checkErrorMessage($error, $nodeName);
Eric - kg6wxc's avatar
Eric - kg6wxc committed
283
				//just skip to the next IP since there was an error
Eric - kg6wxc's avatar
Eric - kg6wxc committed
284
285
286
287
288
289
290
				continue;
			}else {
				//node is there, get all the info we can
				//get all the data from the json file and decode it
				$result = json_decode($sysinfoJson,true);
				
				//if there's nothing really there just skip to the next IP
Eric - kg6wxc's avatar
Eric - kg6wxc committed
291
				if (!$result || empty($result)) {
292
					//$host = wxc_resolveIP($ipAddr);
293
294
295
296
					if($testNodePolling) {
						wxc_echoWithColor("The json file is empty for node: " . $ipAddr . " " . wxc_resolveIP($ipAddr) . "\n", "red");
						echo "\n";
					}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
297
298
					continue;
				}
299
300
				//is RF enabled? (default to "on")
				$meshRF = "on";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
301
				
302
				//pull out API version first
303
304
305
306
307
308
309
310
				//some user-created json files leave out the api_version section
				//so lets set a default
				if (isset($result['api_version'])) {
					$api_version = $result['api_version'];
				}else {
					//just make it 0.0.0 by default, it will always fail a compare_version then.
					$api_version = "0.0.0";
				}
311
312
				
				//let's see what node we are dealing with
Eric - kg6wxc's avatar
Eric - kg6wxc committed
313
				//sometimes this might be blank, catch it
314
315
316
317
318
319
				if (version_compare($api_version, "1.5", "=")) {
					if (isset($result['node_details']['node'])) {
						$node = $result['node_details']['node'];
					}else {
						$node = $result['node'];
					}
320
321
322
323
324
					if (isset($result['meshrf']['status'])) {
						if ($result['meshrf']['status'] == "off") {
							$meshRF = "off";
						}
					}
325
326
327
				}else {
					$node = $result['node'];
				}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
328
				
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
				//check API version first!
				if (version_compare($api_version, "1.5", "=")) {
					if (isset($result['location']['lat'])) {
						$lat  = $result['location']['lat'];
					}else {
						$lat = $result['lat'];
					}
				}else {
					$lat = $result['lat'];
				}
				if (version_compare($api_version, "1.5", "=")) {
					if (isset($result['location']['lon'])) {
						$lon  = $result['location']['lon'];
					}else {
						$lon = $result['lon'];
					}
				}else {
					$lon = $result['lon'];
				}
348
349
350
351
352
353
354
355
				
				//if it's nothing other than the node name, it's some other device
				//or something else entirely...
				//people hack things onto the mesh all the time
				//
				//kg6wxc is *not* guilty of such things... :)
				//
				//just a few checks for nothing usually catches it.
356
357
				
				//***NOTE*** this might not be needed any more
358
359
360
				if ($node && $lat == "" && $lon == "" && $result['api_version'] == "") {
                	continue;
                }
Eric - kg6wxc's avatar
Eric - kg6wxc committed
361
362
				
				//this only seems to affect some nodes.
Eric - kg6wxc's avatar
Eric - kg6wxc committed
363
				//if lat || lon is blank, make it "0"
Eric - kg6wxc's avatar
Eric - kg6wxc committed
364
365
				//this was sometimes screwing up the SQL writing function
				//but not always of course.
366
                if (empty($lat)) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
367
				//if ($result['lat'] == "") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
368
				    $lat = 0.0;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
369
				}
370
371
372
373
				//else {
				//    $lat = $result['lat;
				//}
				if (empty($lon)) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
374
				//if ($result['lon'] == "") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
375
				    $lon = 0.0;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
376
				}
377
378
379
				//else {
				//    $lon = $result['lon'];
				//}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
380
				
381
382
				//save json data to some variables
				//probably don't really need to do this, but it is what it is for now...
Eric - kg6wxc's avatar
Eric - kg6wxc committed
383
				
384
385
386
387
388
389
390
391
				//check for new sysinfo.json API
				if (version_compare($api_version, "1.5", ">=")) {
					if (isset($result['meshrf']['chanbw'])) {
						$chanbw				=	$result['meshrf']['chanbw'];
					}
					else {
						$chanbw = "0";
					}
392
393
394
395
396
					if (isset($result['meshrf']['status'])) {
						if ($result['meshrf']['status'] == "off") {
							$meshRF = "off";
							$ssid = "NONE";
						}
397
398
399
400
401
402
403
404
					}else {
						$ssid				=	$result['meshrf']['ssid'];
					}
					if ($meshRF == "off") {
						$channel = "NONE";
					}else {
						$channel			=	$result['meshrf']['channel'];
					}
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
					$board_id				=	$result['node_details']['board_id'];
					$firmware_version		=	$result['node_details']['firmware_version'];
					$model					=	$result['node_details']['model'];
					$firmware_mfg			=	$result['node_details']['firmware_mfg'];
					$tunnel_installed		=	$result['tunnels']['tunnel_installed'];
					$active_tunnel_count	=	$result['tunnels']['active_tunnel_count'];
					if (isset($result['location']['grid_square'])) {
						$grid_square = $result['location']['grid_square'];
					}
					else {
						$grid_square = $result['grid_square'];
					}
				}else {
					if (isset($result['chanbw'])) {
						$chanbw = $result['chanbw'];
					}
					else {
						$chanbw = "0";
					}
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
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
					if (isset($result['ssid'])) {
						$ssid = $result['ssid'];
					}else {
						$ssid = "None";
					}
					if (isset($result['channel'])) {
						$channel = $result['channel'];
					}else {
						$channel = "N/A";
					}
					if (isset($result['board_id'])) {
						$board_id = $result['board_id'];
					}
					if (isset($result['firmware_version'])) {
						$firmware_version = $result['firmware_version'];
					}
					if (isset($result['model'])) {
						$model = $result['model'];
					}
					if (isset($result['firmware_mfg'])) {
						$firmware_mfg = $result['firmware_mfg'];
					}
					if (isset($result['tunnel_installed'])) {
						$tunnel_installed = $result['tunnel_installed'];
					}
					if (isset($result['active_tunnel_count'])) {
						$active_tunnel_count = $result['active_tunnel_count'];
					}
					if (isset($result['grid_square'])) {
						$grid_square = $result['grid_square'];
					}
					
					//catch some weird ones just in case...
					//If you make a custom json file, try to make sure you conform to the AREDN api_versions.
					//if not, this might catch it... maybe... we'll see.
					if (isset($result['node_details']['board_id'])) {
						$board_id = $result['node_details']['board_id'];
					}
					if (isset($result['node_details']['firmware_version'])) {
						$firmware_version = $result['node_details']['firmware_version'];
					}
					if (isset($result['node_details']['model'])) {
						$model = $result['node_details']['model'];
					}
					if (isset($result['tunnels']['tunnel_installed'])) {
						$tunnel_installed = $result['tunnels']['tunnel_installed'];
					}
					if (isset($result['tunnels']['active_tunnel_count'])) {
						$active_tunnel_count = $result['tunnels']['active_tunnel_count'];
					}
					if (isset($result['node_details']['firmware_mfg'])) {
						$firmware_mfg = $result['node_details']['firmware_mfg'];
					}
477
478
				}

Eric - kg6wxc's avatar
Eric - kg6wxc committed
479
480
				//had to screen scrape the status page for this info before
				//which required an additional call to the node
Eric - kg6wxc's avatar
Eric - kg6wxc committed
481
482
483
484
				//now it is here! :)
				$uptime = "NotAvailable";
				$loadavg = "NotAvailable";
				if (version_compare($api_version, "1.2", ">=")) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
485
486
487
488
489
490
					if (isset($result['sysinfo']['uptime'])) {
						$uptime = $result['sysinfo']['uptime'];
					}
					if (isset($result['sysinfo']['loads'])) {
						$loadavg = serialize($result['sysinfo']['loads']); // <-- this is an array that has been serialized!!
					}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
491
492
493
				}
				
				//local service listing are now in the json file!!  yay!
Eric - kg6wxc's avatar
Eric - kg6wxc committed
494
				//this required an additional call to port 9090 on the node before
Eric - kg6wxc's avatar
Eric - kg6wxc committed
495
496
				$services = "NotAvailable";
				if (version_compare($api_version, "1.3", ">=")) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
497
498
499
					if (isset($result['services_local'])) {
						$services = serialize($result['services_local']); // <-- this is an array that has been serialized!
					}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
500
501
				}
				
Eric - kg6wxc's avatar
Eric - kg6wxc committed
502
				//this only seems to affect some nodes.
Eric - kg6wxc's avatar
Eric - kg6wxc committed
503
				//if grid_square is blank, make it "none"
Eric - kg6wxc's avatar
Eric - kg6wxc committed
504
505
				//this was sometimes screwing up the SQL writing function
				//but not always of course.
506
				if ($grid_square == "") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
507
				    $grid_square = "none";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
508
				}
509
510
511
				//else {
				//    $grid_square = $result['grid_square'];
				//}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
512
513
514
515
				
				//W6BI requested this info to be added, so here it is now. :)
				//current ip/mac address info
				if ($result['interfaces']) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
516
					foreach($result['interfaces'] as $interface => $infInfo) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
517
						$eth = "eth0";
518
519
						$wlan = "wlan0";
						if ($model == "Ubiquiti Nanostation M XW" || $model == "AirRouter " || $model == "NanoStation M5 XW ") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
520
521
522
							//"AirRouter " model name bug caught and fixed by Mark, N2MH 13 March 2017.
							$eth = "eth0.0";
						}
523
524
525
526
527
528
529
530
531
532
						if ($model == "MikroTik RouterBOARD 952Ui-5ac2nD ") {
							$eth = "eth1.0";
						}
						
						//!!!THERE IS ONLY 1 Wireless interface available on this device so far!!!
						//this will be changed in the near future.
						if ($model == "MikroTik RouterBOARD 952Ui-5ac2nD ") {
							$wlan = "wlan1";
						}
						
Eric - kg6wxc's avatar
Eric - kg6wxc committed
533
534
						//
						// This should catch some of those pesky ones
Eric - kg6wxc's avatar
Eric - kg6wxc committed
535
						// finally!
Eric - kg6wxc's avatar
Eric - kg6wxc committed
536
						//
Eric - kg6wxc's avatar
Eric - kg6wxc committed
537
538
						if (is_numeric($interface)) {
							if ($infInfo['name'] == $eth) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
539
								if (isset($infInfo['ip'])) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
540
									if ($infInfo['ip'] == 'none') {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
541
542
										$lan_ip = "NotAvailable";
									}else {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
543
										$lan_ip = $infInfo['ip'];
Eric - kg6wxc's avatar
Eric - kg6wxc committed
544
									}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
545
								}else {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
546
547
									$lan_ip = "NotAvailable";
								}
548
							}elseif ($infInfo['name'] == $wlan && $meshRF == "on") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
549
550
								$wlan_ip = $infInfo['ip'];
								$wifi_mac_address = $infInfo['mac'];
551
552
553
554
555
556
							}elseif ($infInfo['name'] == $wlan && $meshRF == "off") {
								$wifi_mac_address = $infInfo['mac'];
							}elseif ($meshRF == "off") {
								if (strpos($infInfo['name'], ".3975") !== false) {
									$wlan_ip = $infInfo['ip'];
								}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
557
							}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
558
559
560
						}else {
							if ($interface == $eth) {
								$lan_ip = $infInfo['ip'];
561
							}elseif ($interface == $wlan) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
562
								$wlan_ip = $infInfo['ip'];
Eric - kg6wxc's avatar
Eric - kg6wxc committed
563
								$wifi_mac_address = $infInfo['mac'];
Eric - kg6wxc's avatar
Eric - kg6wxc committed
564
							}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
565
566
						}
					}
567
568
569
570
571
572
573
574
575
					if (!isset($wlan_ip) || !isset($wifi_mac_address)) {
						$infInfo = end($result['interfaces']);
						if (!isset($wlan_ip)) {
							$wlan_ip = $infInfo['ip'];
						}
						if (!isset($wifi_mac_address)) {
							$wifi_mac_address = $infInfo['mac'];
						}
					}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
576
577
578
				}
				
				if ($testNodePolling) {
579
					echo "Name: "; wxc_echoWithColor($node, "purple"); echo "\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
580
					echo "MAC Address: " . $wifi_mac_address . "\n";
581
582
583
584
585
					echo "Model: " . $model . "\n";
					if ($firmware_version !== $USER_SETTINGS['current_stable_fw_version']) {
						if (version_compare($firmware_version, $USER_SETTINGS['current_stable_fw_version'], "<")) {
							if ($firmware_version === "Linux" || $firmware_version === "linux") {
								echo "Firmware: " . $firmware_version . "  <- \033[1;32mViva Linux!!!\033[0m\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
586
							}else {
587
								echo "Firmware: " . $firmware_mfg . " " . $firmware_version;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
588
589
590
591
								wxc_echoWithColor(" Should update firmware!", "red");
								echo "\n";
							}
						}
592
						if (version_compare($firmware_version, $USER_SETTINGS['current_stable_fw_version'], ">")) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
593
							//echo "Firmware: " . $result['firmware_mfg'] . " " . $result['firmware_version'] . "  <- \033[31mBeta firmware!\033[0m\n";
594
							echo "Firmware: " . $firmware_mfg . " " . $firmware_version;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
595
596
597
598
599
							wxc_echoWithColor(" Beta firmware!", "red");
							echo "\n";
						}
					}else {
						//echo "Firmware Version: " . $firmware_version . "\n";
600
						echo "Firmware: \033[32m" . $firmware_mfg . " " . $firmware_version . "\033[0m\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
601
					}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
602
603
604
605
606
607
608
609
					
					echo "LAN ip: ";
					if ($lan_ip == "NotAvailable") {
						wxc_echoWithColor($lan_ip, "orange");
					}else {
						echo $lan_ip;
					}
					echo " WLAN ip: " . $wlan_ip . "\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
610
					
611
612
					if (($lat) && ($lon)) {
						echo "Location: \033[32m" . $lat . ", " . $lon. "\033[0m\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
613
614
615
616
617
618
					//}elseif ($nodeLocationFixed = 1) {
					//	echo "\033[31mNo Location Info Set!\033[0m (FIXED!)\n";
					//	$nodeLocationFixed = 0;
					}else {
						echo "\033[31mNo Location Info Set!\033[0m\n";
					    //K6GSE's solution to deal with non-null values in the DB
Eric - kg6wxc's avatar
Eric - kg6wxc committed
619
620
						$lat = 0.0;
						$lon = 0.0;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
621
622
623
						//end
					}
					
Eric - kg6wxc's avatar
Eric - kg6wxc committed
624
625
					if ($uptime !== "NotAvailable") {
						echo "Uptime: \033[32m" . $uptime . "\033[0m\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
626
					}else {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
627
						echo "Uptime: \033[33m" . $uptime . "\033[0m\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
628
					}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
629
630
631
632
633
					//if (!empty($sysinfoJson) || $sysinfoJson !== NULL) {
					//	echo "\033[32mSaved sysinfo.json\033[0m\n";
					//}else {
					//						
					//}
634
					//echo "\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
635
636
637
638
639
640
641
642
643
644
645
646
647
				}
				
				if ($do_sql) {
					$removed_node = wxc_getMySql("SELECT node, wifi_mac_address FROM removed_nodes WHERE node = '$node' OR wifi_mac_address = '$wifi_mac_address'");
					
					if ($removed_node['node'] == $node || $removed_node['wifi_mac_address'] == $wifi_mac_address) {
						wxc_putMySql("DELETE FROM removed_nodes WHERE node = '$node' OR wifi_mac_address = '$wifi_mac_address'");
					}
					
				}
					//our queries
				
				//this is saved in case it's actually needed later
Eric - kg6wxc's avatar
Eric - kg6wxc committed
648
649
				//should be handled by the SQL server now
				/*
Eric - kg6wxc's avatar
Eric - kg6wxc committed
650
651
652
653
654
655
656
657
658
659
				$sql_update_when_mac_addr_has_changed	=	"UPDATE $sql_db_tbl SET
            	wifi_mac_address=$wifi_mac_address,model=$model,
            	firmware_version=$firmware_version,
            	lat=$lat,lon=$lon,ssid=$ssid,chanbw=$chanbw,
            	api_version=$api_version,board_id=$board_id,
            	tunnel_install=$tunnel_installed,
            	active_tunnel_count=$active_tunnel_count,
            	channel=$channel,firmware_mfg=$firmware_mfg,
            	lan_ip=$lan_ip,wlan_ip=$wlan_ip,last_seen=NOW()
            	WHERE node=$node";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
660
				*/
Eric - kg6wxc's avatar
Eric - kg6wxc committed
661
662
663
664
665
666
667
668
669
670
671
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
				
				/* testing trying to make things more readable and
				 * not use extra variables...
				 * it's not really working, quite a bitch to get formated just right
				 *
				 $sql = "INSERT INTO $sql_db_tbl(wifi_mac_address, node, model, firmware_version,
				 lat, lon, ssid, chanbw, api_version, board_id,
				 tunnel_installed, active_tunnel_count, channel,
				 firmware_mfg, lan_ip, wlan_ip, sysinfo_json, olsrinfo_json, last_seen) VALUES('" .
				 $wifi_mac_address . "','" .
				 $result['node'] . "','" .
				 $result['model'] . "','" .
				 $result['firmware_version'] . "','" .
				 $result['lat'] . "','" .
				 $result['lon'] . "','" .
				 $result['ssid'] . "','" .
				 $result['chanbw'] . "','" .
				 $result['api_version'] . "','" .
				 $result['board_id'] . "','" .
				 $result['tunnel_installed'] . "','" .
				 $result['active_tunnel_count'] . "','" .
				 $result['channel'] . "','" .
				 $result['firmware_mfg'] . "','" .
				 $lan_ip . "','" .
				 $wlan_ip . "','" .
				 $sysinfoJson . "','" .
				 $olsrdInfo . "', NOW()) " .
				 "ON DUPLICATE KEY UPDATE " .
				 "node = '" . $result['node'] . "','" .
				 "model = '" . $result['model'] . "','" .
				 "firmware_version = '" . $result['firmware_version'] . "','" .
				 "lat = '" . $result['lat'] . "','" .
				 "lon = '" . $result['lon'] . "','" .
				 "ssid = '" . $result['ssid'] . "','" .
				 "chanbw = '" . $result['chanbw'] . "','" .
				 "api_version = '" . $result['api_version'] . "','" .
				 "board_id = '" . $result['board_id'] . "','" .
				 "tunnel_installed = '" . $result['tunnel_installed'] . "','" .
				 "active_tunnel_count = '" . $result['active_tunnel_count'] . "','" .
				 "channel =' " . $result['channel'] . "','" .
				 "firmware_mfg = '" . $result['firmware_mfg'] . "','" .
				 "lan_ip = '" . $lan_ip . "','" .
				 $wlan_ip . "','" .
				 $sysinfoJson . "','" .
				 $olsrdInfo . "', NOW())";
				 */
				
				$sql	=	"INSERT INTO $sql_db_tbl(
				wifi_mac_address, node, model, firmware_version, lat, lon, grid_square, ssid, chanbw, api_version, board_id,
Eric - kg6wxc's avatar
Eric - kg6wxc committed
710
				tunnel_installed, active_tunnel_count, channel, firmware_mfg, lan_ip, wlan_ip, uptime, loadavg, services, last_seen)
Eric - kg6wxc's avatar
Eric - kg6wxc committed
711
712
713
				VALUES('$wifi_mac_address', '$node', '$model', '$firmware_version',
				'$lat', '$lon', '$grid_square', '$ssid', '$chanbw', '$api_version', '$board_id',
				'$tunnel_installed', '$active_tunnel_count', '$channel',
Eric - kg6wxc's avatar
Eric - kg6wxc committed
714
				'$firmware_mfg', '$lan_ip', '$wlan_ip', '$uptime', '$loadavg', '$services', NOW())
Eric - kg6wxc's avatar
Eric - kg6wxc committed
715
716
717
718
719
				ON DUPLICATE KEY UPDATE wifi_mac_address = '$wifi_mac_address', node = '$node', model = '$model', firmware_version = '$firmware_version',
				lat = '$lat', lon = '$lon', grid_square = '$grid_square', ssid = '$ssid', chanbw = '$chanbw', api_version = '$api_version',
				board_id = '$board_id', tunnel_installed = '$tunnel_installed',
				active_tunnel_count = '$active_tunnel_count', channel = '$channel',
				firmware_mfg = '$firmware_mfg', lan_ip = '$lan_ip', wlan_ip = '$wlan_ip',
Eric - kg6wxc's avatar
Eric - kg6wxc committed
720
				uptime = '$uptime', loadavg = '$loadavg', services = '$services', last_seen = NOW()";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
721
722
723
				
				$sql_no_location_info  =    "INSERT INTO $sql_db_tbl(
				wifi_mac_address, node, model, firmware_version, grid_square, ssid, chanbw, api_version, board_id,
Eric - kg6wxc's avatar
Eric - kg6wxc committed
724
				tunnel_installed, active_tunnel_count, channel, firmware_mfg, lan_ip, wlan_ip, uptime, loadavg, services, last_seen)
Eric - kg6wxc's avatar
Eric - kg6wxc committed
725
726
727
				VALUES('$wifi_mac_address', '$node', '$model', '$firmware_version',
				'$grid_square', '$ssid', '$chanbw', '$api_version', '$board_id',
				'$tunnel_installed', '$active_tunnel_count', '$channel',
Eric - kg6wxc's avatar
Eric - kg6wxc committed
728
				'$firmware_mfg', '$lan_ip', '$wlan_ip', '$uptime', '$loadavg', '$services', NOW())
Eric - kg6wxc's avatar
Eric - kg6wxc committed
729
730
731
732
733
				ON DUPLICATE KEY UPDATE wifi_mac_address = '$wifi_mac_address', node = '$node', model = '$model', firmware_version = '$firmware_version',
				grid_square = '$grid_square', ssid = '$ssid', chanbw = '$chanbw', api_version = '$api_version',
				board_id = '$board_id', tunnel_installed = '$tunnel_installed',
				active_tunnel_count = '$active_tunnel_count', channel = '$channel',
				firmware_mfg = '$firmware_mfg', lan_ip = '$lan_ip', wlan_ip = '$wlan_ip',
Eric - kg6wxc's avatar
Eric - kg6wxc committed
734
				uptime = '$uptime', loadavg = '$loadavg', services = '$services', last_seen = NOW()";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
735
736
737
738
739
740
741
742
743
				
				$sql_update_when_node_name_has_changed	=	"UPDATE $sql_db_tbl SET
            	node = '$node', model = '$model',
            	firmware_version = '$firmware_version',
            	lat = '$lat', lon = '$lon', grid_square = '$grid_square', ssid = '$ssid', chanbw = '$chanbw',
            	api_version = '$api_version', board_id = '$board_id',
            	tunnel_installed = '$tunnel_installed',
            	active_tunnel_count = '$active_tunnel_count',
            	channel = '$channel', firmware_mfg = '$firmware_mfg',
Eric - kg6wxc's avatar
Eric - kg6wxc committed
744
745
            	lan_ip = '$lan_ip', wlan_ip = '$wlan_ip',
				uptime = '$uptime', loadavg = '$loadavg', services = '$services', last_seen=NOW()
Eric - kg6wxc's avatar
Eric - kg6wxc committed
746
747
748
749
750
751
752
753
754
755
            	WHERE wifi_mac_address = '$wifi_mac_address'";
				
				$sql_update_when_node_name_has_changed_no_location_info	=	"UPDATE $sql_db_tbl SET
            	node = '$node', model = '$model',
            	firmware_version = '$firmware_version',
            	grid_square = '$grid_square', ssid = '$ssid', chanbw = '$chanbw',
            	api_version = '$api_version', board_id = '$board_id',
            	tunnel_installed = '$tunnel_installed',
            	active_tunnel_count = '$active_tunnel_count',
            	channel = '$channel', firmware_mfg = '$firmware_mfg',
Eric - kg6wxc's avatar
Eric - kg6wxc committed
756
757
            	lan_ip = '$lan_ip', wlan_ip = '$wlan_ip',
				uptime = '$uptime', loadavg = '$loadavg', services = '$services', last_seen=NOW()
Eric - kg6wxc's avatar
Eric - kg6wxc committed
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
            	WHERE wifi_mac_address = '$wifi_mac_address'";
				
				//find the currently stored name and mac address of the node we are looking at
				if($do_sql) {
				    $node_name_array	=	wxc_getMySql("SELECT node, wifi_mac_address FROM $sql_db_tbl WHERE wifi_mac_address = '$wifi_mac_address'");
				    $existing_node_name	=	$node_name_array['node'];
				    $existing_mac_addr	=	$node_name_array['wifi_mac_address'];
				}
				
				//check if we are updating this nodes location info or not
				if($do_sql) {
				    $fixedLocation = wxc_getMySql("SELECT location_fix FROM $sql_db_tbl_node WHERE node = '$node'");
				    if ($fixedLocation['location_fix'] == "1") {
				        //check if we have changed node name and have the same hardware.
				        //the database itself should handle if there is new hardware with the same node name.
				        //if name has not changed, update the DB as normal for each node
				        if ($do_sql && $sysinfoJson) {
				            if(!$existing_mac_addr) {
				                wxc_putMySql($sql_no_location_info);
				            }elseif($existing_mac_addr == $wifi_mac_address) {
				                if ($existing_node_name !== $node) {
				                    wxc_putMySql($sql_update_when_node_name_has_changed_no_location_info);
				                    //echo "";
				                }else {
				                    wxc_putMySQL($sql_no_location_info);
				                }
				            }
				        }
Eric - kg6wxc's avatar
Eric - kg6wxc committed
786
787
788
789
790
791
				        if ($testNodePolling) {
				        	wxc_echoWithColor('****This nodes location has been "fixed" in the Database!****', 'alert');
				        	echo "\n";
				        	wxc_echoWithColor('****It will not update from polling if the location changes****', 'alert');
				        	echo "\n";
				        }
Eric - kg6wxc's avatar
Eric - kg6wxc committed
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
				    }else {
				        //check if we have changed node name and have the same hardware.
				        //the database itself should handle if there is new hardware with the same node name.
				        //if name has not changed, update the DB as normal for each node
				        if ($do_sql && $sysinfoJson) {
				            if(!$existing_mac_addr) {
				                wxc_putMySql($sql);
				            }elseif($existing_mac_addr == $wifi_mac_address) {
				                if ($existing_node_name !== $node) {
				                    wxc_putMySql($sql_update_when_node_name_has_changed);
				                    //echo "";
				                }else {
				                    wxc_putMySQL($sql);
				                }
				            }
				        }
				    }
				}
810
811
812
				if ($testNodePolling) {
					echo "\n";
				}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
					//Thanks to K6GSE
					// Clear Variables so they do not carry over
					$wifi_mac_address = NULL;
					$node = NULL;
					$model = NULL;
					$firmware_version = NULL;
					$lat = NULL;
					$lon = NULL;
					$ssid = NULL;
					$chanbw = NULL;
					$api_version = NULL;
					$board_id = NULL;
					$tunnel_installed = NULL;
					$active_tunnel_count = NULL;
					$channel = NULL;
					$firmware_mfg = NULL;
					$lan_ip = NULL;
					$wlan_ip = NULL;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
831
832
833
					$uptime = NULL;
					$loadavg = NULL;
					$services = NULL;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
					$sysinfoJson = NULL;
				}
			}
		}
	} //end of foreach loop

	//update the database with the time, so we know when this part of the script last ran
	if($do_sql) {
		wxc_scriptUpdateDateTime("NODEINFO", "node_info");
		wxc_putMySql("UPDATE map_info SET currently_running = '0' WHERE id = 'NODEINFO'");
	}
}
/*
foreach ($parallel_pids as $index => $pid) {
//    $stillRunning = count(exec("ps $pid"))
    exec ("ps $pid", $processState);
    $temp = count($processState >= 2);
    if ($temp = 1) {
        echo "still running...";
        continue;
    }else {
        unset($parallel_pids['$index']);
        $parallel_pids = value_arrays($parallel_pids);
        //continue;
    }
    echo "\nthere should be no more polling scripts running....\n";
    //var_dump($temp);
}
*/
if ($getLinkInfo) {
	if ($do_sql) {
		wxc_putMySQL("TRUNCATE TABLE $sql_db_tbl_topo");	//clear out the old info in the "topology" table first
	}
	
	$meshNodes = wxc_netcat($USER_SETTINGS['localnode'], "2004", null, "linkInfo");	//get the latest link info
	if ($meshNodes) {
		foreach (preg_split("/((\r?\n)|(\r\r?))/", $meshNodes) as $line) {	//split the string on the \n's (new line)
			//if ($line !== "") {
				list ($node, $linkto, $cost) = explode(" ", $line);
				//echo "Node: " . $node . " Linkto: " . $linkto . " Cost: " . $cost . "\n";
			//}
			
			$nodeIp = $node;
			$linktoIp = $linkto;
			//$nodeName = wxc_resolveIP($node);
			//$linktoName = wxc_resolveIP($linkto);
			$nodeName = $node;
			$linktoName = $linkto;
			
			if ($do_sql) {	//put the data into SQL, if we can.	
				wxc_putMySQL("INSERT INTO $sql_db_tbl_topo(node, linkto, cost) VALUES('$nodeName', '$linktoName', '$cost')");
Eric - kg6wxc's avatar
Eric - kg6wxc committed
885
886
887
888
889
890
				//quick hacks to remove/change junk
				wxc_putMySQL("delete from $sql_db_tbl_topo where node is NULL and linkto is NULL");
			//	wxc_putMySql("update topology set nodelat = 0 where nodelat is NULL");
			//	wxc_putMySql("update topology set nodelon = 0 where nodelon is NULL");
			//	wxc_putMySql("update topology set linklat = 0 where linklat is NULL");
			//	wxc_putMySql("update topology set linklon = 0 where linklon is NULL");
Eric - kg6wxc's avatar
Eric - kg6wxc committed
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
			}
			
			if ($testLinkInfo) {	//output to screen if we are in "test mode".
				if ($cost > 0.1 && $cost <=2) {
					$cost = "\033[1;32m" . $cost . "\033[0m";
				}
				if ($cost >2 && $cost <4) {
					$cost = "\033[0;32m" . $cost . "\033[0m";
				}
				if ($cost >4 && $cost <6) {
					$cost = "\033[1;33m" . $cost . "\033[0m";
				}
				if ($cost >6 && $cost <10) {
					$cost = "\033[0;31m" . $cost . "\033[0m";
				}
				if ($cost >10) {
					$cost = "\033[1;31m" . $cost . "\033[0m";
				}
				echo "$nodeName -> $linktoName cost: $cost\n";
			}
			if($do_sql) {
				wxc_scriptUpdateDateTime("LINKINFO", "topology");
			}
		}
	}
}

$mtimeEnd = microtime(true);
$totalTime = $mtimeEnd-$mtimeStart;
if ($showRuntime) {
	echo "Time Elapsed: " . round($totalTime, 2) . " seconds ( " . round($totalTime/60, 2) . " minutes ).\n";
}

?>