mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Use Oxidized's built in diffing capability. Drops the need for xdiff and one http call.
This commit is contained in:
@@ -105,51 +105,60 @@ if (is_admin()) {
|
||||
else if ($config['oxidized']['enabled'] === true && isset($config['oxidized']['url'])) {
|
||||
// fetch info about the node and then a list of versions for that node
|
||||
$node_info = json_decode(file_get_contents($config['oxidized']['url'].'/node/show/'.$device['hostname'].'?format=json'), true);
|
||||
$config_versions = json_decode(file_get_contents($config['oxidized']['url'].'/node/version?node_full='.(!empty($node_info['group']) ? $node_info['group'].'/' : '').$device['hostname'].'&format=json'), true);
|
||||
$config_versions = json_decode(file_get_contents($config['oxidized']['url'].'/node/version?node_full='.(isset($node_info['full_name']) ? $node_info['full_name'] : $device['hostname']).'&format=json'), true);
|
||||
|
||||
if (is_array($config_versions)) {
|
||||
$config_total = count($config_versions);
|
||||
}
|
||||
|
||||
if ($config_total > 1) {
|
||||
if (isset($_POST['config'])) { // versioning with selected version
|
||||
// populate current_version
|
||||
if (isset($_POST['config'])) {
|
||||
list($oid,$date,$version) = explode('|',mres($_POST['config']));
|
||||
$current_version = array('oid'=>$oid, 'date'=>$date, 'version'=>$version);
|
||||
$current_config = array('oid'=>$oid, 'date'=>$date, 'version'=>$version);
|
||||
}
|
||||
else { // no version selected
|
||||
$current_version = array('oid'=>$config_versions[0]['oid'], 'date'=>$current_version[0]['date'], 'version'=>$config_total);
|
||||
$current_config = array('oid'=>$config_versions[0]['oid'], 'date'=>$current_config[0]['date'], 'version'=>$config_total);
|
||||
}
|
||||
|
||||
// fetch current_version
|
||||
$text = file_get_contents($config['oxidized']['url'].'/node/version/view?node='.$device['hostname'].'&group='.(!empty($node_info['group']) ? $node_info['group'] : '').'&oid='.$current_version['oid'].'&date='.urlencode($current_version['date']).'&num='.$current_version['version'].'&format=text');
|
||||
|
||||
if (isset($_POST['diff']) && isset($_POST['prevconfig'])) { // diff requested
|
||||
// populate previous_version
|
||||
if (isset($_POST['diff'])) { // diff requested
|
||||
list($oid,$date,$version) = explode('|',mres($_POST['prevconfig']));
|
||||
if ($current_version['oid'] == $oid) { // the same version is selected, assume the previous revision
|
||||
if (isset($oid) && $oid != $current_config['oid']) {
|
||||
$previous_config = array('oid'=>$oid, 'date'=>$date, 'version'=>$version);
|
||||
}
|
||||
else if ($current_config['version'] != 1) { // assume previous, unless current is first config
|
||||
foreach ($config_versions as $key => $version) {
|
||||
if ($version['oid'] == $current_version['oid']) {
|
||||
if ($version['oid'] == $current_config['oid']) {
|
||||
$prev_key = $key + 1;
|
||||
$oid = $config_versions[$prev_key]['oid'];
|
||||
$date = $config_versions[$prev_key]['date'];
|
||||
$version = $config_total - $prev_key;
|
||||
$previous_config['oid'] = $config_versions[$prev_key]['oid'];
|
||||
$previous_config['date'] = $config_versions[$prev_key]['date'];
|
||||
$previous_config['version'] = $config_total - $prev_key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($version > 0) { // if we know the version doesn't exist, don't even try to fetch it
|
||||
$previous_text = file_get_contents($config['oxidized']['url'].'/node/version/view?node='.$device['hostname'].'&group='.(!empty($node_info['group']) ? $node_info['group'] : '').'&oid='.$oid.'&date='.urlencode($date).'&num='.$version.'&format=text');
|
||||
if (!empty($previous_text)) {
|
||||
$previous_version = array('oid'=>$oid, 'date'=>$date, 'version'=>$version);
|
||||
$text = xdiff_string_diff($text, $previous_text); // requires pecl xdiff
|
||||
}
|
||||
} else {
|
||||
else {
|
||||
print_error('No previous version, please select a different version.');
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($previous_config)) {
|
||||
$url = $config['oxidized']['url'].'/node/version/diffs?node='.$device['hostname'].'&group=';
|
||||
if (!empty($node_info['group'])) {
|
||||
$url .= $node_info['group'];
|
||||
}
|
||||
$url .= '&oid='.$previous_config['oid'].'&date='.urlencode($previous_config['date']).'&num='.$previous_config['version'].'&oid2='.$current_config['oid'].'&format=text';
|
||||
|
||||
$text = file_get_contents($url); // fetch diff
|
||||
} else {
|
||||
// fetch current_version
|
||||
$text = file_get_contents($config['oxidized']['url'].'/node/version/view?node='.$device['hostname'].'&group='.(!empty($node_info['group']) ? $node_info['group'] : '').'&oid='.$current_config['oid'].'&date='.urlencode($current_config['date']).'&num='.$current_config['version'].'&format=text');
|
||||
}
|
||||
}
|
||||
else { // just fetch the only version
|
||||
$text = file_get_contents($config['oxidized']['url'].'/node/fetch/'.(!empty($node_info['group']) ? $node_info['group'].'/' : '').$device['hostname']);
|
||||
|
||||
}
|
||||
|
||||
if (is_array($node_info) || $config_total > 1) {
|
||||
@@ -186,15 +195,15 @@ if (is_admin()) {
|
||||
$i = $config_total;
|
||||
foreach ($config_versions as $version) {
|
||||
echo '<option value="'.$version['oid'].'|'.$version['date'].'|'.$config_total.'" ';
|
||||
if ($current_version['oid'] == $version['oid']) {
|
||||
if (is_array($previous_version)) {
|
||||
if ($current_config['oid'] == $version['oid']) {
|
||||
if (isset($previous_config)) {
|
||||
echo 'selected>+';
|
||||
}
|
||||
else {
|
||||
echo 'selected>*';
|
||||
}
|
||||
}
|
||||
else if ($previous_version['oid'] == $version['oid']) {
|
||||
else if ($previous_config['oid'] == $version['oid']) {
|
||||
echo '> -';
|
||||
}
|
||||
else {
|
||||
@@ -211,7 +220,7 @@ if (is_admin()) {
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-2 col-sm-6">
|
||||
<input type="hidden" name="prevconfig" value="';
|
||||
echo implode('|',$current_version);
|
||||
echo implode('|',$current_config);
|
||||
echo '">
|
||||
<button type="submit" class="btn btn-primary btn-sm" name="show">Show version</button>
|
||||
<button type="submit" class="btn btn-primary btn-sm" name="diff">Show diff</button>
|
||||
@@ -232,13 +241,13 @@ if (is_admin()) {
|
||||
}//end if
|
||||
|
||||
if (!empty($text)) {
|
||||
if (is_array($previous_version)) {
|
||||
if (isset($previous_config)) {
|
||||
$language = 'diff';
|
||||
} else {
|
||||
$language = 'ios';
|
||||
}
|
||||
$geshi = new GeSHi($text, $language);
|
||||
// $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
|
||||
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
|
||||
$geshi->set_overall_style('color: black;');
|
||||
// $geshi->set_line_style('color: #999999');
|
||||
echo '<div class="config">';
|
||||
|
||||
Reference in New Issue
Block a user