mirror of https://github.com/jumpserver/jumpserver
fix: 修复prometheus_metricsAPI数据获取bug;修复组件注册type为空bug (#5253)
* fix: 修复prometheus_metricsAPI数据获取bug;修复组件注册type为空bug * fix: 修改审计migrations/userloginlog/backend的verbose_name字段 Co-authored-by: Bai <bugatti_it@163.com>pull/5255/head
parent
0813cff74f
commit
6e0fbd78e7
|
@ -13,6 +13,6 @@ class Migration(migrations.Migration):
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name='userloginlog',
|
model_name='userloginlog',
|
||||||
name='backend',
|
name='backend',
|
||||||
field=models.CharField(default='', max_length=32, verbose_name='Login backend'),
|
field=models.CharField(default='', max_length=32, verbose_name='Authentication backend'),
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -28,7 +28,7 @@ class ComponentsMetricsAPIView(generics.GenericAPIView):
|
||||||
permission_classes = (IsSuperUser,)
|
permission_classes = (IsSuperUser,)
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
component_type = request.query_params.get('type')
|
tp = request.query_params.get('type')
|
||||||
util = ComponentsMetricsUtil(component_type)
|
util = ComponentsMetricsUtil()
|
||||||
metrics = util.get_metrics()
|
metrics = util.get_metrics(tp)
|
||||||
return Response(metrics, status=status.HTTP_200_OK)
|
return Response(metrics, status=status.HTTP_200_OK)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# Generated by Django 3.1 on 2020-12-10 07:05
|
# Generated by Django 3.1 on 2020-12-15 04:52
|
||||||
|
|
||||||
from django.db import migrations, models
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
@ -36,7 +36,6 @@ class Migration(migrations.Migration):
|
||||||
model_name='terminal',
|
model_name='terminal',
|
||||||
name='type',
|
name='type',
|
||||||
field=models.CharField(choices=[('koko', 'KoKo'), ('guacamole', 'Guacamole'), ('omnidb', 'OmniDB')], default='koko', max_length=64, verbose_name='type'),
|
field=models.CharField(choices=[('koko', 'KoKo'), ('guacamole', 'Guacamole'), ('omnidb', 'OmniDB')], default='koko', max_length=64, verbose_name='type'),
|
||||||
preserve_default=False,
|
|
||||||
),
|
),
|
||||||
migrations.RunPython(migrate_terminal_type)
|
migrations.RunPython(migrate_terminal_type)
|
||||||
]
|
]
|
||||||
|
|
|
@ -129,7 +129,10 @@ class TerminalStatusMixin(TerminalStateMixin):
|
||||||
class Terminal(TerminalStatusMixin, models.Model):
|
class Terminal(TerminalStatusMixin, models.Model):
|
||||||
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
||||||
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
||||||
type = models.CharField(choices=const.TerminalTypeChoices.choices, max_length=64, verbose_name=_('type'))
|
type = models.CharField(
|
||||||
|
choices=const.TerminalTypeChoices.choices, default=const.TerminalTypeChoices.koko.value,
|
||||||
|
max_length=64, verbose_name=_('type')
|
||||||
|
)
|
||||||
remote_addr = models.CharField(max_length=128, blank=True, verbose_name=_('Remote Address'))
|
remote_addr = models.CharField(max_length=128, blank=True, verbose_name=_('Remote Address'))
|
||||||
ssh_port = models.IntegerField(verbose_name=_('SSH Port'), default=2222)
|
ssh_port = models.IntegerField(verbose_name=_('SSH Port'), default=2222)
|
||||||
http_port = models.IntegerField(verbose_name=_('HTTP Port'), default=5000)
|
http_port = models.IntegerField(verbose_name=_('HTTP Port'), default=5000)
|
||||||
|
|
|
@ -18,7 +18,7 @@ class TerminalSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Terminal
|
model = Terminal
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'name', 'remote_addr', 'command_storage',
|
'id', 'name', 'type', 'remote_addr', 'command_storage',
|
||||||
'replay_storage', 'user', 'is_accepted', 'is_deleted',
|
'replay_storage', 'user', 'is_accepted', 'is_deleted',
|
||||||
'date_created', 'comment'
|
'date_created', 'comment'
|
||||||
]
|
]
|
||||||
|
@ -55,5 +55,6 @@ class TerminalSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
class TerminalRegistrationSerializer(serializers.Serializer):
|
class TerminalRegistrationSerializer(serializers.Serializer):
|
||||||
name = serializers.CharField(max_length=128)
|
name = serializers.CharField(max_length=128)
|
||||||
comment = serializers.CharField(max_length=128, )
|
comment = serializers.CharField(max_length=128)
|
||||||
|
type = serializers.CharField(max_length=64)
|
||||||
service_account = ServiceAccountSerializer(read_only=True)
|
service_account = ServiceAccountSerializer(read_only=True)
|
||||||
|
|
|
@ -106,46 +106,43 @@ def send_command_alert_mail(command):
|
||||||
|
|
||||||
class ComponentsMetricsUtil(object):
|
class ComponentsMetricsUtil(object):
|
||||||
|
|
||||||
def __init__(self, component_type=None):
|
@staticmethod
|
||||||
self.type = component_type
|
def get_components(tp=None):
|
||||||
self.components = []
|
|
||||||
self.initial_components()
|
|
||||||
|
|
||||||
def initial_components(self):
|
|
||||||
from .models import Terminal
|
from .models import Terminal
|
||||||
terminals = Terminal.objects.all().order_by('type')
|
components = Terminal.objects.all().order_by('type')
|
||||||
if self.type:
|
if tp:
|
||||||
terminals = terminals.filter(type=self.type)
|
components = components.filter(type=tp)
|
||||||
self.components = list(terminals)
|
return components
|
||||||
|
|
||||||
def get_metrics(self):
|
def get_metrics(self, tp=None):
|
||||||
|
components = self.get_components(tp)
|
||||||
total_count = normal_count = high_count = critical_count = session_active_total = 0
|
total_count = normal_count = high_count = critical_count = session_active_total = 0
|
||||||
for component in self.components:
|
for component in components:
|
||||||
total_count += 1
|
total_count += 1
|
||||||
if not component.is_alive:
|
if component.is_alive:
|
||||||
critical_count += 1
|
|
||||||
continue
|
|
||||||
session_active_total += component.state.get('session_active_count', 0)
|
|
||||||
if component.is_normal:
|
if component.is_normal:
|
||||||
normal_count += 1
|
normal_count += 1
|
||||||
elif component.is_high:
|
elif component.is_high:
|
||||||
high_count += 1
|
high_count += 1
|
||||||
else:
|
else:
|
||||||
|
# critical
|
||||||
critical_count += 1
|
critical_count += 1
|
||||||
metrics = {
|
session_active_total += component.state.get('session_active_count', 0)
|
||||||
|
else:
|
||||||
|
critical_count += 1
|
||||||
|
return {
|
||||||
'total': total_count,
|
'total': total_count,
|
||||||
'normal': normal_count,
|
'normal': normal_count,
|
||||||
'high': high_count,
|
'high': high_count,
|
||||||
'critical': critical_count,
|
'critical': critical_count,
|
||||||
'session_active': session_active_total
|
'session_active': session_active_total
|
||||||
}
|
}
|
||||||
return metrics
|
|
||||||
|
|
||||||
|
|
||||||
class ComponentsPrometheusMetricsUtil(ComponentsMetricsUtil):
|
class ComponentsPrometheusMetricsUtil(ComponentsMetricsUtil):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_status_metrics(metrics):
|
def convert_status_metrics(metrics):
|
||||||
return {
|
return {
|
||||||
'any': metrics['total'],
|
'any': metrics['total'],
|
||||||
'normal': metrics['normal'],
|
'normal': metrics['normal'],
|
||||||
|
@ -154,50 +151,47 @@ class ComponentsPrometheusMetricsUtil(ComponentsMetricsUtil):
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_prometheus_metrics_text(self):
|
def get_prometheus_metrics_text(self):
|
||||||
prometheus_metrics = []
|
prometheus_metrics = list()
|
||||||
|
|
||||||
|
# 各组件状态个数汇总
|
||||||
prometheus_metrics.append('# JumpServer 各组件状态个数汇总')
|
prometheus_metrics.append('# JumpServer 各组件状态个数汇总')
|
||||||
base_status_metric_text = 'jumpserver_components_status_total{component_type="%s", status="%s"} %s'
|
status_metric_text = 'jumpserver_components_status_total{component_type="%s", status="%s"} %s'
|
||||||
for component in self.components:
|
for tp in const.TerminalTypeChoices.types():
|
||||||
component_type = component.type
|
prometheus_metrics.append(f'## 组件: {tp}')
|
||||||
base_metrics = self.get_metrics()
|
metrics_tp = self.get_metrics(tp)
|
||||||
|
status_metrics = self.convert_status_metrics(metrics_tp)
|
||||||
prometheus_metrics.append(f'## 组件: {component_type}')
|
|
||||||
status_metrics = self.get_status_metrics(base_metrics)
|
|
||||||
for status, value in status_metrics.items():
|
for status, value in status_metrics.items():
|
||||||
metric_text = base_status_metric_text % (component_type, status, value)
|
metric_text = status_metric_text % (tp, status, value)
|
||||||
prometheus_metrics.append(metric_text)
|
prometheus_metrics.append(metric_text)
|
||||||
|
|
||||||
prometheus_metrics.append('\n')
|
prometheus_metrics.append('\n')
|
||||||
|
|
||||||
|
# 各组件在线会话数汇总
|
||||||
prometheus_metrics.append('# JumpServer 各组件在线会话数汇总')
|
prometheus_metrics.append('# JumpServer 各组件在线会话数汇总')
|
||||||
base_session_active_metric_text = 'jumpserver_components_session_active_total{component_type="%s"} %s'
|
session_active_metric_text = 'jumpserver_components_session_active_total{component_type="%s"} %s'
|
||||||
for component in self.components:
|
for tp in const.TerminalTypeChoices.types():
|
||||||
component_type = component.type
|
prometheus_metrics.append(f'## 组件: {tp}')
|
||||||
prometheus_metrics.append(f'## 组件: {component_type}')
|
metrics_tp = self.get_metrics(tp)
|
||||||
base_metrics = self.get_metrics()
|
metric_text = session_active_metric_text % (tp, metrics_tp['session_active'])
|
||||||
metric_text = base_session_active_metric_text % (
|
|
||||||
component_type,
|
|
||||||
base_metrics['session_active']
|
|
||||||
)
|
|
||||||
prometheus_metrics.append(metric_text)
|
prometheus_metrics.append(metric_text)
|
||||||
|
|
||||||
prometheus_metrics.append('\n')
|
prometheus_metrics.append('\n')
|
||||||
prometheus_metrics.append('# JumpServer 各组件节点一些指标')
|
|
||||||
base_system_state_metric_text = 'jumpserver_components_%s{component_type="%s", component="%s"} %s'
|
# 各组件节点指标
|
||||||
system_states_name = [
|
prometheus_metrics.append('# JumpServer 各组件一些指标')
|
||||||
|
state_metric_text = 'jumpserver_components_%s{component_type="%s", component="%s"} %s'
|
||||||
|
states = [
|
||||||
'system_cpu_load_1', 'system_memory_used_percent',
|
'system_cpu_load_1', 'system_memory_used_percent',
|
||||||
'system_disk_used_percent', 'session_active_count'
|
'system_disk_used_percent', 'session_active_count'
|
||||||
]
|
]
|
||||||
for system_state_name in system_states_name:
|
for state in states:
|
||||||
prometheus_metrics.append(f'## 指标: {system_state_name}')
|
prometheus_metrics.append(f'## 指标: {state}')
|
||||||
for component in self.components:
|
components = self.get_components()
|
||||||
|
for component in components:
|
||||||
if not component.is_alive:
|
if not component.is_alive:
|
||||||
continue
|
continue
|
||||||
component_type = component.type
|
metric_text = state_metric_text % (
|
||||||
metric_text = base_system_state_metric_text % (
|
state, component.type, component.name, component.state.get(state)
|
||||||
system_state_name,
|
|
||||||
component_type,
|
|
||||||
component.name,
|
|
||||||
component.state.get(system_state_name)
|
|
||||||
)
|
)
|
||||||
prometheus_metrics.append(metric_text)
|
prometheus_metrics.append(metric_text)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue