pull/8605/head
ibuler 2022-04-12 17:53:56 +08:00
parent d418c28e98
commit 9f927f9703
11 changed files with 71 additions and 88 deletions

View File

@ -31,7 +31,6 @@ class AssetViewSet(SuggestionMixin, FilterAssetByNodeMixin, OrgBulkModelViewSet)
'hostname': ['exact'], 'hostname': ['exact'],
'ip': ['exact'], 'ip': ['exact'],
'system_users__id': ['exact'], 'system_users__id': ['exact'],
'platform__base': ['exact'],
'is_active': ['exact'], 'is_active': ['exact'],
'protocols': ['exact', 'icontains'] 'protocols': ['exact', 'icontains']
} }

View File

@ -10,7 +10,7 @@ __all__ = ['AssetPlatformViewSet']
class AssetPlatformViewSet(ModelViewSet): class AssetPlatformViewSet(ModelViewSet):
queryset = Platform.objects.all() queryset = Platform.objects.all()
serializer_class = PlatformSerializer serializer_class = PlatformSerializer
filterset_fields = ['name', 'base'] filterset_fields = ['name']
search_fields = ['name'] search_fields = ['name']
def check_object_permissions(self, request, obj): def check_object_permissions(self, request, obj):

View File

@ -20,7 +20,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='asset', model_name='asset',
name='type', name='type',
field=models.CharField(default='linux', max_length=128, verbose_name='Type'), field=models.CharField(choices=[('linux', 'Linux'), ('windows', 'Windows'), ('unix', 'Unix'), ('bsd', 'BSD'), ('macos', 'MacOS'), ('mainframe', 'Mainframe'), ('other_host', 'Other host'), ('switch', 'Switch'), ('router', 'Router'), ('firewall', 'Firewall'), ('other_network', 'Other device'), ('mysql', 'MySQL'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('oracle', 'Oracle'), ('sqlserver', 'SQLServer'), ('mongodb', 'MongoDB'), ('redis', 'Redis'), ('chrome', 'Chrome'), ('vsphere', 'vSphere client'), ('mysql_workbench', 'MySQL workbench'), ('custom_remote_app', 'Custom'), ('k8s', 'Kubernetes')], max_length=128, verbose_name='Type'),
preserve_default=False, preserve_default=False,
), ),
] ]

View File

