Compare commits

..

No commits in common. "main" and "v.2.1" have entirely different histories.
main ... v.2.1

4 changed files with 34 additions and 59 deletions

View File

@ -1,6 +1,6 @@
import os import os
APP_VERSION="2.3" APP_VERSION="2.1"
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"

12
main.py
View File

@ -44,12 +44,12 @@ def parse_config(cfg):
def init_metric_entities(data): def init_metric_entities(data):
return { return {
M.DiskMetric(data), M.DiskMetric(data, app_config.INSTANCE_PREFIX),
M.HealthMetric(data), M.HealthMetric(data, app_config.INSTANCE_PREFIX),
M.IcmpMetric(data), M.IcmpMetric(data, app_config.INSTANCE_PREFIX),
M.InterfaceMetric(data), M.InterfaceMetric(data, app_config.INSTANCE_PREFIX),
M.RestValueMetric(data), M.RestValueMetric(data, app_config.INSTANCE_PREFIX),
M.ShellValueMetric(data), M.ShellValueMetric(data, app_config.INSTANCE_PREFIX),
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

@ -122,16 +122,13 @@ class HealthData(AbstractData):
self.e_state.labels(name=name, url=url, method=method, server=self.instance_prefix) self.e_state.labels(name=name, url=url, method=method, server=self.instance_prefix)
self.set_data(is_up) self.set_data(is_up)
def set_data(self, is_up, working_time = None): def set_data(self, is_up):
time_ms = get_time_millis() time_ms = get_time_millis()
self.is_up = is_up self.is_up = is_up
self.e_state.labels(name=self.name, url=self.url, method=self.method, server=self.instance_prefix).state(ENUM_UP_DN_STATES[0] if is_up else ENUM_UP_DN_STATES[1]) self.e_state.labels(name=self.name, url=self.url, method=self.method, server=self.instance_prefix).state(ENUM_UP_DN_STATES[0] if is_up else ENUM_UP_DN_STATES[1])
self.set_collect_time(get_time_millis() - time_ms)
self.set_update_time() self.set_update_time()
self.print_trigger_info() self.print_trigger_info()
if working_time:
self.set_collect_time(working_time)
else:
self.set_collect_time(get_time_millis() - time_ms)
class RestValueData(AbstractData): class RestValueData(AbstractData):
@ -156,7 +153,7 @@ class RestValueData(AbstractData):
self.g_value.labels(name=name, url=url, method=method, server=self.instance_prefix) self.g_value.labels(name=name, url=url, method=method, server=self.instance_prefix)
self.set_data(value) self.set_data(value)
def set_data(self, value, working_time = None): def set_data(self, value):
time_ms = get_time_millis() time_ms = get_time_millis()
self.value = value self.value = value
try: try:
@ -164,12 +161,9 @@ class RestValueData(AbstractData):
except: except:
self.g_value.labels(name=self.name, url=self.url, method=self.method, server=self.instance_prefix).set(0) self.g_value.labels(name=self.name, url=self.url, method=self.method, server=self.instance_prefix).set(0)
self.set_collect_time(get_time_millis() - time_ms)
self.set_update_time() self.set_update_time()
self.print_trigger_info() 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):
@ -187,7 +181,7 @@ class ShellValueData(AbstractData):
self.g_value.labels(name=name, command=command, server=self.instance_prefix) self.g_value.labels(name=name, command=command, server=self.instance_prefix)
self.set_data(value) self.set_data(value)
def set_data(self, value, working_time = None): def set_data(self, value):
time_ms = get_time_millis() time_ms = get_time_millis()
self.value = value self.value = value
try: try:
@ -195,12 +189,9 @@ class ShellValueData(AbstractData):
except: except:
self.g_value.labels(name=self.name, command=self.command, server=self.instance_prefix).set(0) self.g_value.labels(name=self.name, command=self.command, server=self.instance_prefix).set(0)
self.set_collect_time(get_time_millis() - time_ms)
self.set_update_time() self.set_update_time()
self.print_trigger_info() self.print_trigger_info()
if working_time:
self.set_collect_time(working_time)
else:
self.set_collect_time(get_time_millis() - time_ms)
class IcmpData(AbstractData): class IcmpData(AbstractData):
@ -216,16 +207,13 @@ class IcmpData(AbstractData):
self.e_state.labels(name=name, ip=ip, server=self.instance_prefix) self.e_state.labels(name=name, ip=ip, server=self.instance_prefix)
self.set_data(is_up) self.set_data(is_up)
def set_data(self, is_up, working_time = None): def set_data(self, is_up):
time_ms = get_time_millis() time_ms = get_time_millis()
self.is_up = is_up self.is_up = is_up
self.e_state.labels(name=self.name, ip=self.ip, server=self.instance_prefix).state(ENUM_UP_DN_STATES[0] if is_up else ENUM_UP_DN_STATES[1]) self.e_state.labels(name=self.name, ip=self.ip, server=self.instance_prefix).state(ENUM_UP_DN_STATES[0] if is_up else ENUM_UP_DN_STATES[1])
self.set_collect_time(get_time_millis() - time_ms)
self.set_update_time() self.set_update_time()
self.print_trigger_info() self.print_trigger_info()
if working_time:
self.set_collect_time(working_time)
else:
self.set_collect_time(get_time_millis() - time_ms)
class InterfaceData(AbstractData): class InterfaceData(AbstractData):
@ -261,7 +249,7 @@ class UptimeData(AbstractData):
def __init__(self, interval, prefix=''): def __init__(self, interval, prefix=''):
super().__init__('uptime', interval, prefix) super().__init__('uptime', interval, prefix)
self.uptime = 0 self.uptime = 0
self.c_uptime = get_counter_metric('das_exporter_uptime', self.c_uptime = get_counter_metric('das_exporter',
'Exporter Uptime for [server] in seconds', 'Exporter Uptime for [server] in seconds',
['server']) ['server'])
self.c_uptime.labels(server=self.instance_prefix) self.c_uptime.labels(server=self.instance_prefix)

View File

@ -18,10 +18,8 @@ from metrics.DataStructures import DiskData, HealthData, IcmpData, ENUM_UP_DN_ST
class AbstractMetric: class AbstractMetric:
metric_key = "" metric_key = ""
config = {} config = {}
prefix = ""
def __init__(self, key, config): def __init__(self, key, config):
self.metric_key = key self.metric_key = key
self.prefix = app_config.INSTANCE_PREFIX
if key and key in config: if key and key in config:
self.config = config[key] self.config = config[key]
self.data_array = [] self.data_array = []
@ -36,7 +34,6 @@ class AbstractMetric:
def is_health_check(url, timeout, method, user, pwd, headers, callback=None): def is_health_check(url, timeout, method, user, pwd, headers, callback=None):
time_ms = get_time_millis()
session = requests.Session() session = requests.Session()
if user and pwd: if user and pwd:
session.auth = (user, pwd) session.auth = (user, pwd)
@ -49,15 +46,13 @@ def is_health_check(url, timeout, method, user, pwd, headers, callback=None):
) )
result = response.status_code == 200 result = response.status_code == 200
if callback is not None: if callback is not None:
working_time = get_time_millis() - time_ms callback(result)
callback(result, working_time)
else: else:
return result return result
except (requests.ConnectTimeout, requests.exceptions.ConnectionError) as e: except (requests.ConnectTimeout, requests.exceptions.ConnectionError) as e:
return False return False
def get_rest_value(url, timeout, method, user, pwd, headers, callback=None, result_type='single', path=''): def get_rest_value(url, timeout, method, user, pwd, headers, callback=None, result_type='single', path=''):
time_ms = get_time_millis()
session = requests.Session() session = requests.Session()
if user and pwd: if user and pwd:
session.auth = (user, pwd) session.auth = (user, pwd)
@ -73,8 +68,7 @@ def get_rest_value(url, timeout, method, user, pwd, headers, callback=None, resu
if not result.isalnum(): if not result.isalnum():
result = 0 result = 0
if callback is not None: if callback is not None:
working_time = get_time_millis() - time_ms callback(result)
callback(result, working_time)
else: else:
return result return result
except (requests.ConnectTimeout, requests.exceptions.ConnectionError) as e: except (requests.ConnectTimeout, requests.exceptions.ConnectionError) as e:
@ -102,7 +96,6 @@ def parse_response(resp, path):
return '' return ''
def get_shell_value(command, args, callback=None): def get_shell_value(command, args, callback=None):
time_ms = get_time_millis()
cmd = [command, ' '.join(str(s) for s in args)] cmd = [command, ' '.join(str(s) for s in args)]
try: try:
output = subprocess.check_output(cmd) output = subprocess.check_output(cmd)
@ -114,13 +107,11 @@ def get_shell_value(command, args, callback=None):
result = 0 result = 0
if callback is not None: if callback is not None:
working_time = get_time_millis() - time_ms callback(result)
callback(result, working_time)
else: else:
return result return result
def is_ping(ip, count, callback=None): def is_ping(ip, count, callback=None):
time_ms = get_time_millis()
param = '-n' if platform.system().lower() == 'windows' else '-c' param = '-n' if platform.system().lower() == 'windows' else '-c'
command = ['ping', param, str(count), ip] command = ['ping', param, str(count), ip]
try: try:
@ -130,10 +121,8 @@ def is_ping(ip, count, callback=None):
'time out'.upper() not in str(output).upper()) 'time out'.upper() not in str(output).upper())
except: except:
result = False result = False
if callback is not None: if callback is not None:
working_time = get_time_millis() - time_ms callback(result)
callback(result, working_time)
else: else:
return result return result
@ -143,16 +132,14 @@ def get_net_iface_stat(name):
def get_next_update_time(d): def get_next_update_time(d):
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(d.updated_at + d.interval)) return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(d.updated_at + d.interval))
def get_time_millis():
return round(time.time() * 1000)
class DiskMetric(AbstractMetric): class DiskMetric(AbstractMetric):
def __init__(self, config): def __init__(self, config, prefix=''):
super().__init__('disk', config) super().__init__('disk', config)
for d in self.config: for d in self.config:
mount_point, interval, name = d['path'], d['interval'], d['name'] mount_point, interval, name = d['path'], d['interval'], d['name']
total, used, free = shutil.disk_usage(mount_point) total, used, free = shutil.disk_usage(mount_point)
self.data_array.append(DiskData(mount_point, total, used, free, interval, name, self.prefix)) self.data_array.append(DiskData(mount_point, total, used, free, interval, name, prefix))
def proceed_metric(self): def proceed_metric(self):
for d in self.data_array: for d in self.data_array:
@ -167,7 +154,7 @@ class DiskMetric(AbstractMetric):
class HealthMetric(AbstractMetric): class HealthMetric(AbstractMetric):
def __init__(self, config): def __init__(self, config, prefix=''):
super().__init__('health', config) super().__init__('health', config)
for d in self.config: for d in self.config:
name, url, interval, timeout, method = d['name'], d['url'], d['interval'], d['timeout'], d['method'] name, url, interval, timeout, method = d['name'], d['url'], d['interval'], d['timeout'], d['method']
@ -182,7 +169,7 @@ class HealthMetric(AbstractMetric):
else: else:
headers = '' headers = ''
result = is_health_check(url, timeout, method, user, pwd, headers) result = is_health_check(url, timeout, method, user, pwd, headers)
self.data_array.append(HealthData(name, url, interval, timeout, result, method, user, pwd, headers, self.prefix)) self.data_array.append(HealthData(name, url, interval, timeout, result, method, user, pwd, headers, prefix))
def proceed_metric(self): def proceed_metric(self):
for d in self.data_array: for d in self.data_array:
@ -196,12 +183,12 @@ class HealthMetric(AbstractMetric):
class IcmpMetric(AbstractMetric): class IcmpMetric(AbstractMetric):
def __init__(self, config): def __init__(self, config, prefix=''):
super().__init__('ping', config) super().__init__('ping', config)
for d in self.config: for d in self.config:
name, ip, count, interval = d['name'], d['ip'], d['count'], d['interval'] name, ip, count, interval = d['name'], d['ip'], d['count'], d['interval']
result = is_ping(ip, count) result = is_ping(ip, count)
self.data_array.append(IcmpData(name, ip, count, interval, result, self.prefix)) self.data_array.append(IcmpData(name, ip, count, interval, result, prefix))
def proceed_metric(self): def proceed_metric(self):
for d in self.data_array: for d in self.data_array:
@ -215,12 +202,12 @@ class IcmpMetric(AbstractMetric):
class InterfaceMetric(AbstractMetric): class InterfaceMetric(AbstractMetric):
def __init__(self, config): def __init__(self, config, prefix=''):
super().__init__('iface', config) super().__init__('iface', config)
for d in self.config: for d in self.config:
name, iface, interval = d['name'], d['iface'], d['interval'] name, iface, interval = d['name'], d['iface'], d['interval']
result = get_net_iface_stat(iface) result = get_net_iface_stat(iface)
self.data_array.append(InterfaceData(name, iface, interval, result.bytes_sent, result.bytes_recv, self.prefix)) self.data_array.append(InterfaceData(name, iface, interval, result.bytes_sent, result.bytes_recv, prefix))
def proceed_metric(self): def proceed_metric(self):
for d in self.data_array: for d in self.data_array:
@ -234,7 +221,7 @@ class InterfaceMetric(AbstractMetric):
class RestValueMetric(AbstractMetric): class RestValueMetric(AbstractMetric):
def __init__(self, config): def __init__(self, config, prefix=''):
super().__init__('rest_value', config) super().__init__('rest_value', config)
for d in self.config: for d in self.config:
name, url, interval, timeout, method = d['name'], d['url'], d['interval'], d['timeout'], d['method'] name, url, interval, timeout, method = d['name'], d['url'], d['interval'], d['timeout'], d['method']
@ -251,7 +238,7 @@ class RestValueMetric(AbstractMetric):
result_type, result_path = d['result_type'], d['result_path'] 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 = get_rest_value(url=url, timeout=timeout, method=method, user=user, pwd=pwd, headers=headers,
result_type=result_type, path=result_path) result_type=result_type, path=result_path)
self.data_array.append(RestValueData(name, url, interval, timeout, result, method, user, pwd, headers, self.prefix, result_type, result_path)) self.data_array.append(RestValueData(name, url, interval, timeout, result, method, user, pwd, headers, prefix, result_type, result_path))
def proceed_metric(self): def proceed_metric(self):
for d in self.data_array: for d in self.data_array:
@ -266,12 +253,12 @@ class RestValueMetric(AbstractMetric):
class ShellValueMetric(AbstractMetric): class ShellValueMetric(AbstractMetric):
def __init__(self, config): def __init__(self, config, prefix=''):
super().__init__('shell_value', config) super().__init__('shell_value', config)
for d in self.config: for d in self.config:
name, command, interval, args = d['name'], d['command'], d['interval'], d['args'] name, command, interval, args = d['name'], d['command'], d['interval'], d['args']
result = get_shell_value(command, args) result = get_shell_value(command, args)
self.data_array.append(ShellValueData(name, interval, command, result, args, self.prefix)) self.data_array.append(ShellValueData(name, interval, command, result, args, prefix))
def proceed_metric(self): def proceed_metric(self):
for d in self.data_array: for d in self.data_array:
@ -287,7 +274,7 @@ class ShellValueMetric(AbstractMetric):
class UptimeMetric(AbstractMetric): class UptimeMetric(AbstractMetric):
def __init__(self, interval): def __init__(self, interval):
super().__init__(None, {}) super().__init__(None, {})
self.data_array.append(UptimeData(interval, self.prefix)) self.data_array.append(UptimeData(interval))
def proceed_metric(self): def proceed_metric(self):
for d in self.data_array: for d in self.data_array:
@ -302,7 +289,7 @@ class UptimeMetric(AbstractMetric):
class SystemMetric(AbstractMetric): class SystemMetric(AbstractMetric):
def __init__(self, interval): def __init__(self, interval):
super().__init__(None, {}) super().__init__(None, {})
self.data_array.append(SystemData(interval, self.prefix)) self.data_array.append(SystemData(interval, app_config.INSTANCE_PREFIX))
def proceed_metric(self): def proceed_metric(self):
for d in self.data_array: for d in self.data_array: