115 lines
3.6 KiB
PHP
Executable File
115 lines
3.6 KiB
PHP
Executable File
#!/usr/bin/env php
|
|
<?php
|
|
/**
|
|
* Observium
|
|
*
|
|
* This file is part of Observium.
|
|
*
|
|
* @package observium
|
|
* @subpackage scripts
|
|
* @copyright (C) 2006-2013 Adam Armstrong, (C) 2013-2023 Observium Limited
|
|
*
|
|
*/
|
|
|
|
chdir(dirname($argv[0]) . '/..');
|
|
|
|
$options = getopt("da");
|
|
if (isset($options['d'])) {
|
|
array_shift($argv);
|
|
} // for compatibility
|
|
if (isset($options['a'])) {
|
|
array_shift($argv);
|
|
} // for compatibility
|
|
|
|
include_once("includes/observium.inc.php");
|
|
|
|
//$config['rancid_version'] = '3.7.1'; // test
|
|
|
|
// Rancid version configured manually
|
|
$rancid_config_version = isset($config['rancid_version']) ? ltrim($config['rancid_version'], 'vV') : 0;
|
|
// Detect locally installed rancid version
|
|
if ($rancid_cmd = external_exec('which rancid-run')) {
|
|
$rancid_cmd_version = explode(' ', external_exec($rancid_cmd . ' -V'))[1];
|
|
} else {
|
|
// Dummy version for compare
|
|
$rancid_cmd_version = 0;
|
|
}
|
|
$rancid_version = version_compare($rancid_cmd_version, $rancid_config_version, '>') ? $rancid_cmd_version : $rancid_config_version;
|
|
$rancid_version = $rancid_version ? trim_number($rancid_version) : '3'; // set default version to 3+
|
|
$rancid_version_major = explode('.', $rancid_version)[0];
|
|
|
|
// Set delimiter
|
|
$delimiter = $rancid_version_major < 3 ? ':' : ';';
|
|
|
|
$os_maps = [];
|
|
// Add user defined os maps first
|
|
if (is_array($config['rancid']['os_map'])) {
|
|
foreach ($config['rancid']['os_map'] as $os => $name) {
|
|
$os_maps[$os][] = [ 'name' => $name ];
|
|
}
|
|
}
|
|
|
|
// OS maps from definitions
|
|
foreach ($config['os'] as $os => $entry) {
|
|
if (!isset($entry['rancid'])) {
|
|
continue;
|
|
}
|
|
|
|
$os_maps[$os] = $entry['rancid'];
|
|
}
|
|
print_debug_vars($os_maps, 1);
|
|
|
|
?>
|
|
# RANCID router.db autogenerated by <?php echo realpath($_SERVER['SCRIPT_FILENAME']) . PHP_EOL; ?>
|
|
# RANCID version <?php echo $rancid_version . PHP_EOL; ?>
|
|
# Do not edit this file directly!
|
|
|
|
<?php
|
|
|
|
// Limit devices with rancid supported os
|
|
$where_array = [];
|
|
if (!isset($options['a'])) {
|
|
$where_array[] = generate_query_values_ng($config['poller_id'], 'poller_id');
|
|
}
|
|
$where_array[] = generate_query_values_ng(array_keys($os_maps), 'os');
|
|
$sql = "SELECT `hostname`, `os`, `version`, `hardware`, `disabled`, `status` FROM `devices` " . generate_where_clause($where_array) . " ORDER BY `hostname`";
|
|
foreach (dbFetchRows($sql) as $device) {
|
|
// blacklisted hosts
|
|
if (isset($config['rancid_host_blacklist']) &&
|
|
(in_array($device['hostname'], (array)$config['rancid_host_blacklist'], TRUE) ||
|
|
in_array($device['ip'], (array)$config['rancid_host_blacklist'], TRUE))) {
|
|
continue;
|
|
}
|
|
|
|
// Up for not disabled and available devices
|
|
$rancid_status = ($device['disabled'] || !$device['status']) ? "down" : "up";
|
|
//print_vars($device);
|
|
|
|
// New defs
|
|
foreach ($os_maps[$device['os']] as $rancid_map) {
|
|
// Need check hardware for map
|
|
if (isset($rancid_map['hardware']) &&
|
|
!preg_match($rancid_map['hardware'], $device['hardware'])) {
|
|
continue;
|
|
}
|
|
|
|
// Need check min os version
|
|
if (isset($rancid_map['version_min']) &&
|
|
version_compare($rancid_map['version_min'], $device['version'], '>')) {
|
|
continue;
|
|
}
|
|
|
|
// Need check min rancid version
|
|
if (isset($rancid_map['rancid_min']) &&
|
|
version_compare(trim_number($rancid_map['rancid_min']), $rancid_version, '>')) {
|
|
continue;
|
|
}
|
|
|
|
// All checks complete, write rancid entry and break loop
|
|
echo($device['hostname'] . $delimiter . $rancid_map['name'] . $delimiter . $rancid_status . PHP_EOL);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// EOF
|