1 Star 0 Fork 0

wlnmp/cacti

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
poller_automation.php 36.59 KB
一键复制 编辑 原始数据 按行查看 历史
cigamit 提交于 2019-11-28 08:27 . Correct minor automation bug
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
#!/usr/bin/php -q
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2019 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);
ini_set('output_buffering', 'Off');
require(__DIR__ . '/include/cli_check.php');
require_once($config['base_path'] . '/lib/api_device.php');
require_once($config['base_path'] . '/lib/api_data_source.php');
require_once($config['base_path'] . '/lib/api_graph.php');
require_once($config['base_path'] . '/lib/api_tree.php');
require_once($config['base_path'] . '/lib/data_query.php');
require_once($config['base_path'] . '/lib/html_form_template.php');
require_once($config['base_path'] . '/lib/ping.php');
require_once($config['base_path'] . '/lib/poller.php');
require_once($config['base_path'] . '/lib/snmp.php');
require_once($config['base_path'] . '/lib/sort.php');
require_once($config['base_path'] . '/lib/template.php');
require_once($config['base_path'] . '/lib/utility.php');
if ($config['poller_id'] > 1) {
if ($config['connection'] == 'online') {
db_force_remote_cnn();
} else {
cacti_log('WARNING: Main Cacti database offline. Can not run automation', false, 'AUTOM8');
exit(1);
}
}
/** 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 (cacti_sizeof($pids)) {
foreach($pids as $pid) {
posix_kill($pid, SIGTERM);
}
}
clearTask($network_id, getmypid());
sleep(5);
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 (cacti_sizeof($pids)) {
foreach($pids as $pid) {
posix_kill($pid, SIGTERM);
}
}
clearTask($network_id, getmypid());
}
exit;
break;
default:
/* ignore all other signals */
}
}
/* 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);
/* 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 (cacti_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) {
cacti_log(automation_get_pid() . " WARNING: The Network ID: $network_id is disabled. You must use the 'force' option to force it's execution.", true, 'AUTOM8');
exit;
}
}
if ($master) {
$networks = db_fetch_assoc_prepared('SELECT *
FROM automation_networks
WHERE poller_id = ?',
array($poller_id));
$launched = 0;
if (cacti_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 (cacti_sizeof($pids)) {
foreach($pids as $pid) {
if (isProcessRunning($pid)) {
killProcess($pid);
cacti_log("WARNING: Automation Process $pid is still running for Network ID: $network_id", true, 'AUTOM8');
} else {
cacti_log("WARNING: Process $pid claims to be running but not found for Network ID: $network_id", true, 'AUTOM8');
}
}
}
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(automation_get_pid() . " Network Discover is now running for Subnet Range '$network_id'", true, 'AUTOM8');
$preexisting_devices = getNetworkDevices($network_id);
automation_primeIPAddressTable($network_id);
$threads = db_fetch_cell_prepared('SELECT threads
FROM automation_networks
WHERE id = ?',
array($network_id));
if ($threads <= 0) {
$threads = 1;
}
automation_debug("Automation will use $threads Threads\n");
db_execute_prepared('UPDATE automation_networks
SET last_started = ?
WHERE id = ?',
array(date('Y-m-d H:i:s', $startTime), $network_id));
$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");
$failcount = 0;
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");
// Are there no more running tasks? Wait up to 15 seconds to
// allow processes to start before checking for failures
if (($running == 0 && $failcount > 3) || $command == 'cancel') {
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);
reportNetworkStatus($network_id, $preexisting_devices);
exit;
} else {
$failcount++;
}
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) {
// Check for cancel
$command = db_fetch_cell_prepared('SELECT command
FROM automation_processes
WHERE network_id = ?
AND task = "tmaster"',
array($network_id));
if ($command == 'cancel' || empty($command)) {
removeMyProcess(getmypid(), $network_id);
killProcess(getmypid());
exit;
}
// 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 (cacti_sizeof($device) && isset($device['ip_address'])) {
$count++;
cacti_log(automation_get_pid() . ' NOTE: Found device IP address \'' . $device['ip_address'] .'\' to check',false,'AUTOM8',POLLER_VERBOSITY_MEDIUM);
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 (!cacti_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_community'] = '';
$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 */
$bypass_ping = false;
$result = false;
if ($network['ping_method'] == PING_SNMP) {
$bypass_ping = true;
}
if ($bypass_ping == false) {
$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 || $bypass_ping) && 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 (cacti_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 != '') {
$hostname = gethostbyaddr($device['ip_address']);
if ($hostname != $device['ip_address']) {
if (strpos($hostname, '.')) {
$hostname = substr($hostname, 0, strpos($hostname, '.') - 1);
}
}
$isCactiSysName = db_fetch_cell_prepared('SELECT COUNT(*)
FROM host
WHERE snmp_sysName = ?
AND (hostname = ? OR hostname LIKE "' . $hostname . '%")',
array($snmp_sysName, $device['ip_address']));
if ($isCactiSysName) {
automation_debug(", Skipping sysName '" . $snmp_sysName . "' already in Cacti!\n");
markIPDone($device['ip_address'], $network_id);
continue;
}
if ($network['same_sysname'] == '') {
$isDuplicateSysNameDiscovery = db_fetch_cell_prepared('SELECT COUNT(*)
FROM automation_devices
WHERE network_id = ?
AND sysName != ""
AND ip != ?
AND sysName = ?',
array($network_id, $device['ip_address'], $snmp_sysName));
$isDuplicateSysNameCacti = db_fetch_cell_prepared('SELECT COUNT(*)
FROM host
WHERE snmp_sysName = ?
AND hostname != ?',
array($snmp_sysName, $device['ip_address']));
if ($isDuplicateSysNameDiscovery || $isDuplicateSysNameCacti) {
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'] . "\n");
$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\n");
} else {
automation_debug(", Template: " . $fos['name']);
$device['os'] = $fos['name'];
automation_debug(", Skipped: Add to Cacti disabled\n");
}
}
// 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, snmp_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_community']) . ', '
. 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);
}
} elseif ($result) {
db_execute('REPLACE INTO automation_devices
(network_id, hostname, ip, snmp_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_community']) . ', '
. 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(automation_get_pid() . ' 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 removeMyProcess($pid, $network_id) {
db_execute_prepared('DELETE FROM automation_processes
WHERE pid = ?
AND network_id = ?',
array($pid, $network_id));
db_execute_prepared('DELETE FROM automation_ips
WHERE pid = ?
AND network_id = ?',
array($pid, $network_id));
}
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 (cacti_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, command)
VALUES (?, ?, ?, ?, 'running', NOW(), 'start')",
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 reportNetworkStatus($network_id, $old_devices) {
$details = db_fetch_row_prepared('SELECT notification_enabled, notification_email,
notification_fromname, notification_fromemail
FROM automation_networks
WHERE id = ?',
array($network_id));
if (cacti_sizeof($details)) {
if ($details['notification_enabled'] == 'on') {
if ($details['notification_fromname'] == '') {
$fromname = read_config_option('automation_fromname');
if ($fromname == '') {
$fromname = read_config_option('settings_from_name');
if ($fromname == '') {
$fromname = __('Cacti Primary Admin');
}
}
} else {
$fromname = $details['notification_fromname'];
}
if ($details['notification_fromemail'] == '') {
$fromemail = read_config_option('automation_fromemail');
if ($fromemail == '') {
$fromemail = read_config_option('settings_from_email');
if ($fromemail == '') {
$fromemail = 'root@cacti.net';
}
}
} else {
$fromemail = $details['notification_fromemail'];
}
$from = $fromname . ' <' . $fromemail . '>';
if ($details['notification_email'] != '') {
$email = $details['notification_email'];
} else {
$email = read_config_option('automation_email');
if ($email == '') {
$admin_user = read_config_option('admin_user');
if ($admin_user == '') {
cacti_log('WARNING: Unable to send Automation Notification Email. No Primary Admin User Account specified.', false, 'POLLER');
return false;
}
$details = db_fetch_cell_prepared('SELECT email_address AS notification_email, full_name
FROM user_auth
WHERE id = ?',
array($admin_user));
if (!cacti_sizeof($details)) {
cacti_log('WARNING: Unable to send Automation Notification Email. The Primary Admin User Account does not exist.', false, 'POLLER');
return false;
}
if ($details['notification_email'] == '') {
cacti_log('WARNING: Unable to send Automation Notification Email. The Primary Admin User Account does not have an Email Address.', false, 'POLLER');
return false;
}
$email = ($details['full_name'] != '' ? $details['full_name']:__('Cacti Primary Admin')) . ' <' . $details['notification_email'] . '>';
}
}
$new_devices = getNetworkDevices($network_id);
$ids = array();
populateDeviceIndex($ids, 0, $old_devices);
populateDeviceIndex($ids, 1, $new_devices);
$table_head_style = 'style="border-bottom: 1px solid black"';
$table_head = '<tr>' .
"<td $table_head_style><i>Hostname</i></td>" .
"<td $table_head_style><i>IP Address</i></td>" .
"<td $table_head_style><i>SNMP Name</i></td>" .
"<td $table_head_style><i>Has SNMP?</i></td>" .
"<td $table_head_style><i>Responding?</i></td>" .
'</tr>';
$table_exist = '';
$table_new = '';
$count_exist = 0;
$count_new = 0;
$font_up = '<font color="green">up</font>';
$font_down = '<font color="red">down</font>';
foreach ($new_devices as $device) {
$id = $device['ip'];
$html_line = '<tr><td>' . $device['hostname'] .
'</td><td>' . $device['ip'] .
'</td><td>' . (empty($device['sysName']) ? '<i><u>None</u></i>' : $device['sysName']) .
'</td><td>' . ($device['snmp'] ? $font_up : $font_down) .
'</td><td>' . ($device['up'] ? $font_up : $font_down) .
'</td></tr>';
if ($ids[$id]['old'] != '') {
$table_exist .= $html_line;
$count_exist++;
} else {
$table_new .= $html_line;
$count_new++;
}
}
if (strlen($table_exist) > 0) {
$table_exist .= '<tr><td colspan="5"</td>&nbsp;</td></tr>';
}
if (strlen($table_new) > 0) {
$table_new .= '<tr><td colspan="5"</td>&nbsp;</td></tr>';
}
$v = get_cacti_version();
$headers['User-Agent'] = 'Cacti-Automation-v' . $v;
$status = ($count_new + $count_exist) . ' devices discovered';
if ($count_new > 0) {
$status .= ', ' . $count_new . ' new!';
}
$network = db_fetch_row_prepared('SELECT id, name, subnet_range, last_started, last_runtime
FROM automation_networks
WHERE id = ?',
array($network_id));
$subject = 'Discovery of ' . $network['name'] . ' (' . $network['subnet_range'] . ') - ' . $status;
$output = '<h1>Discovery of ' . $network['name'] . '</h1><hr><br>' .
'<h2>Summary</h2><table>' .
'<tr><td>Network:</td><td>' . $network['subnet_range'] . '</td></tr>'.
'<tr><td>Started:</td><td>' . $network['last_started'] . '</td></tr>' .
'<tr><td>Duration:</td><td>' . intval($network['last_runtime']) . '</td></tr>' .
'<tr><td>Existing:</td><td>' . $count_exist . ' devices</td></tr>' .
'<tr><td>New:</td><td>' . $count_new . ' devices</td></tr>' .
'</table><br><br>';
if ($count_new > 0 || $count_exist > 0) {
$output .= '<table cellspacing="5" cellpadding="5">';
if ($count_new > 0) {
$output .= '<tr><td colspan="5"><h3>New Devices</h3></td></tr>' . $table_head . $table_new;
}
if ($count_exist > 0) {
$output .= '<tr><td colspan="5"><h3>Existing Devices</h3></td></tr>' . $table_head . $table_exist;
}
$output .= '</table>';
}
$error = mailer(
$from,
$email,
'',
'',
'',
$subject,
$output,
__('Cacti Automation Report requires an html based Email client'),
'',
$headers
);
if (strlen($error)) {
cacti_log("WARNING: Automation had problems sending to '$email' for $status. The error was '$error'", false, 'AUTOM8');
} else {
cacti_log("NOTICE: Email Notification Sent to '$email' for $status.", false, 'AUTOM8');
}
}
}
}
function populateDeviceIndex(&$ids, $is_new, $devices) {
$field = ($is_new ? 'new' : 'old');
foreach ($devices as $index => $device) {
$id = $device['ip'];
if (!isset($ids[$id])) {
$ids[$id] = array('old' => '', 'new' => '');
}
$ids[$id][$field] = $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($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 getNetworkDevices($network_id) {
return db_fetch_assoc_prepared('SELECT id, hostname, ip, sysName, snmp, up, time
FROM automation_devices
WHERE network_id = ?
ORDER BY hostname',
array($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));
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/whsir/cacti.git
git@gitee.com:whsir/cacti.git
whsir
cacti
cacti
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385