Commit c5cc5350 by Nagy Gergő

Refactored client.

parent 3dafc1a1
...@@ -3,12 +3,12 @@ from metrics import * ...@@ -3,12 +3,12 @@ from metrics import *
class collectables: class collectables:
__collectables = { __collectables = {
"cpu.usage": [std.cpu.usage], std.cpu.usage.name: [std.cpu.usage],
"memory.usage": [std.memory.usage], std.memory.usage.name: [std.memory.usage],
"network.packages_sent": [std.network.packages_sent], std.network.packages_sent.name: [std.network.packages_sent],
"network.packages_received": [std.network.packages_received], std.network.packages_received.name: [std.network.packages_received],
"network.bytes_sent": [std.network.bytes_sent], std.network.bytes_sent.name: [std.network.bytes_sent],
"network.bytes_received": [std.network.bytes_received], std.network.bytes_received.name: [std.network.bytes_received],
"network": [std.network.bytes_sent, "network": [std.network.bytes_sent,
std.network.bytes_received, std.network.bytes_received,
std.network.packages_sent, std.network.packages_sent,
...@@ -16,15 +16,19 @@ class collectables: ...@@ -16,15 +16,19 @@ class collectables:
} }
@staticmethod @staticmethod
def list(): def listKeys():
return list(collectables.__collectables.keys()) return list(collectables.__collectables.keys())
@staticmethod @staticmethod
def keyToSet(key): def listMetricsToKey(key):
return collectables.__collectables[key] return collectables.__collectables[key]
@staticmethod @staticmethod
def provide( requests = []): def listMetricsNameToKey(key):
return [x.name for x in collectables.__collectables[key]]
@staticmethod
def provide(requests = []):
collectors = [] collectors = []
for request in requests: for request in requests:
for item in collectables.__collectables[request]: for item in collectables.__collectables[request]:
...@@ -35,6 +39,6 @@ class collectables: ...@@ -35,6 +39,6 @@ class collectables:
@staticmethod @staticmethod
def provideAll(): def provideAll():
return collectables.provide(collectables.list()) return collectables.provide(collectables.listKeys())
...@@ -7,34 +7,45 @@ import collections ...@@ -7,34 +7,45 @@ import collections
Metrics = collections.namedtuple("Metrics", ["name", "value"]) Metrics = collections.namedtuple("Metrics", ["name", "value"])
class Collection (object): class Collection (object):
class Group (object): class Group (object):
class Metric (object): class Metric (object):
name = "unknown"
collector_function = 0
collector_function_arguments = {}
collector_function_result_attr = ""
@classmethod
def harvest(cls):
query = cls.collector_function(**cls.collector_function_arguments)
if ((isinstance(query, list)) or (isinstance(query, dict))):
return Metrics(cls.name,
query[cls.collector_function_result_attr])
elif (isinstance(query,tuple)):
return Metrics(cls.name,
query.__getattribute__(cls.collector_function_result_attr))
else:
return Metrics(cls.name,
query)
@staticmethod
def harvest():
raise NotImplementedError("You must implement the harvest method.")
#################################################################################### ####################################################################################
class std (Collection): class std (Collection):
class cpu (Collection.Group): class cpu (Collection.Group):
class usage (Collection.Group.Metric): class usage (Collection.Group.Metric):
collector_function = ps.cpu_percent
@staticmethod collector_function_arguments = {
def harvest(): 'interval': 0,
return Metrics("cpu.usage", ps.cpu_percent(0)) }
name = "cpu.usage"
class memory (Collection.Group): class memory (Collection.Group):
class usage(Collection.Group.Metric): class usage(Collection.Group.Metric):
name = "memory.usage"
@staticmethod collector_function = ps.virtmem_usage
def harvest(): collector_function_result_attr = "percent"
return Metrics("memory.usage", ps.virtmem_usage().percent)
class user (Collection.Group): class user (Collection.Group):
...@@ -48,33 +59,27 @@ class std (Collection): ...@@ -48,33 +59,27 @@ class std (Collection):
class network (Collection.Group): class network (Collection.Group):
class packages_sent (Collection.Group.Metric): class packages_sent (Collection.Group.Metric):
name = "network.packages_sent"
@staticmethod collector_function = ps.network_io_counters
def harvest(): collector_function_result_attr = "packets_sent"
return Metrics("network.packages_sent", ps.network_io_counters().packets_sent)
class packages_received (Collection.Group.Metric): class packages_received (Collection.Group.Metric):
name = "network.packages_received"
@staticmethod collector_function = ps.network_io_counters
def harvest(): collector_function_result_attr = "packets_recv"
return Metrics("network.packages_received", ps.network_io_counters().packets_recv)
class bytes_sent (Collection.Group.Metric): class bytes_sent (Collection.Group.Metric):
name = "network.bytes_sent"
@staticmethod collector_function = ps.network_io_counters
def harvest(): collector_function_result_attr = "bytes_sent"
return Metrics("network.bytes_sent", ps.network_io_counters().bytes_sent)
class bytes_received(Collection.Group.Metric): class bytes_received(Collection.Group.Metric):
name = "network.bytes_received"
@staticmethod collector_function = ps.network_io_counters
def harvest(): collector_function_result_attr = "bytes_recv"
return Metrics("network.bytes_recv", ps.network_io_counters().bytes_recv)
class system (Collection.Group): class system (Collection.Group):
class boot_time (Collection.Group.Metric): class boot_time (Collection.Group.Metric):
name = "system.boot_time"
@staticmethod collector_function = ps.get_boot_time
def harvest():
return Metrics("system.boot_time", ps.get_boot_time())
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