Esempio n. 1
0
def del_md(nw_name,regist_time,mon_data_db):
    #database handle.
    db_handle = DBHandle()
    try:
        logger.debug('delete monitoring-data -start-')

        #get now time.(UTC:0)
        now_time = calendar.timegm(datetime.utcnow().timetuple())
    
        # search target table name.
        # time-range=registration_time-now_time.
        if not regist_time :
            logger.warn('Target network can not be found.')
            return
        tbl_name_list = search_target_table(logger,int(regist_time),int(now_time),mon_data_db)

        #connect to the database.
        db_con = db_handle.connect(mon_data_db,config.db_addr
                                   ,config.db_port,config.db_user,config.db_pass)
        
        #no data in the search time-range.
        if not tbl_name_list:
            logger.debug('no data in the search time-range.({0}-{1})'.format(regist_time,now_time))
            return

        # search database.(metaID)
        sql = "SELECT metaID FROM metaData WHERE network_name='{0}'".format(nw_name)
        logger.debug(sql)
        db_con.execute(sql)
         
        res_metaid_list = db_con.fetchall()
        sql_where_list = []
        for res_metaid in res_metaid_list:
            sql_where = " metaID={0} ".format(res_metaid['metaID'])
            sql_where_list.append(sql_where)

        if not sql_where_list:
            # no metaData.
            return

        # delete database.(data)
        sql_where = " OR ".join(sql_where_list)
        for tbl_name in tbl_name_list:
            sql = "DELETE FROM {0} WHERE {1}".format(tbl_name,sql_where)
            logger.debug(sql)
            db_con.execute(sql)

        # delete database.(metaData)
        sql = "DELETE FROM metaData WHERE network_name='{0}'".format(nw_name)
        logger.debug(sql)
        db_con.execute(sql)

        # delete commit.(monitoring-data and metaData)
        db_handle.commit()

    except Exception:
        logger.exception('delete monitoring-data error.')
        raise

    finally:
        #close monitoring database connection.
        db_handle.close()
        logger.debug('delete monitoring-data -end-')

    return
