Mini Shell

Direktori : /proc/self/root/proc/thread-self/root/opt/imh-python/lib/python2.7/site-packages/mtrlib/
Upload File :
Current File : //proc/self/root/proc/thread-self/root/opt/imh-python/lib/python2.7/site-packages/mtrlib/nscaweb.py

"""Interact with an nscaweb server"""
import time
import platform
import random
import requests
import dns.resolver
from .nagios import OK

def send_command(**kwargs):
    """
    Send an external Nagios command via nscaweb

    Arguments: The following keyword arguments are understood by
        `send_command`:

        * cmd (required): A valid Nagios external command. See
          http://old.nagios.org/developerinfo/externalcommands/commandlist.php
          for further details
        * timestamp (int, optional): timestamp to supply with the external
          command. time.time() is used as a default
        * nscahost (str, optional): host where nscaweb is running
        * nscauser (str, required): user name to use with nscaweb
        * nscapass (str, required): password for nscaweb
    """
    timestamp = kwargs.get('timestamp', int(time.time()))
    cmd = kwargs.get('cmd')
    nscahost = kwargs.get('nscaserver', None)
    nscauser = kwargs.get('nscauser', None)
    nscapass = kwargs.get('nscapass', None)

    if nscahost is None:
        if platform.node().startswith('lax-'):
            nscahost = 'lax-checkers.imhadmin.net'
        else:
            nscahost = 'ash-checkers.imhadmin.net'

    ips = get_ips(nscahost)

    for nscahost in ips:
        url = 'https://{!s}:5668/queue'.format(nscahost)
        try:
            return requests.post(
                url,
                verify=False,
                timeout=10.0,
                data={
                    'username': nscauser,
                    'password': nscapass,
                    'input': '[{!s}] {!s}'.format(timestamp, cmd),
                }
            )
        except:
            if nscahost == ips[-1]:
                return False

def get_ips(nscahost):
    """Function to resolve IP addresses from a hostname"""
    r = dns.resolver.Resolver()
    ips = [str(x) for x in r.query(nscahost.split(':')[0], "A")]
    random.shuffle(ips)
    return ips

def service_check(**kwargs):
    """
    Send a service check result to Nagios via nscaweb

    Arguments:  The following keyword arguments are understood by
        `service_check`:

        * service (str, required): The service description to be updated
          e.g. "Check Load"
        * host (str, optional): The host to be updated. Defaults to
          the local server's short hostname
        * status (int, optional): The status of the service check. Defaults
          to OK
        * message (str, optional): Check result string. Displayed in Nagios.
          Defaults to and empty string

        Additional kwargs understood by `send_command` may be passed in
        as kwargs to `service_check`

    For more information on this external command see
    http://old.nagios.org/developerinfo/externalcommands/commandinfo.php?command_id=114
    """
    host = kwargs.get('host', platform.node().split('.')[0])
    service = kwargs.get('service')
    status = kwargs.get('status', OK)
    message = kwargs.get('message', '')

    command = 'PROCESS_SERVICE_CHECK_RESULT;{!s};{!s};{!s};{!s}'.format(
        host,
        service,
        status,
        message
    )

    send_command(cmd=command, **kwargs)

def force_service_recheck(**kwargs):
    """
    Force recheck of a service via nscaweb, or all services on a host if
      the `service` kwarg is not supplied

    Arguments: The following keyword arguments are understood by
    `force_service_recheck`:

    * service(str, required): The service description to be updated
      e.g. "Check Blacklists"
    * host (str, optional): The host to be updated. Defaults to
      the local server's short hostname

      Additional kwargs understood by `send_command` may be passed in
      as kwargs to `force_service_recheck`

    For more information on these external commands, see
    http://old.nagios.org/developerinfo/externalcommands/commandinfo.php?command_id=30
    http://old.nagios.org/developerinfo/externalcommands/commandinfo.php?command_id=129
    """
    host = kwargs.get('host', platform.node().split('.')[0])
    service = kwargs.get('service')
    timestamp = kwargs.get('timestamp', int(time.time()))

    if service is None:
        command = 'SCHEDULE_HOST_SVC_CHECKS;{!s};{!s}'.format(
            host,
            timestamp
        )
    else:
        command = 'SCHEDULE_FORCED_SVC_CHECK;{!s};{!s};{!s}'.format(
            host,
            service,
            timestamp
        )

    send_command(cmd=command, timestamp=timestamp, **kwargs)

Zerion Mini Shell 1.0