def test_02_request(self):
        with HTTMock(mock_200):
            client.request("/one/path", method="GET", status_code=200)

        with HTTMock(mock_200):
            client.request("/one/path", method="POST", status_code=200)

        with HTTMock(mock_200):
            client.request(
                "/one/path", method="GET", params={'key': 'value'}, status_code=200
            )

        with HTTMock(mock_200):
            client.request(
                "/one/path", method="POST", data={'key': 'value'}, status_code=200
            )

        with HTTMock(mock_404):
            client.request("/one/path", method="GET", status_code=404)

        with HTTMock(mock_404):
            with self.assertRaises(CheckmywsError):
                client.request("/one/path", method="GET", status_code=200)

        with HTTMock(mock_404):
            with self.assertRaises(CheckmywsError):
                client.request("/one/path", method="GET", status_code=200)

        client_proxy = CheckmywsClient(proxy="http://127.0.0.1")
        with HTTMock(mock_200):
            client_proxy.request("/one/path", method="GET", status_code=200)
Beispiel #2
0
    def test_02_request(self):
        with HTTMock(mock_200):
            client.request("/one/path", method="GET", status_code=200)

        with HTTMock(mock_200):
            client.request("/one/path", method="POST", status_code=200)

        with HTTMock(mock_200):
            client.request("/one/path",
                           method="GET",
                           params={'key': 'value'},
                           status_code=200)

        with HTTMock(mock_200):
            client.request("/one/path",
                           method="POST",
                           data={'key': 'value'},
                           status_code=200)

        with HTTMock(mock_404):
            client.request("/one/path", method="GET", status_code=404)

        with HTTMock(mock_404):
            with self.assertRaises(CheckmywsError):
                client.request("/one/path", method="GET", status_code=200)

        with HTTMock(mock_404):
            with self.assertRaises(CheckmywsError):
                client.request("/one/path", method="GET", status_code=200)

        client_proxy = CheckmywsClient(proxy="http://127.0.0.1")
        with HTTMock(mock_200):
            client_proxy.request("/one/path", method="GET", status_code=200)
Beispiel #3
0
    def test_01_init(self):
        global client

        client = CheckmywsClient(url="https://api.dev.checkmy.ws/api")
        self.assertEqual(client.url, "https://api.dev.checkmy.ws/api")
        self.assertEqual(client.verify, False)

        client = CheckmywsClient()
        self.assertEqual(client.url, "https://api.checkmy.ws/api")

        if not PY26:
            self.assertEqual(client.verify, True)
    def test_04_signin(self):
        client = CheckmywsClient(login='******', passwd='unittest')

        self.assertNotEqual(client.passwd, 'unittest')
        self.assertEqual(client.authed, False)

        with HTTMock(mock_401):
            with self.assertRaises(CheckmywsError):
                client.signin()

        self.assertEqual(client.authed, False)

        with HTTMock(mock_200):
            client.signin()

        self.assertEqual(client.authed, True)
Beispiel #5
0
    def test_05_logout(self):
        client = CheckmywsClient(login='******', token='unittest')
        self.assertEqual(client.authed, False)

        with HTTMock(mock_200):
            client.signin()

        self.assertEqual(client.authed, True)

        with HTTMock(mock_401):
            with self.assertRaises(CheckmywsError):
                client.logout()

        self.assertEqual(client.authed, True)

        with HTTMock(mock_200):
            client.logout()

        self.assertEqual(client.authed, False)
    def test_05_logout(self):
        client = CheckmywsClient(login='******', token='unittest')
        self.assertEqual(client.authed, False)

        with HTTMock(mock_200):
            client.signin()

        self.assertEqual(client.authed, True)

        with HTTMock(mock_401):
            with self.assertRaises(CheckmywsError):
                client.logout()

        self.assertEqual(client.authed, True)

        with HTTMock(mock_200):
            client.logout()

        self.assertEqual(client.authed, False)