Esempio n. 2
0
def monitoring_cp_get(topology_type=""):
    logger.info('GET monitoring-data({0}). -start-'.format(const.TYPE_MON_CP))

    # database handle.
    db_handle = DBHandle()
    try:
        # create <monitoring-data>
        xml_mon_data = Element(const.XML_TAG_MON_DATA)
        
        # topology_type==[physical | slice]
        logger.debug('topology type=' + topology_type)
        
        # param_type_list==[status | cpu_load | etc...]
        param_type_list = request.query.getall(const.HTTP_GET_OPT_TYPE)
        if param_type_list:
            logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_TYPE +')=' + str(param_type_list))
    
        # get HTTP GET option.
        param_topol = request.query.get(const.HTTP_GET_OPT_TOPOL)
        if param_topol:
            logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_TOPOL +')=' + param_topol)
    
        param_node = request.query.get(const.HTTP_GET_OPT_NODE)
        if param_node:
            logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_NODE +')=' + param_node)
       
        param_stime = request.query.get(const.HTTP_GET_OPT_STIME)
        if param_stime:
            logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_STIME +')=' + param_stime)
        
        param_etime = request.query.get(const.HTTP_GET_OPT_ETIME)
        if param_etime:
            logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_ETIME +')=' + param_etime)
        
        param_limit = request.query.get(const.HTTP_GET_OPT_LMT)
        if param_limit:
            logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_LMT +')=' + param_limit)
        else:
            # set the default value if not specified.
            param_limit = str(const.DEFAULT_LIMIT)
            logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_LMT +')=default value(' + param_limit +')')

        # required check.(topology_type)
        if not topology_type == const.TYPE_NW_PHYSICAL and \
            not topology_type == const.TYPE_NW_SLICE:

            logger.warn('topology type is invalid. ('+ topology_type +')')
            return HTTPResponse("GET monitoring-data({0}) error.(topology type({1}) is invalid.)"
                                .format(const.TYPE_MON_CP,topology_type), status=400)

        # required check.(HTTP GET option)
        if not param_type_list:
            logger.warn('Required HTTP GET option({0}) have not been set.'.format(const.HTTP_GET_OPT_TYPE))
            return HTTPResponse("GET monitoring-data({0}) error.(Required HTTP GET option({1}) not specified.)"
                                .format(const.TYPE_MON_CP,const.HTTP_GET_OPT_TYPE), status=400)
        
        if not param_stime:
            logger.warn('Required HTTP GET option({0}) have not been set.'.format(const.HTTP_GET_OPT_STIME))
            return HTTPResponse("GET monitoring-data({0}) error.(Required HTTP GET option({1}) not specified.)"
                                .format(const.TYPE_MON_CP,const.HTTP_GET_OPT_STIME), status=400)

        if not param_etime:
            logger.warn('Required HTTP GET option({0}) have not been set.'.format(const.HTTP_GET_OPT_ETIME))
            return HTTPResponse("GET monitoring-data({0}) error.(Required HTTP GET option({1}) not specified.)"
                                .format(const.TYPE_MON_CP,const.HTTP_GET_OPT_ETIME), status=400)

        # lock -start-
        with util.semaphore_md:
            # open topology database connection.
            tpldb_setup()     
    
            # required check.(HTTP GET option:topology)
            nw_list = []
            if not param_topol:
                # If the topology type is 'slice', topology(sliceID) is required
                if topology_type == const.TYPE_NW_SLICE:
                    logger.warn('Required HTTP GET option({0}) have not been set.'.format(const.HTTP_GET_OPT_TOPOL))
                    return HTTPResponse("GET monitoring-data({0}) error.(Required HTTP GET option({1}) not specified.)"
                                        .format(const.TYPE_MON_CP,const.HTTP_GET_OPT_TOPOL), status=400)
                
                # If the topology type is 'physical',target all network.
                elif topology_type == const.TYPE_NW_PHYSICAL:
                    nw_list = get_all_network(const.TYPE_NW_PHYSICAL)
    
                # Without network specified, if the node is specified, the value of the node are ignored.
                param_node = None
            else:
                tmp_nw = Network.query.filter(Network.network_name == param_topol).first()
                if tmp_nw:
                    nw_list.append(tmp_nw)
    
            # search target table name.
            tbl_name_list = search_target_table(logger,int(param_stime),int(param_etime),config.mon_data_cp_db)
    
            # add <topology_list>
            xml_topol_list = SubElement(xml_mon_data, const.XML_TAG_TOPOL_LIST)
    
            # connect to the database.
            db_con = db_handle.connect(config.mon_data_cp_db,config.db_addr
                                       ,config.db_port,config.db_user,config.db_pass)
        
            for nw in nw_list:
                # add <topology type=topology_type name='xxx'>
                xml_topol = SubElement(xml_topol_list, const.XML_TAG_TOPOL,
                                        {const.XML_ATTR_TYPE:nw.type,const.XML_ATTR_NAME:nw.network_name})
        
                node_list = []
                if not param_node:
                    # not specified. cover all of the node.
                    if topology_type == const.TYPE_NW_SLICE:
                        node_list = get_all_vm(nw)
                    
                    # If the topology type is 'physical',target all network.
                    elif topology_type == const.TYPE_NW_PHYSICAL:
                        node_list = get_all_srv(nw)
    
                else:
                    if topology_type == const.TYPE_NW_SLICE:
                        tmp_node = Node.query.filter(Node.node_name == param_node)\
                                     .filter(Node.network_name == nw.network_name)\
                                     .filter(Node.type == const.TYPE_NODE_VM).first()
                    
                    # If the topology type is 'physical',target all network.
                    elif topology_type == const.TYPE_NW_PHYSICAL:
                        tmp_node = Node.query.filter(Node.node_name == param_node)\
                                     .filter(Node.network_name == nw.network_name)\
                                     .filter(Node.type == const.TYPE_NODE_SRV).first()
                    if tmp_node:
                        node_list.append(tmp_node)
        
                for node in node_list:
                    # add <node id='xxx' type='switch'>
                    xml_node = SubElement(xml_topol, const.XML_TAG_NODE,
                                            {const.XML_ATTR_ID:node.node_name,
                                             const.XML_ATTR_TYPE:node.type
                                                })
            
                    for param_type in param_type_list:
                        # add <parameter type=param_type>
                        xml_param = SubElement(xml_node, const.XML_TAG_PARAM, {const.XML_ATTR_TYPE:param_type})
    
                        # no data in the search time-range.
                        if not tbl_name_list:
                            continue
    
                        # search database.(metaID)
                        sql = "(SELECT metaID FROM metaData " \
                               + "WHERE network_name='{0}' AND node_name='{1}' AND type='{2}')"\
                                    .format(nw.network_name,node.node_name,param_type)
                        logger.debug(sql)
                        db_con.execute(sql)
                         
                        res_metaid = db_con.fetchone()
                        if res_metaid:
                            metaid = res_metaid['metaID']
                        else:
                            continue
                
                        # search database.(data)
                        sql_base = "(SELECT timestamp,value FROM {0} WHERE metaID={1}"\
                                    + " AND timestamp >= {0}".format(param_stime)\
                                    + " AND timestamp <= {0})".format(param_etime)
                        sql_list = []
                        for tbl_name in tbl_name_list:
                            sql_list.append(sql_base.format(tbl_name,metaid))
                        sql = " UNION ".join(sql_list)
                        sql += " ORDER BY timestamp DESC LIMIT " + param_limit
                        logger.debug(sql)
                        db_con.execute(sql)
                        res_mon_data = db_con.fetchall()
                  
                        for mon_data in res_mon_data:
                            # add <value timestamp=mon_data["timestamp"]>
                            xml_value = SubElement(xml_param, const.XML_TAG_VAL, {const.XML_ATTR_TIME_STAMP:str(mon_data["timestamp"])})
                            # add <value>mon_data["value"]</value>
                            tmp_value = util.isinteger(float(mon_data["value"]))
                            xml_value.text = str(tmp_value)
        # lock -end-

    except Exception:
        logger.exception('GET monitoring-data({0}) error.'.format(const.TYPE_MON_CP))
        return HTTPResponse("GET monitoring-data({0}) error."
                            .format(const.TYPE_MON_CP), status=500)

    finally:
        # close monitoring database connection.
        db_handle.close()

        # close topology database connection.
        tpldb_close()     
    logger.info('GET monitoring-data({0}). -end-'.format(const.TYPE_MON_CP))

    return util.prettify(xml_mon_data)
