Пример #1
0
def update_ports(dev_id):
    # 设备列表
    mongo_devices = mongo_common['devices']

    # 端口列表
    mongo_ports = mongo_common['ports']

    mongo_res = mongo_devices.find_one({'_id': ObjectId(dev_id)})
    port_res = mongo_ports.find_one({'dev_id': dev_id})

    if mongo_res is None or port_res is None:
        return {'err': 'port db is empty', 'res': 2}

    port_list = port_list = snmp.get_snmp_port(mongo_res['snmp_ip'],
                                               mongo_res['snmp_community'])

    if port_list is None:
        return {'err': 'update port snmp error', 'res': 3}

    try:
        # 更新 Ports
        mongo_ports.update_one({'dev_id': dev_id},
                               {"$set": {
                                   'port_list': port_list
                               }},
                               upsert=True)
    except:
        return {'err': 'port db update error', 'res': 1}

    return {'data': 'update port success', 'res': 0}
Пример #2
0
def update_ports(dev_id):
    # 设备列表
    mongo_devices = mongo_common['devices']

    # 端口列表
    mongo_ports = mongo_common['ports']

    mongo_res = mongo_devices.find_one({'_id':ObjectId(dev_id)})
    port_res = mongo_ports.find_one({'dev_id':dev_id})

    if mongo_res is None or port_res is None:
        return {'err': 'port db is empty', 'res': 2}

    port_list = port_list = snmp.get_snmp_port(mongo_res['snmp_ip'], mongo_res['snmp_community'])

    if port_list is None:
        return {'err': 'update port snmp error', 'res': 3}

    try:
        # 更新 Ports
        mongo_ports.update_one({'dev_id': dev_id}, {
            "$set":{'port_list': port_list}
        }, upsert=True)
    except:
        return {'err': 'port db update error', 'res': 1}

    return {'data': 'update port success', 'res': 0}
Пример #3
0
def update_device(dev_id, dev_args):
    # 设备列表
    mongo_devices = mongo_common['devices']

    # 端口列表
    mongo_ports = mongo_common['ports']

    # 查找是否已有该设备
    dev_res = mongo_devices.find_one({'_id': ObjectId(dev_id)})

    # 没有相应设备,直接返回
    if dev_res is None:
        return {'err': 'db have no device', 'res': 2}

    # 读取新增设备的 SNMP 信息
    if 'snmp_community' in dev_args and dev_args['snmp_community']:
        new_community = dev_args['snmp_community']
    else:
        new_community = dev_res['snmp_community']

        # 读取新增设备的 SNMP 信息
    if 'snmp_port' in dev_args and dev_args['snmp_port']:
        new_port = dev_args['snmp_port']
    else:
        new_port = dev_res['snmp_port']

    port_list = snmp.get_snmp_port(dev_args['snmp_ip'], new_community)

    if port_list is None:
        return {'err': 'update device snmp info error', 'res': 3}

    # 新增设备写入数据库
    update_data = {
        'snmp_ip': dev_args['snmp_ip'],
        'snmp_community': new_community,
        'snmp_port': int(new_port),
        'dev_owner': dev_args['dev_owner'],
        'dev_group': dev_args['dev_group'],
        'sys_name': dev_args['sys_name'],
        'sys_desc': dev_args['sys_desc'],
        'update_time': int(time())
    }
    write_data = {"$set": update_data}

    try:
        # 更新 Devices
        mongo_devices.update_one({'_id': ObjectId(dev_id)}, write_data)

        # 更新 Ports
        mongo_ports.update_one({'dev_id': dev_id},
                               {"$set": {
                                   'port_list': port_list
                               }},
                               upsert=True)
    except:
        return {'err': 'db update error', 'res': 1}

    return {'data': 'update success', 'res': 0}
Пример #4
0
def update_device(dev_id, dev_args):
    # 设备列表
    mongo_devices = mongo_common['devices']

    # 端口列表
    mongo_ports = mongo_common['ports']

    # 查找是否已有该设备
    dev_res = mongo_devices.find_one({'_id': ObjectId(dev_id)})

    # 没有相应设备,直接返回
    if dev_res is None:
        return {'err': 'db have no device', 'res': 2}

    # 读取新增设备的 SNMP 信息
    if 'snmp_community' in dev_args and dev_args['snmp_community']:
        new_community = dev_args['snmp_community']
    else:
        new_community = dev_res['snmp_community']

        # 读取新增设备的 SNMP 信息
    if 'snmp_port' in dev_args and dev_args['snmp_port']:
        new_port = dev_args['snmp_port']
    else:
        new_port = dev_res['snmp_port']

    port_list = snmp.get_snmp_port(dev_args['snmp_ip'], new_community)

    if port_list is None:
        return {'err': 'update device snmp info error', 'res': 3}

    # 新增设备写入数据库
    update_data = {
        'snmp_ip': dev_args['snmp_ip'],
        'snmp_community': new_community,
        'snmp_port': int(new_port),
        'dev_owner': dev_args['dev_owner'],
        'dev_group': dev_args['dev_group'],
        'sys_name': dev_args['sys_name'],
        'sys_desc': dev_args['sys_desc'],
        'update_time': int(time())
    }
    write_data = {"$set": update_data}

    try:
        # 更新 Devices
        mongo_devices.update_one({'_id': ObjectId(dev_id)}, write_data)

        # 更新 Ports
        mongo_ports.update_one({'dev_id': dev_id}, {
            "$set":{'port_list': port_list}
        }, upsert=True)
    except:
        return {'err': 'db update error', 'res': 1}

    return {'data': 'update success', 'res': 0}