Beispiel #7
0
    def test_04_signin(self):
        client = CheckmywsClient(login='******', passwd='unittest')

        self.assertNotEqual(client.passwd, 'unittest')
        self.assertEqual(client.authed, False)

        with HTTMock(mock_401):
            with self.assertRaises(CheckmywsError):
                client.signin()

        self.assertEqual(client.authed, False)

        with HTTMock(mock_200):
            client.signin()

        self.assertEqual(client.authed, True)

if __name__ == '__main__':
    arguments = docopt(__doc__)

    influxdb_dsn = arguments['--influxdb']
    verbose = arguments['-v']
    asfloat = arguments['-f']

    checkmyws.client.BASE_URL = arguments['--api']
    ssl_verify = True

    if 'dev' in checkmyws.client.BASE_URL:
        ssl_verify = False

    cmws = CheckmywsClient(verify=ssl_verify)

    if arguments['<check_id>'] is not None:
        check_ids = arguments['<check_id>']

    if verbose:
        logger.setLevel(logging.DEBUG)

    check_ids = set(check_ids)

    logger.debug("influxdb_dsn: %s", influxdb_dsn)
    logger.debug("check_ids: %s", check_ids)

    metrics = []

    for check_id in check_ids:
def main():
    # Parse command line arguments
    arguments = docopt(__doc__)

    # Display version
    if arguments['--version'] is True:
        print("Version: {0}".format(__version__))
        sys.exit(3)

    check_id = arguments["<check_id>"]
    proxy = arguments.get("--proxy", None)

    if arguments['--verbose'] is True:
        logger.setLevel(logging.DEBUG)

    logger.debug("Command line arguments:\n%s", arguments)

    logger.debug("Proxy: %s", proxy)

    # Get status from API
    client = CheckmywsClient(proxy=proxy)

    try:
        status = client.status(check_id)

    except Exception as err:
        print(err)
        sys.exit(3)

    logger.debug("Raw:\n%s\n", status)

    # Grab state
    state = status.get('state', 3)
    state_str = status.get("state_str", state)

    logger.debug("State: %s (%s)", state, state_str)

    # Extract Perfdata
    metas = status.get('metas', {})
    metrics = status.get('lastvalues', {})
    metrics = metrics.get('httptime', {})
    perfdata = []

    # Build Perfdata string
    logger.debug("Metrics: %s", metrics)
    values = []

    # Response time by location
    for key in metrics:
        value = metrics[key]
        perfdata.append(perfdata2string(key, value, 'ms', min=0))
        values.append(value)

    # Mean time
    if len(values):
        mean_time = sum(values) / len(values)
        perfdata.append(perfdata2string('mean_time', mean_time, 'ms', min=0))

    # Yslow load time
    yslow_page_load_time = metas.get('yslow_page_load_time', None)
    perfdata.append(
        perfdata2string('yslow_page_load_time',
                        yslow_page_load_time,
                        'ms',
                        min=0))

    # Yslow score
    yslow_score = metas.get('yslow_score', None)
    perfdata.append(perfdata2string('yslow_score', yslow_score, min=0,
                                    max=100))

    # Build Perfdata
    logger.debug("Perfdata: %s", perfdata)
    perfdata = [m for m in perfdata if len(m)]
    perfdata = " ".join(perfdata)

    # Build Output
    output = status.get("state_code_str", state_str)

    if mean_time is not None:
        output = "{0}, Mean response time: {1}ms".format(output, mean_time)

    if arguments['-e'] is True:
        status_url = "http://{0}.status.checkmy.ws".format(check_id)

        console_url = "https://console.checkmy.ws/#/dashboard?_id={0}".format(
            check_id)

        output = "{0} [<a href='{1}'>status page</a>] [<a href='{2}'>console</a>]".format(
            output, status_url, console_url)

    if arguments['-f'] is True and perfdata:
        output = "{0} | {1}".format(output, perfdata)

    # Invalid state
    if state < 0:
        state = 3

    # Print output
    print(output)
    sys.exit(state)
