示例#1
0
def backend_up():
    """Respond with boolean of whether the Flask backend is up or down"""
    for _ in range(100):
        if open_port(FLASKHOST, FLASKPORT):
            return True
        else:
            time.sleep(0.1)
    return False
示例#2
0
 def test_open_port(self):
     """Test that an open port is correctly recognized"""
     host = 'localhost'
     port = 7606
     sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sck.bind((host, port))
     sck.listen(1)
     self.assertTrue(open_port(host, port))
     sck.close()
示例#3
0
def cli(ctx, host, port, debug, node, show_all):
    """
    Connect to the backend at --host and --port and return the Zabbix Low-Level Discovery
    json object.

    This object will contain macros for all endpoints from the config file found
    on --node, unless --show_all is set, in which case it will collect ALL macros
    found, excepting those set as do_not_discover.

    Perform --debug logging upstream to the backend if specified
    """
    # Do a simple socket check first
    if not open_port(host, port):
        exit(1)

    # Now try to get the value
    if debug:
        log_to_listener(host, port, 'debug', {
            'node': node,
            'show_all': show_all
        })
    uri = '/api/discovery/'
    if debug:
        log_to_listener(host, port, 'debug', {
            'host': host,
            'port': port,
            'uri': uri
        })

    show_bool = True if show_all.lower() in ['true', 'yes', 'y', 'show_all'
                                             ] else False

    fail = 'ZBX_NOTSUPPORTED'
    body = {'show_all': show_bool}
    if node:
        body['node'] = node
    method = 'post'

    try:
        result = do_request(host, port, uri, method, body=body)
        msgs = {
            'result': str(result),
            'size_in_bytes': len(str(result).encode('utf-8'))
        }
        if debug:
            log_to_listener(host, port, 'debug', msgs)
        print(result.strip())
    except NotFound:
        mdict = {'error': 'A non-200 HTTP response code was received.'}
        log_to_listener(host, port, 'error', mdict)
        print(fail)
    except FailedExecution:
        mdict = {'error': 'The request was unable to successfully complete.'}
        log_to_listener(host, port, 'error', mdict)
        print(fail)
def cli(ctx, host, port, debug, flag, node):
    """
    Connect to the backend at --host and --port and return the discovery json
    object for all APIs as Zabbix trapper data from "node".

    --flag is an arbitrary value used to make it possible to have multiple Zabbix keys
    reference the same data.

    Perform --debug logging upstream to the backend if specified
    """
    # Do a simple socket check first
    if not open_port(host, port):
        exit(1)

    # Now try to get the value
    if debug:
        log_to_listener(host, port, 'debug', {'node': node})

    uri = '/api/trapperdiscovery/{0}'.format(node)
    if debug:
        log_to_listener(host, port, 'debug', {
            'host': host,
            'port': port,
            'uri': uri
        })

    fail = 'ZBX_NOTSUPPORTED'
    body = {}
    if node:
        body['node'] = node
    method = 'post'

    try:
        result = do_request(host, port, uri, method, body=body)
        msgs = {
            'result': str(result),
            'size_in_bytes': len(str(result).encode('utf-8'))
        }
        if debug:
            log_to_listener(host, port, 'debug', msgs)
        print(result.strip())
    except NotFound:
        log_to_listener(
            host, port, 'error',
            {'error': 'A non-200 HTTP response code was received.'})
        print(fail)
    except FailedExecution:
        mdict = {'error': 'The request was unable to successfully complete.'}
        log_to_listener(host, port, 'error', mdict)
        print(fail)
