dev to main #8

Merged
das merged 2 commits from dev into main 2026-01-20 13:12:42 +02:00
4 changed files with 74 additions and 3 deletions
Showing only changes of commit 9727d660d1 - Show all commits

View File

@ -1,6 +1,6 @@
import os import os
APP_VERSION="2.3" APP_VERSION="2.4"
SCRIPT_PATH = os.path.dirname(__file__) SCRIPT_PATH = os.path.dirname(__file__)
CONFIGS_DIR = SCRIPT_PATH + "/configs" CONFIGS_DIR = SCRIPT_PATH + "/configs"
CONFIG_FILE_NAME = CONFIGS_DIR + "/config.json" CONFIG_FILE_NAME = CONFIGS_DIR + "/config.json"

View File

@ -49,6 +49,7 @@ def init_metric_entities(data):
M.IcmpMetric(data), M.IcmpMetric(data),
M.InterfaceMetric(data), M.InterfaceMetric(data),
M.RestValueMetric(data), M.RestValueMetric(data),
M.RestValueBMetric(data),
M.ShellValueMetric(data), M.ShellValueMetric(data),
M.UptimeMetric(app_config.UPTIME_UPDATE_SECONDS), M.UptimeMetric(app_config.UPTIME_UPDATE_SECONDS),
M.SystemMetric(app_config.SYSTEM_UPDATE_SECONDS) M.SystemMetric(app_config.SYSTEM_UPDATE_SECONDS)

View File

@ -172,6 +172,44 @@ class RestValueData(AbstractData):
self.set_collect_time(get_time_millis() - time_ms) 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): class ShellValueData(AbstractData):
g_value: Gauge g_value: Gauge
def __init__(self, name, interval, command, value=None, args=None, prefix=''): def __init__(self, name, interval, command, value=None, args=None, prefix=''):

View File

@ -12,7 +12,7 @@ import app_config
from threading import Thread from threading import Thread
from metrics.DataStructures import DiskData, HealthData, IcmpData, ENUM_UP_DN_STATES, InterfaceData, UptimeData, \ from metrics.DataStructures import DiskData, HealthData, IcmpData, ENUM_UP_DN_STATES, InterfaceData, UptimeData, \
SystemData, RestValueData, ShellValueData SystemData, RestValueData, ShellValueData, RestValueBData
class AbstractMetric: 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("'", '"')) resp = json.loads(response.content.decode().replace("'", '"'))
result = parse_response(resp, path) result = parse_response(resp, path)
if not result.isalnum(): if not str(result).isalnum():
result = 0 result = 0
if callback is not None: if callback is not None:
working_time = get_time_millis() - time_ms 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}"') 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): class ShellValueMetric(AbstractMetric):
def __init__(self, config): def __init__(self, config):
super().__init__('shell_value', config) super().__init__('shell_value', config)