Beispiel #10
0
def main():
    # Parse command line arguments
    arguments = docopt(__doc__)

    # Display version
    if arguments['--version'] is True:
        print("Version: {0}".format(__version__))
        sys.exit(3)

    check_id = arguments["<check_id>"]
    proxy = arguments.get("--proxy", None)

    if arguments['--verbose'] is True:
        logger.setLevel(logging.DEBUG)

    logger.debug("Command line arguments:\n%s", arguments)

    logger.debug("Proxy: %s", proxy)

    url = arguments.get("--url", None)
    logger.debug("URL: %s", url)

    # Get status from API
    client = CheckmywsClient(proxy=proxy, url=url)

    try:
        status = client.status(check_id)

    except Exception as err:
        print(err)
        sys.exit(3)

    logger.debug("Raw:\n%s\n", status)

    # Grab informations
    metas = status.get('metas', {})
    timestamp = metas.get("lastcheck", int(time.time()))
    url = urlparse(status["url"])
    name = url.netloc.replace(":", ".")

    logger.debug("URL: %s", url)
    logger.debug("Name: %s", name)

    # Grab state
    state = status.get('state', 3)
    state_str = status.get("state_str", state)
    state_code_str = status.get("state_code_str", state_str)

    # Invalid state
    if state < 0:
        state = 3

    logger.debug("State: %s (%s)", state, state_str)

    # Extract metrics_states
    metrics_states = status.get('states', {})
    logger.debug("Metrics_states: %s", metrics_states)

    # Extract Perfdata
    lastvalues = status.get('lastvalues', {})

    # Convert metas to metric
    #for label in ('yslow_page_load_time'):
    #    value = metas.get(label, None)
    #    if value is not None:
    #        lastvalues[label]['backend'] = value

    # Build output
    if arguments['-g']:
        output = output_graphite(name, timestamp, lastvalues)

    else:
        output = output_nagios(name, timestamp, lastvalues, arguments,
                               check_id, state_code_str)

    # Display and quit
    print(output)
    sys.exit(state)
def main():
    # Parse command line arguments
    arguments = docopt(__doc__)

    # Display version
    if arguments['--version'] is True:
        print("Version: {0}".format(__version__))
        sys.exit(3)

    check_id = arguments["<check_id>"]
    proxy = arguments.get("--proxy", None)

    if arguments['--verbose'] is True:
        logger.setLevel(logging.DEBUG)

    logger.debug("Command line arguments:\n%s", arguments)

    logger.debug("Proxy: %s", proxy)

    # Get status from API
    client = CheckmywsClient(proxy=proxy)

    try:
        status = client.status(check_id)
        
    except Exception as err:
        print(err)
        sys.exit(3)

    logger.debug("Raw:\n%s\n", status)

    # Grab state
    state = status.get('state', 3)
    state_str = status.get("state_str", state)

    logger.debug("State: %s (%s)", state, state_str)

    # Extract Perfdata
    metas  = status.get('metas', {})
    metrics = status.get('lastvalues', {})
    metrics = metrics.get('httptime', {})
    perfdata = []

    # Build Perfdata string
    logger.debug("Metrics: %s", metrics)
    values = []

    # Response time by location
    for key in metrics:
        value = metrics[key]
        perfdata.append(
            perfdata2string(key, value, 'ms', min=0)
        )
        values.append(value)

    # Mean time
    if len(values):
        mean_time = sum(values)/len(values)
        perfdata.append(
            perfdata2string('mean_time', mean_time, 'ms', min=0)
        )

    # Yslow load time
    yslow_page_load_time = metas.get('yslow_page_load_time', None)
    perfdata.append(
        perfdata2string('yslow_page_load_time', yslow_page_load_time, 'ms', min=0)
    )

    # Yslow score
    yslow_score = metas.get('yslow_score', None)
    perfdata.append(
        perfdata2string('yslow_score', yslow_score, min=0, max=100)
    )

    # Build Perfdata
    logger.debug("Perfdata: %s", perfdata)
    perfdata = [m for m in perfdata if len(m)]
    perfdata = " ".join(perfdata)

    # Build Output
    output = status.get("state_code_str", state_str)

    if mean_time is not None:
        output = "{0}, Mean response time: {1}ms".format(
            output,
            mean_time
        )

    if arguments['-e'] is True:
        status_url = "http://{0}.status.checkmy.ws".format(
           check_id
        )

        console_url = "https://console.checkmy.ws/#/dashboard?_id={0}".format(
           check_id
        )

        output = "{0} [<a href='{1}'>status page</a>] [<a href='{2}'>console</a>]".format(
            output,
            status_url,
            console_url
        )

    if arguments['-f'] is True and perfdata:
        output = "{0} | {1}".format(
            output,
            perfdata
        )

    # Invalid state
    if state < 0:
        state = 3

    # Print output
    print(output)
    sys.exit(state)
