Getting Threshold value for a hostgroup services in one go

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Getting Threshold value for a hostgroup services in one

Post by ssax »

Another question:

Are the hostgroups going to contain both windows and linux servers? The reason I ask is because the commands and positions of these things are not standardized across OSes for the commands and the flexibility needs to be included if that is the case. Note that the commands and the positions of the warning and critical thresholds per RAM, DISK, CPU, are different as well so please keep that in mind as you've been patient as this would usually be a paid custom development thing because of its complexity but I'm willing to help you out. I'm just setting your expectations on this as it's outside the scope of normal support and should be treated as such. I think this should be finished early this upcoming week.
progressive.nagiosXI
Posts: 277
Joined: Mon Jul 31, 2017 5:54 am

Re: Getting Threshold value for a hostgroup services in one

Post by progressive.nagiosXI »

Hi

only for HostGroups which contain only Linux or contain only Windows

Thanks
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Getting Threshold value for a hostgroup services in one

Post by ssax »

Another question, are you looking to get the data from the current running configuration (nagios DB) or from the CCM (nagiosql DB, which would show the latest CCM data but not necessarily the latest running configuration data, nagios DB has the current running check data).
progressive.nagiosXI
Posts: 277
Joined: Mon Jul 31, 2017 5:54 am

Re: Getting Threshold value for a hostgroup services in one

Post by progressive.nagiosXI »

nagios DB has the current running check data

Please do for nagios DB

Thanks
ssax
Dreams In Code
Posts: 7682
Joined: Wed Feb 11, 2015 12:54 pm

Re: Getting Threshold value for a hostgroup services in one

Post by ssax »

Just so you see what I'm doing, please take this code and put it in a file, then change the API key and the root mysql password (if it's changed):

Code: Select all

<?php

$servername = "localhost";
$username = "root";
$password = 'nagiosxi';
$dbname = 'nagios';
$apikey = 'gIjk7HpetqCQ5EENdd5ZDDQY6rsplHvEF9YM6HgT8SJsMjvYQDfpt5HCPpiam7N0';
$file = '/tmp/thresholds_by_hostgroup.csv';
$csv = "Hostgroup,Host,Service,Arguments\n";
file_put_contents($file, '');

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4","$username","$password");
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully\n";
    $json = "https://$servername/nagiosxi/api/v1/objects/hostgroupmembers?apikey=$apikey&pretty=1";
    $jsonfile = file_get_contents($json);
    $hostgroups = json_decode($jsonfile);

    //var_dump($hostgroups);

    $stmt = $conn->query('select a.name1 as host_name, nagios_services.host_object_id, nagios_services.service_object_id, nagios_services.display_name, nagios_services.check_command_args from nagios_services left join nagios_objects a on a.object_id = nagios_services.host_object_id;');
    $services = $stmt->fetchAll();

        foreach ($hostgroups as $hostgroup) {
                $hostgroup_name = $hostgroup->hostgroup_name;

                foreach ($hostgroup->members as $member) {
                        $host_name = $member->host_name;
                        foreach ($services as $service) {
                                if ($service['host_name'] == "$host_name") {
                                        $display_name = $service['display_name'];
                                        $check_command_args = $service['check_command_args'];
                                        $csv .= "\"$hostgroup_name\",\"$host_name\",\"$display_name\",\"$check_command_args\"\n";
                                }
                        }
                }
        }

        // Write the contents to the file,
        // using the FILE_APPEND flag to append the content to the end of the file
        // and the LOCK_EX flag to prevent anyone else writing to the file at the same time
        file_put_contents($file, $csv, FILE_APPEND | LOCK_EX);
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>
Then run it as root:

Code: Select all

php filename.php
That should create /tmp/thresholds_by_hostgroup.csv which should contain CSV data.

The next step is to work on splitting out the thresholds (-w in some, -warning in others, the same with critical) into the individual Warning / Critical column.
Locked