From 6e0fbd78e7e309cc483c8c95c7d4e9fe61906ab7 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Tue, 15 Dec 2020 13:00:59 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dprometheus=5FmetricsAP?= =?UTF-8?q?I=E6=95=B0=E6=8D=AE=E8=8E=B7=E5=8F=96bug=EF=BC=9B=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E7=BB=84=E4=BB=B6=E6=B3=A8=E5=86=8Ctype=E4=B8=BA?= =?UTF-8?q?=E7=A9=BAbug=20(#5253)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 修复prometheus_metricsAPI数据获取bug;修复组件注册type为空bug * fix: 修改审计migrations/userloginlog/backend的verbose_name字段 Co-authored-by: Bai --- .../migrations/0011_userloginlog_backend.py | 2 +- apps/terminal/api/component.py | 6 +- .../terminal/migrations/0030_terminal_type.py | 3 +- apps/terminal/models/terminal.py | 5 +- apps/terminal/serializers_v2/terminal.py | 5 +- apps/terminal/utils.py | 100 ++++++++---------- 6 files changed, 59 insertions(+), 62 deletions(-) diff --git a/apps/audits/migrations/0011_userloginlog_backend.py b/apps/audits/migrations/0011_userloginlog_backend.py index 5a708b198..4e82d7dda 100644 --- a/apps/audits/migrations/0011_userloginlog_backend.py +++ b/apps/audits/migrations/0011_userloginlog_backend.py @@ -13,6 +13,6 @@ class Migration(migrations.Migration): migrations.AddField( model_name='userloginlog', name='backend', - field=models.CharField(default='', max_length=32, verbose_name='Login backend'), + field=models.CharField(default='', max_length=32, verbose_name='Authentication backend'), ), ] diff --git a/apps/terminal/api/component.py b/apps/terminal/api/component.py index aec404370..f881b5e98 100644 --- a/apps/terminal/api/component.py +++ b/apps/terminal/api/component.py @@ -28,7 +28,7 @@ class ComponentsMetricsAPIView(generics.GenericAPIView): permission_classes = (IsSuperUser,) def get(self, request, *args, **kwargs): - component_type = request.query_params.get('type') - util = ComponentsMetricsUtil(component_type) - metrics = util.get_metrics() + tp = request.query_params.get('type') + util = ComponentsMetricsUtil() + metrics = util.get_metrics(tp) return Response(metrics, status=status.HTTP_200_OK) diff --git a/apps/terminal/migrations/0030_terminal_type.py b/apps/terminal/migrations/0030_terminal_type.py index 4e4d871f8..1ede3ec94 100644 --- a/apps/terminal/migrations/0030_terminal_type.py +++ b/apps/terminal/migrations/0030_terminal_type.py @@ -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 @@ -36,7 +36,6 @@ class Migration(migrations.Migration): model_name='terminal', 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) ] diff --git a/apps/terminal/models/terminal.py b/apps/terminal/models/terminal.py index fb7a1dc24..b8b44dfbc 100644 --- a/apps/terminal/models/terminal.py +++ b/apps/terminal/models/terminal.py @@ -129,7 +129,10 @@ class TerminalStatusMixin(TerminalStateMixin): class Terminal(TerminalStatusMixin, models.Model): id = models.UUIDField(default=uuid.uuid4, primary_key=True) 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')) ssh_port = models.IntegerField(verbose_name=_('SSH Port'), default=2222) http_port = models.IntegerField(verbose_name=_('HTTP Port'), default=5000) diff --git a/apps/terminal/serializers_v2/terminal.py b/apps/terminal/serializers_v2/terminal.py index f4e26b230..8121410e6 100644 --- a/apps/terminal/serializers_v2/terminal.py +++ b/apps/terminal/serializers_v2/terminal.py @@ -18,7 +18,7 @@ class TerminalSerializer(serializers.ModelSerializer): class Meta: model = Terminal fields = [ - 'id', 'name', 'remote_addr', 'command_storage', + 'id', 'name', 'type', 'remote_addr', 'command_storage', 'replay_storage', 'user', 'is_accepted', 'is_deleted', 'date_created', 'comment' ] @@ -55,5 +55,6 @@ class TerminalSerializer(serializers.ModelSerializer): class TerminalRegistrationSerializer(serializers.Serializer): 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) diff --git a/apps/terminal/utils.py b/apps/terminal/utils.py index 68456dfbe..d85e21deb 100644 --- a/apps/terminal/utils.py +++ b/apps/terminal/utils.py @@ -106,46 +106,43 @@ def send_command_alert_mail(command): class ComponentsMetricsUtil(object): - def __init__(self, component_type=None): - self.type = component_type - self.components = [] - self.initial_components() - - def initial_components(self): + @staticmethod + def get_components(tp=None): from .models import Terminal - terminals = Terminal.objects.all().order_by('type') - if self.type: - terminals = terminals.filter(type=self.type) - self.components = list(terminals) + components = Terminal.objects.all().order_by('type') + if tp: + components = components.filter(type=tp) + 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 - for component in self.components: + for component in components: total_count += 1 - if not component.is_alive: - critical_count += 1 - continue - session_active_total += component.state.get('session_active_count', 0) - if component.is_normal: - normal_count += 1 - elif component.is_high: - high_count += 1 + if component.is_alive: + if component.is_normal: + normal_count += 1 + elif component.is_high: + high_count += 1 + else: + # critical + critical_count += 1 + session_active_total += component.state.get('session_active_count', 0) else: critical_count += 1 - metrics = { + return { 'total': total_count, 'normal': normal_count, 'high': high_count, 'critical': critical_count, 'session_active': session_active_total } - return metrics class ComponentsPrometheusMetricsUtil(ComponentsMetricsUtil): @staticmethod - def get_status_metrics(metrics): + def convert_status_metrics(metrics): return { 'any': metrics['total'], 'normal': metrics['normal'], @@ -154,50 +151,47 @@ class ComponentsPrometheusMetricsUtil(ComponentsMetricsUtil): } def get_prometheus_metrics_text(self): - prometheus_metrics = [] - prometheus_metrics.append('# JumpServer 各组件状态个数汇总') - base_status_metric_text = 'jumpserver_components_status_total{component_type="%s", status="%s"} %s' - for component in self.components: - component_type = component.type - base_metrics = self.get_metrics() + prometheus_metrics = list() - prometheus_metrics.append(f'## 组件: {component_type}') - status_metrics = self.get_status_metrics(base_metrics) + # 各组件状态个数汇总 + prometheus_metrics.append('# JumpServer 各组件状态个数汇总') + status_metric_text = 'jumpserver_components_status_total{component_type="%s", status="%s"} %s' + for tp in const.TerminalTypeChoices.types(): + prometheus_metrics.append(f'## 组件: {tp}') + metrics_tp = self.get_metrics(tp) + status_metrics = self.convert_status_metrics(metrics_tp) 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('\n') + + # 各组件在线会话数汇总 prometheus_metrics.append('# JumpServer 各组件在线会话数汇总') - base_session_active_metric_text = 'jumpserver_components_session_active_total{component_type="%s"} %s' - for component in self.components: - component_type = component.type - prometheus_metrics.append(f'## 组件: {component_type}') - base_metrics = self.get_metrics() - metric_text = base_session_active_metric_text % ( - component_type, - base_metrics['session_active'] - ) + session_active_metric_text = 'jumpserver_components_session_active_total{component_type="%s"} %s' + for tp in const.TerminalTypeChoices.types(): + prometheus_metrics.append(f'## 组件: {tp}') + metrics_tp = self.get_metrics(tp) + metric_text = session_active_metric_text % (tp, metrics_tp['session_active']) prometheus_metrics.append(metric_text) 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_disk_used_percent', 'session_active_count' ] - for system_state_name in system_states_name: - prometheus_metrics.append(f'## 指标: {system_state_name}') - for component in self.components: + for state in states: + prometheus_metrics.append(f'## 指标: {state}') + components = self.get_components() + for component in components: if not component.is_alive: continue - component_type = component.type - metric_text = base_system_state_metric_text % ( - system_state_name, - component_type, - component.name, - component.state.get(system_state_name) + metric_text = state_metric_text % ( + state, component.type, component.name, component.state.get(state) ) prometheus_metrics.append(metric_text)