This repository was archived by the owner on Sep 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpollingScript.php
More file actions
executable file
·710 lines (641 loc) · 25.1 KB
/
Copy pathpollingScript.php
File metadata and controls
executable file
·710 lines (641 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
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
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
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
616
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
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
710
#!/usr/bin/env php
<?php
//only run from the command line
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...");
}
//get start time
$mtimeOverallStart = microtime(true);
/************************************
* New polling script for meshmap apr 2021-2024 - kg6wxc
* Original meshmap scripts are from 2016 and beyond.
*
* Licensed under GPLv3 or later
* This script is the heart of kg6wxcs' mesh map system.
* bug fixes, improvements and corrections are welcomed!
*
* 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/>.
************************************/
$INCLUDE_DIR = dirname(__FILE__);
require $INCLUDE_DIR . "/include/pollingFunctions.inc";
require $INCLUDE_DIR . "/include/mysqlFunctions.inc";
require $INCLUDE_DIR . "/include/colorizeOutput.inc";
require $INCLUDE_DIR . "/include/outputToConsole.inc";
require $INCLUDE_DIR . "/include/outputToFile.inc";
require $INCLUDE_DIR . "/include/checkArednVersions.inc";
require $INCLUDE_DIR . "/include/sqliteStuff.inc";
require $INCLUDE_DIR . "/include/calcDistanceAndBearing.inc";
require $INCLUDE_DIR . "/include/createJS.inc";
require $INCLUDE_DIR . "/include/node_report_data.inc";
require $INCLUDE_DIR . "/include/error_report_data.inc";
require $INCLUDE_DIR . "/include/noLocation_report_data.inc";
$USE_SQL = 1;
$TEST_MODE = 0;
$START_POLLING = 1;
if (!empty($argv[1])) {
switch ($argv[1]) {
case "--help":
echo $argv[0] . " Usage:\n\n";
echo $argv[1] . "\tThis help message\n\n";
echo "--data-files-only\tNo polling, only regenerate the data files used by the webpage\n";
echo wxc_addColor("this option does not work yet", "redBold") . "\n\n";
echo "--test-mode-no-sql\tDO NOT access database only output to screen\n";
echo "(or to the log files when using parallel mode)\n";
echo "(this parameter is useful for testing)\n\n";
echo "--test-mode-with-sql\tDO access the database AND output to screen\n";
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";
echo "\n";
exit();
case "--test-mode-no-sql":
$USE_SQL = 0;
$TEST_MODE = 1;
break;
case "--test-mode-with-sql":
$TEST_MODE = 1;
break;
case "--data-files-only":
$USE_SQL = 0;
$START_POLLING = 0;
break;
default:
exit("Unknown parameter\nTry: " . $argv[0] . wxc_addColor(" --help", "red") . "\n");
}
}
global $USER_SETTINGS;
// Function to replace placeholders with environment variables
function replace_env_vars($settings) {
foreach ($settings as $key => $value) {
if (is_array($value)) {
$settings[$key] = replace_env_vars($value);
} else {
if (preg_match('/\$\{(\w+)\}/', $value, $matches)) {
$env_var = getenv($matches[1]);
if ($env_var !== false) {
$settings[$key] = str_replace($matches[0], $env_var, $value);
}
}
}
}
return $settings;
}
//load defaults into $USER_SETTINGS
if (file_exists($INCLUDE_DIR . "/settings/user-settings.ini-default")) {
$DEFAULT_USER_SETTINGS = parse_ini_file($INCLUDE_DIR . "/settings/user-settings.ini-default");
}else {
exit("settings/user-settings.ini-default is missing!\n\n");
}
//check for users custom settings
if (file_exists($INCLUDE_DIR . "/settings/user-settings.ini")) {
$USER_SETTINGS = parse_ini_file($INCLUDE_DIR . "/settings/user-settings.ini");
}else {
exit("\n\nYou **must** copy the user-settings.ini-default file to user-settings.ini and edit it!!\n\n");
}
//merge default settings and users custom settings
$USER_SETTINGS = array_merge($DEFAULT_USER_SETTINGS, $USER_SETTINGS);
// Replace environment variable placeholders
$USER_SETTINGS = replace_env_vars($USER_SETTINGS);
//first grab the localnodes actual name
//this is used to locate the map data "origin"
if($TEST_MODE) {
echo "Polling " . $USER_SETTINGS['localnode'] . " for some info before starting... ";
}
$localInfo = @file_get_contents("http://" . $USER_SETTINGS['localnode'] . "/cgi-bin/sysinfo.json");
if($localInfo == "" || is_null($localInfo)) {
if(!isset(error_get_last()['message']) || is_null(error_get_last()['message']) || error_get_last()['message'] == "") {
exit("Could not get map origin node name: No error, just... nothing, null, nada.\n");
}else {
$failReason = trim(substr(strrchr(error_get_last()['message'], ":"), 1));
//AREDN port 8080 is going away, try new way
if($failReason === "Connection refused") {
$localInfo = "";
$localInfo = @file_get_contents("http://" . $USER_SETTINGS['localnode'] . ":8080/cgi-bin/sysinfo.json");
if($localInfo == "" || is_null($localInfo)) {
if(!isset(error_get_last()['message']) || is_null(error_get_last()['message']) || error_get_last()['message'] == "") {
exit("Could not get map origin node name: No error, just... nothing, null, nada.\n");
}else {
exit("Could not get map origin node name: " . trim(substr(strrchr(error_get_last()['message'], ":"), 1)) . "\n");
}
}
}else {
exit("Could not get map origin node name: " . trim(substr(strrchr(error_get_last()['message'], ":"), 1)) . "\n");
}
}
}
$localInfo = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $localInfo);
$localInfo = json_decode($localInfo,true);
$localNodeName = $localInfo['node'];
unset($localInfo);
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold") . "\n";
}
//TODO: combine this with above check for node name and make into a function... seriously! This is getting messy again.
if($TEST_MODE) {
echo "Attempting to retrieve network topology from " . $USER_SETTINGS['localnode'] . "... ";
}
//attempt to get network topology from AREDN API
//try the new way (any firmware after 10-17-2024)
$allMeshNodes = @url_get_contents("http://" . $USER_SETTINGS['localnode'] . "/cgi-bin/sysinfo.json?topology=1");
//$allMeshNodes = @file_get_contents("http://" . $USER_SETTINGS['localnode'] . "/cgi-bin/sysinfo.json?topology=1");
//if that doesn't work try the old way (pre 10-17-2024)
if (!strpos($allMeshNodes, "topology")) {
$allMeshNodes = "";
$allMeshNodes = @file_get_contents("http://" . $USER_SETTINGS['localnode'] . ":8080/cgi-bin/api?mesh=topology");
if(empty($allMeshNodes)) {
//the dang node did not return anything, they do this sometimes, try again after a 10 seconds
if($TEST_MODE) {
echo "\nTrying again after 10 seconds... ";
}
sleep(10);
//try the new way (any firmware after 10-17-2024)
$allMeshNodes = @file_get_contents("http://" . $USER_SETTINGS['localnode'] . "/cgi-bin/sysinfo.json?topology=1");
//if that doesn't work try the old way (pre 10-17-2024)
if (!strpos($allMeshNodes, "topology")) {
$allMeshNodes = "";
$allMeshNodes = @file_get_contents("http://" . $USER_SETTINGS['localnode'] . ":8080/cgi-bin/api?mesh=topology");
}
if(!strpos($allMeshNodes, "topology") || empty($allMeshNodes)) {
//we have failed 2x, just exit, something is wrong
exit(wxc_addColor("\nTHERE WAS A PROBLEM ACCESSING THE API ON YOUR LOCALNODE!\n" . error_get_last()['message'] . "\n", "redBold"));
}
}
}
//decode the json retrieved from localnode
$allMeshNodes = @json_decode($allMeshNodes, true);
if (!is_array($allMeshNodes)) {
if($TEST_MODE) { echo "\n"; }
exit(wxc_addColor("THERE WAS A PROBLEM DECODING THE TOPOLOGY JSON FROM YOUR LOCALNODE!\n JSON_ERR_MSG: " . json_last_error_msg(), "redBold") . "\n\n");
}
//pull out just the topology info and clear old $var.
if (@is_array($allMeshNodes['pages']['mesh']['topology']['topology'])) {
$topoInfo = $allMeshNodes['pages']['mesh']['topology']['topology'];
}elseif(@is_array($allMeshNodes['topology'])) {
$topoInfo = $allMeshNodes['topology'];
}else {
if($TEST_MODE) { echo "\n"; }
exit(wxc_addColor("YOUR LOCALNODES' API DOES NOT CONTAIN NETWORK TOPOLOGY INFORMATION!", "redBold") . "\n\n");
}
unset($allMeshNodes);
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold") . "\n";
}
//use the 'lastHopIP' to build new array with the info for each device
//using lastHopIP will ensure a unique list of nodes on the network
//add in the "hopsAway" variable (this would be hops away from the localnode)
if($TEST_MODE) {
echo "Building list of node IP addresses and some link info... ";
}
$nodeDevices = [];
$currentlyFoundDevices = [];
$MaxNumHops = 0;
foreach($topoInfo as $link) {
$nodeDevices[$link['lastHopIP']] = [];
$currentlyFoundDevices[] = $link['lastHopIP'];
$currentlyFoundDevices = array_unique($currentlyFoundDevices);
$nodeDevices[$link['lastHopIP']]['hopsAway'] = $link['hops'];
if(intval($link['hops']) > $MaxNumHops) {
$MaxNumHops = intval($link['hops']);
}
}
unset($link);
//build up the $nodeDevices array with info we need about each link
foreach($nodeDevices as $ip => $node) {
foreach($topoInfo as $link) {
if($link['lastHopIP'] == $ip) {
$nodeDevices[$ip]['link_info'][$link['destinationIP']] = $link;
}
}
unset($link);
unset($ip);
unset($node);
}
unset($topoInfo);
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold") . "\n";
}
//get a total count of number of ip's to poll
$TotalToPoll = count($nodeDevices);
//clear the log files from the last run
if($TEST_MODE) {
echo "Clearing log files... ";
}
$logFile = fopen($INCLUDE_DIR . "/logs/polling_output.log", "w");
fwrite($logFile, "### THIS LOG FILE IS RECREATED EACH TIME THE POLLING SCRIPT RUNS.\n### ANY CHANGES WILL BE LOST.\n");
fwrite($logFile, "### LAST RUN: " . date("M j G:i:s T (P)") . "\n\n");
fclose($logFile);
$err_log_file = fopen($INCLUDE_DIR . "/logs/polling_errors.log", "w");
fwrite($err_log_file, "### THIS LOG FILE IS RECREATED EACH TIME THE POLLING SCRIPT RUNS.\n### ANY CHANGES WILL BE LOST.\n");
fwrite($err_log_file, "### LAST RUN: " . date("M j G:i:s T (P)") . "\n\n");
fclose($err_log_file);
$noLoc = fopen($INCLUDE_DIR . "/logs/no_location.log", "w");
fwrite($noLoc, "### THIS LOG FILE IS RECREATED EACH TIME THE POLLING SCRIPT RUNS.\n### ANY CHANGES WILL BE LOST.\n");
fwrite($noLoc, "### LAST RUN: " . date("M j G:i:s T (P)") . "\n\n");
fclose($noLoc);
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold") . "\n";
}
$dbHandle = "";
//use $nodeDevices array to start populating the database.
//$nodeDevice looks like this
//[Mesh Node IP]
// [hopsAway from the "localnode"]
// [some info about how your localnode sees the other nodes on the network]
//
if($TEST_MODE) {
echo "Connecting to " . $USER_SETTINGS['SQL_TYPE'] . "... ";
}
if($USE_SQL && $USER_SETTINGS['SQL_TYPE'] == "sqlite") {
if(!file_exists($INCLUDE_DIR . "/sqlite3_db/mesh-map.sqlite")) {
$dbHandle = create_sqlite_db($INCLUDE_DIR . "/sqlite3_db/mesh-map.sqlite");
}else {
$dbHandle = new SQLite3($INCLUDE_DIR . "/sqlite3_db/mesh-map.sqlite");
}
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold") . "\n";
echo "Updating found IP's and link info into DB... ";
}
foreach($nodeDevices as $wlan_ip => $info) {
$sql = "REPLACE INTO 'node_info' ('wlan_ip', 'hopsAway', 'link_info') VALUES('" .
$wlan_ip . "', " .
$info['hopsAway'] . ", " .
escapeshellarg(serialize($info['link_info'])) . ")";
$dbHandle->exec($sql);
}
$dbHandle->close();
}
if($USE_SQL && $USER_SETTINGS['SQL_TYPE'] == "mysql") {
$sql_connection = wxc_connectToMySQL();
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold") . "\n";
if($USER_SETTINGS['expire_old_nodes']) {
echo "Expiring nodes not polled in ". $USER_SETTINGS['node_expire_interval'] . " days... ";
}
}
if($USER_SETTINGS['expire_old_nodes']) {
$expNodes = expiredNodes($sql_connection, $USER_SETTINGS['node_expire_interval']);
$count = 0;
if(!$expNodes) {
if($TEST_MODE) {
printf("\033[41G(%u) ", $count);
}
}else {
foreach($expNodes as $k => $v) {
$q = "delete from node_info where wlan_ip = '" . $v['wlan_ip'] . "'";
wxc_putMySql($sql_connection, $q);
$count++;
if($TEST_MODE) {
printf("\033[41G(%u) ", $count);
}
}
}
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold") . "\n";
echo "Updating found IP's and link info into DB... ";
}
}
$count = 0;
foreach($nodeDevices as $wlan_ip => $info) {
$sql = "INSERT INTO node_info (wlan_ip, hopsAway, link_info) VALUES ('" .
$wlan_ip . "', " . $info['hopsAway'] . ", " .
escapeshellarg(serialize($info['link_info'])) . ") ON DUPLICATE KEY UPDATE " .
"hopsAway = " . $info['hopsAway'] . ", " .
"link_info = " . escapeshellarg(serialize($info['link_info']));
escapeshellarg(serialize($info['link_info'])) . ")";
wxc_putMySql($sql_connection, $sql);
$count++;
if($TEST_MODE) {
printf("\033[46G(%u) ", $count);
}
}
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold") . "\n";
echo "Clearing links from not currently found devices... ";
}
//find devices that are in the DB, but not currently in the network topology
//they may be off, unable to be reached, temporary mobile devices or whatever.
//compare those to the currently found devices and remove the links from the non-current devices,
//but do not remove the device itself, not yet.
$sql = "SELECT wlan_ip from node_info";
$currentDevicesInDB = wxc_getMysqlFetchAll($sql);
foreach($currentDevicesInDB as $k => $v) {
$currentDevicesInDB[$k] = $v['wlan_ip'];
}
$devicesNotCurrent = array_diff($currentDevicesInDB, $currentlyFoundDevices);
$link_info = serialize([]);
$count = 0;
foreach($devicesNotCurrent as $v) {
if(!empty($v)) {
$sql = "UPDATE node_info SET link_info = '". $link_info . "' WHERE wlan_ip = '" . $v . "'";
wxc_putMySql($sql_connection, $sql);
$count++;
if($TEST_MODE) {
printf("\033[52G(%u) ", $count);
}
}
}
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold") . "\n";
}
mysqli_close($sql_connection);
}
//TODO: move to settings
$autoCheckArednVersions = true;
if($autoCheckArednVersions) {
if($TEST_MODE) {
echo "Checking Internet for the latest AREDN version numbers... ";
}
$versions = get_current_aredn_versions();
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold") . "\n(Stable: " . $versions['AREDN_STABLE_VERSION'] . " Nightly: " . $versions['AREDN_NIGHTLY_VERSION'] . ")\n\n";
}
if($USE_SQL) {
$sql = "INSERT INTO aredn_info (id, current_stable_version, current_nightly_version) VALUES('AREDNINFO', '" .
$versions['AREDN_STABLE_VERSION'] . "', '" .
$versions['AREDN_NIGHTLY_VERSION'] . "') ON DUPLICATE KEY UPDATE " .
"current_stable_version = '" . $versions['AREDN_STABLE_VERSION'] . "', " .
"current_nightly_version = '" . $versions['AREDN_NIGHTLY_VERSION'] . "'";
if($USER_SETTINGS['SQL_TYPE'] == "sqlite") {
$dbHandle = new SQLite3($INCLUDE_DIR . "/sqlite3_db/mesh-map.sqlite");
$dbHandle->exec($sql);
$dbHandle->close();
}elseif($USER_SETTINGS['SQL_TYPE'] == "mysql") {
$sql_connection = wxc_connectToMySQL();
wxc_putMySql($sql_connection, $sql);
mysqli_close($sql_connection);
}
}
//also save to a file for parallel script
$version_file = fopen($INCLUDE_DIR . "/logs/latest_versions.txt", "w");
fwrite($version_file, $versions['AREDN_STABLE_VERSION'] . "\n");
fwrite($version_file, $versions['AREDN_NIGHTLY_VERSION'] . "\n");
fclose($version_file);
}else {
//TODO: don't autocheck, use the value from the config file from stable FW version.
}
$mysql_db = "";
$sqlite3_db = "";
$nodeCount = 0;
$mtimePollingStart = microtime(true);
//lets go polling!
if($START_POLLING) {
$donePolling = 0;
//TODO: DO NOT LEAVE IT LIKE THIS
$USER_SETTINGS['node_polling_parallel'] = "true";
if($USER_SETTINGS['node_polling_parallel']) {
if($TEST_MODE) {
echo "Polling Progress (" . $USER_SETTINGS['numParallelThreads'] . "x): ";
}
$numParallelProcesses = $USER_SETTINGS['numParallelThreads'];
$pProcessingChunks = array_chunk($nodeDevices, 1, true);
unset($nodeDevices);
$pProcessingPIDS = [];
for($i = 0; $i < count($pProcessingChunks); $i++) {
$chunk = $pProcessingChunks[$i];
foreach($chunk as $ip => $info) {
$ipExtraInfo = escapeshellarg(serialize($info));
$pProcessingPIDS[] = exec("php ". $INCLUDE_DIR . "/parallel/parallelPolling.php " . $ip . " " . $USE_SQL . " >> " . $INCLUDE_DIR . "/logs/polling_output.log & echo $!");
$nodeCount++;
}
while(count($pProcessingPIDS) > $numParallelProcesses) {
foreach($pProcessingPIDS as $index => $pid) {
if(!file_exists("/proc/$pid")) {
unset($pProcessingPIDS[$index]);
$donePolling++;
$percent = floor(($donePolling / $TotalToPoll) * 100);
$numLeft = 100 - $percent;
if($TEST_MODE) {
printf("\033[26G%u%% (%u/%u)... ", $percent, $donePolling, $TotalToPoll);
}
//echo $progress;
}
}
}
}
//wait for all scripts to finish so we can see how long it actually takes
while(count($pProcessingPIDS) > 0) {
foreach($pProcessingPIDS as $index => $pid) {
if(!file_exists("/proc/$pid")) {
unset($pProcessingPIDS[$index]);
$donePolling++;
$percent = floor(($donePolling / $TotalToPoll) * 100);
$numLeft = 100 - $percent;
if($TEST_MODE) {
printf("\033[26G%u%% (%u/%u)... ", $percent, $donePolling, $TotalToPoll);
}
}
}
}
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold") . "\n";
}
}
}
$mtimePollingEnd = microtime(true);
$totalPollingTime = $mtimePollingEnd-$mtimePollingStart;
//connect back to SQL
$sql_connection = wxc_connectToMySQL();
//create the topology info
if($TEST_MODE) {
echo "Building Topology information: ";
}
$link_count = 0;
$query = wxc_getMysqlFetchAll("select node from node_info");
foreach($query as $v) {
$node = $v['node'];
$query = "SELECT node, lat, lon, link_info from node_info where node like '" . $v['node'] . "' and (lat != '0' || lon != '0')";
$q_results = wxc_getMySql($query);
if(isset($q_results['link_info'])) {
$links = unserialize($q_results['link_info']);
}
if(!empty($links)) {
foreach($links as $k => $v){
$query = "SELECT lat, lon from node_info where wlan_ip = '" . $k . "' and (lat != '0' || lon != '0')";
$link_coords = wxc_getMySql($query);
if(isset($q_results['lat']) && isset($q_results['lon']) && isset($link_coords['lat']) && isset($link_coords['lon'])) {
if(!empty($q_results['lat']) && !empty($q_results['lon']) && !empty($link_coords['lat']) && !empty($link_coords['lon'])) {
$links[$k]['linkLat'] = $link_coords['lat'];
$links[$k]['linkLon'] = $link_coords['lon'];
if(isset($links[$k]['linkType'])) {
if($links[$k]['linkType'] == "RF") {
$dist_bear = wxc_getDistanceAndBearing($q_results['lat'], $q_results['lon'], $link_coords['lat'], $link_coords['lon']);
$links[$k]['distanceKM'] = $dist_bear['distanceKM'];
$links[$k]['distanceMiles'] = $dist_bear['distanceMiles'];
$links[$k]['bearing'] = $dist_bear['bearing'];
}
}
$link_count++;
unset($v);
if($TEST_MODE) {
printf("\033[32G(%u)... ", $link_count);
}
}
}
}
$update_link_info = "update node_info set link_info = " . escapeshellarg(serialize($links)) . " where node = '" . $node . "'";
wxc_putMySql($sql_connection, $update_link_info);
}
}
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold");
echo "\n\n";
}
//get polling stats and echo in test mode
$pollingInfo = [];
//$pollingInfo['id'] = "POLLINFO";
$pollingInfo['numParallelThreads'] = $USER_SETTINGS['numParallelThreads'];
$pollingInfo['nodeTotal'] = $nodeCount;
if ($TEST_MODE) {
//total nodes found to try and poll
echo "Total Node Count: " . $nodeCount . "\n";
}
//nodes that returned garbage
$fCount = 0;
$f = $INCLUDE_DIR . "/logs/polling_errors.log";
$h = fopen($f, "r");
while(!feof($h)) {
$line = fgets($h);
if (strpos($line, "#") !== false || empty(trim($line))) {
continue;
//|| !preg_match('/^#/', $line) || !preg_match('/^\n/', $line)) {
//$fCount++;
}else {
$fCount++;
}
}
fclose($h);
$pollingInfo['garbageReturned'] = $fCount;
if($TEST_MODE) {
echo "Garbage Returned: " . $fCount . "\n";
}
$pollingInfo['highestHops'] = $MaxNumHops;
if($TEST_MODE) {
echo "Highest Hops Away: " . $MaxNumHops . "\n";
}
//total nodes found minus ones that return garbage
$totalPolled = intval($nodeCount) - intval($fCount);
$pollingInfo['totalPolled'] = $totalPolled;
if($TEST_MODE) {
echo "Total Nodes polled: " . $totalPolled . "\n";
}
$fCount = 0;
$f = $INCLUDE_DIR . "/logs/no_location.log";
$h = fopen($f, "r");
while(!feof($h)) {
$line = fgets($h);
if (strpos($line, "#") !== false || empty(trim($line))) {
continue;
// || strpos($line, "\n") !== false) {
// if (!preg_match('/^#/', $line) || !preg_match('/^\n/', $line)) {
//$fCount++;
}else {
$fCount++;
}
}
fclose($h);
$mapTotal = intval($totalPolled) - intval($fCount);
$pollingInfo['noLocation'] = $fCount;
$pollingInfo['mappableNodes'] = $mapTotal;
if($TEST_MODE) {
echo "Nodes with no location: " . $fCount . "\n";
echo "Total that can be shown on map: " . $mapTotal . "\n";
}
$pollingInfo['mappableLinks'] = $link_count;
if($TEST_MODE) {
echo "Links found that can be mapped: " . $link_count . "\n";
}
//total time taken to run the script
$mtimeOverallEnd = microtime(true);
$totalTime = $mtimeOverallEnd-$mtimeOverallStart;
$pollingInfo['pollingTimeSec'] = round($totalPollingTime, 2);
// add this later: $pollingInfo['scriptTimeSec'] = round($totalTime, 2);
if($TEST_MODE) {
//display how long it took to poll all the nodes
echo "Total Polling Time Elapsed: " . round($totalPollingTime, 2) . " seconds ( " . round($totalPollingTime/60, 2) . " minutes ).\n";
echo "Total Script Time Elapsed: " . round($totalTime, 2) . " seconds ( " . round($totalTime/60, 2) . " minutes ).\n\n";
}
$q = "INSERT INTO map_info (id, ";
foreach($pollingInfo as $k => $v) {
$q .= $k . ", ";
}
$q .= "lastPollingRun) VALUES ('POLLINFO', ";
foreach($pollingInfo as $k => $v) {
$q .= $v . ", ";
}
$q .= "NOW()) ON DUPLICATE KEY UPDATE ";
foreach($pollingInfo as $k => $v) {
if($k == "id") {
continue;
}else {
$q .= $k . " = " . $v . ", ";
}
}
$q .= "lastPollingRun = NOW()";
wxc_putMySql($sql_connection, $q);
$mapInfo = [];
$mapInfo['localnode'] = $localNodeName;
$mapInfo['mapTileServers'] = $USER_SETTINGS['mapTileServers'];
$mapInfo['title'] = $USER_SETTINGS['pageTitle'];
$mapInfo['attribution'] = $USER_SETTINGS['attribution'];
$mapInfo['mapContact'] = $USER_SETTINGS['mapContact'];
$mapInfo['kilometers'] = $USER_SETTINGS['kilometers'];
$mapInfo['webpageDataDir'] = "";
$mapInfo['mapCenterCoords'] = array($USER_SETTINGS['map_center_coordinates']['lat'], $USER_SETTINGS['map_center_coordinates']['lon']);
$mapInfo['mapInitialZoom'] = $USER_SETTINGS['map_initial_zoom_level'];
$mapInfo['totalNodesInDB'] = count(wxc_getMysqlFetchAll("SELECT node from node_info where lat is not null"));
$mapInfo['weekPlusOld'] = count(wxc_getMysqlFetchAll("select node from node_info where last_seen is null or last_seen < now() - interval 1 week"));
$pollingInfo['lastPollingRun'] = gmdate("Y-m-d H:i:s");
if($TEST_MODE) {
echo "Creating webpage data files in: ". $USER_SETTINGS['webpageDataDir'] . "... ";
}
$mapDataFileName = $USER_SETTINGS['webpageDataDir'] . "/map_data.js";
$fh = fopen($mapDataFileName, "w") or die ("could not open file");
fwrite($fh, createJS($pollingInfo, $mapInfo, $versions));
fclose($fh);
$node_report_data_json = $USER_SETTINGS['webpageDataDir'] . "/node_report_data.json";
createNodeReportJSON($sql_connection, $node_report_data_json);
createErrorReportJSON($USER_SETTINGS['webpageDataDir'] . "/error_report.json");
createNoLocReportJSON($USER_SETTINGS['webpageDataDir'] . "/no_location.json");
if($TEST_MODE) {
echo wxc_addColor("Done!", "greenBold");
echo "\n\n";
}
//upload the js and json file to another server via SSH
//must be able to login via SSH key with no password for this to work.
if($USER_SETTINGS['uploadToCloud']) {
if($TEST_MODE) {
echo "Uploading map data files... ";
}
foreach($USER_SETTINGS['cloudServer'] as $k => $v) {
if(str_contains($k, "Example")) {
continue;
}
exec("scp -i " . $USER_SETTINGS['cloudSSHKeyFile'] . " " . $mapDataFileName . " " . $v . "/map_data.js >> /dev/null 2>&1");
exec("scp -i " . $USER_SETTINGS['cloudSSHKeyFile'] . " " . $node_report_data_json . " " . $v . "/node_report_data.json >> /dev/null 2>&1");
exec("scp -i " . $USER_SETTINGS['cloudSSHKeyFile'] . " " . $USER_SETTINGS['webpageDataDir'] . "/error_report.json" . " " . $v . "/error_report_data.json >> /dev/null 2>&1");
exec("scp -i " . $USER_SETTINGS['cloudSSHKeyFile'] . " " . $USER_SETTINGS['webpageDataDir'] . "/no_location.json" . " " . $v . "/no_location.json >> /dev/null 2>&1");
if($TEST_MODE) {
echo $k . " " . wxc_addColor("Done!", "greenBold");
echo " | ";
}
}
if($TEST_MODE) {
echo "\n";
}
}
?>