From 579de2599a21aef9cac0f2776de68f851f7451df Mon Sep 17 00:00:00 2001 From: "thomas.guiseppin" Date: Wed, 15 Nov 2017 12:35:34 +0100 Subject: [PATCH] Some small changes --- code/scripts/main/core/core.py | 31 ++++----- code/scripts/main/core/modules/mod_analyst.py | 4 +- code/scripts/main/core/modules/mod_crawler.py | 64 +++++++++++++++++++ 3 files changed, 82 insertions(+), 17 deletions(-) create mode 100644 code/scripts/main/core/modules/mod_crawler.py diff --git a/code/scripts/main/core/core.py b/code/scripts/main/core/core.py index bf086ea..34cbad0 100644 --- a/code/scripts/main/core/core.py +++ b/code/scripts/main/core/core.py @@ -10,23 +10,23 @@ from core.modules.mod_proxmox import * from core.modules.mod_database import * from core.modules.mod_analyst import * +from core.modules.mod_crawler import * from core.modules.mod_access import * from core.libs.hcrypt import * from netaddr import iter_iprange import threading import time - import base64 def RunAnalyse(clusters_conf, generalconf, delay=300): - play = Analyse(clusters_conf, generalconf) + play = Crawler(clusters_conf, generalconf) + while True: play.run() time.sleep(delay) - class Core: def __init__(self, generalconf, Lredis): @@ -63,7 +63,7 @@ class Core: # INSTANCE MANAGEMENT # ####################### """ - def insert_instance(self, target, count=1, command_id=000000): + def insert_instance(self, target, count=1, command_id=000000, instancetype="lxc"): """ Find cluster informations from node """ lastkeyvalid = self.mongo.get_last_datekey() @@ -135,12 +135,12 @@ class Core: """ INSTANCE INSERTION """ result_new = proxmox.create_instance("{0}:{1}".format(proxmox_cluster_url, - int(proxmox_cluster_port)), target, "lxc", + int(proxmox_cluster_port)), target, instancetype, data) """ Get first digest """ digest_init = proxmox.get_config("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), - target, "lxc", next_instance_id)['value']['data']['digest'] + target, instancetype, next_instance_id)['value']['data']['digest'] """ VERIFY THE RESULT BY PROXMOX STATUS REQUEST CODE """ @@ -160,7 +160,7 @@ class Core: """ Limit creation DDOS based on digest """ while digest_init == proxmox.get_config("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), - target, "lxc", next_instance_id)['value']['data']['digest']: + target, instancetype, next_instance_id)['value']['data']['digest']: time.sleep(5) returnlistresult.append(result_new) @@ -170,7 +170,7 @@ class Core: return - def delete_instance(self, vmid): + def delete_instance(self, vmid, instancetype="lxc"): try: """ Find node/cluster informations from vmid """ @@ -194,7 +194,8 @@ class Core: proxmox_cluster_pwd) result = proxmox.delete_instance("{0}:{1}".format(proxmox_cluster_url, - int(proxmox_cluster_port)), instance_informations['node'], "lxc", vmid) + int(proxmox_cluster_port)), + instance_informations['node'], instancetype, vmid) if result['result'] == "OK": self.mongo.delete_instance(vmid) @@ -209,7 +210,7 @@ class Core: return result - def status_instance(self, vmid, action): + def status_instance(self, vmid, action, instancetype="lxc"): """ Find node/cluster informations from vmid """ try: instance_informations = self.mongo.get_instance(vmid) @@ -234,7 +235,7 @@ class Core: result = proxmox.status_instance("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), instance_informations['node'], - "lxc", + instancetype, vmid, action) except IndexError as ierror: @@ -246,7 +247,7 @@ class Core: return result - def info_instance(self, vmid): + def info_instance(self, vmid, instancetype="lxc"): """ Find node/cluster informations from vmid """ try: instance_informations = self.mongo.get_instance(vmid) @@ -271,7 +272,7 @@ class Core: result = proxmox.get_config("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), instance_informations['node'], - "lxc", + instancetype, vmid) except IndexError as ierror: @@ -283,7 +284,7 @@ class Core: return result - def change_instance(self, vmid, data): + def change_instance(self, vmid, data, instancetype="lxc"): """ Find node/cluster informations from vmid """ try: instance_informations = self.mongo.get_instance(vmid) @@ -309,7 +310,7 @@ class Core: result = proxmox.resize_instance("{0}:{1}".format(proxmox_cluster_url, int(proxmox_cluster_port)), instance_informations['node'], - "lxc", + instancetype, vmid, data) if result['result'] == "OK": diff --git a/code/scripts/main/core/modules/mod_analyst.py b/code/scripts/main/core/modules/mod_analyst.py index 509a767..fdccc32 100644 --- a/code/scripts/main/core/modules/mod_analyst.py +++ b/code/scripts/main/core/modules/mod_analyst.py @@ -49,7 +49,7 @@ class Analyse: self.mongo.client = self.mongo.connect() self.mongo.db = self.mongo.client.db - def run(self): + def run(self, instancetype="lxc"): insert_time = time.time() self.mongo.insert_datekey(insert_time, 'running') @@ -77,7 +77,7 @@ class Analyse: if value_nodes_list["node"] not in exclude_nodes: """ TOTAL COUNT CPU and RAM allocate""" list_instances = proxmox.get_instance("{0}:{1}".format(cluster["url"], int(cluster["port"])), - value_nodes_list["node"], "lxc")["value"] + value_nodes_list["node"], instancetype)["value"] totalcpu = 0 totalram = 0 diff --git a/code/scripts/main/core/modules/mod_crawler.py b/code/scripts/main/core/modules/mod_crawler.py new file mode 100644 index 0000000..6622097 --- /dev/null +++ b/code/scripts/main/core/modules/mod_crawler.py @@ -0,0 +1,64 @@ +""" +Author: Tlams +Langage: Python +Minimum version require: 3.4 + +Module function: +Crawl all node to update the database statistics +""" + +from core.modules.mod_proxmox import * +from core.modules.mod_database import * +from core.libs.hcrypt import * +import time +import operator +import random +import base64 + + +class Crawler: + def __init__(self, clusters_conf, generalconf): + """ + :param clusters_conf: Proxmox configurations + :param generalconf : General configuration + """ + self.generalconf = generalconf + self.clusters_conf = clusters_conf + + """ LOAD MONGODB """ + self.mongo = MongoDB(generalconf["mongodb"]["ip"]) + self.mongo.client = self.mongo.connect() + self.mongo.db = self.mongo.client.db + + def run(self, instancetype="lxc"): + insert_time = time.time() + + self.mongo.insert_datekey(insert_time, 'running') + + for cluster in self.clusters_conf: + """ Decode data """ + + proxmox_cluster_user = pdecrypt(base64.b64decode(cluster["user"]), + self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8') + + proxmox_cluster_pwd = pdecrypt(base64.b64decode(cluster["password"]), + self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8') + + """ AUTH """ + proxmox = Proxmox("Analyse") + proxmox.get_ticket("{0}:{1}".format(cluster["url"], int(cluster["port"])), proxmox_cluster_user, proxmox_cluster_pwd) + + """ UPDATE NODES LIST """ + nodes_list = proxmox.get_nodes("{0}:{1}".format(cluster["url"], int(cluster["port"]))) + if nodes_list["result"] == "OK": + for value_nodes_list in nodes_list["value"]["data"]: + """ TOTAL COUNT CPU and RAM allocate""" + list_instances = proxmox.get_instance("{0}:{1}".format(cluster["url"], int(cluster["port"])), + value_nodes_list["node"], instancetype)["value"] + + for key_list_instances, value_list_instances in list_instances.items(): + for instances in value_list_instances: + instances["node"] = value_nodes_list["node"] + print(instances) + + return \ No newline at end of file