parallel_node_polling.php 21.7 KB
Newer Older
Eric - kg6wxc's avatar
Eric - kg6wxc committed
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/******
* parallel_node_polling script v3 by kg6wxc\eric satterlee kg6wxc@gmail.com
* Licensed under GPLv3 or later
*
* 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/>.
******/

21
22
23
24
25
26
27
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...");
}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
28
$INCLUDE_DIR = "..";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
29
30
$USER_SETTINGS = parse_ini_file($INCLUDE_DIR . "/scripts/user-settings.ini");
require $INCLUDE_DIR . "/scripts/wxc_functions.inc";
31
32
33
34
35
36
@include $INCLUDE_DIR . "/custom.inc";
//foreach (get_included_files() as $filename) {
//	if (strpos($filename, 'wxc_custom.inc')) {
//		$wxc_custom = 1;
//	}
//}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

$ipAddr = $argv[1];
$do_sql = $argv[2];
$testNodePolling = $argv[3];
if ($do_sql) {
	$sql_connection =  wxc_connectToMySql();
}
$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'];

$sysinfoJson = @file_get_contents("http://$ipAddr:8080/cgi-bin/sysinfo.json"); //get the .json file
if($sysinfoJson === FALSE) {
	$error = error_get_last();
	wxc_checkErrorMessage($error, $ipAddr);
Eric - kg6wxc's avatar
Eric - kg6wxc committed
52
53
54
55
	//just exit since there was an error
	//when running in parallel mode you wont see this
	//this script can be used to "target" a node and see what it returns tho
	if ($testNodePolling) {
56
		wxc_echoWithColor("There was an error retreiving the json file for: $ipAddr\n", "red");
Eric - kg6wxc's avatar
Eric - kg6wxc committed
57
58
	}
	exit();
Eric - kg6wxc's avatar
Eric - kg6wxc committed
59
60
61
62
63
}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);
	
Eric - kg6wxc's avatar
Eric - kg6wxc committed
64
65
	//if there's nothing really there just exit
	//this is the parallel script!
Eric - kg6wxc's avatar
Eric - kg6wxc committed
66
	if (!$result || empty($result)) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
67
		if ($testNodePolling) {
68
			wxc_echoWithColor("The json file is empty for node: $ipAddr\n", "red");
Eric - kg6wxc's avatar
Eric - kg6wxc committed
69
70
		}
		exit();
Eric - kg6wxc's avatar
Eric - kg6wxc committed
71
	}
72
73
	//is RF enabled? (default to "on")
	$meshRF = "on";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
74
	
75
	//pull out API version first
76
77
78
79
80
81
82
83
	//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";
	}
84
85
86
87
88
89
90
91
92
	
	//let's see what node we are dealing with
	//sometimes this might be blank, catch it
	if (version_compare($api_version, "1.5", "=")) {
		if (isset($result['node_details']['node'])) {
			$node = $result['node_details']['node'];
		}else {
			$node = $result['node'];
		}
93
94
95
96
97
		if (isset($result['meshrf']['status'])) {
			if ($result['meshrf']['status'] == "off") {
				$meshRF = "off";
			}
		}
98
99
100
	}else {
		$node = $result['node'];
	}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
101
	
Eric - kg6wxc's avatar
Eric - kg6wxc committed
102
103
104
105
106
107
108
	//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.
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	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'];
	}
	if ($node && $lat == "" && $lon == "" && $result['api_version'] == "") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
128
				if ($testNodePolling) {
129
					wxc_echoWithColor("This is probably some linux device running OLSR: " . $result['node'] . "\n", "red");
Eric - kg6wxc's avatar
Eric - kg6wxc committed
130
131
				}
				exit();
Eric - kg6wxc's avatar
Eric - kg6wxc committed
132
133
134
135
136
137
	}

	//this only seems to affect some nodes.
	//if lat || lon is blank, make it "0"
	//this was sometimes screwing up the SQL writing function
	//but not always of course.
138
139
	if (empty($lat)) {
		//if ($result['lat'] == "") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
140
141
		$lat = 0.0;
	}
142
143
144
145
146
	//else {
	//    $lat = $result['lat;
	//}
	if (empty($lon)) {
		//if ($result['lon'] == "") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
147
		$lon = 0.0;
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
	}
	//else {
	//    $lon = $result['lon'];
	//}

	//save json data to some variables
	//probably don't really need to do this, but it is what it is for now...
	//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";
		}
163
164
165
166
167
		if (isset($result['meshrf']['status'])) {
			if ($result['meshrf']['status'] == "off") {
				$meshRF = "off";
				$ssid = "NONE";
			}
168
169
170
171
172
173
174
175
		}else {
			$ssid				=	$result['meshrf']['ssid'];
		}
		if ($meshRF == "off") {
			$channel = "NONE";
		}else {
			$channel			=	$result['meshrf']['channel'];
		}
