Manage Monitor script schedule and health checks for router
| 24 | |
| 25 | |
| 26 | class CsMonitor(CsDataBag): |
| 27 | """ Manage Monitor script schedule and health checks for router """ |
| 28 | |
| 29 | def get_basic_check_interval(self): |
| 30 | return self.dbag["health_checks_basic_run_interval"] if "health_checks_basic_run_interval" in self.dbag else 3 |
| 31 | |
| 32 | def get_advanced_check_interval(self): |
| 33 | return self.dbag["health_checks_advanced_run_interval"] if "health_checks_advanced_run_interval" in self.dbag else 0 |
| 34 | |
| 35 | def setupMonitorConfigFile(self): |
| 36 | if "config" in self.dbag: |
| 37 | procs = [x.strip() for x in self.dbag['config'].split(',')] |
| 38 | file = CsFile(MON_CONFIG) |
| 39 | for proc in procs: |
| 40 | bits = [x for x in proc.split(':')] |
| 41 | if len(bits) < 5: |
| 42 | continue |
| 43 | for i in range(0, 4): |
| 44 | file.add(bits[i], -1) |
| 45 | file.commit() |
| 46 | |
| 47 | def setupHealthCheckCronJobs(self): |
| 48 | cron_rep_basic = self.get_basic_check_interval() |
| 49 | cron_rep_advanced = self.get_advanced_check_interval() |
| 50 | cron = CsFile("/etc/cron.d/process") |
| 51 | cron.deleteLine("root /usr/bin/python /root/monitorServices.py") |
| 52 | cron.add("SHELL=/bin/bash", 0) |
| 53 | cron.add("PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin", 1) |
| 54 | if cron_rep_basic > 0: |
| 55 | cron.add("*/" + str(cron_rep_basic) + " * * * * root /usr/bin/python /root/monitorServices.py basic", -1) |
| 56 | if cron_rep_advanced > 0: |
| 57 | cron.add("*/" + str(cron_rep_advanced) + " * * * * root /usr/bin/python /root/monitorServices.py advanced", -1) |
| 58 | cron.commit() |
| 59 | |
| 60 | def setupHealthChecksConfigFile(self): |
| 61 | hc_data = {} |
| 62 | hc_data["health_checks_basic_run_interval"] = self.get_basic_check_interval() |
| 63 | hc_data["health_checks_advanced_run_interval"] = self.get_advanced_check_interval() |
| 64 | hc_data["health_checks_enabled"] = self.dbag["health_checks_enabled"] if "health_checks_enabled" in self.dbag else False |
| 65 | |
| 66 | if "excluded_health_checks" in self.dbag: |
| 67 | excluded_checks = self.dbag["excluded_health_checks"] |
| 68 | hc_data["excluded_health_checks"] = [ch.strip() for ch in excluded_checks.split(",")] if len(excluded_checks) > 0 else [] |
| 69 | else: |
| 70 | hc_data["excluded_health_checks"] = [] |
| 71 | |
| 72 | if "health_checks_config" in self.dbag: |
| 73 | hc_data["health_checks_config"] = self.dbag["health_checks_config"] |
| 74 | else: |
| 75 | hc_data["health_checks_config"] = {} |
| 76 | |
| 77 | with open(HC_CONFIG, 'w') as f: |
| 78 | json.dump(hc_data, f, ensure_ascii=False, indent=4) |
| 79 | |
| 80 | def process(self): |
| 81 | self.setupMonitorConfigFile() |
| 82 | self.setupHealthChecksConfigFile() |
| 83 | self.setupHealthCheckCronJobs() |