def init_cmdb(): try: # 取host (在cmdb_host表里) # fields = ['id', 'hostname', 'ip', 'vm_status', 'st', 'uuid', 'manufacturers', 'server_type', 'server_cpu', 'os', # 'server_disk', 'server_mem', 'mac_address', 'manufacture_date', 'check_update_time', 'server_purpose', # 'server_run', 'expire', 'server_up_time'] fields = ['id','hostname','ip'] # 将角色对应的p_id都转为name,最终返回的结果p_id的值都是name hosts = db.Cursor(util.get_config(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'service.conf'), 'api')).get_results('cmdb_host', fields) for h in hosts: data = {'cmdb_hostid': h['id']} where = {'ip': h['ip']} result = db.Cursor( util.get_config(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'service.conf'), 'api')).execute_update_sql('zbhost', data, where) # 更新到cache表, ip except: return ""
def init_zabbix(): try: # 第一步 取出所有host,要ip,host,id # zb_hosts = app.config['zabbix'].get_hosts() zb_hosts = zabbix_api.Zabbix( util.get_config(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'service.conf'), 'zabbix')).get_hosts() zb_hosts_interface = zabbix_api.Zabbix( util.get_config(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'service.conf'), 'zabbix')).get_interface([z['hostid'] for z in zb_hosts]) data = [] ret = [] for h in zb_hosts: h['ip'] = zb_hosts_interface[h['hostid']] data.append(h) ###数据插入数据库 for i in data: result = db.Cursor( util.get_config(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'service.conf'), 'api')).execute_insert_sql('zbhost', i) except: return ""
#!/bin/env python # -*- encoding: utf-8 -*- from web import app import os,sys import db reload(sys) sys.setdefaultencoding('utf-8') import utils app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT' config = utils.get_config('web') #print config #将参数追加到app.config字典中,就可以随意调用了 app.config.update(config) #实例化数据库类,并将实例化的对象导入配置 app.config['cursor'] = db.Cursor(config) #print app.config if __name__ == '__main__': app.run(host=config.get('bind', '0.0.0.0'),port=int(config.get('port')), debug=True)
#!/usr/bin/python #coding:utf-8 from api import app import utils import db #导入自定义的参数,以字典形式返回 config = utils.get_config('api') #导入全局变量 app.config.update(config) #实例化db类,并将实例化的对象存入app.config app.config['db'] = db.Cursor(config) #utils.write_log('web').info('web test') #utils.write_log('api').info('api test') if __name__ == '__main__': app.run(host=app.config['bind'], port=int(app.config['port']), debug=True)
work_dir = os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))) sys.path.append(work_dir) import util import db ONE_DAY = 24 * 60 * 60 if __name__ == '__main__': conf_name = os.path.join(work_dir, 'service.conf') config = util.get_config(conf_name) now = int(time.time()) / ONE_DAY * ONE_DAY expire = time.strftime('%Y-%m-%d', time.localtime(now + 20 * ONE_DAY)) content = [] cursor = db.Cursor(config) sql = "SELECT type, expire, remark FROM soft_asset WHERE expire < '%s' ORDER BY expire" % expire cursor.execute(sql) for row in cursor.fetchall(): t = time.mktime(time.strptime(str(row[1]), '%Y-%m-%d')) last_day = (int(t) - now + 8 * 60 * 60) / ONE_DAY if last_day >= 0: msg = '[%s - %s] 到期时间:%s 剩余%s天' % (row[0].encode('utf-8'), row[2].encode('utf-8'), row[1], last_day) else: msg = '[%s - %s] 到期时间:%s 已过期%s天' % (row[0].encode('utf-8'), row[2].encode('utf-8'), row[1], -last_day) content.append(msg)
def init(): # app.config['cursor'].execute_clean_sql('zbhost') work_dir = os.path.dirname(os.path.realpath(__file__)) db.Cursor(util.get_config(os.path.join(work_dir, 'service.conf'), 'api')).execute_clean_sql('zbhost') init_zabbix() init_cmdb()