Пример #5
0
def add_devices(dev_args):
    # 设备列表
    mongo_devices = mongo_common['devices']

    # 端口列表
    mongo_ports = mongo_common['ports']

    # 查找是否已有该设备
    mongo_key = {
        'snmp_ip': dev_args['snmp_ip'],
        'snmp_community': dev_args['snmp_community'],
        'dev_owner': dev_args['dev_owner']
    }
    dev_res = mongo_devices.find_one(mongo_key)

    # 对于已经有的设备,直接返回
    if dev_res:
        return {'err': 'db already have this device', 'res': 2}

    # 读取新增设备的 SNMP 信息
    port_list = snmp.get_snmp_port(dev_args['snmp_ip'],
                                   dev_args['snmp_community'])

    if port_list is None:
        return {'err': 'get device snmp info error', 'res': 2}

    # 新增设备写入数据库
    try:
        dev_insert_res = mongo_devices.insert_one({
            'snmp_ip':
            dev_args['snmp_ip'],
            'snmp_community':
            dev_args['snmp_community'],
            'snmp_port':
            int(dev_args['snmp_port']),
            'dev_owner':
            dev_args['dev_owner'],
            'dev_group':
            dev_args['dev_group'],
            'sys_name':
            dev_args['sys_name'],
            'sys_desc':
            dev_args['sys_desc'],
            'update_time':
            int(time())
        })

        mongo_ports.insert_one({
            'dev_id': str(dev_insert_res.inserted_id),
            'port_list': port_list
        })
    except:
        return {'err': 'db insert error', 'res': 1}

    return {'data': 'insert success', 'res': 0}
Пример #6
0
def get_snmp_ports():
    device_ip = request.values.get('ip')
    device_community = request.values.get('community', 'public')

    if device_ip:
        port_info = snmp.get_snmp_port(device_ip, device_community)
    else:
        return jsonify({'data': 'params error', 'res': 2})

    if port_info:
        return jsonify({'data': port_info, 'res': 0})
    else:
        return jsonify({'data': 'cannot get snmp message', 'res': 1})
Пример #7
0
def get_snmp_ports():
    device_ip = request.values.get('ip')
    device_community = request.values.get('community', 'public')

    if device_ip:
        port_info = snmp.get_snmp_port(device_ip, device_community)
    else:
        return jsonify({'data': 'params error', 'res': 2})

    if port_info:
        return jsonify({'data': port_info, 'res': 0})
    else:
        return jsonify({'data': 'cannot get snmp message', 'res': 1})
Пример #8
0
def add_devices(dev_args):
    # 设备列表
    mongo_devices = mongo_common['devices']

    # 端口列表
    mongo_ports = mongo_common['ports']

    # 查找是否已有该设备
    mongo_key = {'snmp_ip':dev_args['snmp_ip'],
                 'snmp_community':dev_args['snmp_community'],
                 'dev_owner': dev_args['dev_owner']}
    dev_res = mongo_devices.find_one(mongo_key)

    # 对于已经有的设备,直接返回
    if dev_res:
        return {'err': 'db already have this device', 'res': 2}

    # 读取新增设备的 SNMP 信息
    port_list = snmp.get_snmp_port(dev_args['snmp_ip'], dev_args['snmp_community'])

    if port_list is None:
        return {'err': 'get device snmp info error', 'res': 2}

    # 新增设备写入数据库
    try:
        dev_insert_res = mongo_devices.insert_one({
            'snmp_ip': dev_args['snmp_ip'],
            'snmp_community': dev_args['snmp_community'],
            'snmp_port': int(dev_args['snmp_port']),
            'dev_owner': dev_args['dev_owner'],
            'dev_group': dev_args['dev_group'],
            'sys_name': dev_args['sys_name'],
            'sys_desc': dev_args['sys_desc'],
            'update_time': int(time())
        })

        mongo_ports.insert_one({
            'dev_id': str(dev_insert_res.inserted_id),
            'port_list': port_list
        })
    except:
        return {'err': 'db insert error', 'res': 1}

    return {'data': 'insert success', 'res': 0}