Commit bd5fb513 by Gregory Nagy

client.conf: added Debug=True

client.py: scheduler for kvm proc monitoring added, fixed bugs
metrics.py: nothing changed
parent 1b5529a0
[Client] [Client]
Debug = False Debug = True
[Metrics] [Metrics]
cpuUsage = 5 cpuUsage = 5
...@@ -11,5 +11,5 @@ packageTraffic = 10 ...@@ -11,5 +11,5 @@ packageTraffic = 10
dataTraffic = 10 dataTraffic = 10
[KVM] [KVM]
cpuUsage =5 cpuUsage = 5
memoryUsage = 5 memoryUsage = 5
[Client]
Freuency = 5
Debug = True
[Server]
Address = 127.0.0.1
Port = 5672
[AMQP]
Queue = graphite
User = guest
Pass = guest
[Metrics]
cpuUsage = True
memoryUsage = True
userCount = True
swapUsage = True
systemBootTime = False
packageTraffic = False
dataTraffic = False
...@@ -14,43 +14,60 @@ logging.basicConfig() ...@@ -14,43 +14,60 @@ logging.basicConfig()
class Client: class Client:
def __init__(self, config): def __init__(self, config):
""" """
Constructor of the client requires a configuration provided by cnfparse Constructor of the client class that is responsible for handling the
modul. It is a dictionary: {debugMode} communication between the graphite server and the data source. In
order to initialize a client you must have the following
environmental varriables:
- GRAPHITE_SERVER_ADDRESS:
- GRAPHITE_SERVER_PORT:
- GRAPHITE_AMQP_USER:
- GRAPHITE_AMQP_PASSWORD:
- GRAPHITE_AMQP_QUEUE:
- GRAPHITE_AMQP_VHOST:
Missing only one of these variables will cause the client not to work.
""" """
hostname = socket.gethostname().split('.') hostname = socket.gethostname().split('.')
hostname.reverse() hostname.reverse()
self.name = "circle." + ".".join(hostname) self.name = "circle." + ".".join(hostname)
if os.getenv("GRAPHITE_SERVER_ADDRESS") is None: if os.getenv("GRAPHITE_SERVER_ADDRESS") is "":
print("GRAPHITE_SERVER_ADDRESS cannot be found in environmental " print("GRAPHITE_SERVER_ADDRESS cannot be found in environmental "
"variables" "variables"
) )
if os.getenv("GRAPHITE_SERVER_PORT") is None: return
if os.getenv("GRAPHITE_SERVER_PORT") is "":
print("GRAPHITE_SERVER_PORT cannot be found in environmental " print("GRAPHITE_SERVER_PORT cannot be found in environmental "
"variables. (AMQP standard is: 5672" "variables. (AMQP standard is: 5672"
) )
if os.getenv("GRAPHITE_AMQP_USER") is None or os.getenv("GRAPHITE_AMQP_PASSWORD") is None: return
if os.getenv("GRAPHITE_AMQP_USER") is "" or os.getenv(
"GRAPHITE_AMQP_PASSWORD") is "":
print("GRAPHITE_AMQP_USER or GRAPHITE_AMQP_PASSWORD cannot be " print("GRAPHITE_AMQP_USER or GRAPHITE_AMQP_PASSWORD cannot be "
"found in environmental variables. (AMQP standard is: " "found in environmental variables. (AMQP standard is: "
"guest-guest)" "guest-guest)"
) )
if os.getenv("GRAPHITE_AMQP_QUEUE") is None or os.getenv("GRAPHITE_AMQP_VHOST") is None: return
if os.getenv("GRAPHITE_AMQP_QUEUE") is "" or os.getenv(
"GRAPHITE_AMQP_VHOST") is "":
print("GRAPHITE_AMQP_QUEUE or GRAPHITE_AMQP_VHOST cannot be " print("GRAPHITE_AMQP_QUEUE or GRAPHITE_AMQP_VHOST cannot be "
"found in environmental variables." "found in environmental variables."
) )
return
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")) self.server_port = int(os.getenv("GRAPHITE_SERVER_PORT"))
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.debugMode = config["debugMode"]
self.kvmCPU = int(config["kvmCpuUsage"])
self.kvmMem = int(config["kvmMemoryUsage"])
self.beat = 1 self.beat = 1
def __connect(self): def __connect(self):
""" """
This method creates the connection to the queue of the graphite server. This method creates the connection to the queue of the graphite
server using the environmental variables given in the constructor.
Returns true if the connection was successful. Returns true if the connection was successful.
""" """
try: try:
...@@ -66,10 +83,17 @@ class Client: ...@@ -66,10 +83,17 @@ class Client:
print ("[ERROR] Cannot connect to the server. " print ("[ERROR] Cannot connect to the server. "
"Parameters could be wrong." "Parameters could be wrong."
) )
return False
except:
print ("[ERROR] Cannot connect to the server. There is no one "
"listening on the other side."
)
return False
def __disconnect(self): def __disconnect(self):
""" """
Break up the connection to the graphite server. Break up the connection to the graphite server. If something went
wrong while disconnecting it simply cut the connection up.
""" """
try: try:
self.channel.close() self.channel.close()
...@@ -81,7 +105,10 @@ class Client: ...@@ -81,7 +105,10 @@ class Client:
def __send(self, message): def __send(self, message):
""" """
Send the message given in the parameters. Send the message given in the parameters given in the message
parameter. This function expects that the graphite server want the
metric name given in the message body. (This option must be enabled
on the server. Otherwise it can't parse the data sent.)
""" """
try: try:
self.channel.basic_publish(exchange=self.amqp_queue, self.channel.basic_publish(exchange=self.amqp_queue,
...@@ -96,7 +123,8 @@ class Client: ...@@ -96,7 +123,8 @@ class Client:
def __collectFromNode(self, metricCollectors): def __collectFromNode(self, metricCollectors):
""" """
It harvests the given metrics in the metricCollectors list. This list It harvests the given metrics in the metricCollectors list. This list
should be provided by the collectables modul. should be provided by the collectables modul. It is important that
only the information collected from the node is provided here.
""" """
metrics = [] metrics = []
for collector in metricCollectors: for collector in metricCollectors:
...@@ -109,6 +137,11 @@ class Client: ...@@ -109,6 +137,11 @@ class Client:
return metrics return metrics
def __collectFromVMs(self): def __collectFromVMs(self):
"""
This method is used for fetching the kvm processes running on the
node and using the cmdline parameters calculates different types of
resource usages about the vms.
"""
metrics = [] metrics = []
running_vms = [] running_vms = []
for entry in psutil.get_process_list(): for entry in psutil.get_process_list():
...@@ -125,27 +158,34 @@ class Client: ...@@ -125,27 +158,34 @@ class Client:
memory[0] + 1])]) 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." + if (self.beat % self.kvmCPU) is 0:
vm[0] + "." + "memory.usage." + metrics.append((self.name + "." + "kvm." +
" %d" % (vm_proc.get_memory_percent() / 100 * vm[2]) vm[0] + "." + "memory.usage" +
+ " %d" % (time.time()) " %d" % (
)) vm_proc.get_memory_percent() / 100 * vm[2])
metrics.append((self.name + "." + "kvm." + + " %d" % (time.time())
vm[0] + "." + "cpu.usage" + ))
" %d" % (vm_proc.get_cpu_times().system + if (self.beat % self.kvmMem) is 0:
vm_proc.get_cpu_times().user) metrics.append((self.name + "." + "kvm." +
+ " %d" % (time.time()) vm[0] + "." + "cpu.usage" +
)) " %d" % (vm_proc.get_cpu_times().system +
vm_proc.get_cpu_times().user)
+ " %d" % (time.time())
))
return metrics return metrics
def getMaxFrequency(self, metricCollectors=[]): def getMaxFrequency(self, metricCollectors=[]):
max = metricCollectors[0][1] """
for item in metricCollectors: """
items = metricCollectors + [["kvmCpuUsage", self.kvmMem], [
"kvmMemoryUsage", self.kvmCPU]]
max = items[0][1]
for item in items:
if max < item[1]: if max < item[1]:
max = item[1] max = item[1]
return max return max
def startReporting(self, metricCollectors=[], debugMode=False): def startReporting(self, metricCollectors=[]):
""" """
Call this method to start reporting to the server, it needs the Call this method to start reporting to the server, it needs the
metricCollectors parameter that should be provided by the collectables metricCollectors parameter that should be provided by the collectables
...@@ -163,13 +203,11 @@ class Client: ...@@ -163,13 +203,11 @@ class Client:
try: try:
maxFrequency = self.getMaxFrequency(metricCollectors) maxFrequency = self.getMaxFrequency(metricCollectors)
while True: while True:
metrics = self.__collectFromNode(metricCollectors) nodeMetrics = self.__collectFromNode(metricCollectors)
vmMetrics = self.__collectFromVMs() vmMetrics = self.__collectFromVMs()
if len(vmMetrics) is not 0: metrics = nodeMetrics + vmMetrics
metrics.append(vmMetrics) if self.debugMode == "True":
if self.debugMode == "True" and len(metrics) is not 0:
print(metrics) print(metrics)
if self.__send(metrics) is False: if self.__send(metrics) is False:
raise RuntimeError raise RuntimeError
time.sleep(1) time.sleep(1)
......
...@@ -39,7 +39,7 @@ class std(Collection): ...@@ -39,7 +39,7 @@ class std(Collection):
name = "cpu.usage" name = "cpu.usage"
collector_function = ps.cpu_percent collector_function = ps.cpu_percent
collector_function_arguments = { collector_function_arguments = {
'interval': 0.0, 'interval': 0.0,
} }
class memory(Collection.Group): class memory(Collection.Group):
......
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