Esempio n. 1
0
    def sync_cmdb(self):
        """
        写入CMDB
        :return:
        """

        server_list = self.get_server_info()
        if not server_list:
            ins_log.read_log('info', 'Not fount server info')
            return False
        with DBContext('w') as session:
            for server in server_list:
                ip = server.get('public_ip')
                instance_id = server.get('instance_id', 'Null')
                hostname = server.get('hostname', instance_id)
                if hostname == '' or not hostname:
                    hostname = instance_id
                region = server.get('region', 'Null')
                instance_type = server.get('instance_type', 'Null')
                instance_state = server.get('instance_state', 'Null')
                cpu = server.get('cpu', 'Null')
                cpu_cores = server.get('cpu_cores', 'Null')
                memory = server.get('memory', 'Null')
                disk = server.get('disk', 'Null')  # 阿里云接口里面好像没有disk信息
                os_type = server.get('os_type', 'Null')
                os_name = server.get('os_name', 'Null')
                os_kernel = server.get('os_kernel', 'Null')
                sn = server.get('sn', 'Null')

                exist_hostname = session.query(Server).filter(
                    Server.hostname == hostname).first()
                if exist_hostname:
                    session.query(Server).filter(
                        Server.hostname == hostname).update({
                            Server.ip:
                            ip,
                            Server.public_ip:
                            ip,
                            Server.idc:
                            self.account,
                            Server.region:
                            region,
                            Server.admin_user:
                            self.default_admin_user
                        })

                else:
                    if os_type == 'windows':
                        # windows机器不绑定管理用户,资产信息只是平台拿到的一些基础信息
                        new_windows_server = Server(ip=ip,
                                                    public_ip=ip,
                                                    hostname=hostname,
                                                    port=3389,
                                                    idc=self.account,
                                                    region=region,
                                                    state=self.state)
                        session.add(new_windows_server)

                    else:
                        # unix机器给默认绑定上管理用户,用于后续登陆机器拿详细资产使用的
                        new_server = Server(ip=ip,
                                            public_ip=ip,
                                            hostname=hostname,
                                            port=22,
                                            idc=self.account,
                                            region=region,
                                            state=self.state,
                                            admin_user=self.default_admin_user)
                        session.add(new_server)

                exist_ip = session.query(ServerDetail).filter(
                    ServerDetail.ip == ip).first()
                if exist_ip:
                    session.query(ServerDetail).filter(
                        ServerDetail.ip == ip).update({
                            ServerDetail.instance_id:
                            instance_id,
                            ServerDetail.instance_type:
                            instance_type,
                            ServerDetail.instance_state:
                            instance_state
                        })
                else:
                    if os_type == 'windows':

                        new_serve_detail = ServerDetail(
                            ip=ip,
                            instance_id=instance_id,
                            instance_type=instance_type,
                            instance_state=instance_state,
                            cpu=cpu,
                            cpu_cores=cpu_cores,
                            memory=memory,
                            os_type=os_name,
                            disk=disk,
                            os_kernel=os_kernel,
                            sn=sn)
                        session.add(new_serve_detail)

                    else:
                        new_serve_detail = ServerDetail(
                            ip=ip,
                            instance_id=instance_id,
                            instance_type=instance_type,
                            instance_state=instance_state,
                            cpu=cpu,
                            cpu_cores=cpu_cores,
                            memory=memory,
                            os_type=os_name,
                            disk=disk,
                            os_kernel=os_kernel,
                            sn=sn)
                        session.add(new_serve_detail)
            session.commit()
