diff --git a/apps/cmdb/urls.py b/apps/cmdb/urls.py index c64f485..f39f734 100644 --- a/apps/cmdb/urls.py +++ b/apps/cmdb/urls.py @@ -14,4 +14,10 @@ urlpatterns = [ path('portal/code/delete/', views_code.CodeDeleteView.as_view(), name='portal-code-delete'), path('portal/scan_config/', views_scan.ScanConfigView.as_view(), name='portal-scan_config'), + path('portal/device_scan/', views_scan.DeviceScanView.as_view(), name='portal-device_scan'), + path('portal/device_scan/list/', views_scan.DeviceScanListView.as_view(), name='portal-device_scan-list'), + path('portal/device_scan/detail/', views_scan.DeviceScanDetailView.as_view(), name='portal-device_scan-detail'), + path('portal/device_scan/delete/', views_scan.DeviceScanDeleteView.as_view(), name='portal-device_scan-delete'), + path('portal/device_scan/exec/', views_scan.DeviceScanExecView.as_view(), name='portal-device_scan-exec'), + ] diff --git a/apps/cmdb/views_scan.py b/apps/cmdb/views_scan.py index bffd7c5..205ccf2 100644 --- a/apps/cmdb/views_scan.py +++ b/apps/cmdb/views_scan.py @@ -6,15 +6,16 @@ import ast import logging from ruamel import yaml -from django.views.generic import View +from django.views.generic import View, TemplateView from django.http import JsonResponse -from django.shortcuts import render +from django.shortcuts import render, get_object_or_404 from system.mixin import LoginRequiredMixin -from custom import BreadcrumbMixin +from custom import BreadcrumbMixin, SandboxListView, SandboxDeleteView from utils.sandbox_utils import ConfigFileMixin from system.models import Menu +from .models import DeviceScanInfo error_logger = logging.getLogger('sandbox_error') @@ -52,3 +53,78 @@ class ScanConfigView(LoginRequiredMixin, BreadcrumbMixin, ConfigFileMixin, View) error_logger.error(e) return JsonResponse(ret) + + +class DeviceScanView(LoginRequiredMixin, BreadcrumbMixin, TemplateView): + template_name = 'cmdb/device_scan.html' + + +class DeviceScanListView(SandboxListView): + model = DeviceScanInfo + fields = ['id', 'sys_hostname', 'hostname', 'mac_address', 'auth_type', 'status', 'os_type', 'device_type'] + + +class DeviceScanDetailView(LoginRequiredMixin, View): + + def get(self, request): + ret = Menu.get_menu_by_request_url(request.path_info) + if 'id' in request.GET and request.GET['id']: + device = get_object_or_404(DeviceScanInfo, pk=int(request.GET['id'])) + ret['device'] = device + return render(request, 'cmdb/device_scan_detail.html', ret) + + +class DeviceScanDeleteView(SandboxDeleteView): + model = DeviceScanInfo + + +class DeviceScanExecView(LoginRequiredMixin, View): + + def get(self, request): + import time + from utils.sandbox_utils import SandboxScan, LoginExecution + info_logger = logging.getLogger('sandbox_info') + ret = dict(result=False) + scan = SandboxScan() + execution = LoginExecution() + scan_type = execution.get_scan_type() + auth_type = execution.get_auth_type() + start_time = time.time() + if scan_type == 'basic_scan': + hosts = scan.basic_scan() + for host in hosts: + DeviceScanInfo.objects.update_or_create( + hostname=host, + ) + else: + hosts = scan.os_scan() + login_hosts = [host for host in hosts if host['os'] in ['Linux', 'embedded']] + nologin_hosts = [host for host in hosts if host not in login_hosts] + for host in nologin_hosts: + DeviceScanInfo.objects.update_or_create( + hostname=host['host'], + defaults={ + 'os_type': host['os'] + } + ) + for host in login_hosts: + kwargs = { + 'hostname': host['host'], + 'username': execution.get_ssh_username(), + 'port': execution.get_ssh_port(), + 'password': execution.get_ssh_password(), + 'private_key': execution.get_ssh_private_key() + } + defaults = execution.login_execution(auth_type=auth_type, **kwargs) + DeviceScanInfo.objects.update_or_create( + hostname=host['host'], + defaults=defaults + ) + end_time = time.time() + msg = 'Scan task has been completed, execution time: %(time)s, %(num)s hosts are up.' % { + 'time': end_time - start_time, + 'num': len(hosts) + } + info_logger.info(msg) + ret['result'] = True + return JsonResponse(ret) \ No newline at end of file diff --git a/templates/cmdb/device_scan.html b/templates/cmdb/device_scan.html new file mode 100644 index 0000000..da37af9 --- /dev/null +++ b/templates/cmdb/device_scan.html @@ -0,0 +1,298 @@ +{% extends "base-left.html" %} +{% load staticfiles %} + +{% block css %} + + + +{% endblock %} + +{% block content %} + + +
+
+
+
+
+ +
+
 
+
+ +
+
 
+
+ +
+
 
+
+ +
+
+ +
+ + + + + + + + + + + + + + + + + +
ID主机名IP地址MAC地址认证类型登陆状态系统类型设备类型操作
+
+ 点击【执行入库】可将扫描结果中,登陆状态为:成功(succeed)的设备数据导入正式设备管理数据库。 +
+
+
+ +
+ + + +{% endblock %} + + +{% block javascripts %} + + + + + + + +{% endblock %} \ No newline at end of file diff --git a/templates/cmdb/device_scan_detail.html b/templates/cmdb/device_scan_detail.html new file mode 100644 index 0000000..3c73a77 --- /dev/null +++ b/templates/cmdb/device_scan_detail.html @@ -0,0 +1,113 @@ +{% extends "base-left.html" %} +{% load staticfiles %} + +{% block css %} + +{% endblock %} + +{% block content %} + + + +
+
+
+
+
+

设备详情

+ +
+ +
+
+
+
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
主机名{{ device.sys_hostname }}SN编号{{ device.sn_number }}
SSH用户名{{ device.username }}SSH端口{{ device.port }}
认证类型{{ device.auth_type }}登陆状态{{ device.status }}
IP地址{{ device.hostname }}MAC地址{{ device.mac_address }}
系统类型{{ device.os_type }}设备类型{{ device.device_type }}
入库时间{{ device.add_time }}变更时间{{ device.modify_time }}
错误信息{{ device.error_message }}
+
+
+ + +
+ +
+
+ + + +
+ + + +{% endblock %} + +{% block javascripts %} + + + + + +{% endblock %} \ No newline at end of file