def cli(ctx, host, port, debug, interval, node, nodetype):
    """
    Connect to the backend at --host and --port using "node" as the target Zabbix host name.
    Collect stats, and ship them to Zabbix via the trapper protocol.

    "node" indicates both the node name and the Zabbix host trapper target
    "nodetype" is the type of node, as defined in the YAML config file. It is one of
        "cluster",
        "coordinating",
        "master",
        "data",
        "ingest",
        "ml",
    "cluster" is special, as it collects cluster-level stats from the clusterstats API endpoint
    "interval" is also defined in the YAML config file.  If not specified, it will be '60s'
    Return the number of stats which failed to be read by Zabbix. A zero indicates all succeeded.

    Perform --debug logging upstream to the backend if specified
    """
    # Do a simple socket check first
    if not open_port(host, port):
        exit(1)

    # Now try to get the value
    if debug:
        mdict = {
            'user_parms':
            'node: {0}, nodetype: {1}, interval: {2}'.format(
                node, nodetype, interval)
        }
        log_to_listener(host, port, 'debug', mdict)

    uri = '/api/trapperstats/{0}'.format(node)
    if debug:
        log_to_listener(host, port, 'debug', {
            'host': host,
            'port': port,
            'uri': uri
        })
    fail = 'ZBX_NOTSUPPORTED'
    body = {'nodetype': nodetype, 'interval': interval}
    method = 'post'

    try:
        print(do_request(host, port, uri, method, body=body).strip())
    except FailedExecution:
        mdict = {'error': 'The request was unable to successfully complete.'}
        log_to_listener(host, port, 'error', mdict)
        print(fail)
示例#6
0
def cli(ctx, host, port, debug, node, api, endpoint):
    """
    Connect to the backend at --host and --port and return the value
    associated with the provided API and ENDPOINT.

    Specify a particular node name with --node
    Perform --debug logging upstream to the backend if specified
    """
    # Do a simple socket check first
    if not open_port(host, port):
        exit(1)

    # Now try to get the value
    node = None if node == '' else node
    if debug:
        mdict = {
            'user_parms':
            'api: {0}, endpoint: {1}, node: {2}'.format(api, endpoint, node)
        }
        log_to_listener(host, port, 'debug', mdict)

    if not api in apis():
        msg = "'{0}' is not a valid API: {1}".format(api, apis())
        log_to_listener(host, port, 'critical', msg)
        exit(1)

    uri = '/api/{0}/{1}'.format(api, endpoint)
    if debug:
        log_to_listener(host, port, 'debug', {
            'host': host,
            'port': port,
            'uri': uri
        })
    fail = 'ZBX_NOTSUPPORTED'
    if node is not None:
        body = {'node': node}
        method = 'post'
    else:
        method = 'get'
        body = None

    try:
        print(do_request(host, port, uri, method, body=body).strip())
    except FailedExecution:
        mdict = {'error': 'The request was unable to successfully complete.'}
        log_to_listener(host, port, 'error', mdict)
        print(fail)
def cli(ctx, host, port, debug, node):
    """
    Connect to the backend at --host and --port and display all APIs and endpoints
    associated with --node

    Perform --debug logging upstream to the backend if specified
    """
    # Do a simple socket check first
    if not open_port(host, port):
        exit(1)

    # Now try to get the value
    if debug:
        log_to_listener(host, port, 'debug', {'node': node})
    uri = '/api/display/'
    if debug:
        log_to_listener(host, port, 'debug', {'host':host, 'port':port, 'uri':uri})

    fail = 'ZBX_NOTSUPPORTED'
    body = {'node': node}
    method = 'post'

    try:
        if node:
            result = do_request(host, port, uri, method, body=body)
        else:
            result = do_request(host, port, uri, 'get')
        msgs = {
            'result': str(result),
            'size_in_bytes': len(str(result).encode('utf-8'))
        }
        if debug:
            log_to_listener(host, port, 'debug', msgs)
        print(result.strip())
    except NotFound:
        mdict = {'error':'A non-200 HTTP response code was received.'}
        log_to_listener(host, port, 'error', mdict)
        print(fail)
    except FailedExecution:
        mdict = {'error':'The request was unable to successfully complete.'}
        log_to_listener(host, port, 'error', mdict)
        print(fail)
示例#8
0
 def test_closed_port(self):
     """Test that a closed port is correctly recognized"""
     host = 'localhost'
     port = 7606
     self.assertFalse(open_port(host, port))