Commit f1892912 by Gregory Nagy

Refomated code using pep8 conventions.

manage.py: error handling added
parent 99be2c47
import sys import sys
from src import cnfparse, client, collectables from src import cnfparse, client, collectables
def main():
if (len(sys.argv)<2): def main():
if len(sys.argv) < 2:
print("usage: manage.py run [options]") print("usage: manage.py run [options]")
print("""
options:
--config <path> : path to the configuration file you intend
to start the client with
--debug : enables the debug mode and writes metrics sent to the
server
""")
if (len(sys.argv)==2 and sys.argv[1]=="run"): if len(sys.argv) is not 2 and sys.argv[1] is not "run":
configuration, metrics = cnfparse.importConf("config/client.conf") print("[ERROR] Command cannot be parsed. Exiting...")
return
configuration, metrics = cnfparse.importConf("config/client.conf")
cli = client.Client(configuration) cli = client.Client(configuration)
cli.startReporting(metricCollectors = cli.startReporting(metricCollectors=
collectables.collectables.provide(metrics)) collectables.collectables.provide(metrics))
if __name__ == "__main__": if __name__ == "__main__":
main() main()
...@@ -8,9 +8,9 @@ import collections ...@@ -8,9 +8,9 @@ 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" name = "unknown"
collector_function = 0 collector_function = 0
collector_function_arguments = {} collector_function_arguments = {}
...@@ -18,66 +18,62 @@ class Collection (object): ...@@ -18,66 +18,62 @@ class Collection (object):
@classmethod @classmethod
def harvest(cls): def harvest(cls):
query = cls.collector_function.im_func(**cls.collector_function_arguments) query = cls.collector_function.im_func(
**cls.collector_function_arguments)
if ((isinstance(query, list)) or (isinstance(query, dict))): if ((isinstance(query, list)) or (isinstance(query, dict))):
return Metrics(cls.name, return Metrics(cls.name,
query[cls.collector_function_result_attr]) query[cls.collector_function_result_attr])
elif (isinstance(query, tuple)): elif (isinstance(query, tuple)):
return Metrics(cls.name, return Metrics(cls.name,
query.__getattribute__(cls.collector_function_result_attr)) query.__getattribute__(
cls.collector_function_result_attr))
else: else:
return Metrics(cls.name, query) return Metrics(cls.name, query)
############################################################################## ##############################################################################
class std (Collection): class std(Collection):
class cpu(Collection.Group):
class cpu (Collection.Group): class usage(Collection.Group.Metric):
class usage (Collection.Group.Metric):
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):
class usage(Collection.Group.Metric): class usage(Collection.Group.Metric):
name = "memory.usage" name = "memory.usage"
collector_function = ps.virtmem_usage collector_function = ps.virtmem_usage
collector_function_result_attr = "percent" collector_function_result_attr = "percent"
class swap (Collection.Group): class swap(Collection.Group):
class usage(Collection.Group.Metric): class usage(Collection.Group.Metric):
name = "swap.usage" name = "swap.usage"
collector_function = ps.swap_memory collector_function = ps.swap_memory
collector_function_result_attr = "percent" collector_function_result_attr = "percent"
class user (Collection.Group): class user(Collection.Group):
class count(Collection.Group.Metric):
class count (Collection.Group.Metric):
name = "user.count" name = "user.count"
@classmethod @classmethod
def harvest(cls): def harvest(cls):
return Metrics(cls.name, len(ps.get_users())) return Metrics(cls.name, len(ps.get_users()))
class network (Collection.Group): class network(Collection.Group):
class packages_sent(Collection.Group.Metric):
class packages_sent (Collection.Group.Metric):
name = "network.packages_sent" name = "network.packages_sent"
collector_function = ps.network_io_counters collector_function = ps.network_io_counters
collector_function_result_attr = "packets_sent" collector_function_result_attr = "packets_sent"
class packages_received (Collection.Group.Metric): class packages_received(Collection.Group.Metric):
name = "network.packages_received" name = "network.packages_received"
collector_function = ps.network_io_counters collector_function = ps.network_io_counters
collector_function_result_attr = "packets_recv" collector_function_result_attr = "packets_recv"
class bytes_sent (Collection.Group.Metric): class bytes_sent(Collection.Group.Metric):
name = "network.bytes_sent" name = "network.bytes_sent"
collector_function = ps.network_io_counters collector_function = ps.network_io_counters
collector_function_result_attr = "bytes_sent" collector_function_result_attr = "bytes_sent"
...@@ -87,8 +83,7 @@ class std (Collection): ...@@ -87,8 +83,7 @@ class std (Collection):
collector_function = ps.network_io_counters collector_function = ps.network_io_counters
collector_function_result_attr = "bytes_recv" collector_function_result_attr = "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" name = "system.boot_time"
collector_function = ps.get_boot_time collector_function = ps.get_boot_time
\ No newline at end of file
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