-
Notifications
You must be signed in to change notification settings - Fork 9
/
collectd.py
147 lines (110 loc) · 4.16 KB
/
collectd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
This is a mock of the collectd framework that will be available when the plugin
actually run as a plugin to collectd. It was written based on the information
given on https://collectd.org/documentation/manpages/collectd-python.5.shtml
"""
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
import sys
class Conf(object):
""" Collectd create Conf objects from the config file (see example in
collectsphere.conf) and passes a single Conf object tree to the plugin on
configuration. """
key = None
values = ()
children = []
def __init__(self, key, values, children=None):
self.children = children
self.key = key
self.values = values
class PluginData(object):
""" This is the base class for Values and Notification classes. """
host = None
plugin = None
plugin_instance = None
time = None
type = None # as specified in types.db
type_instance = None
def __init__(
self, host=None, plugin=None, plugin_instance=None, time=None,
type=None, type_instance=None
):
self.host = host
self.plugin = plugin
self.plugin_instance = plugin_instance
self.time = time
self.type = type
self.type_instance = type_instance
data = dict()
class Values(PluginData):
""" Represents values a plugin collected and provides a function to
dispatch the information to collectd. """
values = None
meta = None
def __init__(
self, host=None, plugin=None, plugin_instance=None, time=None,
type=None, type_instance=None, values=None, meta=None
):
PluginData.__init__(
self, host, plugin, plugin_instance, time, type, type_instance
)
self.values = values
self.meta = meta
def dispatch(
self, time=None, type=None, type_instance=None, values=None,
meta=None
):
if not time:
time = self.time
if not type:
type = self.type
if not type_instance:
type_instance = self.type_instance
if not values:
values = self.values
if not meta:
meta = self.meta
if type + '-' + type_instance not in data:
data[type + '-' + type_instance] = dict()
data[type + '-' + type_instance][time] = values
elif time in data[type + '-' + type_instance]:
print "Value too old"
print(
"host=%s, plugin=%s, plugin_instance=%s, time=%s, type=%s, type_instance=%s, values=%s, meta=%s" % (
self.host, self.plugin, self.plugin_instance, time, type,
type_instance, values, meta
)
)
class Notfication(PluginData):
message = None
severity = None
def dispatch(self, type, values, plugin_instance, type_instance, plugin,
host, time, interval):
return
################################################################################
# Logging functions provided by collectd. Messages passed like this run through
# collectd's logging mechanisms.
################################################################################
def info(message):
sys.stderr.write("collectd [INFO]: " + message + "\n")
def warning(message):
sys.stderr.write("collectd [WARNING]: " + message + "\n")
def error(message):
sys.stderr.write("collectd [ERROR]: " + message + "\n")
################################################################################
# The plugin itself must register its methods, so that collectd knows which
# functions to call when. The config function is called once, so is the init
# function. The read function is called for once every couple of seconds or
# whatevery collecd's metric gathering interval is configured to be. The write
# function is not used in this project as this plugin is supposed to gather
# data not write it somewhere.
################################################################################
def register_config(func):
return
def register_init(func):
return
def register_read(callback, interval=60):
return
def register_write(func):
return
def register_shutdown(func):
return