2017-12-04 08:41:00 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-09-22 10:47:16 +00:00
|
|
|
from itertools import groupby
|
2018-12-19 02:49:30 +00:00
|
|
|
|
2021-12-08 07:35:22 +00:00
|
|
|
from common.utils import get_logger
|
2022-12-05 08:07:14 +00:00
|
|
|
from terminal.const import ComponentLoad
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
2017-12-12 04:19:45 +00:00
|
|
|
|
|
|
|
|
2022-11-04 03:40:16 +00:00
|
|
|
class ComputeLoadUtil:
|
2021-03-26 11:09:34 +00:00
|
|
|
# system status
|
2020-12-15 05:00:59 +00:00
|
|
|
@staticmethod
|
2021-03-26 11:09:34 +00:00
|
|
|
def _common_compute_system_status(value, thresholds):
|
|
|
|
if thresholds[0] <= value <= thresholds[1]:
|
2022-12-05 08:07:14 +00:00
|
|
|
return ComponentLoad.normal.value
|
2021-03-26 11:09:34 +00:00
|
|
|
elif thresholds[1] < value <= thresholds[2]:
|
2022-12-05 08:07:14 +00:00
|
|
|
return ComponentLoad.high.value
|
2021-03-26 11:09:34 +00:00
|
|
|
else:
|
2022-12-05 08:07:14 +00:00
|
|
|
return ComponentLoad.critical.value
|
2021-03-26 11:09:34 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _compute_system_stat_status(cls, stat):
|
|
|
|
system_stat_thresholds_mapper = {
|
|
|
|
'cpu_load': [0, 5, 20],
|
|
|
|
'memory_used': [0, 85, 95],
|
|
|
|
'disk_used': [0, 80, 99]
|
|
|
|
}
|
|
|
|
system_status = {}
|
|
|
|
for stat_key, thresholds in system_stat_thresholds_mapper.items():
|
|
|
|
stat_value = getattr(stat, stat_key)
|
|
|
|
if stat_value is None:
|
|
|
|
msg = 'stat: {}, stat_key: {}, stat_value: {}'
|
|
|
|
logger.debug(msg.format(stat, stat_key, stat_value))
|
|
|
|
stat_value = 0
|
|
|
|
status = cls._common_compute_system_status(stat_value, thresholds)
|
|
|
|
system_status[stat_key] = status
|
|
|
|
return system_status
|
|
|
|
|
|
|
|
@classmethod
|
2022-11-04 03:40:16 +00:00
|
|
|
def compute_load(cls, stat):
|
2023-04-06 02:31:41 +00:00
|
|
|
if not stat:
|
2022-12-05 08:07:14 +00:00
|
|
|
return ComponentLoad.offline
|
2021-03-26 11:09:34 +00:00
|
|
|
system_status_values = cls._compute_system_stat_status(stat).values()
|
2022-12-05 08:07:14 +00:00
|
|
|
if ComponentLoad.critical in system_status_values:
|
|
|
|
return ComponentLoad.critical
|
|
|
|
elif ComponentLoad.high in system_status_values:
|
|
|
|
return ComponentLoad.high
|
2021-03-26 11:09:34 +00:00
|
|
|
else:
|
2022-12-05 08:07:14 +00:00
|
|
|
return ComponentLoad.normal
|
2021-03-26 11:09:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TypedComponentsStatusMetricsUtil(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.components = []
|
|
|
|
self.grouped_components = []
|
|
|
|
self.get_components()
|
|
|
|
|
|
|
|
def get_components(self):
|
2022-09-22 10:47:16 +00:00
|
|
|
from ..models import Terminal
|
2020-12-17 09:31:44 +00:00
|
|
|
components = Terminal.objects.filter(is_deleted=False).order_by('type')
|
2021-03-26 11:09:34 +00:00
|
|
|
grouped_components = groupby(components, lambda c: c.type)
|
|
|
|
grouped_components = [(i[0], list(i[1])) for i in grouped_components]
|
|
|
|
self.grouped_components = grouped_components
|
|
|
|
self.components = components
|
|
|
|
|
|
|
|
def get_metrics(self):
|
|
|
|
metrics = []
|
|
|
|
for _tp, components in self.grouped_components:
|
2022-11-04 03:40:16 +00:00
|
|
|
metric = {
|
2023-08-08 10:45:10 +00:00
|
|
|
'total': 0,
|
|
|
|
'type': _tp,
|
|
|
|
'session_active': 0,
|
|
|
|
ComponentLoad.high: [],
|
|
|
|
ComponentLoad.normal: [],
|
|
|
|
ComponentLoad.offline: [],
|
|
|
|
ComponentLoad.critical: [],
|
2022-11-04 03:40:16 +00:00
|
|
|
}
|
2021-03-26 11:09:34 +00:00
|
|
|
for component in components:
|
2023-08-08 10:45:10 +00:00
|
|
|
metric[component.load].append(component.name)
|
2022-11-04 03:40:16 +00:00
|
|
|
metric['total'] += 1
|
|
|
|
metric['session_active'] += component.get_online_session_count()
|
|
|
|
metrics.append(metric)
|
2021-03-26 11:09:34 +00:00
|
|
|
return metrics
|
|
|
|
|
|
|
|
|
|
|
|
class ComponentsPrometheusMetricsUtil(TypedComponentsStatusMetricsUtil):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.metrics = self.get_metrics()
|
2020-12-10 12:50:22 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2020-12-15 05:00:59 +00:00
|
|
|
def convert_status_metrics(metrics):
|
2020-12-10 12:50:22 +00:00
|
|
|
return {
|
|
|
|
'any': metrics['total'],
|
|
|
|
'normal': metrics['normal'],
|
|
|
|
'high': metrics['high'],
|
2020-12-15 07:46:02 +00:00
|
|
|
'critical': metrics['critical'],
|
|
|
|
'offline': metrics['offline']
|
2020-12-10 12:50:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
def get_component_status_metrics(self):
|
2020-12-15 05:00:59 +00:00
|
|
|
prometheus_metrics = list()
|
|
|
|
# 各组件状态个数汇总
|
|
|
|
prometheus_metrics.append('# JumpServer 各组件状态个数汇总')
|
|
|
|
status_metric_text = 'jumpserver_components_status_total{component_type="%s", status="%s"} %s'
|
2021-03-26 11:09:34 +00:00
|
|
|
for metric in self.metrics:
|
|
|
|
tp = metric['type']
|
2020-12-15 05:00:59 +00:00
|
|
|
prometheus_metrics.append(f'## 组件: {tp}')
|
2021-03-26 11:09:34 +00:00
|
|
|
status_metrics = self.convert_status_metrics(metric)
|
2020-12-10 12:50:22 +00:00
|
|
|
for status, value in status_metrics.items():
|
2020-12-15 05:00:59 +00:00
|
|
|
metric_text = status_metric_text % (tp, status, value)
|
2020-12-10 12:50:22 +00:00
|
|
|
prometheus_metrics.append(metric_text)
|
2021-03-26 11:09:34 +00:00
|
|
|
return prometheus_metrics
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
def get_component_session_metrics(self):
|
|
|
|
prometheus_metrics = list()
|
2020-12-15 05:00:59 +00:00
|
|
|
# 各组件在线会话数汇总
|
2020-12-10 12:50:22 +00:00
|
|
|
prometheus_metrics.append('# JumpServer 各组件在线会话数汇总')
|
2020-12-15 05:00:59 +00:00
|
|
|
session_active_metric_text = 'jumpserver_components_session_active_total{component_type="%s"} %s'
|
2021-03-26 11:09:34 +00:00
|
|
|
|
|
|
|
for metric in self.metrics:
|
|
|
|
tp = metric['type']
|
2020-12-15 05:00:59 +00:00
|
|
|
prometheus_metrics.append(f'## 组件: {tp}')
|
2021-03-26 11:09:34 +00:00
|
|
|
metric_text = session_active_metric_text % (tp, metric['session_active'])
|
2020-12-10 12:50:22 +00:00
|
|
|
prometheus_metrics.append(metric_text)
|
2021-03-26 11:09:34 +00:00
|
|
|
return prometheus_metrics
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
def get_component_stat_metrics(self):
|
|
|
|
prometheus_metrics = list()
|
2020-12-15 05:00:59 +00:00
|
|
|
# 各组件节点指标
|
|
|
|
prometheus_metrics.append('# JumpServer 各组件一些指标')
|
|
|
|
state_metric_text = 'jumpserver_components_%s{component_type="%s", component="%s"} %s'
|
2021-03-26 11:09:34 +00:00
|
|
|
stats_key = [
|
|
|
|
'cpu_load', 'memory_used', 'disk_used', 'session_online'
|
|
|
|
]
|
|
|
|
old_stats_key = [
|
2020-12-10 12:50:22 +00:00
|
|
|
'system_cpu_load_1', 'system_memory_used_percent',
|
|
|
|
'system_disk_used_percent', 'session_active_count'
|
|
|
|
]
|
2021-03-26 11:09:34 +00:00
|
|
|
old_stats_key_mapper = dict(zip(stats_key, old_stats_key))
|
|
|
|
|
|
|
|
for stat_key in stats_key:
|
|
|
|
prometheus_metrics.append(f'## 指标: {stat_key}')
|
|
|
|
for component in self.components:
|
2020-12-10 12:50:22 +00:00
|
|
|
if not component.is_alive:
|
|
|
|
continue
|
2023-05-08 06:37:43 +00:00
|
|
|
component_stat = component.last_stat
|
2021-03-26 11:09:34 +00:00
|
|
|
if not component_stat:
|
|
|
|
continue
|
2020-12-15 05:00:59 +00:00
|
|
|
metric_text = state_metric_text % (
|
2021-03-26 11:09:34 +00:00
|
|
|
stat_key, component.type, component.name, getattr(component_stat, stat_key)
|
2020-12-10 12:50:22 +00:00
|
|
|
)
|
|
|
|
prometheus_metrics.append(metric_text)
|
2021-03-26 11:09:34 +00:00
|
|
|
old_stat_key = old_stats_key_mapper.get(stat_key)
|
|
|
|
old_metric_text = state_metric_text % (
|
|
|
|
old_stat_key, component.type, component.name, getattr(component_stat, stat_key)
|
|
|
|
)
|
|
|
|
prometheus_metrics.append(old_metric_text)
|
|
|
|
return prometheus_metrics
|
2020-12-10 12:50:22 +00:00
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
def get_prometheus_metrics_text(self):
|
|
|
|
prometheus_metrics = list()
|
|
|
|
for method in [
|
|
|
|
self.get_component_status_metrics,
|
|
|
|
self.get_component_session_metrics,
|
|
|
|
self.get_component_stat_metrics
|
|
|
|
]:
|
|
|
|
prometheus_metrics.extend(method())
|
|
|
|
prometheus_metrics.append('\n')
|
2020-12-10 12:50:22 +00:00
|
|
|
prometheus_metrics_text = '\n'.join(prometheus_metrics)
|
|
|
|
return prometheus_metrics_text
|