def main():
    # Parse command line arguments
    arguments = docopt(__doc__)

    # Display version
    if arguments['--version'] is True:
        print("Version: {0}".format(__version__))
        sys.exit(3)

    check_id = arguments["<check_id>"]
    proxy = arguments.get("--proxy", None)

    if arguments['--verbose'] is True:
        logger.setLevel(logging.DEBUG)

    logger.debug("Command line arguments:\n%s", arguments)

    logger.debug("Proxy: %s", proxy)

    url = arguments.get("--url", None)
    logger.debug("URL: %s", url)

    # Get status from API
    client = CheckmywsClient(proxy=proxy, url=url)

    try:
        status = client.status(check_id)

    except Exception as err:
        print(err)
        sys.exit(3)

    logger.debug("Raw:\n%s\n", status)

    # Grab informations
    metas = status.get('metas', {})
    timestamp = metas.get("lastcheck", int(time.time()))
    url = urlparse(status["url"])
    name = url.netloc.replace(":", ".")

    logger.debug("URL: %s", url)
    logger.debug("Name: %s", name)

    # Extract metrics_states
    metrics_states = status.get('states', {})
    logger.debug("Metrics_states: %s", metrics_states)

    # Extract Perfdata
    lastvalues = status.get('lastvalues', {})
    logger.debug("lastvalues: %s", lastvalues)

    # Grab httpping state
    httpping_state = metas.get('httpping_state', 3)
    httpping_state_str = status.get("state_str", httpping_state)
    httpping_state_code_str = status.get("state_code_str", httpping_state_str)

    # Invalid state
    if httpping_state < 0:
        httpping_state = 3

    logger.debug("httpping State: %s (%s: %s)", httpping_state, httpping_state_str, httpping_state_code_str)

    state = httpping_state
    state_code_str = httpping_state_str

    if httpping_state_str != httpping_state_code_str:
        state_code_str = "%s: %s" % (httpping_state_str, httpping_state_code_str)

    if arguments['-w'] is True:
        webtest_state = metas.get('webtest_state', None)
        if webtest_state is not None:
            # Invalid state
            if webtest_state < 0 or webtest_state > 3:
                webtest_state = 3

            webtest_state_str = [
                "Webtest Ok",
                "Webtest Warning",
                "Webtest Critical",
                "Webtest Unknown"
            ]

            webtest_state_str = webtest_state_str[webtest_state]

            if webtest_state > httpping_state:
                state = webtest_state
                state_code_str = webtest_state_str

                if state == 1:
                    lastvalues['state']['backend'] = 50

                elif state == 2:
                    lastvalues['state']['backend'] = 0

            logger.debug("webtest State: %s (%s)", webtest_state, webtest_state_str)

    # Convert metas to metric
    #for label in ('yslow_page_load_time'):
    #    value = metas.get(label, None)
    #    if value is not None:
    #        lastvalues[label]['backend'] = value

    # Build output
    if arguments['-g']:
        output = output_graphite(
            name, timestamp, lastvalues
        )

    else:
        output = output_nagios(
            name, timestamp, lastvalues,
            arguments, check_id, state_code_str
        )

    # Display and quit
    print(output)
    sys.exit(state)