@ -22,9 +22,9 @@ class Migration(migrations.Migration):
preserve_default=False, preserve_default=False,
), ),
migrations.AlterField( migrations.AlterField(
model_name='asset', model_name='platform',
name='type', name='type',
field=models.CharField(choices=[('linux', 'Linux'), ('unix', 'Unix'), ('windows', 'Windows'), ('macos', 'MacOS'), ('mainframe', 'Mainframe'), ('other_host', 'Other host'), ('switch', 'Switch'), ('router', 'Router'), ('firewall', 'Firewall'), ('other_network', 'Other device'), ('mysql', 'MySQL'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('oracle', 'Oracle'), ('sqlserver', 'SQLServer'), ('mongodb', 'MongoDB'), ('redis', 'Redis'), ('chrome', 'Chrome'), ('vsphere', 'vSphere client'), ('mysql_workbench', 'MySQL workbench'), ('custom_remote_app', 'Custom'), ('k8s', 'Kubernetes')], max_length=128, verbose_name='Type'), field=models.CharField(choices=[('linux', 'Linux'), ('windows', 'Windows'), ('unix', 'Unix'), ('bsd', 'BSD'), ('macos', 'MacOS'), ('mainframe', 'Mainframe'), ('other_host', 'Other host'), ('switch', 'Switch'), ('router', 'Router'), ('firewall', 'Firewall'), ('other_network', 'Other device'), ('mysql', 'MySQL'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('oracle', 'Oracle'), ('sqlserver', 'SQLServer'), ('mongodb', 'MongoDB'), ('redis', 'Redis'), ('chrome', 'Chrome'), ('vsphere', 'vSphere client'), ('mysql_workbench', 'MySQL workbench'), ('custom_remote_app', 'Custom'), ('k8s', 'Kubernetes')], default='Linux', max_length=32, verbose_name='Type'),
), ),
migrations.AlterField( migrations.AlterField(
model_name='systemuser', model_name='systemuser',

View File

@ -1,23 +0,0 @@
# Generated by Django 3.1.14 on 2022-04-07 09:30
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('assets', '0097_auto_20220407_1726'),
]
operations = [
migrations.AlterField(
model_name='asset',
name='type',
field=models.CharField(choices=[('linux', 'Linux'), ('windows', 'Windows'), ('unix', 'Unix'), ('bsd', 'BSD'), ('macos', 'MacOS'), ('mainframe', 'Mainframe'), ('other_host', 'Other host'), ('switch', 'Switch'), ('router', 'Router'), ('firewall', 'Firewall'), ('other_network', 'Other device'), ('mysql', 'MySQL'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('oracle', 'Oracle'), ('sqlserver', 'SQLServer'), ('mongodb', 'MongoDB'), ('redis', 'Redis'), ('chrome', 'Chrome'), ('vsphere', 'vSphere client'), ('mysql_workbench', 'MySQL workbench'), ('custom_remote_app', 'Custom'), ('k8s', 'Kubernetes')], max_length=128, verbose_name='Type'),
),
migrations.AlterField(
model_name='platform',
name='type',
field=models.CharField(choices=[('linux', 'Linux'), ('windows', 'Windows'), ('unix', 'Unix'), ('bsd', 'BSD'), ('macos', 'MacOS'), ('mainframe', 'Mainframe'), ('other_host', 'Other host'), ('switch', 'Switch'), ('router', 'Router'), ('firewall', 'Firewall'), ('other_network', 'Other device'), ('mysql', 'MySQL'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('oracle', 'Oracle'), ('sqlserver', 'SQLServer'), ('mongodb', 'MongoDB'), ('redis', 'Redis'), ('chrome', 'Chrome'), ('vsphere', 'vSphere client'), ('mysql_workbench', 'MySQL workbench'), ('custom_remote_app', 'Custom'), ('k8s', 'Kubernetes')], default='Linux', max_length=32, verbose_name='Base'),
),
]

View File

@ -1,2 +1,3 @@
from .common import * from .common import *
from .device_info import *
from .host import * from .host import *

View File

@ -0,0 +1,51 @@
from django.utils.translation import gettext_lazy as _
from django.db import models
from common.mixins.models import CommonModelMixin
__all__ = ['DeviceInfo']
class DeviceInfo(CommonModelMixin):
# Collect
vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor'))
model = models.CharField(max_length=54, null=True, blank=True, verbose_name=_('Model'))
sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number'))
cpu_model = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU model'))
cpu_count = models.IntegerField(null=True, verbose_name=_('CPU count'))
cpu_cores = models.IntegerField(null=True, verbose_name=_('CPU cores'))
cpu_vcpus = models.IntegerField(null=True, verbose_name=_('CPU vcpus'))
memory = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Memory'))
disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total'))
disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info'))
os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS'))
os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version'))
os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch'))
hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw'))
@property
def cpu_info(self):
info = ""
if self.cpu_model:
info += self.cpu_model
if self.cpu_count and self.cpu_cores:
info += "{}*{}".format(self.cpu_count, self.cpu_cores)
return info
@property
def hardware_info(self):
if self.cpu_count:
return '{} Core {} {}'.format(
self.cpu_vcpus or self.cpu_count * self.cpu_cores,
self.memory, self.disk_total
)
else:
return ''
def __str__(self):
return '{} of {}'.format(self.hardware_info, self.host.hostname)
class Meta:
verbose_name = _("DeviceInfo")

View File

@ -1,59 +1,17 @@
from django.utils.translation import gettext_lazy as _
from django.db import models from django.db import models
from django.utils.translation import gettext_lazy as _
from assets.const import Category from assets.const import Category
from common.mixins.models import CommonModelMixin
from .common import Asset from .common import Asset
from .device_info import DeviceInfo
class Host(Asset): class Host(Asset):
device_info = models.ForeignKey(DeviceInfo, on_delete=models.SET_NULL,
null=True, verbose_name=_("Device info"))
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.category = Category.HOST self.category = Category.HOST
return super().save(*args, **kwargs) return super().save(*args, **kwargs)
class DeviceInfo(CommonModelMixin):
host = models.OneToOneField(Host, related_name='device_info', on_delete=models.CASCADE,
verbose_name=_("Host"), unique=True)
# Collect
vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor'))
model = models.CharField(max_length=54, null=True, blank=True, verbose_name=_('Model'))
sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number'))
cpu_model = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU model'))
cpu_count = models.IntegerField(null=True, verbose_name=_('CPU count'))
cpu_cores = models.IntegerField(null=True, verbose_name=_('CPU cores'))
cpu_vcpus = models.IntegerField(null=True, verbose_name=_('CPU vcpus'))
memory = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Memory'))
disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total'))
disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info'))
os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS'))
os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version'))
os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch'))
hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw'))
@property
def cpu_info(self):
info = ""
if self.cpu_model:
info += self.cpu_model
if self.cpu_count and self.cpu_cores:
info += "{}*{}".format(self.cpu_count, self.cpu_cores)
return info
@property
def hardware_info(self):
if self.cpu_count:
return '{} Core {} {}'.format(
self.cpu_vcpus or self.cpu_count * self.cpu_cores,
self.memory, self.disk_total
)
else:
return ''
def __str__(self):
return '{} of {}'.format(self.hardware_info, self.host.hostname)
class Meta:
verbose_name = _("DeviceInfo")

View File

@ -1,5 +1,10 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from .common import Asset from .common import Asset
from .device_info import DeviceInfo
class Network(Asset): class Network(Asset):
pass device_info = models.ForeignKey(DeviceInfo, on_delete=models.SET_NULL,
null=True, verbose_name=_("Device info"))

View File

@ -12,17 +12,9 @@ class Platform(models.Model):
('utf8', 'UTF-8'), ('utf8', 'UTF-8'),
('gbk', 'GBK'), ('gbk', 'GBK'),
) )
BASE_CHOICES = (
('Linux', 'Linux'),
('Unix', 'Unix'),
('MacOS', 'MacOS'),
('BSD', 'BSD'),
('Windows', 'Windows'),
('Other', 'Other'),
)
name = models.SlugField(verbose_name=_("Name"), unique=True, allow_unicode=True) name = models.SlugField(verbose_name=_("Name"), unique=True, allow_unicode=True)
category = models.CharField(max_length=16, choices=Category.choices, verbose_name=_("Category")) category = models.CharField(max_length=16, choices=Category.choices, verbose_name=_("Category"))
type = models.CharField(choices=AllTypes.choices, max_length=32, default='Linux', verbose_name=_("Base")) type = models.CharField(choices=AllTypes.choices, max_length=32, default='Linux', verbose_name=_("Type"))
charset = models.CharField(default='utf8', choices=CHARSET_CHOICES, max_length=8, verbose_name=_("Charset")) charset = models.CharField(default='utf8', choices=CHARSET_CHOICES, max_length=8, verbose_name=_("Charset"))
meta = JsonDictTextField(blank=True, null=True, verbose_name=_("Meta")) meta = JsonDictTextField(blank=True, null=True, verbose_name=_("Meta"))
internal = models.BooleanField(default=False, verbose_name=_("Internal")) internal = models.BooleanField(default=False, verbose_name=_("Internal"))
@ -36,10 +28,10 @@ class Platform(models.Model):
return linux.id return linux.id
def is_windows(self): def is_windows(self):
return self.base.lower() in ('windows',) return self.type.lower() in ('windows',)
def is_unixlike(self): def is_unixlike(self):
return self.base.lower() in ("linux", "unix", "macos", "bsd") return self.type.lower() in ("linux", "unix", "macos", "bsd")
def __str__(self): def __str__(self):
return self.name return self.name

View File

@ -182,7 +182,7 @@ class PlatformSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Platform model = Platform
fields = [ fields = [
'id', 'name', 'base', 'charset', 'id', 'name', 'category', 'type', 'charset',
'internal', 'meta', 'comment' 'internal', 'meta', 'comment'
] ]