Esempio n. 2
0
    def post(self, *args, **kwargs):
        data = json.loads(self.request.body.decode("utf-8"))
        nickname = self.get_current_nickname()
        hostname = data.get('hostname', None)
        ip = data.get('ip', None)
        port = data.get('port', 22)
        public_ip = data.get('public_ip', None)
        idc = data.get('idc', None)
        admin_user = data.get('admin_user', None)
        region = data.get('region', None)
        state = data.get('state', 'new')
        tag_list = data.get('tag_list', [])
        detail = data.get('detail', None)

        if not hostname or not ip or not port:
            return self.write(dict(code=-1, msg='关键参数不能为空'))

        if not admin_user:
            return self.write(dict(code=-1, msg='管理用户不能为空'))

        if not check_ip(ip):
            return self.write(dict(code=-1, msg="IP格式不正确"))

        if not type(port) is int and int(port) >= 65535:
            return self.write(dict(code=-1, msg="端口格式不正确"))

        with DBContext('r') as session:
            exist_id = session.query(
                Server.id).filter(Server.hostname == hostname).first()
            exist_ip = session.query(Server.id).filter(Server.ip == ip).first()
        if exist_id or exist_ip:
            return self.write(dict(code=-2, msg='不要重复记录'))

        with DBContext('w', None, True) as session:
            new_server = Server(hostname=hostname,
                                ip=ip,
                                public_ip=public_ip,
                                port=int(port),
                                idc=idc,
                                admin_user=admin_user,
                                region=region,
                                state=state,
                                detail=detail)
            session.add(new_server)

            all_tags = session.query(Tag.id).filter(
                Tag.tag_name.in_(tag_list)).order_by(Tag.id).all()
            # print('all_tags', all_tags)
            if all_tags:
                for tag_id in all_tags:
                    session.add(
                        ServerTag(server_id=new_server.id, tag_id=tag_id[0]))

        # 记录,记录错误也不要影响用户正常添加
        try:
            with DBContext('w', None, True) as session:

                new_record = AssetOperationalAudit(username=nickname,
                                                   request_object='主机',
                                                   request_host=ip,
                                                   request_method='新增',
                                                   modify_data=data)
                session.add(new_record)
        except Exception as err:
            ins_log.read_log('error',
                             'operational_audit error:{err}'.format(err=err))

        return self.write(dict(code=0, msg='添加成功'))
Esempio n. 3
0
from models.server import Server

if __name__ == "__main__":
    # El ultimo valor es el id del servidor
    puerto = int(input(">> "))
    serv = Server(puerto, "Servidorcito de SUDOKU", 1)
Esempio n. 4
0
def server_save(request, server_id=0):
    server_id = server_id or int(request.REQUEST.get('sid', '') or '0')
    new_server_id = int(request.REQUEST.get('new_server_id', '') or 0)
    model = None
    source_model = None
    is_add = True

    if server_id:
        model = Server.objects.get(id=server_id)
        source_model = copy.copy(model)
        is_add = False

    if not model:
        model = Server()
        model.id = new_server_id

    list_group = Group.objects.all()
    exists = False
    update_id = False
    err_msg = ''

    if server_id != new_server_id:
        exists = Server.objects.filter(id=new_server_id).exists()
        if exists:
            err_msg = '保存的服务器ID已存在'
            return render_to_response('feedback.html', locals())
        else:
            update_id = True

    if not request.POST.get("tabId"):
        err_msg = '请填入分组ID'
        return render_to_response('feedback.html', locals())

    model.tabId = int(request.POST.get("tabId") or 0)
    # model.client_ver = request.POST.get('client_ver', '') # 客户端版本
    model.name = request.POST.get('name', '')
    model.status = int(request.POST.get('status', '2'))
    model.commend = int(request.POST.get('commend', '2'))
    # model.require_ver = int(request.POST.get('require_ver', '0')) # 要求最低版本
    model.game_addr = request.POST.get('game_addr', '')
    model.game_port = int(request.POST.get('game_port', '2008'))
    model.report_url = request.POST.get('report_url', '')
    model.log_db_config = request.POST.get('log_db_config', '')
    model.remark = request.POST.get('remark', '')
    model.order = new_server_id if server_id == 0 else server_id
    model.json_data = request.POST.get('json_data', '')
    model.alias = request.REQUEST.get('alias', '')
    # model.is_ios = int(request.POST.get('is_ios', '0'))   #ios标识
    create_time = request.POST.get('create_time', '')
    model.last_time = datetime.datetime.now()
    model.game_db_addr_port = request.POST.get('game_db_addr_port', '')
    model.game_db_name = request.POST.get('game_db_name', '')
    model.game_db_user = request.POST.get('game_db_user', '')
    model.game_db_password = request.POST.get('game_db_password', '')
    if create_time == '':
        create_time = datetime.datetime.now()
    else:
        create_time = datetime.datetime.strptime(create_time,
                                                 '%Y-%m-%d %H:%M:%S')
    model.create_time = create_time

    # 保存详细操作日志
    save_server_log(server_id, is_add, request, source_model, model)

    if not exists:
        if model.name != '' and model.game_addr != '':
            try:
                # 修改ID
                old_server = copy.copy(model)
                if update_id:
                    model.id = new_server_id
                model.save()

                group_ids = request.POST.getlist('group_id')
                group_ids = [int(x) for x in group_ids]

                for group_model in list_group:
                    if group_model.id in group_ids:
                        group_model.server.add(model)
                        try:
                            # 添加服务器分区后,在增加到角色资源里面
                            from models.admin import Role, Resource
                            role_exclude = Role.objects.exclude(
                                name__contains="系统管理员")
                            for r in role_exclude:
                                role_group_list = r.get_resource(
                                    'server_group')
                                if group_model in role_group_list:
                                    ser_l = list(
                                        group_model.server.values_list('id'))
                                    extend_l = r.get_resource(
                                        'server').values_list('id')
                                    ser_l.extend(extend_l)
                                    ser_l = list(set([i[0] for i in ser_l]))
                                    r.create_resource('server', ser_l)
                                    r.save()
                        except:

                            print trace_msg()

                    else:
                        group_model.server.remove(model)

                # 前面保存没有出错
                if update_id:
                    old_server.status = Server.Status.TEST
                    old_server.save()

            except Exception, e:

                err_msg = trace_msg()

        else:
            err_msg = '填写必要数据!'
