-
Notifications
You must be signed in to change notification settings - Fork 25
/
poller_mactrack.php
1065 lines (889 loc) · 33.7 KB
/
poller_mactrack.php
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
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
885
886
887
888
889
890
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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/php -q
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2024 The Cacti Group |
| |
| This program 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 2 |
| of the License, or (at your option) any later version. |
| |
| This program 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. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
$dir = dirname(__FILE__);
chdir($dir);
if (substr_count(strtolower($dir), 'mactrack')) {
chdir('../../');
}
include('./include/cli_check.php');
include_once($config['base_path'] . '/lib/poller.php');
include_once($config['base_path'] . '/plugins/mactrack/lib/mactrack_functions.php');
/* get the mactrack polling cycle */
$collect_frequency = read_config_option('mt_collection_timing');
$poller_interval = read_config_option('poller_interval');
if (is_numeric($collect_frequency)) {
/* let PHP a 5 minutes less than the rerun frequency */
$max_run_duration = ($collect_frequency * 60) - $poller_interval;
ini_set('max_execution_time', $max_run_duration);
}
/* Disable Mib File Loading */
putenv('MIBS=:');
/* Allow Mactrack to Use Memory */
ini_set('memory_limit', '-1');
global $config, $debug, $web, $track_errors;
/* initialize variables */
$site_id = 0;
$debug = false;
$forcerun = false;
$web = false;
$track_errors = 0;
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
if (cacti_sizeof($parms)) {
foreach($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-sid':
$site_id = $value;
break;
case '-d':
case '--debug':
$debug = true;
break;
case '-f':
case '--force':
$forcerun = true;
break;
case '-w':
case '--web':
$web = true;
break;
case '--version':
case '-V':
case '-v':
display_version();
exit;
case '--help':
case '-H':
case '-h':
display_help();
exit;
default:
print 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
display_help();
exit;
}
}
}
/* silently end if the registered process is still running, or process table missing */
if (function_exists('register_process_start')) {
if (!register_process_start('mactrack', 'master', $config['poller_id'], $max_run_duration)) {
mactrack_debug('Another Mactrack Process Still Running');
exit(0);
}
}
// Get rid of old/hung processes
clear_old_processes($site_id);
if ($collect_frequency == 'disabled') {
echo "WARNING: Mactrack is disabled, exiting\n";
if (function_exists('unregister_process')) {
unregister_process('matrack', 'master', $config['poller_id']);
}
exit(1);
} else {
/* for manual scans, verify if we should run or not */
$running_processes = db_fetch_cell('SELECT COUNT(*) FROM mac_track_processes');
if ($running_processes) {
$start_date = db_fetch_cell('SELECT MIN(start_date) FROM mac_track_processes');
if (time() < (strtotime($start_date) + $max_run_duration) && !$forcerun) {
echo "NOTE: Mactrack currently running and max run duration not eclipsed.\n";
if (function_exists('unregister_process')) {
unregister_process('matrack', 'master', $config['poller_id']);
}
exit(0);
} elseif ($forcerun) {
mactrack_debug('WARNING: Forcing Collection although Collection Appears in Process', true, 'MACTRACK');
db_execute('TRUNCATE mac_track_processes');
} else {
mactrack_debug('WARNING: Stale data found in Matrack process table', true, 'MACTRACK');
db_execute('TRUNCATE mac_track_processes');
}
}
/* we don't want to see errors in the cacti log */
errors_disable();
if ($site_id > 0) {
mactrack_debug('About to enter Mactrack Site Scan Processing');
/* take time and log performance data */
$start = microtime(true);
collect_mactrack_data($start, $site_id);
} else {
mactrack_debug('About to enter Mactrack poller processing');
if ($forcerun || $collect_frequency > 0) {
mactrack_debug('Into Processing. Checking to determine if it\'s time to run.');
$collect_frequency = $collect_frequency * 60;
/* find out if it's time to collect device information */
$base_start_time = read_config_option('mt_base_time');
$database_maint_time = read_config_option('mt_maint_time');
$last_run_time = read_config_option('mt_last_run_time');
$last_db_maint_time = read_config_option('mt_last_db_maint_time');
$previous_base_start_time = read_config_option('mt_prev_base_time');
$previous_db_maint_time = read_config_option('mt_prev_db_maint_time');
/* see if the user desires a new start time */
mactrack_debug('Checking if user changed the start time');
if (!empty($previous_base_start_time)) {
if ($base_start_time <> $previous_base_start_time) {
mactrack_debug('Detected that user changed the start time');
unset($last_run_time);
db_execute("DELETE FROM settings WHERE name='mt_last_run_time'");
}
}
/* see if the user desires a new db maintenance time */
mactrack_debug('Checking if user changed the maintenance time');
if (!empty($previous_db_maint_time)) {
if ($database_maint_time <> $previous_db_maint_time) {
mactrack_debug('Detected that user changed the db maintenance time');
unset($last_db_maint_time);
db_execute("DELETE FROM settings WHERE name='mt_last_db_maint_time'");
}
}
/* set to detect if the user cleared the time between polling cycles */
set_config_option('mt_prev_base_time', $base_start_time);
set_config_option('mt_prev_db_maint_time', $database_maint_time);
/* determine the next start time */
$current_time = strtotime('now');
if (empty($last_run_time)) {
if ($current_time > strtotime($base_start_time)) {
/* if timer expired within a polling interval, then poll */
if (($current_time - 300) < strtotime($base_start_time)) {
$next_run_time = strtotime(date('Y-m-d') . ' ' . $base_start_time);
} else {
$next_run_time = strtotime(date('Y-m-d') . ' ' . $base_start_time) + 3600*24;
}
} else {
$next_run_time = strtotime(date('Y-m-d') . ' ' . $base_start_time);
}
} else {
$next_run_time = $last_run_time + $collect_frequency;
}
$time_till_next_run = $next_run_time - $current_time;
if ($time_till_next_run < 0) {
mactrack_debug('The next run time has been determined to be NOW');
} else {
mactrack_debug("The next run time has been determined to be at '" . date('Y-m-d G:i:s', $next_run_time) . "'");
}
if (empty($last_db_maint_time)) {
$next_db_maint_time = strtotime(date('Y-m-d') . ' ' . $database_maint_time);
} else {
$next_db_maint_time = $last_db_maint_time + 24*3600;
}
$time_till_next_db_maint = $next_db_maint_time - $current_time;
if ($time_till_next_db_maint < 0) {
mactrack_debug('The next database maintenance run time has been determined to be NOW');
} else {
mactrack_debug("The next database maintenance run time has been determined to be at '" . date('Y-m-d G:i:s', $next_db_maint_time) . "'");
}
if ($time_till_next_run < 0 || $forcerun == true) {
mactrack_debug('Either a scan has been forced, or it\'s time to check for macs');
/* take time and log performance data */
$start = microtime(true);
set_config_option('mt_last_run_time', $current_time);
collect_mactrack_data($start, $site_id);
log_mactrack_statistics('collect');
}
if ($time_till_next_db_maint < 0) {
/* take time and log performance data */
$start = microtime(true);
set_config_option('mt_last_db_maint_time', $current_time);
perform_mactrack_db_maint();
log_mactrack_statistics('maint');
}
}
}
/* show errors now */
errors_restore();
}
if (function_exists('unregister_process')) {
unregister_process('mactrack', 'master', $config['poller_id']);
}
function errors_disable() {
global $track_errors;
$track_errors = ini_get('track_errors');
ini_set('track_errors', 0);
set_error_handler('mactrack_error_handler');
}
function errors_restore() {
global $track_errors;
ini_set('track_errors', $track_errors);
restore_error_handler();
}
function mactrack_error_handler($level, $message, $file, $line, $context) {
global $phperrors;
if (IgnoreErrorHandler($message)) {
return true;
}
preg_match("/.*\/plugins\/([\w-]*)\/.*/", $file, $output_array);
$plugin = (isset($output_array[1]) ? $output_array[1] : '');
$error = 'PHP ' . $phperrors[$level] . ($plugin != '' ? " in Plugin '$plugin'" : '') . ": $message in file: $file on line: $line";
switch ($level) {
case E_COMPILE_ERROR:
case E_CORE_ERROR:
case E_ERROR:
case E_PARSE:
if ($plugin != '') {
api_plugin_disable_all($plugin);
cacti_log("ERRORS DETECTED - DISABLING PLUGIN '$plugin'");
}
cacti_log($error, false, 'ERROR');
cacti_debug_backtrace('PHP ERROR PARSE');
break;
case E_RECOVERABLE_ERROR:
case E_USER_ERROR:
cacti_log($error, false, 'ERROR');
cacti_debug_backtrace('PHP ERROR');
break;
case E_COMPILE_WARNING:
case E_CORE_WARNING:
case E_USER_WARNING:
case E_WARNING:
cacti_log($error, false, 'ERROR');
cacti_debug_backtrace('PHP ERROR WARNING');
break;
case E_NOTICE:
case E_USER_NOTICE:
break;
case E_STRICT:
cacti_log($error, false, 'ERROR');
cacti_debug_backtrace('PHP ERROR STRICT');
break;
default:
cacti_log($error, false, 'ERROR');
cacti_debug_backtrace('PHP ERROR');
}
return false;
}
function display_version() {
global $config;
$info = plugin_mactrack_version();
print 'Mactrack Master Poller, Version ' . $info['version'] . ', ' . COPYRIGHT_YEARS . "\n";
}
/* display_help - displays the usage of the function */
function display_help () {
display_version();
print "\nusage: poller_mactrack.php [-sid=site_id] [--web] [--force] [--debug]\n\n";
print "-sid=site_id - The mac_track_sites site_id to scan\n";
print "-w | --web - Display output suitable for the web\n";
print "-f | --force - Force the execution of a collection process\n";
print "-d | --debug - Display verbose output during execution\n";
}
function clear_old_processes($site_id) {
/* get the max script runtime and kill old scripts */
$max_script_runtime = read_config_option('mt_script_runtime');
$delete_time = date('Y-m-d H:i:s', strtotime('-' . $max_script_runtime . ' Minutes'));
/* remove old processes from the system if they exist */
$old_procs = db_fetch_assoc_prepared('SELECT mtp.*
FROM mac_track_processes AS mtp
INNER JOIN mac_track_devices AS mtd
ON mtp.device_id=mtd.device_id
WHERE start_date < ?' . ($site_id > 0 ? ' AND site_id=' . $site_id:''),
array($delete_time));
if (cacti_sizeof($old_procs)) {
foreach($old_procs as $p) {
if ($p['process_id'] > 0) {
if (strstr(PHP_OS, 'WIN')) {
exec('taskkill /pid ' . $p['process_id']);
} else {
exec('kill ' . $p['process_id']);
}
cacti_log("WARNING: Removing Hung Mactrack Process for Device '" . $p['device_id'] . "' With Status '" . $p['status'] . "'");
} else {
cacti_log("WARNING: Removing Hung Mactrack Process Entry for Device '" . $p['device_id'] . "' With Status '" . $p['status'] . "'");
}
db_execute_prepared('DELETE FROM mac_track_processes
WHERE process_id = ?',
array($p['process_id']));
}
}
}
function collect_mactrack_data($start, $site_id = 0) {
global $max_run_duration, $config, $debug, $scan_date;
if (defined('CACTI_BASE_PATH')) {
$config['base_path'] = CACTI_BASE_PATH;
}
/* dns resolver binary */
$resolver_launched = false;
if (read_config_option('mt_reverse_dns') == 'on') {
$dns_resolver_required = true;
} else {
$dns_resolver_required = false;
}
/* get php binary path */
$command_string = read_config_option('path_php_binary');
/* save the scan date information */
$scan_date = date('Y-m-d H:i:s');
set_config_option('mt_scan_date', $scan_date);
/* just in case we've run too long */
$exit_mactrack = false;
/* start mainline processing, order by site_id to keep routers grouped with switches */
if ($site_id > 0) {
$device_ids = db_fetch_assoc_prepared('SELECT device_id
FROM mac_track_devices
WHERE site_id = ?
AND disabled = ""
ORDER BY last_runduration DESC',
array($site_id));
} else {
$device_ids = db_fetch_assoc('SELECT device_id
FROM mac_track_devices
WHERE disabled = ""
ORDER BY last_runduration DESC');
}
$total_devices = cacti_sizeof($device_ids);
$concurrent_processes = read_config_option('mt_processes');
if ($debug == true) {
$e_debug = ' -d';
} else {
$e_debug = '';
}
if ($site_id > 0) {
$e_site = " -sid=$site_id";
} else {
$e_site = '';
}
/* add the parent process to the process list */
db_process_add('-1');
if ($total_devices) {
/* grab arpwatch data */
if (read_config_option('mt_arpwatch') == 'on') {
$arp_db = read_config_option('mt_arpwatch_path');
$delim = read_config_option('mt_mac_delim');
$mac_ip_dns = array();
if (file_exists($arp_db)) {
$arp_dat = fopen($arp_db, 'r');
if ($arp_dat) {
while (!feof($arp_dat)) {
$line = fgets($arp_dat, 4096);
if ($line != null) {
$line = explode (' ', $line);
$mac_ad = explode(':',$line[0]);
for ($k = 0; $k < 6; $k++) {
$mac_ad[$k] = strtoupper($mac_ad[$k]);
if (strlen($mac_ad[$k]) == 1) {
$mac_ad[$k] = '0' . $mac_ad[$k];
}
}
/* create the mac address */
$mac = $mac_ad[0] . $delim . $mac_ad[1] . $delim . $mac_ad[2] . $delim . $mac_ad[3] . $delim . $mac_ad[4] . $delim . $mac_ad[5];
/* update the array */
$mac_ip_dns[$mac]['ip'] = $line[1];
$mac_ip_dns[$mac]['dns'] = $line[3];
}
}
fclose($arp_dat);
mactrack_debug('ARPWATCH: IP, DNS & MAC collection complete with ArpWatch');
} else {
cacti_log("ERROR: cannot open file ArpWatch database '$arp_db'");
}
}
}
/* scan through all devices */
$j = 0;
$i = 0;
$last_time = strtotime('now');
$processes_available = $concurrent_processes;
while ($j < $total_devices) {
/**
* retrieve the number of concurrent mac_track processes to run
* default to 10 for now
*/
$concurrent_processes = db_fetch_cell("SELECT value FROM settings WHERE name='mt_processes'");
for ($i = 0; $i < $processes_available; $i++) {
if (($j+$i) >= $total_devices) break;
$extra_args = ' -q ' . $config['base_path'] . '/plugins/mactrack/mactrack_scanner.php -id=' . $device_ids[$i+$j]['device_id'] . $e_debug;
mactrack_debug('CMD: ' . $command_string . $extra_args);
exec_background($command_string, $extra_args);
}
$j = $j + $i;
/* launch the dns resolver if it hasn't been yet */
if (($dns_resolver_required) && (!$resolver_launched)) {
sleep(2);
exec_background($command_string, ' -q ' . $config['base_path'] . '/plugins/mactrack/mactrack_resolver.php' . $e_debug . $e_site);
$resolver_launched = true;
mactrack_debug('DNS Resolver process launched');
}
mactrack_debug('A process cycle launch just completed.');
/**
* wait the correct number of seconds for processes prior to
* attempting to update records
*/
sleep(2);
$current_time = strtotime('now');
if (($current_time - $last_time) > read_config_option('mt_dns_prime_interval')) {
/* resolve some ip's to mac addresses to let the resolver knock them out */
db_execute_prepared('UPDATE mac_track_temp_ports
INNER JOIN mac_track_ips
ON mac_track_temp_ports.mac_address=mac_track_ips.mac_address
AND mac_track_temp_ports.site_id=mac_track_ips.site_id
SET mac_track_temp_ports.ip_address=mac_track_ips.ip_address,
mac_track_temp_ports.updated=1
WHERE mac_track_temp_ports.updated=0
AND mac_track_ips.scan_date = ?',
array($scan_date));
mactrack_debug('Interum IP addresses to MAC addresses association pass complete.');
$last_time = $current_time;
}
$processes_running = db_fetch_cell('SELECT COUNT(*) FROM mac_track_processes');
if ($dns_resolver_required) {
$processes_available = $concurrent_processes - $processes_running + 1;
} else {
$processes_available = $concurrent_processes - $processes_running;
}
/* take time to check for an exit condition */
$current = microtime(true);
/* exit if we've run too long */
if (($current - $start) > $max_run_duration) {
$exit_mactrack = true;
cacti_log('ERROR: Mactrack timed out during main script processing.');
db_execute("DELETE FROM settings WHERE name='mactrack_process_status'");
db_process_remove('-1');
break;
} else {
set_config_option('mactrack_process_status', "Total:$total_devices Completed:$j");
}
}
/* wait for last process to exit */
$processes_running = db_fetch_cell('SELECT COUNT(*)
FROM mac_track_processes
WHERE device_id > 0');
while ($processes_running > 0 && !$exit_mactrack) {
$processes_running = db_fetch_cell('SELECT COUNT(*)
FROM mac_track_processes
WHERE device_id > 0');
$devices_running = db_fetch_cell("SELECT GROUP_CONCAT(CAST(`device_id` as CHAR) SEPARATOR ', ')
FROM mac_track_processes");
/**
* wait the correct number of seconds for processes prior to
* attempting to update records
*/
sleep(2);
$current_time = strtotime('now');
if (($current_time - $last_time) > read_config_option('mt_dns_prime_interval')) {
/* resolve some ip's to mac addresses to let the resolver knock them out */
db_execute_prepared('UPDATE mac_track_temp_ports
INNER JOIN mac_track_ips
ON mac_track_temp_ports.mac_address=mac_track_ips.mac_address
AND mac_track_temp_ports.site_id=mac_track_ips.site_id
SET mac_track_temp_ports.ip_address=mac_track_ips.ip_address,
mac_track_temp_ports.updated=1
WHERE mac_track_temp_ports.updated=0
AND mac_track_ips.scan_date = ?',
array($scan_date));
mactrack_debug('Interum IP addresses to MAC addresses association pass complete.');
}
/* take time to check for an exit condition */
$current = microtime(true);
/* exit if we've run too long */
if (($current - $start) > $max_run_duration) {
$exit_mactrack = true;
cacti_log('ERROR: Mactrack timed out during main script processing.');
break;
}
mactrack_debug('Waiting on ' . $processes_running . ' with id = [' . $devices_running .'] to complete prior to exiting.');
}
/* if arpwatch is enabled, let's let it pick up the stragglers, based upon IP address first */
if ((read_config_option('mt_arpwatch') == 'on') && (cacti_sizeof($mac_ip_dns))) {
$ports = db_fetch_assoc('SELECT site_id, device_id, mac_address
FROM mac_track_temp_ports
WHERE updated = 0');
if (cacti_sizeof($ports)) {
foreach($ports as $port) {
if (isset($mac_ip_dns[$port['mac_address']])) {
if ($mac_ip_dns[$port['mac_address']]['dns'] != '') {
db_execute_prepared('UPDATE mac_track_temp_ports
SET updated = 1, ip_address = ?, dns_hostname = ?
WHERE site_id = ?
AND device_id = ?
AND mac_address = ?',
array(
$mac_ip_dns[$port['mac_address']]['ip'],
$mac_ip_dns[$port['mac_address']]['dns'],
$port['site_id'],
$port['device_id'],
$port['mac_address']
)
);
} else {
db_execute_prepared('UPDATE mac_track_temp_ports
SET updated = 1, ip_address = ?
WHERE site_id = ?
AND device_id = ?
AND mac_address = ?',
array(
$mac_ip_dns[$port['mac_address']]['ip'],
$port['site_id'],
$port['device_id'],
$port['mac_address']
)
);
}
}
}
}
}
/* check the arp table for entries */
$ports = db_fetch_assoc('SELECT site_id, device_id, mac_address, ip_address
FROM mac_track_temp_ports
WHERE updated = 0');
if (cacti_sizeof($ports)) {
foreach($ports as $port) {
$macs = db_fetch_row_prepared('SELECT mac_address,ip_address
FROM mac_track_arp
WHERE mac_address = ?',
array($port['mac_address']));
if (isset($macs['ip_address'])) {
db_execute_prepared('UPDATE mac_track_temp_ports
SET updated=1, ip_address = ?
WHERE site_id = ?
AND device_id = ?
AND mac_address = ?',
array($macs['ip_address'], $port['site_id'], $port['device_id'] . $port['mac_address']));
}
}
}
/* resolve some ip's to mac addresses to let the resolver knock them out */
db_execute_prepared('UPDATE mac_track_temp_ports
INNER JOIN mac_track_ips
ON mac_track_temp_ports.mac_address = mac_track_ips.mac_address
AND mac_track_temp_ports.site_id = mac_track_ips.site_id
SET mac_track_temp_ports.ip_address = mac_track_ips.ip_address
WHERE mac_track_temp_ports.updated = 0
AND mac_track_ips.scan_date = ?',
array($scan_date));
mactrack_debug('Interum IP addresses to MAC addresses association pass complete.');
/* report out some debugging information */
if ($debug) {
$site_data = db_fetch_assoc("SELECT site_id, SUBSTRING_INDEX(ip_address, '.', 2) AS subnet_range
FROM mac_track_temp_ports
GROUP BY site_id, subnet_range");
if (cacti_sizeof($site_data)) {
foreach($site_data as $record) {
mactrack_debug(sprintf('Site %s has ip range %s', $record['site_id'], $record['subnet_range']));
}
}
$errors_tp = db_fetch_cell('SELECT COUNT(*) FROM mac_track_temp_ports WHERE mac_address like "% %"');
$errors_arp = db_fetch_cell('SELECT COUNT(*) FROM mac_track_ips WHERE mac_address like "% %"');
mactrack_debug("The Temp Ports table had $errors_tp mac tracks with spaces");
mactrack_debug("The IP Address table had $errors_arp mac tracks with spaces");
}
/* populate the vendor_macs for this pass */
db_execute('UPDATE mac_track_temp_ports
SET vendor_mac=SUBSTRING(mac_address,1,8)');
mactrack_debug('MAC addresses to Vendor MACS association pass complete.');
/* update the vlan id's table */
db_execute("UPDATE mac_track_vlans SET present='0'");
db_execute("INSERT INTO mac_track_vlans
(vlan_id, site_id, device_id, vlan_name, present)
SELECT vlan_id, site_id, device_id, vlan_name, '1' AS present
FROM mac_track_temp_ports
ON DUPLICATE KEY UPDATE vlan_name=VALUES(vlan_name), present=VALUES(present);");
db_execute("DELETE FROM mac_track_vlans WHERE present='0'");
mactrack_debug('MAC VLANs in VLAN Table Updated.');
/**
* let the resolver know that the parent process is finished and then wait
* for the resolver if applicable
*/
db_process_remove('-1');
while (!$exit_mactrack) {
/* checking to see if the resolver is running */
$resolver_running = db_fetch_row('SELECT *
FROM mac_track_processes
WHERE device_id = 0');
if (cacti_sizeof($resolver_running) == 0) {
break;
}
/* take time to check for an exit condition */
$current = microtime(true);
/* exit if we've run too long */
if (($current - $start) > $max_run_duration) {
$exit_mactrack = true;
cacti_log('ERROR: Mactrack timed out during main script processing.');
break;
}
}
/* transfer temp port results into permanent table */
db_execute('INSERT INTO mac_track_ports
(site_id, device_id, hostname, dns_hostname, device_name,
vlan_id, vlan_name, mac_address, vendor_mac, ip_address,
port_number, port_name, scan_date, authorized)
SELECT site_id, device_id, hostname, dns_hostname, device_name,
vlan_id, vlan_name, mac_address, vendor_mac, ip_address,
port_number, port_name, scan_date, authorized
FROM mac_track_temp_ports
ON DUPLICATE KEY UPDATE site_id=VALUES(site_id), hostname=VALUES(hostname),
device_name=VALUES(device_name), vlan_id=VALUES(vlan_id), vlan_name=VALUES(vlan_name),
vendor_mac=VALUES(vendor_mac), ip_address=VALUES(ip_address), dns_hostname=VALUES(dns_hostname),
port_name=VALUES(port_name), authorized=VALUES(authorized)');
mactrack_debug('Finished transferring scan results to main table.');
/* transfer the subnet information, although primitive, into the ip_ranges table */
$ip_ranges = db_fetch_assoc("SELECT SUBSTRING_INDEX(`ip_address`,'.',3) AS ip_range,
site_id,
COUNT(DISTINCT ip_address) AS ips_current,
scan_date AS ips_current_date
FROM mac_track_temp_ports
WHERE ip_address != ''
GROUP BY ip_range, site_id");
if (is_array($ip_ranges)) {
foreach($ip_ranges as $ip_range) {
$range_record = db_fetch_row_prepared('SELECT *
FROM mac_track_ip_ranges
WHERE ip_range = ?
AND site_id = ?',
array($ip_range['ip_range'], $ip_range['site_id']));
if (cacti_sizeof($range_record) == 0) {
db_execute_prepared('REPLACE INTO `mac_track_ip_ranges`
(ip_range, site_id, ips_current, ips_current_date)
VALUES (?, ?, ?, ?)',
array(
$ip_range['ip_range'],
$ip_range['site_id'],
$ip_range['ips_current'],
$ip_range['ips_current_date']
)
);
} else {
db_execute_prepared('UPDATE mac_track_ip_ranges
SET ips_current = ?,
ips_current_date = ?
WHERE ip_range = ?
AND site_id = ?',
array(
$ip_range['ips_current'],
$ip_range['ips_current_date'],
$range_record['ip_range'],
$range_record['site_id']
)
);
}
}
}
/* update the max values if required */
db_execute('UPDATE `mac_track_ip_ranges`
SET ips_max = ips_current, ips_max_date = ips_current_date
WHERE ips_current > ips_max');
/* collect statistics */
if ($site_id == 0) {
$stats = db_fetch_assoc('SELECT site_id,
COUNT(device_id) AS total_devices,
SUM(ports_active) AS total_oper_ports,
SUM(macs_active) AS total_macs,
SUM(ports_total) AS total_user_ports
FROM mac_track_devices
GROUP BY site_id');
} else {
$stats = db_fetch_assoc_prepared('SELECT site_id,
COUNT(device_id) AS total_devices,
SUM(ports_active) AS total_oper_ports,
SUM(macs_active) AS total_macs,
SUM(ports_total) AS total_user_ports
FROM mac_track_devices
WHERE site_id = ?
GROUP BY site_id',
array($site_id));
}
/* collect total device errors */
$errors = db_fetch_assoc('SELECT site_id, snmp_status, COUNT(device_id) AS total_device_errors
FROM mac_track_devices
GROUP BY site_id, snmp_status');
$ips = array_rekey(
db_fetch_assoc_prepared('SELECT site_id, COUNT(ip_address) AS total_ips
FROM mac_track_ips
WHERE scan_date = ?
GROUP BY site_id',
array($scan_date)),
'site_id', 'total_ips'
);
foreach($errors as $error) {
if (!isset($error_count[$error['site_id']])) {
$error_count[$error['site_id']] = 0;
}
if ($error['snmp_status'] <> 3) {
$error_count[$error['site_id']] += $error['total_device_errors'];
}
}
foreach($stats as $stat) {
if (isset($ips[$stat['site_id']])) {
$num_ips = $ips[$stat['site_id']];
} else {
$num_ips = 0;
}
db_execute_prepared('UPDATE mac_track_sites
SET total_devices = ?, total_ips = ?, total_macs = ?,
total_oper_ports = ?, total_user_ports = ?, total_device_errors = ?
WHERE site_id = ?',
array(
$stat['total_devices'],
$num_ips,
$stat['total_macs'],
$stat['total_oper_ports'],
$stat['total_user_ports'],
$error_count[$stat['site_id']],
$stat['site_id']
)
);
}
mactrack_debug('Finished updating site table with collection statistics.');
/* process macwatch data */
$macwatches = db_fetch_assoc('SELECT * FROM mac_track_macwatch');
if (cacti_sizeof($macwatches)) {
$from = read_config_option('mt_from_email');
$fromname = read_config_option('mt_from_name');
foreach($macwatches as $record) {
/* determine if we should check this one */
$found = db_fetch_row_prepared('SELECT *
FROM mac_track_temp_ports
WHERE mac_address = ?',
array($record['mac_address']));
if (cacti_sizeof($found)) {
/* set the subject */
$subject = "MACAUTH Notification: Mac Address '" . $record['mac_address'] . "' Found, For: '" . $record['name'] . "'";
/* set the message with replacements */
$message = str_replace('<IP>', $found['ip_address'], $record['description']);
$message = str_replace('<MAC>', $found['mac_address'], $message);
$message = str_replace('<TICKET>', $record['ticket_number'], $message);
$message = str_replace('<SITENAME>', db_fetch_cell_prepared('SELECT site_name FROM mac_track_sites WHERE site_id = ?', array($found['site_id'])), $message);
$message = str_replace('<DEVICEIP>', $found['hostname'], $message);
$message = str_replace('<DEVICENAME>', $found['device_name'], $message);
$message = str_replace('<PORTNUMBER>', $found['port_number'], $message);
$message = str_replace('<PORTNAME>', $found['port_name'], $message);
/* send out the email */
if (!$record['discovered'] || $record['notify_schedule'] >= '2') {
$mail = true;
if ($record['notify_schedule'] > 2) {
if (strtotime($record['date_last_notif']) + $record['notify_schedule'] > time()) {
$mail = false;
}
}
if ($mail) {
mactrack_mail($record['email_addresses'], $from, $fromname, $subject, $message);
}
}
/* update the the correct information */
db_execute_prepared('UPDATE mac_track_macwatch
SET discovered = 1, date_last_seen=NOW()' .
(strtotime($record['date_first_seen']) == 0 ? ', date_first_seen=NOW()':'') . '
WHERE mac_address = ?',
array($record['mac_address']));
}
}
}
/* process macauth data */
$mac_auth_frequency = read_config_option('mt_macauth_email_frequency');
if ($mac_auth_frequency != 'disabled') {
$last_macauth_time = read_config_option('mt_last_macauth_time');
/* if it's time to e-mail */
if (($last_macauth_time + ($mac_auth_frequency*60) > time()) ||
($mac_auth_frequency == 0)) {
mactrack_process_mac_auth_report($mac_auth_frequency, $last_macauth_time);
}
}
/* process aggregated data */
db_execute('UPDATE mac_track_aggregated_ports SET active_last=0');
db_execute('INSERT INTO mac_track_aggregated_ports
(site_id, device_id, hostname, device_name,
vlan_id, vlan_name, mac_address, vendor_mac, ip_address, dns_hostname,
port_number, port_name, date_last, first_scan_date, count_rec, active_last, authorized)
SELECT site_id, device_id, hostname, device_name,
vlan_id, vlan_name, mac_address, vendor_mac, ip_address, dns_hostname,
port_number, port_name, scan_date, scan_date, 1, 1, authorized
FROM mac_track_temp_ports
ON DUPLICATE KEY UPDATE count_rec=count_rec + 1, active_last = 1, date_last = mac_track_temp_ports.scan_date');
/* purge the ip address and temp port table */
db_execute('TRUNCATE TABLE mac_track_temp_ports');
db_execute_prepared('DELETE FROM mac_track_ips
WHERE scan_date < NOW() - interval ? DAY',
array(read_config_option('mt_data_retention_ip')));
db_execute('OPTIMIZE TABLE mac_track_ips');
db_execute('TRUNCATE TABLE mac_track_scan_dates');
db_execute('REPLACE INTO mac_track_scan_dates (SELECT DISTINCT scan_date from mac_track_ports)');
api_plugin_hook_function('mac_track_finish_scan');
} else {
cacti_log('NOTE: MACTRACK has no devices to process at this time', false, 'MACTRACK');
}
}
function mactrack_process_mac_auth_report($mac_auth_frequency, $last_macauth_time) {
if ($mac_auth_frequency == 0) {
$ports = db_fetch_assoc('SELECT mac_track_temp_ports.*, mac_track_sites.site_name
FROM mac_track_temp_ports
LEFT JOIN mac_track_sites
ON mac_track_sites.site_id = mac_track_temp_ports.site_id
WHERE authorized = 0');