diff --git a/README.md b/README.md index 88d9f04..45002e3 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,10 @@ From version 2.0 the Application supports internal metrics to collect time. See - `result_path` - path to result value in response JSON separated by `app_config.RESPONSE_PATH_SEPARATOR` character. Could be configured in [Application config](#AppConfig). - `timeout` - timeout to wait for response +#### REST value Binary Metrics +**_Gets the responses value from http request to REST service_** +THe same as [REST value Metrics] but works with 'ON/OFF' and 'TRUE/FALSE' values + #### Shell value Metrics **_Gets the shell command executed result value_** ```json diff --git a/app_config.py b/app_config.py index 25a92e2..b73d7d1 100755 --- a/app_config.py +++ b/app_config.py @@ -1,6 +1,6 @@ import os -APP_VERSION="2.3" +APP_VERSION="2.4" SCRIPT_PATH = os.path.dirname(__file__) CONFIGS_DIR = SCRIPT_PATH + "/configs" CONFIG_FILE_NAME = CONFIGS_DIR + "/config.json" diff --git a/main.py b/main.py index a47fb63..7fdd813 100755 --- a/main.py +++ b/main.py @@ -49,6 +49,7 @@ def init_metric_entities(data): M.IcmpMetric(data), M.InterfaceMetric(data), M.RestValueMetric(data), + M.RestValueBMetric(data), M.ShellValueMetric(data), M.UptimeMetric(app_config.UPTIME_UPDATE_SECONDS), M.SystemMetric(app_config.SYSTEM_UPDATE_SECONDS) diff --git a/metrics/DataStructures.py b/metrics/DataStructures.py index 30eeaab..4854d4e 100755 --- a/metrics/DataStructures.py +++ b/metrics/DataStructures.py @@ -172,6 +172,44 @@ class RestValueData(AbstractData): self.set_collect_time(get_time_millis() - time_ms) +class RestValueBData(AbstractData): + g_value: Gauge + def __init__(self, name, url, interval, timeout, value=None, method='GET', user=None, password=None, headers=None, prefix='', + result_type='single', result_path=''): + super().__init__(name, interval, prefix) + if headers is None: + headers = {} + self.url = url + self.timeout = timeout + self.method = method.upper() + self.user = user + self.password = password + self.headers = headers + self.value = value + self.type = result_type + self.path = result_path + self.g_value = get_gauge_metric('das_rest_value_b', + 'Remote REST API [name, url, method, server] Binary Value', + ['name', 'url', 'method', 'server']) + self.g_value.labels(name=name, url=url, method=method, server=self.instance_prefix) + self.set_data(value) + + def set_data(self, value, working_time = None): + time_ms = get_time_millis() + self.value = value + try: + self.g_value.labels(name=self.name, url=self.url, method=self.method, server=self.instance_prefix).set(1 if str(value).upper() in ['ON', 'TRUE'] else 0) + except: + self.g_value.labels(name=self.name, url=self.url, method=self.method, server=self.instance_prefix).set(-1) + + self.set_update_time() + self.print_trigger_info() + if working_time: + self.set_collect_time(working_time) + else: + self.set_collect_time(get_time_millis() - time_ms) + + class ShellValueData(AbstractData): g_value: Gauge def __init__(self, name, interval, command, value=None, args=None, prefix=''): diff --git a/metrics/MetricClasses.py b/metrics/MetricClasses.py index 85ad87c..3d09461 100755 --- a/metrics/MetricClasses.py +++ b/metrics/MetricClasses.py @@ -12,7 +12,7 @@ import app_config from threading import Thread from metrics.DataStructures import DiskData, HealthData, IcmpData, ENUM_UP_DN_STATES, InterfaceData, UptimeData, \ - SystemData, RestValueData, ShellValueData + SystemData, RestValueData, ShellValueData, RestValueBData class AbstractMetric: @@ -70,7 +70,7 @@ def get_rest_value(url, timeout, method, user, pwd, headers, callback=None, resu ) resp = json.loads(response.content.decode().replace("'", '"')) result = parse_response(resp, path) - if not result.isalnum(): + if not str(result).isalnum(): result = 0 if callback is not None: working_time = get_time_millis() - time_ms @@ -265,6 +265,38 @@ class RestValueMetric(AbstractMetric): print(f'[DEBUG] (next update at {get_next_update_time(d)}) on {d.url}: by {d.method} in {d.path} got value="{d.value}"') +class RestValueBMetric(AbstractMetric): + def __init__(self, config): + super().__init__('rest_value_b', config) + for d in self.config: + name, url, interval, timeout, method = d['name'], d['url'], d['interval'], d['timeout'], d['method'] + if 'auth' in self.config: + user = d['auth']['user'] + pwd = d['auth']['pass'] + else: + user = '' + pwd = '' + if 'headers' in self.config: + headers = d['headers'] + else: + headers = '' + result_type, result_path = d['result_type'], d['result_path'] + result = get_rest_value(url=url, timeout=timeout, method=method, user=user, pwd=pwd, headers=headers, + result_type=result_type, path=result_path) + self.data_array.append(RestValueBData(name, url, interval, timeout, result, method, user, pwd, headers, self.prefix, result_type, result_path)) + + def proceed_metric(self): + for d in self.data_array: + if d.is_need_to_update(): + thread = Thread(target=get_rest_value, args=(d.url, d.timeout, d.method, d.user, d.password, d.headers, + d.set_data, d.type, d.path)) + thread.start() + + def print_debug_info(self): + for d in self.data_array: + print(f'[DEBUG] (next update at {get_next_update_time(d)}) on {d.url}: by {d.method} in {d.path} got value="{d.value}"') + + class ShellValueMetric(AbstractMetric): def __init__(self, config): super().__init__('shell_value', config)