176
177
178
179
180
181
182
183
184
185
186
187
		$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'];
		}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
188
	}else {
189
190
191
192
193
194
		if (isset($result['chanbw'])) {
			$chanbw = $result['chanbw'];
		}
		else {
			$chanbw = "0";
		}
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
		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'];
		}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
248
249
	}
	
Eric - kg6wxc's avatar
Eric - kg6wxc committed
250
	//had to screen scrape the status page for this info before
Eric - kg6wxc's avatar
Eric - kg6wxc committed
251
252
253
254
	//now it is here! :)
	$uptime = "NotAvailable";
	$loadavg = "NotAvailable";
	if (version_compare($api_version, "1.2", ">=")) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
255
256
257
258
259
260
		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
261
262
263
264
265
	}
	
	//local service listing are now in the json file!!  yay!
	$services = "NotAvailable";
	if (version_compare($api_version, "1.3", ">=")) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
266
267
268
		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
269
270
271
272
273
274
	}
	
	//this only seems to affect some nodes.
	//if grid_square is blank, make it "none"
	//this was sometimes screwing up the SQL writing function
	//but not always of course.
275
	if ($grid_square == "") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
276
277
		$grid_square = "none";
	}
278
279
280
	//else {
	//    $grid_square = $result['grid_square'];
	//}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
281
282
283
284
			
	//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
285
		foreach($result['interfaces'] as $interface => $infInfo) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
286
			$eth = "eth0";
287
288
			$wlan = "wlan0";
			if ($model == "Ubiquiti Nanostation M XW" || $model == "AirRouter " || $model == "NanoStation M5 XW ") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
289
290
291
				//"AirRouter " model name bug caught and fixed by Mark, N2MH 13 March 2017.
				$eth = "eth0.0";
			}
292
293
294
295
296
297
298
299
300
301
			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
302
303
			//
			// This should catch some of those pesky ones
Eric - kg6wxc's avatar
Eric - kg6wxc committed
304
			// finally!
Eric - kg6wxc's avatar
Eric - kg6wxc committed
305
			//
Eric - kg6wxc's avatar
Eric - kg6wxc committed
306
307
			if (is_numeric($interface)) {
				if ($infInfo['name'] == $eth) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
308
					if (isset($infInfo['ip'])) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
309
						if ($infInfo['ip'] == 'none') {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
310
311
							$lan_ip = "NotAvailable";
						}else {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
312
							$lan_ip = $infInfo['ip'];
Eric - kg6wxc's avatar
Eric - kg6wxc committed
313
						}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
314
					}else {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
315
316
						$lan_ip = "NotAvailable";
					}
317
				}elseif ($infInfo['name'] == $wlan && $meshRF == "on") {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
318
319
					$wlan_ip = $infInfo['ip'];
					$wifi_mac_address = $infInfo['mac'];
320
321
322
323
324
325
				}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
326
				}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
327
328
329
			}else {
				if ($interface == $eth) {
					$lan_ip = $infInfo['ip'];
330
				}elseif ($interface == $wlan) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
331
					$wlan_ip = $infInfo['ip'];
Eric - kg6wxc's avatar
Eric - kg6wxc committed
332
					$wifi_mac_address = $infInfo['mac'];
Eric - kg6wxc's avatar
Eric - kg6wxc committed
333
				}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
334
335
			}
		}
336
337
338
339
340
341
342
343
344
		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
345
	}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
346
347
	//in true parallel mode you wont see anything output, there is no screen
	//but you can use this script to "target" a node and see what it is returning.
Eric - kg6wxc's avatar
Eric - kg6wxc committed
348
	if ($testNodePolling) {
349
		echo "Name: "; wxc_echoWithColor($node, "purple"); echo "\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
350
		echo "MAC Address: " . $wifi_mac_address . "\n";
351
352
353
354
355
		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
356
				}else {
357
					echo "Firmware: " . $firmware_mfg . " " . $firmware_version;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
358
359
360
361
					wxc_echoWithColor(" Should update firmware!", "red");
					echo "\n";
				}
			}
362
			if (version_compare($firmware_version, $USER_SETTINGS['current_stable_fw_version'], ">")) {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
363
				//echo "Firmware: " . $result['firmware_mfg'] . " " . $result['firmware_version'] . "  <- \033[31mBeta firmware!\033[0m\n";
364
				echo "Firmware: " . $firmware_mfg . " " . $firmware_version;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
365
366
367
368
369
				wxc_echoWithColor(" Beta firmware!", "red");
				echo "\n";
			}
		}else {
			//echo "Firmware Version: " . $firmware_version . "\n";
370
			echo "Firmware: \033[32m" . $firmware_mfg . " " . $firmware_version . "\033[0m\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
371
		}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
