mirror of
https://github.com/ThomasGsp/HyperProxmox.git
synced 2025-03-09 15:40:18 +00:00
Add error managment
This commit is contained in:
parent
a47d251612
commit
d9de9384a2
5 changed files with 182 additions and 92 deletions
|
@ -57,56 +57,58 @@ class Analyse:
|
|||
for cluster in self.clusters_conf:
|
||||
""" Decode data """
|
||||
|
||||
user = pdecrypt(base64.b64decode(cluster["user"]),
|
||||
proxmox_cluster_user = pdecrypt(base64.b64decode(cluster["user"]),
|
||||
self.generalconf["keys"]["key_pvt"])["data"].decode('utf-8')
|
||||
|
||||
password = pdecrypt(base64.b64decode(cluster["password"]),
|
||||
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"])), user, password)
|
||||
proxmox.get_ticket("{0}:{1}".format(cluster["url"], int(cluster["port"])), proxmox_cluster_user, proxmox_cluster_pwd)
|
||||
|
||||
""" Get excluded nodes """
|
||||
exclude_nodes = cluster["exclude_nodes"]
|
||||
|
||||
""" UPDATE NODES LIST """
|
||||
nodes_list = proxmox.get_nodes("{0}:{1}".format(cluster["url"], int(cluster["port"])))["value"]
|
||||
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"]:
|
||||
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"]
|
||||
|
||||
for value_nodes_list in nodes_list["data"]:
|
||||
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"]
|
||||
totalcpu = 0
|
||||
totalram = 0
|
||||
for key_list_instances, value_list_instances in list_instances.items():
|
||||
for instances in value_list_instances:
|
||||
totalcpu = totalcpu + instances["cpus"]
|
||||
totalram = totalram + instances["maxmem"]
|
||||
|
||||
totalcpu = 0
|
||||
totalram = 0
|
||||
for key_list_instances, value_list_instances in list_instances.items():
|
||||
for instances in value_list_instances:
|
||||
totalcpu = totalcpu + instances["cpus"]
|
||||
totalram = totalram + instances["maxmem"]
|
||||
value_nodes_list["totalalloccpu"] = totalcpu
|
||||
value_nodes_list["totalallocram"] = totalram
|
||||
value_nodes_list["vmcount"] = len(list_instances.items())
|
||||
|
||||
value_nodes_list["totalalloccpu"] = totalcpu
|
||||
value_nodes_list["totalallocram"] = totalram
|
||||
value_nodes_list["vmcount"] = len(list_instances.items())
|
||||
percent_cpu_alloc = (totalcpu / value_nodes_list["maxcpu"]) * 100
|
||||
percent_ram_alloc = (totalram / value_nodes_list["mem"]) * 100
|
||||
|
||||
percent_cpu_alloc = (totalcpu / value_nodes_list["maxcpu"]) * 100
|
||||
percent_ram_alloc = (totalram / value_nodes_list["mem"]) * 100
|
||||
"""
|
||||
weight of node =
|
||||
(((Percent Alloc CPU x coef) + ( Percent Alloc RAM x coef)) / Total coef ) * Cluster weight
|
||||
"""
|
||||
weight = (((percent_cpu_alloc * 2) + (percent_ram_alloc * 4)) / 6) * int(cluster["weight"])
|
||||
|
||||
"""
|
||||
weight of node =
|
||||
(((Percent Alloc CPU x coef) + ( Percent Alloc RAM x coef)) / Total coef ) * Cluster weight
|
||||
"""
|
||||
weight = (((percent_cpu_alloc * 2) + (percent_ram_alloc * 4)) / 6) * int(cluster["weight"])
|
||||
value_nodes_list["weight"] = int(weight)
|
||||
value_nodes_list["date"] = int(insert_time)
|
||||
value_nodes_list["cluster"] = cluster["name"]
|
||||
|
||||
value_nodes_list["weight"] = int(weight)
|
||||
value_nodes_list["date"] = int(insert_time)
|
||||
value_nodes_list["cluster"] = cluster["name"]
|
||||
self.mongo.insert_node(value_nodes_list)
|
||||
|
||||
self.mongo.insert_node(value_nodes_list)
|
||||
else:
|
||||
print(nodes_list)
|
||||
|
||||
self.mongo.update_datekey(int(insert_time), "OK")
|
||||
|
||||
return
|
||||
|
||||
def set_attribution(self, count):
|
||||
|
|
|
@ -110,26 +110,65 @@ class MongoDB:
|
|||
|
||||
""" CLUSTER """
|
||||
def get_clusters_conf(self, cluster=None):
|
||||
if cluster:
|
||||
return json.loads(dumps(self.db[self.collection_clusters].find_one({"name": cluster})))
|
||||
else:
|
||||
return json.loads(dumps(self.db[self.collection_clusters].find({})))
|
||||
try:
|
||||
if cluster:
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(self.db[self.collection_clusters].find_one({"name": cluster})))
|
||||
}
|
||||
else:
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": json.loads(dumps(self.db[self.collection_clusters].find({})))
|
||||
}
|
||||
except BaseException as e:
|
||||
result = {
|
||||
"result": "ERROR",
|
||||
"value": "{0} {1}".format("Invalid request", e)
|
||||
}
|
||||
return result
|
||||
|
||||
def insert_new_cluster(self, data):
|
||||
try:
|
||||
self.db[self.collection_clusters].insert(data)
|
||||
result = {"value": "{0} {1}".format(data["name"], "is now available")}
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": "{0} {1}".format(data["name"], "is now available")
|
||||
}
|
||||
except BaseException as e:
|
||||
result = {"value": "{0} {1}".format("Invalid request", e)}
|
||||
|
||||
result = {
|
||||
"result": "ERROR",
|
||||
"value": "{0} {1}".format("Invalid request", e)
|
||||
}
|
||||
return result
|
||||
|
||||
def update_cluster(self, cluster, data):
|
||||
return self.db[self.collection_clusters].update({"vmid": str(cluster)}, {'$set': data}, upsert=False)
|
||||
try:
|
||||
self.db[self.collection_clusters].update({"vmid": str(cluster)}, {'$set': data}, upsert=False)
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": "{0} {1}".format(data["name"], "has been updated")
|
||||
}
|
||||
except BaseException as e:
|
||||
result = {
|
||||
"result": "ERROR",
|
||||
"value": "{0} {1}".format("Invalid request", e)
|
||||
}
|
||||
return result
|
||||
|
||||
def delete_cluster(self, cluster):
|
||||
return self.db[self.collection_clusters].remove({"cluster": str(cluster)})
|
||||
|
||||
try:
|
||||
self.db[self.collection_clusters].remove({"cluster": str(cluster)})
|
||||
result = {
|
||||
"result": "OK",
|
||||
"value": "{0} {1}".format(cluster, "has been deleted")
|
||||
}
|
||||
except BaseException as e:
|
||||
result = {
|
||||
"result": "ERROR",
|
||||
"value": "{0} {1}".format("Invalid request", e)
|
||||
}
|
||||
return result
|
||||
|
||||
""" SYSTEM """
|
||||
def get_system_info(self):
|
||||
|
|
|
@ -47,16 +47,16 @@ class Proxmox:
|
|||
self.csrf = {'CSRFPreventionToken': self.ticket.json()['data']['CSRFPreventionToken']}
|
||||
else:
|
||||
result = {"result": "ERROR",
|
||||
"target": "{0}".format(url),
|
||||
"target": "{0}".format(request),
|
||||
"type": "PROXMOX - STATUS CODE",
|
||||
"customerror": "Error nodes informations. Bad HTTP Status code : "
|
||||
"value": "Error nodes informations. Bad HTTP Status code : "
|
||||
"{0} -- {1}".format(self.ticket.status_code, self.ticket.text)
|
||||
}
|
||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||
result = {"result": "ERROR",
|
||||
"target": "{0}".format(url),
|
||||
"type": "PYTHON",
|
||||
"customerror": "Cannot get ticket session {0} ({1})".format(url, e)}
|
||||
"value": "Cannot get ticket session {0} ({1})".format(url, e)}
|
||||
|
||||
return result
|
||||
|
||||
|
@ -79,18 +79,18 @@ class Proxmox:
|
|||
else:
|
||||
result = {
|
||||
"result": "ERROR",
|
||||
"target": "{0}".format(url),
|
||||
"target": "{0}".format(request),
|
||||
"type": "PROXMOX - STATUS CODE",
|
||||
"customerror": "Error nodes informations. Bad HTTP Status code : "
|
||||
"value": "Error nodes informations. Bad HTTP Status code : "
|
||||
"{0} -- {1}".format(nodes.status_code, nodes.text)
|
||||
}
|
||||
|
||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||
result = {
|
||||
"result": "ERROR",
|
||||
"target": "{0}".format(url),
|
||||
"target": "{0}".format(request),
|
||||
"type": "PYTHON",
|
||||
"customerror": "Cannot get node information for {0} ({1})".format(url, e)
|
||||
"value": "Cannot get node information for {0} ({1})".format(url, e)
|
||||
}
|
||||
|
||||
return result
|
||||
|
@ -107,7 +107,7 @@ class Proxmox:
|
|||
result = {"result": "OK"}
|
||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||
result = {"result": "ERROR", "target": "[{3}]",
|
||||
"customerror": "Cannot get node information for {0} ({1})".format(url, e, nodename)}
|
||||
"value": "Cannot get node information for {0} ({1})".format(url, e, nodename)}
|
||||
|
||||
return result
|
||||
|
||||
|
@ -123,7 +123,7 @@ class Proxmox:
|
|||
result = {"result": "OK"}
|
||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||
result = {"result": "ERROR", "target": "[{3}]",
|
||||
"customerror": "Cannot get storage information for {0} ({1})".format(url, e, nodename)}
|
||||
"value": "Cannot get storage information for {0} ({1})".format(url, e, nodename)}
|
||||
|
||||
return result
|
||||
|
||||
|
@ -140,7 +140,7 @@ class Proxmox:
|
|||
result = {"result": "OK"}
|
||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||
result = {"result": "ERROR", "target": "[{3}]",
|
||||
"customerror": "Cannot get disks information for {0} ({1})".format(url, e, nodename)}
|
||||
"value": "Cannot get disks information for {0} ({1})".format(url, e, nodename)}
|
||||
|
||||
return result
|
||||
|
||||
|
@ -167,7 +167,7 @@ class Proxmox:
|
|||
"result": "ERROR",
|
||||
"target": "{0}".format(url),
|
||||
"type": "PROXMOX - STATUS CODE",
|
||||
"customerror": "Error nodes informations. Bad HTTP Status code : "
|
||||
"value": "Error nodes informations. Bad HTTP Status code : "
|
||||
"{0} -- {1}".format(instances.status_code, instances.text)
|
||||
}
|
||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||
|
@ -175,7 +175,7 @@ class Proxmox:
|
|||
"result": "ERROR",
|
||||
"target": "{0}".format(url),
|
||||
"type": "PYTHON",
|
||||
"customerror": "Cannot get VM information for {0} {1} ({2})".format(url, nodename, e)
|
||||
"value": "Cannot get VM information for {0} {1} ({2})".format(url, nodename, e)
|
||||
}
|
||||
|
||||
return result
|
||||
|
@ -201,7 +201,7 @@ class Proxmox:
|
|||
"result": "ERROR",
|
||||
"target": "{0}".format(url),
|
||||
"type": "PROXMOX - STATUS CODE",
|
||||
"customerror": "Error nodes informations. Bad HTTP Status code : "
|
||||
"value": "Error nodes informations. Bad HTTP Status code : "
|
||||
"{0} -- {1}".format(config.status_code, config.text)
|
||||
}
|
||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||
|
@ -209,7 +209,7 @@ class Proxmox:
|
|||
"result": "ERROR",
|
||||
"target": "{0}".format(url),
|
||||
"type": "PYTHON",
|
||||
"customerror": "Cannot get VM information for {0} {1} ({2})".format(url, nodename, e)
|
||||
"value": "Cannot get VM information for {0} {1} ({2})".format(url, nodename, e)
|
||||
}
|
||||
|
||||
return result
|
||||
|
@ -241,7 +241,7 @@ class Proxmox:
|
|||
"result": "ERROR",
|
||||
"target": "{0}".format(nodename),
|
||||
"type": "PROXMOX - STATUS CODE",
|
||||
"customerror": "Error creating Container. Bad HTTP Status code : "
|
||||
"value": "Error creating Container. Bad HTTP Status code : "
|
||||
"{0} -- {1}".format(createvm.status_code, createvm.text)
|
||||
}
|
||||
|
||||
|
@ -249,7 +249,7 @@ class Proxmox:
|
|||
result = {"result": "ERROR",
|
||||
"target": "{0}".format(nodename),
|
||||
"type": "PYTHON",
|
||||
"customerror": "Cannot create this instance on {0} : ({1})".format(url, e)}
|
||||
"value": "Cannot create this instance on {0} : ({1})".format(url, e)}
|
||||
|
||||
return result
|
||||
|
||||
|
@ -281,7 +281,7 @@ class Proxmox:
|
|||
"result": "ERROR",
|
||||
"target": "{0}".format(nodename),
|
||||
"type": "PROXMOX - STATUS CODE",
|
||||
"customerror": "Error delete Container. Bad HTTP Status code : "
|
||||
"value": "Error delete Container. Bad HTTP Status code : "
|
||||
"{0} -- {1}".format(deletevm.status_code, deletevm.text)
|
||||
}
|
||||
|
||||
|
@ -289,7 +289,7 @@ class Proxmox:
|
|||
result = {"result": "ERROR",
|
||||
"target": "{0}".format(nodename),
|
||||
"type": "PYTHON",
|
||||
"customerror": "Cannot delete Container ({2}) on {0} : ({1})".format(url, e, vmid)}
|
||||
"value": "Cannot delete Container ({2}) on {0} : ({1})".format(url, e, vmid)}
|
||||
|
||||
return result
|
||||
|
||||
|
@ -326,7 +326,7 @@ class Proxmox:
|
|||
"result": "ERROR",
|
||||
"target": "{0}".format(nodename),
|
||||
"type": "PROXMOX - STATUS CODE",
|
||||
"customerror": "Error action Container. Bad HTTP Status code : "
|
||||
"value": "Error action Container. Bad HTTP Status code : "
|
||||
"{0} -- {1}".format(statusm.status_code, statusm.text)
|
||||
}
|
||||
|
||||
|
@ -334,7 +334,7 @@ class Proxmox:
|
|||
result = {"result": "ERROR",
|
||||
"target": "{0}".format(nodename),
|
||||
"type": "PYTHON",
|
||||
"customerror": "Cannot do this action this instance ({2}) on {0} : ({1})".format(url, e, vmid)}
|
||||
"value": "Cannot do this action this instance ({2}) on {0} : ({1})".format(url, e, vmid)}
|
||||
|
||||
return result
|
||||
|
||||
|
@ -366,7 +366,7 @@ class Proxmox:
|
|||
"result": "ERROR",
|
||||
"target": "{0}".format(nodename),
|
||||
"type": "PROXMOX - STATUS CODE",
|
||||
"customerror": "Error resizing container. Bad HTTP Status code : "
|
||||
"value": "Error resizing container. Bad HTTP Status code : "
|
||||
"{0} -- {1}".format(resizevm.status_code, resizevm.text)
|
||||
}
|
||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||
|
@ -374,7 +374,7 @@ class Proxmox:
|
|||
"result": "ERROR",
|
||||
"target": "{0}".format(nodename),
|
||||
"type": "PYTHON",
|
||||
"customerror": "Cannot resize this instance {2} on {0} : ({1})".format(url, e, instanceid)
|
||||
"value": "Cannot resize this instance {2} on {0} : ({1})".format(url, e, instanceid)
|
||||
}
|
||||
|
||||
return result
|
||||
|
@ -401,7 +401,7 @@ class Proxmox:
|
|||
"result": "ERROR",
|
||||
"target": "{0}".format(nodename),
|
||||
"type": "PROXMOX - STATUS CODE",
|
||||
"customerror": "Error to find statistic for instance {2}. Bad HTTP Status code : "
|
||||
"value": "Error to find statistic for instance {2}. Bad HTTP Status code : "
|
||||
"{0} -- {1}".format(statsvm.status_code, statsvm.text, instanceid)
|
||||
}
|
||||
except (TypeError, ValueError, requests.exceptions.RequestException) as e:
|
||||
|
@ -409,7 +409,7 @@ class Proxmox:
|
|||
"result": "ERROR",
|
||||
"target": "{0}".format(nodename),
|
||||
"type": "PYTHON",
|
||||
"customerror": "Cannot find statistic for this instance {2} on {0} : ({1})".format(url, e, instanceid)
|
||||
"value": "Cannot find statistic for this instance {2} on {0} : ({1})".format(url, e, instanceid)
|
||||
}
|
||||
|
||||
return result
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue