Commit 99be2c47 by Gregory Nagy

Debug messages and exception handling added.

parent ba7f4804
...@@ -21,13 +21,31 @@ class Client: ...@@ -21,13 +21,31 @@ class Client:
hostname.reverse() hostname.reverse()
self.name = "circle." + ".".join(hostname) self.name = "circle." + ".".join(hostname)
self.server_address = str(os.getenv("GRAPHITE_SERVER_ADDRESS")) self.server_address = str(os.getenv("GRAPHITE_SERVER_ADDRESS"))
self.server_port = int(os.getenv("GRAPHITE_SERVER_PORT").__str__()) self.server_port = int(os.getenv("GRAPHITE_SERVER_PORT"))
self.debugMode = config["debugMode"] self.debugMode = config["debugMode"]
self.amqp_user = str(os.getenv("GRAPHITE_AMQP_USER")) self.amqp_user = str(os.getenv("GRAPHITE_AMQP_USER"))
self.amqp_pass = str(os.getenv("GRAPHITE_AMQP_PASSWORD")) self.amqp_pass = str(os.getenv("GRAPHITE_AMQP_PASSWORD"))
self.amqp_queue = str(os.getenv("GRAPHITE_AMQP_QUEUE")) self.amqp_queue = str(os.getenv("GRAPHITE_AMQP_QUEUE"))
self.amqp_vhost = str(os.getenv("GRAPHITE_AMQP_VHOST")) self.amqp_vhost = str(os.getenv("GRAPHITE_AMQP_VHOST"))
self.beat = 1 self.beat = 1
if self.server_address is None:
print("GRAPHITE_SERVER_ADDRESS cannot be found in environmental "
"variables"
)
if self.server_port is None:
print("GRAPHITE_SERVER_PORT cannot be found in environmental "
"variables. (AMQP standard is: 5672"
)
if self.amqp_user is None or self.amqp_user is None:
print("GRAPHITE_AMQP_USER or GRAPHITE_AMQP_PASSWORD cannot be "
"found in environmental variables. (AMQP standard is: "
"guest-guest)"
)
if self.amqp_queue is None or self.amqp_vhost is None:
print("GRAPHITE_AMQP_QUEUE or GRAPHITE_AMQP_VHOST cannot be "
"found in environmental variables."
)
def __connect(self): def __connect(self):
""" """
...@@ -43,22 +61,36 @@ class Client: ...@@ -43,22 +61,36 @@ class Client:
self.connection = pika.BlockingConnection(params) self.connection = pika.BlockingConnection(params)
self.channel = self.connection.channel() self.channel = self.connection.channel()
return True return True
except: except RuntimeError:
raise print ("[ERROR] Cannot connect to the server. "
"Parameters could be wrong."
)
def __disconnect(self): def __disconnect(self):
""" """
Break up the connection to the graphite server. Break up the connection to the graphite server.
""" """
try:
self.channel.close() self.channel.close()
self.connection.close() self.connection.close()
except RuntimeError:
print("[ERROR] An error has occured while disconnecting from the "
"server."
)
def __send(self, message): def __send(self, message):
""" """
Send the message given in the parameters. Send the message given in the parameters.
""" """
try:
self.channel.basic_publish(exchange=self.amqp_queue, self.channel.basic_publish(exchange=self.amqp_queue,
routing_key='', body="\n".join(message)) routing_key='', body="\n".join(message))
return True
except:
print("[ERROR] An error has occured while sending metrics to the "
"server."
)
return False
def __collectFromNode(self, metricCollectors): def __collectFromNode(self, metricCollectors):
""" """
...@@ -88,7 +120,8 @@ class Client: ...@@ -88,7 +120,8 @@ class Client:
if cmd_param == "-m"] if cmd_param == "-m"]
running_vms.append([entry.as_dict()["cmdline"][search[0] + 1], running_vms.append([entry.as_dict()["cmdline"][search[0] + 1],
entry.pid, entry.pid,
int(entry.as_dict()["cmdline"][memory[0] + 1])]) int(entry.as_dict()["cmdline"][
memory[0] + 1])])
for vm in running_vms: for vm in running_vms:
vm_proc = psutil.Process(vm[1]) vm_proc = psutil.Process(vm[1])
metrics.append((self.name + "." + "kvm." + metrics.append((self.name + "." + "kvm." +
...@@ -118,10 +151,11 @@ class Client: ...@@ -118,10 +151,11 @@ class Client:
modul to work properly. modul to work properly.
""" """
if self.__connect() is False: if self.__connect() is False:
print("An error has occured while connecting to the server on %s" print("[ERROR] An error has occured while connecting to the "
"server on %s."
% (self.server_address + ":" + str(self.server_port))) % (self.server_address + ":" + str(self.server_port)))
else: else:
print("Connection established to %s on port %s. \ print("[SUCCESS] Connection established to %s on port %s. \
Clientname: %s" Clientname: %s"
% (self.server_address, self.server_port, % (self.server_address, self.server_port,
self.name)) self.name))
...@@ -129,15 +163,18 @@ class Client: ...@@ -129,15 +163,18 @@ class Client:
maxFrequency = self.getMaxFrequency(metricCollectors) maxFrequency = self.getMaxFrequency(metricCollectors)
while True: while True:
metrics = self.__collectFromNode(metricCollectors) metrics = self.__collectFromNode(metricCollectors)
metrics.append(self.__collectFromVMs()) vmMetrics = self.__collectFromVMs()
if self.debugMode == "True": if len(vmMetrics) is not 0:
print(metrics) metrics.append(vmMetrics)
self.__send(metrics) if self.debugMode == "True" and len(metrics) is not 0:
print("[*" + self.beat + "] " + metrics)
if self.__send(metrics) is False:
raise RuntimeError
time.sleep(1) time.sleep(1)
self.beat = self.beat + 1 self.beat = self.beat + 1
if ((self.beat % (maxFrequency + 1)) is 0): if ((self.beat % (maxFrequency + 1)) is 0):
self.beat = 1 self.beat = 1
except KeyboardInterrupt: except KeyboardInterrupt:
print("Reporting has stopped by the user. Exiting...") print("[x] Reporting has stopped by the user. Exiting...")
finally: finally:
self.__disconnect() self.__disconnect()
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