|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
"""Abstract classes that define the reader and writer plugin base classes. |
|
3
|
|
|
|
|
4
|
|
|
.. moduleauthor:: Kenny Freeman <[email protected]> |
|
5
|
|
|
|
|
6
|
|
|
""" |
|
7
|
|
|
__author__ = 'Kenny Freeman' |
|
8
|
|
|
__email__ = '[email protected]' |
|
9
|
|
|
__license__ = "ISCL" |
|
10
|
|
|
__docformat__ = 'reStructuredText' |
|
11
|
|
|
|
|
12
|
|
|
from abc import ABCMeta, abstractmethod |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class Plugin(object): |
|
16
|
|
|
"""The base class for all plugins. |
|
17
|
|
|
|
|
18
|
|
|
:param config: an instance of plumd.conf configuration helper |
|
19
|
|
|
:type config: conf |
|
20
|
|
|
""" |
|
21
|
|
|
__metaclass__ = ABCMeta |
|
22
|
|
|
defaults = {} |
|
23
|
|
|
|
|
24
|
|
|
def __init__(self, log, config): |
|
25
|
|
|
"""reader abstract class constructor - each plugin must sub class. |
|
26
|
|
|
|
|
27
|
|
|
:param config: an instance of plumd.conf configuration helper |
|
28
|
|
|
:type config: conf |
|
29
|
|
|
""" |
|
30
|
|
|
self.config = config |
|
31
|
|
|
self.log = log |
|
32
|
|
|
# plugins can set their own defaults byt defining self.defaults |
|
33
|
|
|
self.config.defaults(self.defaults) |
|
34
|
|
|
|
|
35
|
|
|
def __str__(self): |
|
36
|
|
|
"""Return a human readable str. |
|
37
|
|
|
:rtype: str""" |
|
38
|
|
|
s = "Plugin(class={1})".format(self.__class__.__name__) |
|
39
|
|
|
return s |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def __repr__(self): |
|
43
|
|
|
"""Return a human readable str. |
|
44
|
|
|
:rtype: str""" |
|
45
|
|
|
s = "Plugin(class={1})".format(self.__class__.__name__) |
|
46
|
|
|
return s |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
def onstart(self): |
|
50
|
|
|
"""onstart() is called before the first call to push().""" |
|
51
|
|
|
self.log.debug("Plugin: onstart: {0}".format(self.config.get('name'))) |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def onstop(self): |
|
55
|
|
|
"""onstop() is called just before the main process exits.""" |
|
56
|
|
|
self.log.debug("Plugin: onstop: {0}".format(self.config.get('name'))) |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def onreload(self): |
|
60
|
|
|
"""onreload() is called to reload configuration at runtime.""" |
|
61
|
|
|
raise NotImplementedError("todo") |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
class Reader(Plugin): |
|
65
|
|
|
"""An abstract base class that all writer pluguns subclass.""" |
|
66
|
|
|
__metaclass__ = ABCMeta |
|
67
|
|
|
|
|
68
|
|
|
@abstractmethod |
|
69
|
|
|
def poll(self): |
|
70
|
|
|
"""Reader plugins must define this method, it measures and returns a |
|
71
|
|
|
metrics string. |
|
72
|
|
|
|
|
73
|
|
|
format <name:str> <value:float|int> <timestamp:time.time()>\n[...] |
|
74
|
|
|
set to change to a python object. |
|
75
|
|
|
|
|
76
|
|
|
:rtype: str |
|
77
|
|
|
""" |
|
78
|
|
|
# this will never get called |
|
79
|
|
|
raise NotImplementedError("poll() not defined") |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
class Writer(Plugin): |
|
83
|
|
|
"""An abstract base class that all writer pluguns subclass.""" |
|
84
|
|
|
__metaclass__ = ABCMeta |
|
85
|
|
|
|
|
86
|
|
|
@abstractmethod |
|
87
|
|
|
def push(self, metrics): |
|
88
|
|
|
"""Writer plugins must define this method, it accepts a metrics string |
|
89
|
|
|
and sends to its backend(s). |
|
90
|
|
|
|
|
91
|
|
|
:param metrics: metrics collection from reader poll() call. |
|
92
|
|
|
|
|
93
|
|
|
:type metrics: str |
|
94
|
|
|
""" |
|
95
|
|
|
# this will never get called |
|
96
|
|
|
raise NotImplementedError("push() not defined") |
|
97
|
|
|
|