Esempio n. 3
0
def del_md(nw_name, regist_time, mon_data_db):
    #database handle.
    db_handle = DBHandle()
    try:
        logger.debug('delete monitoring-data -start-')

        #get now time.(UTC:0)
        now_time = calendar.timegm(datetime.utcnow().timetuple())

        # search target table name.
        # time-range=registration_time-now_time.
        if not regist_time:
            logger.warn('Target network can not be found.')
            return
        tbl_name_list = search_target_table(logger, int(regist_time),
                                            int(now_time), mon_data_db)

        #connect to the database.
        db_con = db_handle.connect(mon_data_db, config.db_addr, config.db_port,
                                   config.db_user, config.db_pass)

        #no data in the search time-range.
        if not tbl_name_list:
            logger.debug('no data in the search time-range.({0}-{1})'.format(
                regist_time, now_time))
            return

        # search database.(metaID)
        sql = "SELECT metaID FROM metaData WHERE network_name='{0}'".format(
            nw_name)
        logger.debug(sql)
        db_con.execute(sql)

        res_metaid_list = db_con.fetchall()
        sql_where_list = []
        for res_metaid in res_metaid_list:
            sql_where = " metaID={0} ".format(res_metaid['metaID'])
            sql_where_list.append(sql_where)

        if not sql_where_list:
            # no metaData.
            return

        # delete database.(data)
        sql_where = " OR ".join(sql_where_list)
        for tbl_name in tbl_name_list:
            sql = "DELETE FROM {0} WHERE {1}".format(tbl_name, sql_where)
            logger.debug(sql)
            db_con.execute(sql)

        # delete database.(metaData)
        sql = "DELETE FROM metaData WHERE network_name='{0}'".format(nw_name)
        logger.debug(sql)
        db_con.execute(sql)

        # delete commit.(monitoring-data and metaData)
        db_handle.commit()

    except Exception:
        logger.exception('delete monitoring-data error.')
        raise

    finally:
        #close monitoring database connection.
        db_handle.close()
        logger.debug('delete monitoring-data -end-')

    return