From a76bdb790ca06dde346c4357667e2888f8cfd1ac Mon Sep 17 00:00:00 2001 From: Usman Nasir Date: Tue, 13 Apr 2021 13:29:30 +0500 Subject: [PATCH] update monitor --- cloudAPI/cloudManager.py | 25 +++++++++++++++++ cloudAPI/views.py | 2 ++ plogical/ClusterManager.py | 56 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/cloudAPI/cloudManager.py b/cloudAPI/cloudManager.py index f2ccd09fb..0d98b6e7a 100755 --- a/cloudAPI/cloudManager.py +++ b/cloudAPI/cloudManager.py @@ -2945,3 +2945,28 @@ class CloudManager: final_dic = {'status': 0, 'fetchStatus': 0, 'error_message': str(msg)} final_json = json.dumps(final_dic) return HttpResponse(final_json) + + def UptimeMonitor(self): + try: + try: + del self.data['controller'] + del self.data['serverUserName'] + del self.data['serverPassword'] + except: + pass + + CloudConfigPath = '/home/cyberpanel/cloud' + writeToFile = open(CloudConfigPath, 'w') + writeToFile.write(json.dumps(self.data)) + writeToFile.close() + + execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/ClusterManager.py --function UptimeMonitor --type All" + ProcessUtilities.executioner(execPath) + + final_json = json.dumps({'status': 1}) + return HttpResponse(final_json) + + except BaseException as msg: + final_dic = {'status': 0, 'fetchStatus': 0, 'error_message': str(msg)} + final_json = json.dumps(final_dic) + return HttpResponse(final_json) diff --git a/cloudAPI/views.py b/cloudAPI/views.py index 72c08f0c8..758977737 100755 --- a/cloudAPI/views.py +++ b/cloudAPI/views.py @@ -50,6 +50,8 @@ def router(request): return cm.DetachCluster() elif controller == 'DebugCluster': return cm.DebugCluster() + elif controller == 'UptimeMonitor': + return cm.UptimeMonitor() elif controller == 'FetchMasterBootStrapStatus': return cm.FetchMasterBootStrapStatus() elif controller == 'FetchChildBootStrapStatus': diff --git a/plogical/ClusterManager.py b/plogical/ClusterManager.py index 50251d51a..eed2ef377 100644 --- a/plogical/ClusterManager.py +++ b/plogical/ClusterManager.py @@ -17,7 +17,9 @@ from plogical.mysqlUtilities import mysqlUtilities class ClusterManager: LogURL = "https://cloud.cyberpanel.net/HighAvailability/RecvData" + UptimeURL = "https://cloud.cyberpanel.net/servers/UptimeReport" ClusterFile = '/home/cyberpanel/cluster' + CloudConfig = '/home/cyberpanel/cloud' vhostConfPath = '/usr/local/lsws/conf/vhosts' def __init__(self, type): @@ -27,14 +29,19 @@ class ClusterManager: ipData = f.read() self.ipAddress = ipData.split('\n', 1)[0] ## - self.config = json.loads(open(ClusterManager.ClusterFile, 'r').read()) + if os.path.exists(ClusterManager.ClusterFile): + self.config = json.loads(open(ClusterManager.ClusterFile, 'r').read()) + elif os.path.exists(ClusterManager.CloudConfig): + self.config = json.loads(open(ClusterManager.CloudConfig, 'r').read()) + self.type = type def PostStatus(self, message): try: finalData = {'name': self.config['name'], 'type': self.type, 'message': message, 'token': self.config['token']} resp = requests.post(ClusterManager.LogURL, data=json.dumps(finalData), verify=False) - # logging.writeToFile(resp.text + '[info]') + if os.path.exists(ProcessUtilities.debugPath): + logging.writeToFile(resp.text + '[info]') except BaseException as msg: logging.writeToFile('%s. [31:404]' % (str(msg))) @@ -85,7 +92,6 @@ class ClusterManager: writeToFile = open(cronPath, 'a') writeToFile.write('*/5 * * * * /usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/ClusterManager.py --function CreatePendingVirtualHosts --type Child\n') writeToFile.close() - else: writeToFile = open(ClusterPath, 'w') writeToFile.write(config['ClusterConfigMaster']) @@ -133,6 +139,12 @@ class ClusterManager: except: self.PostStatus('Failed to add Firewall rules, manually open the required ports..') + ## Change permissions of cluster config path + + command = 'chmod 600 %s' % (ClusterConfigPath) + ProcessUtilities.executioner(command) + + ## self.PostStatus('Successfully attached to cluster. [200]') @@ -361,6 +373,40 @@ password=%s""" % (rootdbpassword, rootdbpassword) except BaseException as msg: self.PostStatus('Failed to debug cluster, error %s [404].' % (str(msg))) + def UptimeMonitor(self): + try: + + CentOSPath = '/etc/redhat-release' + + if os.path.exists(CentOSPath): + cronPath = '/var/spool/cron/root' + else: + cronPath = '/var/spool/cron/crontabs/root' + + writeToFile = open(cronPath, 'a') + + writeToFile.write('*/3 * * * * /usr/local/CyberCP/bin/python /usr/local/CyberCP/plogical/ClusterManager.py --function Uptime --type All\n') + writeToFile.close() + + + command = 'systemctl restart cron' + ProcessUtilities.normalExecutioner(command) + + + ### + + except BaseException as msg: + logging.writeToFile('Error while setting up Uptime cron. Error %s' % (str(msg))) + + def Uptime(self): + try: + finalData = {'name': self.config['name'], 'token': self.config['token']} + resp = requests.post(ClusterManager.LogURL, data=json.dumps(finalData), verify=False) + if os.path.exists(ProcessUtilities.debugPath): + logging.writeToFile(resp.text + '[Uptime:info]') + except BaseException as msg: + logging.writeToFile('%s. [31:404]' % (str(msg))) + def main(): parser = argparse.ArgumentParser(description='CyberPanel Installer') @@ -387,6 +433,10 @@ def main(): uc.PingNow() elif args.function == 'DebugCluster': uc.DebugCluster() + elif args.function == 'UptimeMonitor': + uc.UptimeMonitor() + elif args.function == 'Uptime': + uc.Uptime() if __name__ == "__main__":