Commit 0dd53d40 by Gregory Nagy

pep8 namingconvention + cpu_usage, ram_usage

parent 2b8a9a59
...@@ -3,174 +3,170 @@ import os ...@@ -3,174 +3,170 @@ import os
class GraphiteHandler: class GraphiteHandler:
def __init__(self):
if os.getenv("NODE_MONITOR_SERVER") is "":
raise RuntimeError
self.__server_name = os.getenv("NODE_MONITOR_SERVER")
if os.getenv("NODE_MONITOR_PORT") is "":
raise RuntimeError
self.__server_port = os.getenv("NODE_MONITOR_PORT")
self.__queries = []
self.__responses = []
def put(self, query): def __init__(self):
self.__queries.append(query) if os.getenv("NODE_MONITOR_SERVER") is "":
raise RuntimeError
self.__server_name = os.getenv("NODE_MONITOR_SERVER")
def cleanUpQueries(self): if os.getenv("NODE_MONITOR_PORT") is "":
self.__queries = [] raise RuntimeError
self.__server_port = os.getenv("NODE_MONITOR_PORT")
self.__queries = []
def cleanUpResponses(self): self.__responses = []
self.__responses = []
def put(self, query):
self.__queries.append(query)
def isEmpty(self):
return len(self.__queries) is 0 def cleanUpQueries(self):
self.__queries = []
def generateAll(self): def cleanUpResponses(self):
""" self.__responses = []
Regenerate the queries before sending.
""" def isEmpty(self):
for query in self.__queries: return len(self.__queries) is 0
query.generate()
def generateAll(self):
"""
def send(self): Regenerate the queries before sending.
""" """
Generates the corrent query for the Graphite webAPI and flush all the for query in self.__queries:
queries in the fifo. query.generate()
Important: After sending queries to the server the fifo will lost its
content. def send(self):
""" """
url_base = "http://%s:%s/render?" % (self.__server_name, Generates the corrent query for the Graphite webAPI and flush all the
self.__server_port) queries in the fifo.
for query in self.__queries: Important: After sending queries to the server the fifo will lost its
response = requests.get(url_base + query.getGenerated()) content.
if query.getFormat() is "json": """
self.__responses.append(response.json()) # DICT url_base = "http://%s:%s/render?" % (self.__server_name,
else: self.__server_port)
self.__responses.append(response) for query in self.__queries:
self.cleanUpQueries() response = requests.get(url_base + query.getGenerated())
if query.getFormat() is "json":
self.__responses.append(response.json()) # DICT
def pop(self): else:
""" self.__responses.append(response)
Pop the first query has got from the server. self.cleanUpQueries()
"""
try: def pop(self):
return self.__responses.pop(0) # Transform to dictionary """
except: Pop the first query has got from the server.
raise RuntimeError """
try:
return self.__responses.pop(0) # Transform to dictionary
except:
raise RuntimeError
class Query: class Query:
def __init__(self):
""" def __init__(self):
Query initializaion: """
default format is json dictionary Query initializaion:
keys: ("target <string>","datapoints <list>") default format is json dictionary
""" keys: ("target <string>","datapoints <list>")
self.__target = "" """
self.__metric = "" self.__target = ""
self.__start = "" self.__metric = ""
self.__end = "" self.__start = ""
self.__function = "" self.__end = ""
self.__response_format = "json" self.__function = ""
self.__generated = "" self.__response_format = "json"
self.__generated = ""
def setTarget(self, target):
""" def setTarget(self, target):
Hostname of the target we should get the information from. """
After the hostname you should use the domain the target is in. Hostname of the target we should get the information from.
Example: "foo.foodomain.domain.com.DOMAIN" where DOMAIN is After the hostname you should use the domain the target is in.
the root of the graphite server. Example: "foo.foodomain.domain.com.DOMAIN" where DOMAIN is
""" the root of the graphite server.
self.__target = '.'.join(target.split('.')[::-1]) """
self.__target = '.'.join(target.split('.')[::-1])
def getTarget(self):
return self.__target def getTarget(self):
return self.__target
def setMetric(self, metric):
self.__metric = metric def setMetric(self, metric):
self.__metric = metric
def getMetric(self):
return self.__metric def getMetric(self):
return self.__metric
def setAbsoluteStart(self, year, month, day, hour, minute):
""" def setAbsoluteStart(self, year, month, day, hour, minute):
Function for setting the time you want to get the reports from. """
""" Function for setting the time you want to get the reports from.
if (len(year) > 4 or len(year) < 2): """
raise if (len(year) > 4 or len(year) < 2):
self.__start = hour + ":" + minute + "_" + year + month + day raise
self.__start = hour + ":" + minute + "_" + year + month + day
def setRelativeStart(self, value, scale):
""" def setRelativeStart(self, value, scale):
Function for setting the time you want to get the reports from. """
""" Function for setting the time you want to get the reports from.
if (scale not in ["years", """
"months", "days", "hours", "minutes", "seconds"]): if (scale not in ["years",
raise "months", "days", "hours", "minutes", "seconds"]):
self.__start = "-" + str(value) + scale raise
self.__start = "-" + str(value) + scale
def getStart(self):
return self.__start def getStart(self):
return self.__start
def setAbsoluteEnd(self, year, month, day, hour, minute):
""" def setAbsoluteEnd(self, year, month, day, hour, minute):
Function for setting the time until you want to get the reports from. """
""" Function for setting the time until you want to get the reports from.
if (len(year) > 4 or len(year) < 2): """
raise if (len(year) > 4 or len(year) < 2):
self.__end = hour + ":" + minute + "_" + year + month + day raise
self.__end = hour + ":" + minute + "_" + year + month + day
def setRelativeEnd(self, value, scale):
""" def setRelativeEnd(self, value, scale):
Function for setting the time until you want to get the reports from. """
""" Function for setting the time until you want to get the reports from.
if (scale not in ["years", """
"months", "days", "hours", "minutes", "seconds"]): if (scale not in ["years",
raise "months", "days", "hours", "minutes", "seconds"]):
self.__end = "-" + str(value) + scale raise
self.__end = "-" + str(value) + scale
def getEnd(self):
return self.__end def getEnd(self):
return self.__end
def setFormat(self, fmat):
""" def setFormat(self, fmat):
Function for setting the format of the response from the server. """
Valid values: ["csv", "raw", "json"] Function for setting the format of the response from the server.
""" Valid values: ["csv", "raw", "json"]
valid_formats = ["csv", "raw", "json"] """
if fmat not in valid_formats: valid_formats = ["csv", "raw", "json"]
raise if fmat not in valid_formats:
self.__response_format = fmat raise
self.__response_format = fmat
def getFormat(self):
return self.__response_format def getFormat(self):
return self.__response_format
def generate(self):
""" def generate(self):
You must always call this function before sending the metric to the """
server for it generates the valid format that the graphite API can You must always call this function before sending the metric to the
parse. server for it generates the valid format that the graphite API can
""" parse.
tmp = "target=" + self.__target + "." + self.__metric """
if len(self.__start) is not 0: tmp = "target=" + self.__target + "." + self.__metric
tmp = tmp + "&from=" + self.__start if len(self.__start) is not 0:
if len(self.__end) is not 0: tmp = tmp + "&from=" + self.__start
tmp = tmp + "&until=" + self.__end if len(self.__end) is not 0:
tmp = tmp + "&format=" + self.__response_format tmp = tmp + "&until=" + self.__end
self.__generated = tmp tmp = tmp + "&format=" + self.__response_format
return self.__generated self.__generated = tmp
return self.__generated
def getGenerated(self):
""" def getGenerated(self):
Returns the generated query string. """
Throws exception if it haven't been done yet. Returns the generated query string.
""" Throws exception if it haven't been done yet.
if len(self.__generated) is 0: """
raise if len(self.__generated) is 0:
return self.__generated raise
return self.__generated
...@@ -16,6 +16,9 @@ from firewall.models import Host ...@@ -16,6 +16,9 @@ from firewall.models import Host
from ..tasks import vm_tasks from ..tasks import vm_tasks
from .common import Trait from .common import Trait
from monitor.calvin import Query
from monitor.calvin import GraphiteHandler
logger = getLogger(__name__) logger = getLogger(__name__)
...@@ -60,6 +63,7 @@ class Node(TimeStampedModel): ...@@ -60,6 +63,7 @@ class Node(TimeStampedModel):
def num_cores(self): def num_cores(self):
"""Number of CPU threads available to the virtual machines. """Number of CPU threads available to the virtual machines.
""" """
return self.remote_query(vm_tasks.get_core_num) return self.remote_query(vm_tasks.get_core_num)
@property @property
...@@ -93,13 +97,11 @@ class Node(TimeStampedModel): ...@@ -93,13 +97,11 @@ class Node(TimeStampedModel):
def get_remote_queue_name(self, queue_id): def get_remote_queue_name(self, queue_id):
return self.host.hostname + "." + queue_id return self.host.hostname + "." + queue_id
def remote_query(self, task, timeout=1, raise_=False, default=None): def remote_query(self, task, timeout=30, raise_=False, default=None):
"""Query the given task, and get the result. """Query the given task, and get the result.
If the result is not ready in timeout secs, return default value or If the result is not ready in timeout secs, return default value or
raise a TimeoutError.""" raise a TimeoutError."""
if task != vm_tasks.ping and not self.online:
return default
r = task.apply_async( r = task.apply_async(
queue=self.get_remote_queue_name('vm'), expires=timeout + 60) queue=self.get_remote_queue_name('vm'), expires=timeout + 60)
try: try:
...@@ -110,6 +112,35 @@ class Node(TimeStampedModel): ...@@ -110,6 +112,35 @@ class Node(TimeStampedModel):
else: else:
return default return default
def get_monitor_info(self):
query = Query()
handler = GraphiteHandler()
query.setTarget(self.host.hostname + ".circle")
query.setFormat("json")
query.setRelativeStart(5, "minutes")
metrics = ["cpu.usage", "memory.usage", "network.bytes_sent",
"network.bytes_received"]
collected = {}
for metric in metrics:
query.setMetric(metric)
query.generate()
handler.put(query)
handler.send()
for metric in metrics:
response = query.pop()
length = len(response[0]["datapoints"])
cache = response[0]["datapoints"][length - 1][0]
if cache is None:
cache = 0
collected[metric] = cache
return collected
def cpu_usage(self):
return self.get_monitor_info()["cpu.usage"]
def ram_usage(self):
return self.get_monitor_info()["memory.usage"]
def update_vm_states(self): def update_vm_states(self):
domains = {} domains = {}
for i in self.remote_query(vm_tasks.list_domains_info, timeout=5): for i in self.remote_query(vm_tasks.list_domains_info, timeout=5):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment