forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller_automation.php
858 lines (728 loc) · 28.3 KB
/
poller_automation.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
#!/usr/bin/php -q
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2017 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/ |
+-------------------------------------------------------------------------+
*/
/* tick use required as of PHP 4.3.0 to accomodate signal handling */
declare(ticks = 1);
/* we are not talking to the browser */
$no_http_headers = true;
ini_set('output_buffering', 'Off');
/** sig_handler - provides a generic means to catch exceptions to the Cacti log.
* @arg $signo - (int) the signal that was thrown by the interface.
* @return - null */
function sig_handler($signo) {
global $network_id, $thread, $master, $poller_id;
switch ($signo) {
case SIGTERM:
case SIGINT:
if ($thread > 0) {
clearTask($network_id, getmypid());
exit;
} elseif($thread == 0 && !$master) {
$pids = array_rekey(db_fetch_assoc_prepared("SELECT pid
FROM automation_processes
WHERE network_id = ?
AND task!='tmaster'", array($network_id)), 'pid', 'pid');
if (sizeof($pids)) {
foreach($pids as $pid) {
posix_kill($pid, SIGTERM);
}
}
clearTask($network_id, getmypid());
db_execute_prepared('DELETE FROM automation_ips WHERE network_id = ?', array($network_id));
} else {
$pids = array_rekey(db_fetch_assoc_prepared("SELECT pid
FROM automation_processes
WHERE poller_id = ?
AND task='tmaster'", array($poller_id)), 'pid', 'pid');
if (sizeof($pids)) {
foreach($pids as $pid) {
posix_kill($pid, SIGTERM);
}
}
clearTask($network_id, getmypid());
}
exit;
break;
default:
/* ignore all other signals */
}
}
/* let PHP run just as long as it has to */
if (!isset($_SERVER['argv'][0]) || isset($_SERVER['REQUEST_METHOD']) || isset($_SERVER['REMOTE_ADDR'])) {
die('<br><strong>This script is only meant to run at the command line.</strong>');
}
/* take time and log performance data */
$start = microtime(true);
// Unix Timestamp for Database
$startTime = time();
/* let PHP run just as long as it has to */
ini_set('max_execution_time', '0');
$dir = dirname(__FILE__);
chdir($dir);
include('./include/global.php');
include_once($config['base_path'] . '/lib/snmp.php');
include_once($config['base_path'] . '/lib/ping.php');
include_once($config['base_path'] . '/lib/poller.php');
include_once($config['base_path'] . '/lib/utility.php');
include_once($config['base_path'] . '/lib/api_data_source.php');
include_once($config['base_path'] . '/lib/api_graph.php');
include_once($config['base_path'] . '/lib/snmp.php');
include_once($config['base_path'] . '/lib/data_query.php');
include_once($config['base_path'] . '/lib/api_device.php');
include_once($config['base_path'] . '/lib/sort.php');
include_once($config['base_path'] . '/lib/html_form_template.php');
include_once($config['base_path'] . '/lib/template.php');
include_once($config['base_path'] . '/lib/api_tree.php');
include_once($config['base_path'] . '/lib/api_automation.php');
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
$debug = false;
$force = false;
$network_id = 0;
$poller_id = $config['poller_id'];
$thread = 0;
$master = false;
global $debug, $poller_id, $network_id, $thread, $master;
if (sizeof($parms)) {
foreach($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-d':
case '--debug':
$debug = true;
break;
case '-M':
case '--master':
$master = true;
break;
case '--poller':
$poller_id = $value;
break;
case '-f':
case '--force':
$force = true;
break;
case '--network':
$network_id = $value;
break;
case '--thread':
$thread = $value;
break;
case '-v':
case '--version':
display_version();
exit;
case '-h':
case '--help':
display_help();
exit;
default:
print 'ERROR: Invalid Parameter ' . $parameter . "\n\n";
display_help();
exit;
}
}
}
/* install signal handlers for UNIX only */
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');
}
// Let's ensure that we were called correctly
if (!$master && !$network_id) {
print "FATAL: You must specify -M to Start the Master Control Process, or the Network ID using --network\n";
exit;
}
// Simple check for a disabled network
if (!$master && $thread == 0) {
$status = db_fetch_cell_prepared('SELECT enabled
FROM automation_networks
WHERE id = ?
AND poller_id = ?',
array($network_id, $poller_id));
if ($status != 'on' && !$force) {
print "ERROR: This Subnet Range is disabled. You must use the 'force' option to force it's execution.\n";
exit;
}
}
if ($master) {
$networks = db_fetch_assoc_prepared('SELECT * FROM automation_networks WHERE poller_id = ?', array($poller_id));
$launched = 0;
if (sizeof($networks)) {
foreach($networks as $network) {
if (api_automation_is_time_to_start($network['id']) || $force) {
automation_debug("Launching Network Master for '" . $network['name'] . "'\n");
exec_background(read_config_option('path_php_binary'), '-q ' . read_config_option('path_webroot') . '/poller_automation.php --poller=' . $poller_id . ' --network=' . $network['id'] . ($force ? ' --force':'') . ($debug ? ' --debug':''));
$launched++;
} else {
automation_debug("Not time to Run Discovery for '" . $network['name'] . "'\n");
}
}
}
exit;
}
// Check for Network Master
if (!$master && $thread == 0) {
automation_debug("Thread master about to launch collector threads\n");
// Remove any stale entries
$pids = array_rekey(
db_fetch_assoc_prepared('SELECT pid
FROM automation_processes
WHERE network_id = ?',
array($network_id)),
'pid', 'pid'
);
automation_debug("Killing any prior running threads\n");
if (sizeof($pids)) {
foreach($pids as $pid) {
if (isProcessRunning($pid)) {
killProcess($pid);
print "NOTE: Killing Process $pid\n";
} else {
print "NOTE: Process $pid claims to be running but not found\n";
}
}
}
automation_debug("Removing any orphan entries\n");
db_execute_prepared('DELETE FROM automation_ips WHERE network_id = ?', array($network_id));
db_execute_prepared('DELETE FROM automation_processes WHERE network_id = ?', array($network_id));
registerTask($network_id, getmypid(), $poller_id, 'tmaster');
cacti_log("Network Discover is now running for Subnet Range '$network_id'", true, 'AUTOM8');
automation_primeIPAddressTable($network_id);
$threads = db_fetch_cell_prepared('SELECT threads FROM automation_networks WHERE id = ?', array($network_id));
automation_debug("Automation will use $threads Threads\n");
$curthread = 1;
while($curthread <= $threads) {
automation_debug("Launching Thread $curthread\n");
exec_background(read_config_option('path_php_binary'), '-q ' . read_config_option('path_webroot') . "/poller_automation.php --poller=" . $poller_id . " --thread=$curthread --network=$network_id" . ($force ? ' --force':'') . ($debug ? ' --debug':''));
$curthread++;
}
sleep(5);
automation_debug("Checking for Running Threads\n");
while (true) {
$command = db_fetch_cell_prepared('SELECT command
FROM automation_processes
WHERE network_id = ?
AND task="tmaster"',
array($network_id));
if ($command == 'cancel') {
killProcess(getmypid());
}
$running = db_fetch_cell_prepared('SELECT count(*)
FROM automation_processes
WHERE network_id = ?
AND task!="tmaster"
AND status="running"',
array($network_id));
automation_debug("Found $running Threads\n");
if ($running == 0) {
db_execute_prepared('DELETE FROM automation_ips WHERE network_id = ?', array($network_id));
$totals = db_fetch_row_prepared('SELECT SUM(up_hosts) AS up, SUM(snmp_hosts) AS snmp
FROM automation_processes
WHERE network_id=?',
array($network_id));
/* take time and log performance data */
$end = microtime(true);
db_execute_prepared('UPDATE automation_networks
SET up_hosts = ?,
snmp_hosts = ?,
last_started = ?,
last_runtime = ? WHERE id = ?',
array($totals['up'], $totals['snmp'], date('Y-m-d H:i:s', $startTime), ($end - $start), $network_id));
clearAllTasks($network_id);
exit;
}
sleep(5);
}
} else {
registerTask($network_id, getmypid(), $poller_id);
discoverDevices($network_id, $thread);
endTask($network_id, getmypid());
}
exit;
function discoverDevices($network_id, $thread) {
$network = db_fetch_row_prepared('SELECT *
FROM automation_networks
WHERE id = ?',
array($network_id));
$temp = db_fetch_assoc('SELECT automation_templates.*, host_template.name
FROM automation_templates
LEFT JOIN host_template
ON (automation_templates.host_template=host_template.id)');
$dns = trim($network['dns_servers']);
/* Let's do some stats! */
$stats = array();
$stats['scanned'] = 0;
$stats['ping'] = 0;
$stats['snmp'] = 0;
$stats['added'] = 0;
$count_graph = 0;
$count = 0;
while(true) {
// set and ip to be scanned
db_execute_prepared('UPDATE automation_ips
SET pid = ?, thread = ?
WHERE network_id = ?
AND status=0
AND pid=0
LIMIT 1',
array(getmypid(), $thread, $network_id));
$device = db_fetch_row_prepared('SELECT *
FROM automation_ips
WHERE pid = ?
AND thread = ?
AND status=0',
array(getmypid(), $thread));
if (sizeof($device) && isset($device['ip_address'])) {
$count++;
if ($dns != '') {
$dnsname = automation_get_dns_from_ip($device['ip_address'], $dns, 300);
if ($dnsname != $device['ip_address'] && $dnsname != 'timed_out') {
automation_debug("Device: " . $device['ip_address'] . ", Checking DNS: Found '" . $dnsname . "'");
db_execute_prepared('UPDATE automation_ips
SET hostname = ?
WHERE ip_address = ?',
array($dnsname, $device['ip_address']));
$device['hostname'] = $dnsname;
$device['dnsname'] = $dnsname;
$device['dnsname_short'] = preg_split('/[\.]+/', strtolower($dnsname), -1, PREG_SPLIT_NO_EMPTY);
} elseif ($network['enable_netbios'] == 'on') {
automation_debug("Device: " . $device['ip_address'] . ", Checking DNS: Not found, Checking NetBIOS:");
$netbios = ping_netbios_name($device['ip_address']);
if ($netbios === false) {
automation_debug(" Not found");
$device['hostname'] = $device['ip_address'];
$device['dnsname'] = '';
$device['dnsname_short'] = '';
} else {
automation_debug(" Found: '" . $netbios . "'");
db_execute_prepared('UPDATE automation_ips
SET hostname = ?
WHERE ip_address = ?',
array($device['hostname'], $device['ip_address']));
$device['dnsname'] = $netbios;
$device['dnsname_short'] = $netbios;
}
} else {
automation_debug("Device: " . $device['ip_address'] . ", Checking DNS: Not found");
$device['hostname'] = $device['ip_address'];
$device['dnsname'] = '';
$device['dnsname_short'] = '';
}
} else {
$dnsname = @gethostbyaddr($device['ip_address']);
$device['hostname'] = $dnsname;
if ($dnsname != $device['ip_address']) {
automation_debug("Device: " . $device['ip_address'] . ", Checking DNS: Found '" . $dnsname . "'");
db_execute_prepared('UPDATE automation_ips
SET hostname = ?
WHERE ip_address = ?',
array($dnsname, $device['ip_address']));
$device['dnsname'] = $dnsname;
$device['dnsname_short'] = preg_split('/[\.]+/', strtolower($dnsname), -1, PREG_SPLIT_NO_EMPTY);
} elseif ($network['enable_netbios'] == 'on') {
automation_debug("Device: " . $device['ip_address'] . ", Checking DNS: Not found, Checking NetBIOS:");
$netbios = ping_netbios_name($device['ip_address']);
if ($netbios === false) {
automation_debug(" Not found");
$device['hostname'] = $device['ip_address'];
$device['dnsname'] = '';
$device['dnsname_short'] = '';
} else {
automation_debug(" Found: '" . $netbios . "'");
db_execute_prepared('UPDATE automation_ips
SET hostname = ?
WHERE ip_address = ?',
array($device['hostname'], $device['ip_address']));
$device['dnsname'] = $netbios;
$device['dnsname_short'] = $netbios;
}
} else {
automation_debug("Device: " . $device['ip_address'] . ", Checking DNS: Not found");
$device['hostname'] = $device['ip_address'];
$device['dnsname'] = '';
$device['dnsname_short'] = '';
}
}
$exists = db_fetch_row_prepared('SELECT id, snmp_version, status
FROM host
WHERE hostname IN (?,?)',
array($device['ip_address'], $device['hostname']));
if (!sizeof($exists)) {
automation_debug(", Status: Not in Cacti");
if (substr($device['ip_address'], -3) < 255) {
automation_debug(', Ping: ');
// Set status to running
markIPRunning($device['ip_address'], $network_id);
$stats['scanned']++;
$device['snmp_status'] = 0;
$device['ping_status'] = 0;
$device['snmp_id'] = $network['snmp_id'];
$device['poller_id'] = $network['poller_id'];
$device['site_id'] = $network['site_id'];
$device['snmp_version'] = '';
$device['snmp_port'] = '';
$device['snmp_readstring'] = '';
$device['snmp_username'] = '';
$device['snmp_password'] = '';
$device['snmp_auth_protocol'] = '';
$device['snmp_auth_passphrase'] = '';
$device['snmp_auth_protocol'] = '';
$device['snmp_context'] = '';
$device['snmp_port'] = '';
$device['snmp_timeout'] = '';
$device['snmp_sysDescr'] = '';
$device['snmp_sysObjectID'] = '';
$device['snmp_sysUptime'] = 0;
$device['snmp_sysName'] = '';
$device['snmp_sysName_short'] = '';
$device['snmp_sysLocation'] = '';
$device['snmp_sysContact'] = '';
$device['os'] = '';
$device['snmp_priv_passphrase'] = '';
$device['snmp_priv_protocol'] = '';
/* create new ping socket for host pinging */
$ping = new Net_Ping;
$ping->host['hostname'] = $device['ip_address'];
$ping->retries = $network['ping_retries'];
$ping->port = $network['ping_port'];;
/* perform the appropriate ping check of the host */
$result = $ping->ping(AVAIL_PING, $network['ping_method'], $network['ping_timeout'], 1);
if (!$result) {
automation_debug(" No response");
updateDownDevice($network_id, $device['ip_address']);
} else {
automation_debug(" Responded");
$stats['ping']++;
addUpDevice($network_id, getmypid());
}
if ($result && automation_valid_snmp_device($device)) {
$snmp_sysName = trim($device['snmp_sysName']);
$snmp_sysName_short = '';
if (!is_ipaddress($snmp_sysName)) {
$parts = explode('.', $snmp_sysName);
foreach($parts as $part) {
if (is_numeric($part)) {
$snmp_sysName_short = $snmp_sysName;
break;
}
}
if ($snmp_sysName_short == '') {
$snmp_sysName_short = $parts[0];
}
} else {
$snmp_sysName_short = $snmp_sysName;
}
$exists = db_fetch_row_prepared('SELECT id, status, snmp_version
FROM host
WHERE hostname IN (?,?)',
array($snmp_sysName_short, $snmp_sysName));
if (sizeof($exists)) {
if ($exists['status'] == 3 || $exists['status'] == 2) {
addUpDevice($network_id, getmypid());
if ($exists['snmp_version'] > 0) {
addSNMPDevice($network_id, getmypid());
}
// Rerun data queries if specified
rerunDataQueries($exists['id'], $network);
}
automation_debug(' Device is in Cacti!');
markIPDone($device['ip_address'], $network_id);
} else {
$host_id = 0;
if ($snmp_sysName != '') {
$isCactiSysName = db_fetch_cell_prepared('SELECT COUNT(*)
FROM host
WHERE snmp_sysName = ?',
array($snmp_sysName));
if ($isCactiSysName) {
automation_debug(", Skipping sysName '" . $snmp_sysName . "' already in Cacti!\n");
markIPDone($device['ip_address'], $network_id);
continue;
}
$isDuplicateSysName = db_fetch_cell_prepared('SELECT COUNT(*)
FROM automation_devices
WHERE network_id = ?
AND sysName != ""
AND ip != ?
AND sysName = ?',
array($device['ip_address'], $network_id, $snmp_sysName));
if ($isDuplicateSysName) {
automation_debug(", Skipping sysName '" . $snmp_sysName . "' already Discovered!\n");
markIPDone($device['ip_address'], $network_id);
continue;
}
$stats['snmp']++;
addSNMPDevice($network_id, getmypid());
automation_debug(" Responded");
$fos = automation_find_os($device['snmp_sysDescr'], $device['snmp_sysObjectID'], $device['snmp_sysName']);
if ($fos != false && $network['add_to_cacti'] == 'on') {
automation_debug(", Template: " . $fos['name']);
$device['os'] = $fos['name'];
$device['host_template'] = $fos['host_template'];
$device['availability_method'] = $fos['availability_method'];
$host_id = automation_add_device($device);
if (!empty($host_id)) {
if (isset($device['snmp_sysDescr']) && $device['snmp_sysDescr'] != '') {
db_execute_prepared('UPDATE host
SET snmp_sysDescr = ?
WHERE id = ?',
array($device['snmp_sysDescr'], $host_id));
}
if (isset($device['snmp_sysObjectID']) && $device['snmp_sysObjectID'] != '') {
db_execute_prepared('UPDATE host
SET snmp_sysObjectID = ?
WHERE id = ?',
array($device['snmp_sysObjectID'], $host_id));
}
if (isset($device['snmp_sysUptime']) && $device['snmp_sysUptime'] != '') {
db_execute_prepared('UPDATE host
SET snmp_sysUptimeInstance = ?
WHERE id = ?',
array($device['snmp_sysUptime'], $host_id));
}
if (isset($device['snmp_sysContact']) && $device['snmp_sysContact'] != '') {
db_execute_prepared('UPDATE host
SET snmp_sysContact = ?
WHERE id = ?',
array($device['snmp_sysContact'], $host_id));
}
if (isset($device['snmp_sysName']) && $device['snmp_sysName'] != '') {
db_execute_prepared('UPDATE host
SET snmp_sysName = ?
WHERE id = ?',
array($device['snmp_sysName'], $host_id));
}
if (isset($device['snmp_sysLocation']) && $device['snmp_sysLocation'] != '') {
db_execute_prepared('UPDATE host
SET snmp_sysLocation = ?
WHERE id = ?',
array($device['snmp_sysLocation'], $host_id));
}
automation_update_device($host_id);
}
$stats['added']++;
} elseif ($fos == false) {
automation_debug(", Template: Not found, Not adding to Cacti");
} else {
automation_debug(", Template: " . $fos['name']);
$device['os'] = $fos['name'];
automation_debug(", Skipped: Add to Cacti disabled");
}
}
// if the devices template is not discovered, add to found table
if ($host_id == 0) {
db_execute('REPLACE INTO automation_devices
(network_id, hostname, ip, community, snmp_version, snmp_port, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, sysName, sysLocation, sysContact, sysDescr, sysUptime, os, snmp, up, time) VALUES ('
. $network_id . ', '
. db_qstr($device['dnsname']) . ', '
. db_qstr($device['ip_address']) . ', '
. db_qstr($device['snmp_readstring']) . ', '
. db_qstr($device['snmp_version']) . ', '
. db_qstr($device['snmp_port']) . ', '
. db_qstr($device['snmp_username']) . ', '
. db_qstr($device['snmp_password']) . ', '
. db_qstr($device['snmp_auth_protocol']) . ', '
. db_qstr($device['snmp_priv_passphrase']) . ', '
. db_qstr($device['snmp_priv_protocol']) . ', '
. db_qstr($device['snmp_context']) . ', '
. db_qstr($device['snmp_sysName']) . ', '
. db_qstr($device['snmp_sysLocation']) . ', '
. db_qstr($device['snmp_sysContact']) . ', '
. db_qstr($device['snmp_sysDescr']) . ', '
. db_qstr($device['snmp_sysUptime']) . ', '
. db_qstr($device['os']) . ', '
. '1, 1,' . time() . ')');
}
markIPDone($device['ip_address'], $network_id);
}
}else if ($result) {
db_execute('REPLACE INTO automation_devices
(network_id, hostname, ip, community, snmp_version, snmp_port, snmp_username, snmp_password, snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context, sysName, sysLocation, sysContact, sysDescr, sysUptime, os, snmp, up, time) VALUES ('
. $network_id . ', '
. db_qstr($device['dnsname']) . ', '
. db_qstr($device['ip_address']) . ', '
. db_qstr($device['snmp_readstring']) . ', '
. db_qstr($device['snmp_version']) . ', '
. db_qstr($device['snmp_port']) . ', '
. db_qstr($device['snmp_username']) . ', '
. db_qstr($device['snmp_password']) . ', '
. db_qstr($device['snmp_auth_protocol']) . ', '
. db_qstr($device['snmp_priv_passphrase']) . ', '
. db_qstr($device['snmp_priv_protocol']) . ', '
. db_qstr($device['snmp_context']) . ', '
. db_qstr($device['snmp_sysName']) . ', '
. db_qstr($device['snmp_sysLocation']) . ', '
. db_qstr($device['snmp_sysContact']) . ', '
. db_qstr($device['snmp_sysDescr']) . ', '
. db_qstr($device['snmp_sysUptime']) . ', '
. '"", 0, 1,' . time() . ')');
automation_debug(", Alive no SNMP!");
markIPDone($device['ip_address'], $network_id);
} else {
markIPDone($device['ip_address'], $network_id);
}
automation_debug("\n");
} else {
automation_debug(", Status: Ignoring Address (PHP Bug does not allow us to ping .255 as it thinks its a broadcast IP)!\n");
markIPDone($device['ip_address'], $network_id);
}
} else {
if ($exists['status'] == 3 || $exists['status'] == 2) {
addUpDevice($network_id, getmypid());
if ($exists['snmp_version'] > 0) {
addSNMPDevice($network_id, getmypid());
}
// Rerun data queries if specified
rerunDataQueries($exists['id'], $network);
}
automation_debug(", Status: Already in Cacti\n");
markIPDone($device['ip_address'], $network_id);
}
} else {
// no more ips to scan
break;
}
}
cacti_log('Network ' . $network['name'] . " Thread $thread Finished, " . $stats['scanned'] . ' IPs Scanned, ' . $stats['ping'] . ' IPs Responded to Ping, ' . $stats['snmp'] . ' Responded to SNMP, ' . $stats['added'] . ' Device Added, ' . $count_graph . ' Graphs Added to Cacti', true, 'AUTOM8');
return true;
}
/* display_version - displays version information */
function display_version() {
$version = get_cacti_version();
print "Cacti Network Discovery Scanner, Version $version, " . COPYRIGHT_YEARS . "\n";
}
/* display_help - displays the usage of the function */
function display_help () {
display_version();
print "\nusage: poller_automation.php -M [--poller=ID] | --network=network_id [-T=thread_id]\n";
print " [--debug] [--force]\n\n";
print "Cacti's automation poller. This poller has two operating modes, Master and Slave.\n";
print "The Master process tracks and launches all Slaves based upon Cacti's automation\n";
print "settings. If you only want to force a network to be collected, you only need to\n";
print "specify the Network ID and the force options.\n\n";
print "Master Process:\n";
print " -M | --master - Master poller for all Automation\n";
print " --poller=ID - Master Poller ID, Defaults to 0 or WebServer\n\n";
print "Network Masters and Workers:\n";
print " --network=n - Network ID to discover\n";
print " --thread=n - Thread ID, Defaults to 0 or Network Master\n\n";
print "General Options:\n";
print " --force - Force the execution of a discovery process\n";
print " --debug - Display verbose output during execution\n\n";
}
function isProcessRunning($pid) {
return posix_kill($pid, 0);
}
function killProcess($pid) {
return posix_kill($pid, SIGTERM);
}
function rerunDataQueries($host_id, &$network) {
if ($network['rerun_data_queries'] == 'on') {
$snmp_queries = db_fetch_assoc_prepared('SELECT snmp_query_id
FROM host_snmp_query
WHERE host_id = ?',
array($host_id));
if (sizeof($snmp_queries)) {
foreach($snmp_queries as $query) {
run_data_query($host_id, $query['snmp_query_id']);
}
}
}
}
function registerTask($network_id, $pid, $poller_id, $task = 'collector') {
db_execute_prepared("REPLACE INTO automation_processes
(pid, poller_id, network_id, task, status, heartbeat)
VALUES (?, ?, ?, ?, 'running', NOW())",
array($pid, $poller_id, $network_id, $task));
}
function endTask($network_id, $pid) {
db_execute_prepared("UPDATE automation_processes
SET status='done', heartbeat=NOW()
WHERE pid = ?
AND network_id = ?",
array($pid, $network_id));
}
function addUpDevice($network_id, $pid) {
db_execute_prepared('UPDATE automation_processes
SET up_hosts=up_hosts+1, heartbeat=NOW()
WHERE pid = ?
AND network_id = ?',
array($pid, $network_id));
}
function addSNMPDevice($network_id, $pid) {
db_execute_prepared('UPDATE automation_processes
SET snmp_hosts=snmp_hosts+1, heartbeat=NOW()
WHERE pid = ?
AND network_id = ?',
array($pid, $network_id));
}
function clearTask($network_id, $pid) {
db_execute_prepared('DELETE FROM automation_processes
WHERE pid = ?
AND network_id = ?',
array($pid, $network_id));
db_execute_prepared('DELETE FROM automation_ips
WHERE network_id = ?',
array($pid, $network_id));
}
function clearAllTasks($network_id) {
db_execute_prepared('DELETE FROM automation_processes
WHERE network_id = ?',
array($network_id));
}
function markIPRunning($ip_address, $network_id) {
db_execute_prepared('UPDATE automation_ips
SET status=1
WHERE ip_address = ?
AND network_id = ?',
array($ip_address, $network_id));
}
function markIPDone($ip_address, $network_id) {
db_execute_prepared('UPDATE automation_ips
SET status=2
WHERE ip_address = ?
AND network_id = ?',
array($ip_address, $network_id));
}
function updateDownDevice($network_id, $ip) {
$exists = db_fetch_cell_prepared('SELECT COUNT(*)
FROM automation_devices
WHERE ip = ?
AND network_id = ?',
array($ip, $network_id));
if ($exists) {
db_execute_prepared("UPDATE automation_devices
SET up='0'
WHERE ip = ?
AND network_id = ?",
array($ip, $network_id));
}
}