From 797c7635a7f9bb819c386fa7986faff9fbef8ad3 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Tue, 28 Feb 2023 18:45:38 +0800 Subject: [PATCH] perf: asset hardwareinfo (#9813) Co-authored-by: feng <1304903146@qq.com> --- .../automations/gather_accounts/filter.py | 4 ++- .../automations/gather_accounts/manager.py | 4 +-- .../gather_facts/format_asset_info.py | 35 +++++++++++++++++++ .../gather_facts/host/posix/main.yml | 2 +- .../automations/gather_facts/manager.py | 10 ++++-- apps/assets/serializers/asset/host.py | 19 +++++----- 6 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 apps/assets/automations/gather_facts/format_asset_info.py diff --git a/apps/accounts/automations/gather_accounts/filter.py b/apps/accounts/automations/gather_accounts/filter.py index f3c9e583d..af7cdefa4 100644 --- a/apps/accounts/automations/gather_accounts/filter.py +++ b/apps/accounts/automations/gather_accounts/filter.py @@ -60,4 +60,6 @@ class GatherAccountsFilter: if not run_method_name: return info - return getattr(self, f'{run_method_name}_filter')(info) + if hasattr(self, f'{run_method_name}_filter'): + return getattr(self, f'{run_method_name}_filter')(info) + return info diff --git a/apps/accounts/automations/gather_accounts/manager.py b/apps/accounts/automations/gather_accounts/manager.py index 5be1f423b..2ecd3d2e1 100644 --- a/apps/accounts/automations/gather_accounts/manager.py +++ b/apps/accounts/automations/gather_accounts/manager.py @@ -22,8 +22,8 @@ class GatherAccountsManager(AccountBasePlaybookManager): self.host_asset_mapper[host['name']] = asset return host - def filter_success_result(self, host, result): - result = GatherAccountsFilter(host).run(self.method_id_meta_mapper, result) + def filter_success_result(self, tp, result): + result = GatherAccountsFilter(tp).run(self.method_id_meta_mapper, result) return result @staticmethod diff --git a/apps/assets/automations/gather_facts/format_asset_info.py b/apps/assets/automations/gather_facts/format_asset_info.py new file mode 100644 index 000000000..d3184bf59 --- /dev/null +++ b/apps/assets/automations/gather_facts/format_asset_info.py @@ -0,0 +1,35 @@ +__all__ = ['FormatAssetInfo'] + + +class FormatAssetInfo: + + def __init__(self, tp): + self.tp = tp + + @staticmethod + def posix_format(info): + for cpu_model in info.get('cpu_model', []): + if cpu_model.endswith('GHz') or cpu_model.startswith("Intel"): + break + else: + cpu_model = '' + info['cpu_model'] = cpu_model[:48] + info['cpu_count'] = info.get('cpu_count', 0) + return info + + def run(self, method_id_meta_mapper, info): + for k, v in info.items(): + info[k] = v.strip() if isinstance(v, str) else v + + run_method_name = None + for k, v in method_id_meta_mapper.items(): + if self.tp not in v['type']: + continue + run_method_name = k.replace(f'{v["method"]}_', '') + + if not run_method_name: + return info + + if hasattr(self, f'{run_method_name}_format'): + return getattr(self, f'{run_method_name}_format')(info) + return info diff --git a/apps/assets/automations/gather_facts/host/posix/main.yml b/apps/assets/automations/gather_facts/host/posix/main.yml index 760ca601e..ba86d9a91 100644 --- a/apps/assets/automations/gather_facts/host/posix/main.yml +++ b/apps/assets/automations/gather_facts/host/posix/main.yml @@ -11,7 +11,7 @@ cpu_count: "{{ ansible_processor_count }}" cpu_cores: "{{ ansible_processor_cores }}" cpu_vcpus: "{{ ansible_processor_vcpus }}" - memory: "{{ ansible_memtotal_mb }}" + memory: "{{ ansible_memtotal_mb / 1024 | round(2) }}" disk_total: "{{ (ansible_mounts | map(attribute='size_total') | sum / 1024 / 1024 / 1024) | round(2) }}" distribution: "{{ ansible_distribution }}" distribution_version: "{{ ansible_distribution_version }}" diff --git a/apps/assets/automations/gather_facts/manager.py b/apps/assets/automations/gather_facts/manager.py index 5ec25cb17..07b432379 100644 --- a/apps/assets/automations/gather_facts/manager.py +++ b/apps/assets/automations/gather_facts/manager.py @@ -1,5 +1,6 @@ -from common.utils import get_logger from assets.const import AutomationTypes +from common.utils import get_logger +from .format_asset_info import FormatAssetInfo from ..base.manager import BasePlaybookManager logger = get_logger(__name__) @@ -19,12 +20,15 @@ class GatherFactsManager(BasePlaybookManager): self.host_asset_mapper[host['name']] = asset return host + def format_asset_info(self, tp, info): + info = FormatAssetInfo(tp).run(self.method_id_meta_mapper, info) + return info + def on_host_success(self, host, result): info = result.get('debug', {}).get('res', {}).get('info', {}) asset = self.host_asset_mapper.get(host) if asset and info: - for k, v in info.items(): - info[k] = v.strip() if isinstance(v, str) else v + info = self.format_asset_info(asset.type, info) asset.info = info asset.save() else: diff --git a/apps/assets/serializers/asset/host.py b/apps/assets/serializers/asset/host.py index 6a733c1e3..0f6bd8f5d 100644 --- a/apps/assets/serializers/asset/host.py +++ b/apps/assets/serializers/asset/host.py @@ -1,26 +1,25 @@ -from rest_framework import serializers from django.utils.translation import gettext_lazy as _ +from rest_framework import serializers from assets.models import Host from .common import AssetSerializer - __all__ = ['HostInfoSerializer', 'HostSerializer'] class HostInfoSerializer(serializers.Serializer): vendor = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('Vendor')) - model = serializers.CharField(max_length=54, required=False, allow_blank=True, label=_('Model')) - sn = serializers.CharField(max_length=128, required=False, allow_blank=True, label=_('Serial number')) - cpu_model = serializers.ListField(child=serializers.CharField(max_length=64, allow_blank=True), required=False, label=_('CPU model')) - cpu_count = serializers.IntegerField(required=False, label=_('CPU count')) - cpu_cores = serializers.IntegerField(required=False, label=_('CPU cores')) - cpu_vcpus = serializers.IntegerField(required=False, label=_('CPU vcpus')) + model = serializers.CharField(max_length=54, required=False, allow_blank=True, label=_('Model')) + sn = serializers.CharField(max_length=128, required=False, allow_blank=True, label=_('Serial number')) + cpu_model = serializers.CharField(max_length=64, allow_blank=True, required=False, label=_('CPU model')) + cpu_count = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('CPU count')) + cpu_cores = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('CPU cores')) + cpu_vcpus = serializers.CharField(max_length=64, required=False, allow_blank=True, label=_('CPU vcpus')) memory = serializers.CharField(max_length=64, allow_blank=True, required=False, label=_('Memory')) disk_total = serializers.CharField(max_length=1024, allow_blank=True, required=False, label=_('Disk total')) distribution = serializers.CharField(max_length=128, allow_blank=True, required=False, label=_('OS')) - distribution_version = serializers.CharField(max_length=16, allow_blank=True, required=False, label=_('OS version')) + distribution_version = serializers.CharField(max_length=16, allow_blank=True, required=False, label=_('OS version')) arch = serializers.CharField(max_length=16, allow_blank=True, required=False, label=_('OS arch')) @@ -36,5 +35,3 @@ class HostSerializer(AssetSerializer): 'label': _("IP/Host") }, } - -