Esempio n. 5
0
    def sync_cmdb(self):
        """
        数据写CMDB,华为的信息比较少
        :return:
        """
        server_list = self.get_server_info()
        if not server_list:
            ins_log.read_log('info', 'Not fount server info...')
            return False
        with DBContext('w') as session:
            for server in server_list:
                print(server)
                ip = server.get('public_ip')
                instance_id = server.get('instance_id', 'Null')
                hostname = server.get('hostname', instance_id)
                if not hostname.strip():
                    hostname = instance_id
                region = server.get('region', 'Null')
                instance_type = server.get('instance_type', 'Null')
                instance_status = server.get('instance_status')
                # 华为云接口没看到CPU这类信息
                cpu = server.get('cpu', 'Null')  # CPU型号
                cpu_cores = server.get('cpu_cores', 'Null')
                memory = server.get('memory', 'Null')
                disk = server.get('disk', 'Null')
                os_type = server.get('os_type', 'Null')
                os_kernel = server.get('os_kernel', 'Null')
                sn = server.get('sn', 'Null')
                exist_hostname = session.query(Server).filter(
                    Server.hostname == hostname).first()
                # exist_ip = session.query(Server).filter(Server.ip == ip).first()
                if exist_hostname:
                    session.query(Server).filter(
                        Server.hostname == hostname).update({
                            Server.ip:
                            ip,
                            Server.public_ip:
                            ip,
                            Server.idc:
                            self.account,
                            Server.region:
                            region
                        })

                else:
                    new_server = Server(ip=ip,
                                        public_ip=ip,
                                        hostname=hostname,
                                        port=22,
                                        idc=self.account,
                                        region=region,
                                        state=self.state,
                                        admin_user=self.default_admin_user)
                    session.add(new_server)

                exist_ip = session.query(ServerDetail).filter(
                    ServerDetail.ip == ip).first()
                if exist_ip:
                    session.query(ServerDetail).filter(
                        ServerDetail.ip == ip).update({
                            ServerDetail.instance_id:
                            instance_id,
                            ServerDetail.instance_type:
                            instance_type,
                            ServerDetail.instance_state:
                            instance_status
                        })
                else:
                    new_serve_detail = ServerDetail(
                        ip=ip,
                        instance_id=instance_id,
                        instance_type=instance_type,
                        instance_state=instance_status,
                        cpu=cpu,
                        cpu_cores=cpu_cores,
                        memory=memory,
                        disk=disk,
                        os_type=os_type,
                        os_kernel=os_kernel,
                        sn=sn)
                    session.add(new_serve_detail)
            session.commit()
Esempio n. 6
0
def get_urls_and_add_server(initial_url):
    request = requests.get(initial_url)
    add_entry_to_db(Server(server_name=request.headers['Server']))
    return get_urls(request)