<commit_before>#!/usr/bin/env python
#
# Makes http calls to the different components to gather metric data
#
# Output service metrics for time taken, HTTP status code and status(0=Fail, 1=OK)
# When failing on a dependency, dummy HTTP status codes (600 or 700) are used
#
#
# <NAME> <<EMAIL>>

import argparse
import re
import sys
import time
import socket
import os
import json
import subprocess
import requests

STATE_OK = 0
STATE_WARNING = 1
STATE_CRITICAL = 2



DEFAULT_SCHEME = socket.gethostname()

def print_metric(metric_data):
    for line in metric_data:
        print(line)

def ceph_metric(cmd, metric_data, scheme):

    ceph_health = subprocess.Popen( cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = ceph_health.communicate()

    if ceph_health.returncode != 0 :
        print('Ceph Health failed: %s' % stderr)
        sys.exit(STATE_WARNING)

    if ('HEALTH_OK' in stdout) or ('HEALTH_WARN' in stdout):
        metric_data.append('%s.status 1' % scheme )

    else:
        metric_data.append('%s.status 0' % scheme )

    return metric_data

def service_list_metric(json_data, metric_data, scheme):
    services = json_data
    services_count = {}
    status = 1

    for i in services :
        name = i['binary']
        state = i['state']
        if name not in services_count:
            services_count[name] = 0

        if name == 'nova-compute':
            if i['status'] == 'enabled':
                if state != 'up':
                    metric_data.append('%s.status 0' % scheme )
                    return metric_data

        if state == 'up':
            services_count[name] += 1

    for key in services_count:
        if (services_count[key] == 0):
            status = 0
            break

    metric_data.append('%s.status %s ' % (scheme, status))
    return metric_data


def request_token(url, method='GET', retries=2, **kwargs):
    r = None
    try:
        for i in range(retries + 1):
            if i > 0:
                time.sleep(2)
            r = requests.request(method, url, **kwargs)
            if r.status_code :
                break
    except requests.exceptions.RequestException as e:
        print("%s returned %s" % (url, e))

    return r

def request(metric_data, scheme, url, timestamp, method='GET', retries=2, **kwargs):

    start = time.time()
    r = None
    try:
        for i in range(retries + 1):
            if i > 0:
                time.sleep(2)
            r = requests.request(method, url, **kwargs)
            if r.status_code:
                break
    except requests.exceptions.RequestException as e:
        print("%s returned %s" % (url, e))

    if r is None:
        status = 0
        status_code = 600
        time_taken = -10
    elif r.status_code != 200:
        status = 0
        status_code = r.status_code
        time_taken = -5
    else:
        status = 1
        status_code = r.status_code
        time_taken = round(time.time() - start, 3)


    metric_data.append('%s.time %s %s' % (scheme, time_taken, timestamp))
    metric_data.append('%s.code %s %s' % (scheme, status_code, timestamp))
    metric_data.append('%s.status %s %s' % (scheme, status, timestamp))
    return (r, metric_data)

def main():
    timestamp = int(time.time())

    parser = argparse.ArgumentParser()
    parser.add_argument('-u', '--user', default=os.environ['OS_USERNAME'])
    parser.add_argument('-p', '--password', default=os.environ['OS_PASSWORD'])
    parser.add_argument('-t', '--tenant', default=os.environ['OS_TENANT_NAME'])
    parser.add_argument('-a', '--auth-url', default=os.environ['OS_AUTH_URL'])
    parser.add_argument('-S', '--service-type', default='nova',
                        help='Service type must be provided')
    parser.add_argument('-s', '--scheme', default=DEFAULT_SCHEME)
    parser.add_argument('-f', '--fqdn', default=None,
                        help='fqdn must be provided for horizon')
    args = parser.parse_args()

    metric_data = []

    if len(sys.argv)==1:
        parser.print_help()
        sys.exit(STATE_WARNING)

    scheme = ''
    if args.scheme == DEFAULT_SCHEME :
        scheme = args.scheme + '.' + args.service_type + '.api'
    else:
        scheme = args.scheme + '.' + args.service_type<commit_msg>Suppressed SubjectAltNameWarning that disturbed SLA-metric output<commit_after>#!/usr/bin/env python
#
# Makes http calls to the different components to gather metric data
#
# Output service metrics for time taken, HTTP status code and status(0=Fail, 1=OK)
# When failing on a dependency, dummy HTTP status codes (600 or 700) are used
#
#
# <NAME> <<EMAIL>>

import argparse
import re
import sys
import time
import socket
import os
import json
import subprocess
import requests

STATE_OK = 0
STATE_WARNING = 1
STATE_CRITICAL = 2



DEFAULT_SCHEME = socket.gethostname()

def print_metric(metric_data):
    for line in metric_data:
        print(line)

def ceph_metric(cmd, metric_data, scheme):

    ceph_health = subprocess.Popen( cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = ceph_health.communicate()

    if ceph_health.returncode != 0 :
        print('Ceph Health failed: %s' % stderr)
        sys.exit(STATE_WARNING)

    if ('HEALTH_OK' in stdout) or ('HEALTH_WARN' in stdout):
        metric_data.append('%s.status 1' % scheme )

    else:
        metric_data.append('%s.status 0' % scheme )

    return metric_data

def service_list_metric(json_data, metric_data, scheme):
    services = json_data
    services_count = {}
    status = 1

    for i in services :
        name = i['binary']
        state = i['state']
        if name not in services_count:
            services_count[name] = 0

        if name == 'nova-compute':
            if i['status'] == 'enabled':
                if state != 'up':
                    metric_data.append('%s.status 0' % scheme )
                    return metric_data

        if state == 'up':
            services_count[name] += 1

    for key in services_count:
        if (services_count[key] == 0):
            status = 0
            break

    metric_data.append('%s.status %s ' % (scheme, status))
    return metric_data


def request_token(url, method='GET', retries=2, **kwargs):
    r = None
    try:
        for i in range(retries + 1):
            if i > 0:
                time.sleep(2)
            r = requests.request(method, url, **kwargs)
            if r.status_code :
                break
    except requests.exceptions.RequestException as e:
        print("%s returned %s" % (url, e))

    return r

def request(metric_data, scheme, url, timestamp, method='GET', retries=2, **kwargs):

    start = time.time()
    r = None
    try:
        for i in range(retries + 1):
            if i > 0:
                time.sleep(2)
            r = requests.request(method, url, **kwargs)
            if r.status_code:
                break
    except requests.exceptions.RequestException as e:
        print("%s returned %s" % (url, e))

    if r is None:
        status = 0
        status_code = 600
        time_taken = -10
    elif r.status_code != 200:
        status = 0
        status_code = r.status_code
        time_taken = -5
    else:
        status = 1
        status_code = r.status_code
        time_taken = round(time.time() - start, 3)


    metric_data.append('%s.time %s %s' % (scheme, time_taken, timestamp))
    metric_data.append('%s.code %s %s' % (scheme, status_code, timestamp))
    metric_data.append('%s.status %s %s' % (scheme, status, timestamp))
    return (r, metric_data)

def main():
    timestamp = int(time.time())
    requests.packages.urllib3.disable_warnings()    

    parser = argparse.ArgumentParser()
    parser.add_argument('-u', '--user', default=os.environ['OS_USERNAME'])
    parser.add_argument('-p', '--password', default=os.environ['OS_PASSWORD'])
    parser.add_argument('-t', '--tenant', default=os.environ['OS_TENANT_NAME'])
    parser.add_argument('-a', '--auth-url', default=os.environ['OS_AUTH_URL'])
    parser.add_argument('-S', '--service-type', default='nova',
                        help='Service type must be provided')
    parser.add_argument('-s', '--scheme', default=DEFAULT_SCHEME)
    parser.add_argument('-f', '--fqdn', default=None,
                        help='fqdn must be provided for horizon')
    args = parser.parse_args()

    metric_data = []

    if len(sys.argv)==1:
        parser.print_help()
        sys.exit(STATE_WARNING)

    scheme = ''
    if args.scheme == DEFAULT_SCHEME :
        scheme = args.scheme + '.' + args.service_type + '.api'
    else:
        scheme = args.scheme + '.' + args.service_type
{'fraction_non_alphanumeric': 0.1260824385174922, 'fraction_numerical': 0.009814109225262671, 'mean_word_length': 2.4223627024891345, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '5877050', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2681, 'n_words': 705}