ちょっとした監視をしたいときに使える監視スクリプトフレームワークのサンプルです。
実際に使うには細かいところの作りこみが必要です。
from multiprocessing import Process import time import json monitor_success = 0 monitor_failed = 1 limit_failed_count = 3 def monitor_network(info): # to do. ping. return monitor_failed def monitor_proc(cate, name, info): f = open('tmp-' + name + '-result.json', 'r') json_dict = json.load(f) f.close() last_failed_count = json_dict["failed"] # Monitoring if cate == "network": result = monitor_network(info) if result == monitor_failed: last_failed_count = last_failed_count + 1 else: last_failed_count = 0 json_dict["failed"] = last_failed_count f = open('tmp-' + name + '-result.json', 'w') json.dump(json_dict, f) f.close() def monitor(target_cate_list): f = open('target.json', 'r') json_dict = json.load(f) f.close() p_monitor_list=[] for target_cate in target_cate_list: for target in json_dict[target_cate]: p_monitor = Process(target=monitor_proc, args=(target_cate, target['name'], target,)) p_monitor_list.append(p_monitor) for p_monitor in p_monitor_list: p_monitor.start() for p_monitor in p_monitor_list: p_monitor.join() def judge(target_cate_list): f = open('target.json', 'r') json_dict = json.load(f) f.close() is_alert = False alert_target = [] for target_cate in target_cate_list: for target in json_dict[target_cate]: f = open('tmp-' + target['name'] + '-result.json', 'r') tmp_json_dict = json.load(f) f.close() if tmp_json_dict["failed"] >= limit_failed_count: is_alert = True alert_target.append(target['name']) if is_alert: print("System is abnormal") print(alert_target) else: print("System is normal") if __name__ == "__main__": target_cate_list=['network'] monitor(target_cate_list) judge(target_cate_list)