赞
踩
#encoding: utf-8 from aliyun.log import * import sys def get_log_index(day): logIndex = {'index_mode': 'v2', 'keys': {'content': {'alias': '', 'caseSensitive': False, 'chn': False, 'doc_value': True, 'token': [',', ' ', "'", '"', ';', '=', '(', ')', '[', ']', '{', '}', '?', '@', '&', '<', '>', '/', ':', '\n', '\t', '\r'], 'type': 'text'} }, 'log_reduce': False, 'max_text_len': 2048, 'storage': 'pg', 'ttl': day, 'lastModifyTime': 1578536382 } return logIndex def update_log(logstore): if "order" in logstore: day = 60 elif "lover" in logstore: day = 15 elif "kong" in logstore: day =3 elif "dns" in logstore: day =1 else: day = 7 client.update_logstore(project, logstore, ttl=day) print("%s,日志保存天数已更新%d" % (logstore, day)) return day def update_index(logstore,day): res = client.get_index_config(project, logstore) try: get_index = res.body["keys"] if "content" not in get_index.keys() and logstore not in logsystem_logstore_list: request = IndexConfig() request.from_json(get_log_index(day)) client.delete_index(project, logstore) client.create_index(project, logstore, request) print("%s,索引已删除重新创建" % logstore) except Exception as e: print(e) def main(): print("start ...") for logstore in logstore_list: if logstore in logsystem_logstore_list: print("log:",logstore) continue else: print(logstore) day = update_log(logstore) update_index(logstore,day) print("finish ...") if __name__ == '__main__': logsystem_logstore_list = ["k8s-event","internal-alert-history"] endpoint = 'https://cn-hangzhou.log.aliyuncs.com' # 选择与上面步骤创建Project所属区域匹配的Endpoint access_key_id = '********************************' # 使用您的阿里云访问密钥AccessKeyId access_key = '**********************************' # 使用您的阿里云访问密钥AccessKeySecret #project = "k8s-log-cc0a555aae9254132b45d06792c3eaa99" #test 集群 project = sys.argv[1] client = LogClient(endpoint, access_key_id, access_key) req1 = ListLogstoresRequest(project) res1 = client.list_logstores(req1) logstore_list = res1.logstores main()
最后把该脚本加入定时任务,再也不用手动修改了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。