372
373
374
375
376
377
378
379
380
		
		echo "LAN ip: ";
		if ($lan_ip == "NotAvailable") {
			wxc_echoWithColor($lan_ip, "orange");
		}else {
			echo $lan_ip;
		}
		echo " WLAN ip: " . $wlan_ip . "\n";
		
381
382
		if (($lat) && ($lon)) {
			echo "Location: \033[32m" . $lat . ", " . $lon. "\033[0m\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
383
384
385
386
387
			//}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";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
388
389
390
			//K6GSE's solution to deal with non-null values in the DB
			$lat = 0.0;
			$lon = 0.0;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
391
392
			//end
		}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
393
394
395
		
		if ($uptime !== "NotAvailable") {
			echo "Uptime: \033[32m" . $uptime . "\033[0m\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
396
		}else {
Eric - kg6wxc's avatar
Eric - kg6wxc committed
397
			echo "Uptime: \033[33m" . $uptime . "\033[0m\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
398
		}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
399
400
401
402
403
		//if (!empty($sysinfoJson) || $sysinfoJson !== NULL) {
		//	echo "\033[32mSaved sysinfo.json\033[0m\n";
		//}else {
		//
		//}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
404
		//echo "\n";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
405
406
407
408
409
410
411
412
	}
			
	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'");
		}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
413
		
Eric - kg6wxc's avatar
Eric - kg6wxc committed
414
415
416
417
418
419
420
421
422
423
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
	}
	//our queries
				
	//this is saved in case it's actually needed later
	$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";
				
	/* 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
477
				tunnel_installed, active_tunnel_count, channel, firmware_mfg, lan_ip, wlan_ip, uptime, loadavg, services, last_seen)
Eric - kg6wxc's avatar
Eric - kg6wxc committed
478
479
480
				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
481
				'$firmware_mfg', '$lan_ip', '$wlan_ip', '$uptime', '$loadavg', '$services', NOW())
Eric - kg6wxc's avatar
Eric - kg6wxc committed
482
483
484
485
486
				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
487
				uptime = '$uptime', loadavg = '$loadavg', services = '$services', last_seen = NOW()";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
488
489
490
	
	$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
491
				tunnel_installed, active_tunnel_count, channel, firmware_mfg, lan_ip, wlan_ip, uptime, loadavg, services, last_seen)
Eric - kg6wxc's avatar
Eric - kg6wxc committed
492
493
494
				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
495
				'$firmware_mfg', '$lan_ip', '$wlan_ip', '$uptime', '$loadavg', '$services', NOW())
Eric - kg6wxc's avatar
Eric - kg6wxc committed
496
497
498
499
500
				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
501
				uptime = '$uptime', loadavg = '$loadavg', services = '$services', last_seen = NOW()";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
502
503
504
505
506
507
508
509
510
	
	$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
511
512
            	lan_ip = '$lan_ip', wlan_ip = '$wlan_ip',
				uptime = '$uptime', loadavg = '$loadavg', services = '$services', last_seen=NOW()
Eric - kg6wxc's avatar
Eric - kg6wxc committed
513
            	WHERE wifi_mac_address = '$wifi_mac_address'";
Eric - kg6wxc's avatar
Eric - kg6wxc committed
514
	
Eric - kg6wxc's avatar
Eric - kg6wxc committed
515
516
517
518
519
520
521
522
	$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
523
524
            	lan_ip = '$lan_ip', wlan_ip = '$wlan_ip',
				uptime = '$uptime', loadavg = '$loadavg', services = '$services', last_seen=NOW()
Eric - kg6wxc's avatar
Eric - kg6wxc committed
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
            	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
553
554
555
556
557
558
	        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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
	    }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);
    	            }
    	        }
    	    }
	    }
	}
	//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
595
596
597
	$uptime = NULL;
	$loadavg = NULL;
	$services = NULL;
Eric - kg6wxc's avatar
Eric - kg6wxc committed
598
599
600
	$sysinfoJson = NULL;
}

Eric - kg6wxc's avatar
Eric - kg6wxc committed
601
602
603
604
605
//update the database with the time, so we know when the last update is
if($do_sql) {
	wxc_scriptUpdateDateTime("NODEINFO", "node_info");
	wxc_putMySql("UPDATE map_info SET currently_running = '0' WHERE id = 'NODEINFO'");
}
Eric - kg6wxc's avatar
Eric - kg6wxc committed
606
?>