Page 1 of 1

[SOLVED] Individualising multiple host service files

Posted: Thu May 31, 2018 7:17 am
by JGCG
Edit: this has been solved by runnnig the configs through a script to extract the required data.

Hi,

We're looking for a way to convert our service check files which have multiple hosts assigned to them to invidudual service files.

For example, say we have a service check for CPU usuage with 80 hosts assigned to this check, we'd instead like 80 indivudual service checks (named by hostname - service.cfg) with one host assigned to each.

We have around 465 different service checks with around 600 hosts assigend to various ones (most are doing the same check but have different tresholds/contacts assigned), so doing this by hand is not possible.

Is there an easy way to do this?

Thanks.

Re: Individualising multiple host service files

Posted: Thu May 31, 2018 3:37 pm
by cdienger
The bulk cloning wizard will use an existing host as a template to create individual service checks for each host. https://assets.nagios.com/downloads/nag ... Wizard.pdf has more details.

That said, I would recommend sticking with the current configuration if possible. Having essentially duplicate services in multiple files can quickly add up and impact performance.

Re: Individualising multiple host service files

Posted: Fri Jun 01, 2018 8:26 am
by JGCG
Thanks for the reply, I had a quick look but I don't think that method would work for us.

I created a quick PHP script which now takes care of this. Code is below if this helps any one else.

*Take a copy of the service cfg files into a new directory.
*Change into the newly created directory.
*Run:

Code: Select all

for file in *.cfg; do php converter.php "$file"; done
This will go through each service.cfg file, and for each host assigned to the service check it will create a new service file for that host (with the original defined settings), saving the file to 'hostname' + 'service description' + '.cfg'.

Code: Select all

<?php

//Load the passed in filename and store the contents of the file in $fileContents
$currentFile = $argv[1];
if(is_null($currentFile)){
    die("Invalid file name passed in");
}

$fileContents = file_get_contents($currentFile);

//Script will not process a file that has more than 1 service definition within it
//Check the errors.txt after running
$numberOfServicesInFile = preg_match_all("/^.*\bhost_name\b.*$/m", $fileContents, $counter);
if($numberOfServicesInFile != 1){
    file_put_contents("new/errors.txt", "Multiple or no services in file - failed to process {$currentFile}\n", FILE_APPEND | LOCK_EX);
    die("Will not process $argv[1]\n");
}

//Find the host_name line and store all hosts in an array
preg_match("/^.*\bhost_name\b.*$/m", $fileContents, $hosts);
$hosts = preg_replace('/\s+/', '', str_replace("host_name", "", $hosts));
$hosts = explode(',', $hosts[0]);

//Store the service definition (everything between {}) in $template
$template = extractBetween($fileContents, "{", "}");

//Get the 'Service Description'
preg_match("/^.*\bservice_description\b.*$/m", $fileContents, $serviceDescription);
$serviceDescription = str_replace("service_description", "", $serviceDescription);
$serviceDescription = trim($serviceDescription[0]);
$serviceDescription = preg_replace('/[^A-Za-z0-9\-]/', '_', $serviceDescription);

//For each host in the array
foreach ($hosts as $host){
    //Replace the 'host_name' line with 'host_name' + current $host
    $singleHostTemplate = preg_replace("/.*\bhost_name\b.*\n/u", "\n\thost_name\t\t\t$host\n", $template);
    $singleHostTemplate = "define service{".$singleHostTemplate;
    $singleHostTemplate .= "\n}\n";

    //File name of new service file will be 'hostname' + 'service description' + '.cfg'
    $fileName = "new/".$host.".cfg";

    //Write new service definition to file.
    if(file_put_contents($fileName, $singleHostTemplate, FILE_APPEND | LOCK_EX)){
        echo "{$fileName} - wrote to file\n";
    }else {
        file_put_contents("new/errors.txt", "Error writing file {$fileName}", FILE_APPEND | LOCK_EX);
        die("Error writing file $fileName - exiting");
    }
}

function extractBetween($string, $start, $end) {
    $pos = stripos($string, $start);
    $str = substr($string, $pos);
    $str_two = substr($str, strlen($start));
    $second_pos = stripos($str_two, $end);
    $str_three = substr($str_two, 0, $second_pos);
    $between = trim($str_three);
    return $between;
}

?>




Re: Individualising multiple host service files

Posted: Fri Jun 01, 2018 2:09 pm
by cdienger
Thanks for the update and the code :) !