move_items.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:zabbix-scripts 作者: globocom 项目源码 文件源码
def main():
    '''
    Controls general flow of operations
    '''
    # If it exists, use the cached data of hosts and items
    if (os.path.isfile(move_items_file)):
        with open(move_items_file) as infile:
            hosts=json.load(infile)
            logger.info('Cache loaded from file (%s)' % move_items_file)
    else:
        hosts=getItems()
        with open(move_items_file, 'w') as outfile:
            json.dump(hosts, outfile)
            logger.info('Cache written to file (%s)' % move_items_file)

    for host in hosts:
        logger.info('Geting trends data of host: %s' % host['name'])
        host['trends']=list()
        host['trends_uint']=list()
        if host['itens'].__len__() > 0:
            bar=ProgressBar(maxval=host['itens'].__len__(),widgets=[Percentage(), ReverseBar(), ETA(), RotatingMarker(), Timer()]).start()
            i=0
            for item in host['itens']:
                temp=getTrends(hostname=host['name'],item=item)
                i+=1
                if args.loglevel.upper() != 'DEBUG':
                    bar.update(i)
                if temp['table'] == 'trends':
                    for value in temp['values']:
                        host['trends'].append('(%d, %d, %d, %d, %d, %d)' % (int(item['itemid']), int(value[1]), int(value[2]), int(value[3]), int(value[4]), int(value[5])))
                elif temp['table'] == 'trends_uint':
                    for value in temp['values']:
                        host['trends_uint'].append('(%d, %d, %d, %d, %d, %d)' % (int(item['itemid']), int(value[1]), int(value[2]), int(value[3]), int(value[4]), int(value[5])))
                else:
                    logger.warning('Unknown value type: %s' % temp['table'])
            bar.finish()
        '''
        Now, we send in blocks of up to ~1M values to generate the SQL files
        '''
        if host['trends'].__len__() > 0:
            createSQL(table='trends',values=host['trends'],name=host['name'])
        elif host['trends_uint'].__len__() > 0:
            createSQL(table='trends_uint',values=host['trends_uint'],name=host['name'])
        else:
            logger.warning('No data from %s found to be sent.' % host['name'])



# Start DB connection
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号