def monitor_alert(): while True: r_time = (datetime.datetime.now() - datetime.timedelta(minutes=INTERVAL)).strftime("%Y-%m-%d %H:%M:%S") _, rt_list = dbutils.db_operating(SELECT_MONITOR_HOST, True, (r_time,)) rt_dict = {} for line in rt_list: rt_dict.setdefault(line[0], {'cpu' : 0, 'mem' : 0, 'disk' : 0}) for key, value in SELECT_MONITOR_HOST_COLUMNS.items(): if line[key] > policies[value]['ceil']: rt_dict[line[0]][value] += 1 for key in rt_dict: messages = [] for resource in SELECT_MONITOR_HOST_COLUMNS.values(): if rt_dict[key][resource] > policies[resource]['count']: messages.append('%s超过%s分钟内%s次超过阀值%s%%' % (resource, INTERVAL, policies[resource]['count'], policies[resource]['ceil'])) if messages: asset = model.get_asset_by_ip(key) admin = asset.get('admin', DEFAULT_ADMIN) # if admin != DEFAULT_ADMIN: # admin = [admin, DEFAULT_ADMIN] c_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") dbutils.db_operating(SQL_ALERT_CREATE, False, (key, ','.join(messages), admin, c_time)) RET = mail.mail(admin, '资源告警', key + '<br/>' + '<br/>'.join(messages)) print("start mail", RET) time.sleep(3)
def monitor_host_list(ip): start_time = (datetime.datetime.now() - datetime.timedelta(days=20)).strftime('%Y-%m-%d %H:%M:%S') _, rt_list = dbutils.db_operating(SQL_MONITOR_HOST_LIST, True, (ip, start_time)) categoy_list, cpu_list, disk_list, mem_list = [], [], [], [] for line in rt_list: categoy_list.append(line[4].strftime('%H:%M')) cpu_list.append(line[1]) disk_list.append(line[3]) mem_list.append(line[2]) result = { 'categories': categoy_list, 'serial': [{ 'name': 'CPU', 'data': cpu_list }, { 'name': '磁盘', 'data': disk_list }, { 'name': '内存', 'data': mem_list }] } return result
def get_user_by_id(cls, uid): cnt, record = dbutils.db_operating(cls.SQL_GET_USER_BY_ID, True, (uid, )) if len(record) != 0: record = record[0] return dict(zip(cls.SQL_GET_USER_BY_ID_COLUMS, record)) if record else {}
def get_asset_by_ip(ip): _, assets = dbutils.db_operating(SQL_GET_ASSET_BY_IP, True, (ip)) asset = dict(zip(SQL_GET_ASSET_BY_IP_COLUMS, assets[0])) for key in ('time_on_shelves', 'over_guaranteed_date'): if asset[key]: asset[key] = asset[key].strftime('%Y-%m-%d') return asset
def get_log_analysis(topn): _, res_list = dbutils.db_operating(SQL_GET_LOG_ANALYSIS, True, (topn, )) req = [] for line in res_list: res = dict(zip(SQL_GET_LOG_ANALYSIS_COLUMS, line)) req.append(res) return req
def log_code_time_dist(): sql2 = "select code, count(*) as count, date_format(a_time, '%%Y-%%m-%%d %%H:00:00') as time from log group by time, code limit 100;" _, rt_list = dbutils.db_operating(sql2, True) legend = [] xAxis = [] series = [] temp_data = {} for line in rt_list: if line[0] not in legend: legend.append(line[0]) if line[2] not in xAxis: xAxis.append(line[2]) temp_data.setdefault(line[0], {}) temp_data[line[0]][line[2]] = line[1] xAxis.sort() for code in legend: code_time_date = [] for time in xAxis: code_time_date.append(temp_data[code].get(time, 0)) series.append({ 'name': code, 'type': 'bar', 'stack': 'code', 'data': code_time_date }) return legend, xAxis, series
def get_by_key(cls, value, key=None): sql = cls.SQL_GET_USER_BY_KEY.format( key=cls.KEY if key is None else key) _, record = dbutils.db_operating(sql, True, (value, )) if len(record) != 0: record = record[0] return dict(zip(cls.SQL_GET_USER_BY_KEY_COLUMS, record)) if record else {}
def log_code_dist(): log_code_dist_data_list = [] log_code_dist_legend_list = [] _, data = dbutils.db_operating(SQL_GET_CODE_COUNT, True) for line in data: log_code_dist_legend_list.append(line[1]) line = dict(zip(SQL_GET_CODE_COUNT_COLUMS, line)) log_code_dist_data_list.append(line) return log_code_dist_data_list, log_code_dist_legend_list
def get_moitor_log(): _, moitor_log_list = dbutils.db_operating(SQL_GET_MOITOR_LOG, True) log_list = [] for moitor_log in moitor_log_list: moitor_log = dict(zip(SQL_GET_MOITOR_LOG_COLUMS, moitor_log)) if moitor_log['c_time']: moitor_log['c_time'] = moitor_log['c_time'].strftime( '%Y-%m-%d %H:%M:%S') log_list.append(moitor_log) return log_list
def get_asset(): _, asset_list = dbutils.db_operating(SQL_GET_ASSET, True) assets = [] for asset in asset_list: asset = dict(zip(SQL_GET_ASSET_COLUMS, asset)) for key in ('time_on_shelves', 'over_guaranteed_date'): if asset[key]: asset[key] = asset[key].strftime('%Y-%m-%d') asset['machine_room_id'] = idc_tails_get( asset['machine_room_id'])['idcname'] assets.append(asset) return assets
def log_ip_distributed(): server_ip = '14.17.88.1' log_ip_distributed_geoCoord = {} log_ip_distributed_markLine = [] log_ip_distributed_markPoint = [] try: #print(app.config['GeoIP']) geo_reader = geoip2.database.Reader(app.config['GeoIP']) response = geo_reader.city(server_ip) server_name = response.city.names.get('zh-CN', '') server_lgt = response.location.longitude server_lat = response.location.latitude except Exception as e: return e finally: geo_reader.close() log_ip_distributed_geoCoord[server_name] = [server_lgt, server_lat] _, rt_list = dbutils.db_operating(SQL_log_ip_distributed1, True) for line in rt_list: log_ip_distributed_geoCoord[line[0]] = [line[1], line[2]] _, rt_list = dbutils.db_operating(SQL_log_ip_distributed2, True) #print(_, rt_list) for line in rt_list: log_ip_distributed_markLine.append([{ 'name': line[0] }, { 'name': server_name, 'value': line[1] }]) log_ip_distributed_markPoint.append({ 'name': line[0], 'value': line[1] }) return log_ip_distributed_geoCoord, log_ip_distributed_markLine, log_ip_distributed_markPoint
def delete_by_key(cls, value, key=None): sql = cls.SQL_USER_DELETE_BY_KEY.format( key=cls.KEY if key is None else key) dbutils.db_operating(sql, False, (value, )) return True
def engineroom_list(): _, rt_list = dbutils.db_operating(SQL_ENGINEROOM_LIST, True) return [ dict(zip(SQL_ENGINEROOM_LIST_COLUMS, engineroom)) for engineroom in rt_list ]
def save(self): dbutils.db_operating(self.SQL_USER_CREATE, False, (self.username, encryption.md5_str( self.password), self.age, self.email))
def user_edit_save(self): dbutils.db_operating(self.SQL_USER_EDIT_SAVE, False, (self.username, self.age, self.email, self.id))
def asset_save(asset_args): dbutils.db_operating(SQL_ASSET_SAVE, False, asset_args) return 200
def asset_update(update_args): dbutils.db_operating(SQL_ASSET_UPDATE, False, update_args) return 200
def asset_delete(id): dbutils.db_operating(SQL_ASSET_DELETE, False, (id, ))
def judgment_old_password(cls, uid, old_password): cnt, rt_list = dbutils.db_operating( cls.SET_PASSWORD_SELECT, False, (uid, encryption.md5_str(old_password))) return cnt > 0
def idcroom_delete(id): dbutils.db_operating(SQL_IDC_DELETE, False, (id, ))
def set_password(self): dbutils.db_operating(self.SET_PASSWORD_UPDATE, False, (encryption.md5_str(self.password), self.id))
def idc_view_save(idcid, idcname, area, ip_segment, machine_number): dbutils.db_operating(SQL_IDC_VIEW_SAVE, False, (idcname, area, ip_segment, machine_number, idcid))
def monitor_host_create(req): values = [] for key in ['ip', 'cpu', 'mem', 'disk', 'm_time']: values.append(req.get(key, '')) values.append(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) dbutils.db_operating(SQL_MONITOR_HOST_CREATE, False, tuple(values))
def validate_login(cls, username, password): cnt, record = dbutils.db_operating( cls.SQL_VALIDATE_LOGIN, True, (username, encryption.md5_str(password))) return dict(zip(cls.SQL_VALIDATE_LOGIN_COLUMS, record[0])) if record else None
def idc_tails_get(id): _, idc_tails = dbutils.db_operating(SQL_IDC_TAILS_GET, True, (id, )) if len(idc_tails) != 0: idc_tails = idc_tails[0] return dict(zip(SQL_IDC_TAILS_GET_COLUMS, idc_tails))
def monitor_log_delete(id): d_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") dbutils.db_operating(SQL_MONITOR_LOG_DELETE, False, (d_time, id)) return {'code': 200}
def idc_add_save(idcname, area, ip_segment, machine_number): dbutils.db_operating(SQL_IDC_ADD_SAVE, False, (idcname, area, ip_segment, machine_number))
def get_list(cls): cnt, users = dbutils.db_operating(cls.SQL_USER_LIST, True) return [dict(zip(cls.SQL_USER_LIST_COLUMS, user)) for user in users]