<commit_before>#!/usr/bin/env python

import argparse
from ipaddr import IPv4Address
from maas_common import get_nova_client, status_err, status_ok, metric_bool
from novaclient.client import exceptions as exc


def check(args):

    COMPUTE_ENDPOINT = 'http://{ip}:8774/v3'.format(ip=args.ip)

    try:
        get_nova_client(bypass_url=COMPUTE_ENDPOINT)
        status_ok()
        metric_bool('nova_api_local_status', True)

    # if we get a ClientException don't bother sending any other metric
    # The API IS DOWN
    except exc.ClientException:
        status_ok()
        metric_bool('nova_api_local_status', False)
    # Any other exception presumably isn't an API error
    except Exception as e:
        status_err(str(e))


def main(args):
    check(args)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Check nova services.')
    parser.add_argument('ip',
                        type=IPv4Address,
                        help='nova service IP address.')
    args = parser.parse_args()
    main(args)<commit_msg>refactor (thanks @sigmavirus24)<commit_after>#!/usr/bin/env python

import argparse
from ipaddr import IPv4Address
from maas_common import get_nova_client, status_err, status_ok, metric_bool
from novaclient.client import exceptions as exc


def check(args):

    COMPUTE_ENDPOINT = 'http://{ip}:8774/v3'.format(ip=args.ip)

    try:
        get_nova_client(bypass_url=COMPUTE_ENDPOINT)
        is_up = True
    except exc.ClientException:
        is_up = False
    # Any other exception presumably isn't an API error
    except Exception as e:
        status_err(str(e))
    finally:
        status_ok()
        metric_bool('nova_api_local_status', is_up)


def main(args):
    check(args)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Check nova services.')
    parser.add_argument('ip',
                        type=IPv4Address,
                        help='nova service IP address.')
    args = parser.parse_args()
    main(args)
{'fraction_non_alphanumeric': 0.09288537549407115, 'fraction_numerical': 0.007905138339920948, 'mean_word_length': 3.05811623246493, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5074613', 'n_tokens_mistral': 649, 'n_tokens_neox': 624, 'n_words': 152}