Page 1 of 1

Deadpool action for object matching string

Posted: Thu May 17, 2018 10:56 am
by vazudevan
Hi There,

We are using federated monitoring with inbound transfers. We would want to setup dead pool based not just on the CRITICAL for a period of time, but in addition the dead pool processor should match specific string in from the check command output of the dummy freshness check say for example "CRITICAL: No check result received in the past 3 days"

Is there a method this can be setup ? The dead pool settings page has option only for exclusion, but not a string match.

Also, Can the dead pool look for OK state with the specific string in command output, instead of problem state. It would keep the Host and Services summaries clean.

Thanks,
Vasudevan

Re: Deadpool action for object matching string

Posted: Thu May 17, 2018 3:07 pm
by cdienger
Deadpool only works with DOWN, CRITICAL AND UNKNOWN. It would probably be pretty easy to make it work with OK as well, but without being able to apply the additional string matching filter, it would be pretty pointless. I can see where this and how if there were dummy checks with differing outputs that this could be useful and will file a feature request.

Re: Deadpool action for object matching string

Posted: Thu May 17, 2018 11:30 pm
by vazudevan
Thank you for filing in the feature request.

In the interim, would it be a right approach to perform select operation on nagiosql for hoststats and servicestats to match up objects, following NDOUtils DB Model, and use xiapi to delete objects? Is there a better and simpler alternative?

Re: Deadpool action for object matching string

Posted: Fri May 18, 2018 11:03 am
by cdienger
I would use the api to find hosts and services instead of querying the db directly. The objects/servicestatus and objects/hoststatus endpoints should give you what you need.

Re: Deadpool action for object matching string

Posted: Fri May 25, 2018 7:43 am
by vazudevan
Thank you. I was able to get this through the API. I filtered against last_check time, to fetch the deadpool objects and then delete them. Posting the draft code, just in case it might be of use to others with similar requirement.

Can someone confirm if the applyconfig between services and hosts is really required?

Code: Select all

#!/usr/bin/env python

NagiosUrl="http://127.0.0.1/nagiosxi/api/v1/"
APIKey="your-api-key"

import requests
import datetime
import json
import syslog
from time import sleep

stage2=(datetime.date.today() - datetime.timedelta(days=5)).strftime("%Y-%m-%d")
stage1=(datetime.date.today() - datetime.timedelta(days=3)).strftime("%Y-%m-%d")
syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_LOCAL6)

def fetchServiceObjects(days):
    service_objects = []
    if not days:
        return False

    API_Response = requests.get(
        NagiosUrl + 'objects/servicestatus?apikey='
        + APIKey + '&last_check=lt:' + days,
        verify=False
    )
    if (API_Response.status_code != 200):
        syslog.syslog(syslog.LOG_ERR,
                      'Error occured reterving ServicesObjects')
        return False

    data = json.loads(API_Response.content)
    
    if int(data['servicestatuslist']['recordcount']) == 0:
        return False

    # XI API returns array only if objects are more than 1
    if int(data['servicestatuslist']['recordcount']) == 1:
        object = data['servicestatuslist']['servicestatus']
        service_objects.append( {'host_name': object['host_name'], 
            'service_description': object['name']} )

    if int(data['servicestatuslist']['recordcount']) > 1:  
        for object in data['servicestatuslist']['servicestatus']:
            service_objects.append( {'host_name': object['host_name'], 
            'service_description': object['name']} )

    return service_objects

def fetchHostObjects(days):
    host_objects = []
    if not days:
        return False

    API_Response = requests.get(
        NagiosUrl + 'objects/hoststatus?apikey='
        + APIKey + '&last_check=lt:' + days,
        verify=False
    )
    if (API_Response.status_code != 200):
        syslog.syslog(syslog.LOG_ERR,
                      'Error occured reterving HostObjects')
        return False

    data = json.loads(API_Response.content)
    
    if int(data['hoststatuslist']['recordcount']) == 0:
        return False
    if int(data['hoststatuslist']['recordcount']) == 1:
        object = data['hoststatuslist']['hoststatus']
        host_objects.append({ 'host_name': object['name'] })

    if int(data['hoststatuslist']['recordcount']) > 1:
        for object in data['hoststatuslist']['hoststatus']:
            host_objects.append({ 'host_name': object['name'] })

    return host_objects


# Fetch and Delete
host_has_dependents = []
hosts_deleted = 0
service_has_dependents = []
service_deleted = 0

Stage2ServiceObjects = fetchServiceObjects(stage2)
if Stage2ServiceObjects:
    for object in Stage2ServiceObjects:
        API_Response = requests.delete(
            NagiosUrl + 'config/service?apikey='
            + APIKey + '&host_name=' + object['host_name']
            + '&service_description=' + object['service_description'],
            verify=False)
        if (API_Response.status_code == 200):
            deleteResponse = json.loads(API_Response.content)
            if ('error' in deleteResponse.keys()):
                service_has_dependents.append(object['host_name'] 
                + ':' + object['service_description'])
                print deleteResponse['error']
                syslog.syslog(syslog.LOG_INFO, deleteResponse['error'])
            else:
                service_deleted += 1
                print deleteResponse['success']
                syslog.syslog(syslog.LOG_INFO, deleteResponse['success'])

if service_deleted > 0:
    API_Response = requests.post(NagiosUrl + 'system/applyconfig?apikey=' 
    + APIKey, verify=False)
    # Lets wait for applyconfig to complete 
    # (this will resolve dependent service objects if any for deleting hosts)
    sleep(30)

Stage2HostObjects = fetchHostObjects(stage2)
if Stage2HostObjects:
    for object in Stage2HostObjects:
        API_Response = requests.delete(
            NagiosUrl + 'config/host?apikey='
            + APIKey + '&host_name=' + object['host_name'],
            verify=False
        )
        if (API_Response.status_code == 200):
            deleteResponse = json.loads(API_Response.content)
            if ('error' in deleteResponse.keys()):
                host_has_dependents.append(object['host_name'])
                print deleteResponse['error']
                syslog.syslog(syslog.LOG_INFO, deleteResponse['error'])
            else:
                hosts_deleted += 1
                print deleteResponse['success']
                syslog.syslog(syslog.LOG_INFO, deleteResponse['success'])
            
if hosts_deleted > 0:
    API_Response = requests.post(NagiosUrl + 'system/applyconfig?apikey=' 
    + APIKey, verify=False)

if (hosts_deleted == 0) and (service_deleted ==0):
    print 'no objects identified for deletion'
    

Stage1ServiceObjects = fetchServiceObjects(stage1)

if Stage1ServiceObjects:
    for object in Stage1ServiceObjects:
        print object['host_name'] + ': ' + object['service_description']

Stage1HostObjects = fetchHostObjects(stage1)
if Stage1HostObjects:
    for object in Stage1HostObjects:
        print object['host_name']

exit(0)
#TODO: Compose Email and send summary of Stage 1 and Stage 2 including errors

# Fetch stage 2 services
# Delete stage 2 services
# Apply configuraiton
# Wait for 30 seconds
# Fetch stage 2 hosts
# Check if stage 2 hosts have associated services
# Delete stage 2 hosts
# Apply configuration
# Fetch Satge 1 hosts
# Fetch stage 1 services
# Compose Email and send summary of Stage 1 and Stage 2 including errors

Re: Deadpool action for object matching string

Posted: Fri May 25, 2018 12:17 pm
by lmiltchev
Thank you. I was able to get this through the API. I filtered against last_check time, to fetch the deadpool objects and then delete them. Posting the draft code, just in case it might be of use to others with similar requirement.
Thanks for sharing!
Can someone confirm if the applyconfig between services and hosts is really required?
No, it is not required.

Is it ok if we close this topic? Thanks!

Re: Deadpool action for object matching string

Posted: Fri May 25, 2018 12:21 pm
by scottwilkerson
Thanks for sharing!

Re: Deadpool action for object matching string

Posted: Mon May 28, 2018 7:10 am
by vazudevan
Thank you for the support. This topic can be closed.

Re: Deadpool action for object matching string

Posted: Tue May 29, 2018 7:57 am
by scottwilkerson
Great, Locking as resolved