This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_plugin.py
executable file
·189 lines (142 loc) · 5.21 KB
/
example_plugin.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/python
# Sample python plugin
# For more information see https://collectd.org/documentation/manpages/collectd-python.5.shtml
import sys
import time
from math import pi, sin
try:
import collectd
import logging
logging.basicConfig(level=logging.INFO)
except ImportError:
try:
import dummy_collectd as collectd
except:
pass
PLUGIN_NAME = 'hello-world'
FREQUENCY = 1.0
DATAPOINT_COUNT = 0
NOTIFICATION_COUNT = 0
PLUGIN_INSTANCE = "example[frequency=%s]"
SEND = True
def log(param):
"""
Log messages to either collectd or stdout depending on how it was called.
:param param: the message
:return: None
"""
if __name__ != '__main__':
collectd.info("%s: %s" % (PLUGIN_NAME, param))
else:
sys.stderr.write("%s\n" % param)
def config(conf):
"""
This method has been registered as the config callback and is used to parse options from
given config. Note that you cannot receive the whole config files this way, only Module blocks
inside the Python configuration block. Additionally you will only receive blocks where your
callback identifier matches your plugin.
In this case Frequency is a float value that will modify the frequency of the sine wave. This
in conjunction with the polling interval can give you as smooth or blocky a curve as you want.
:param conf: a Config object
:return: None
"""
for kv in conf.children:
if kv.key == 'Frequency':
global FREQUENCY
FREQUENCY = float(kv.values[0])
def read():
"""
This method has been registered as the read callback and will be called every polling interval
to dispatch metrics. We emit three metrics: one gauge, a sine wave; two counters for the
number of datapoints and notifications we've seen.
:return: None
"""
val = sin(time.time() * 2 * pi / 60 * FREQUENCY)
collectd.Values(plugin=PLUGIN_NAME,
type_instance="sine",
plugin_instance=PLUGIN_INSTANCE % FREQUENCY,
type="gauge",
values=[val]).dispatch()
collectd.Values(plugin=PLUGIN_NAME,
type_instance="datapoints",
type="counter",
values=[DATAPOINT_COUNT]).dispatch()
collectd.Values(plugin=PLUGIN_NAME,
type_instance="notifications",
type="counter",
values=[NOTIFICATION_COUNT]).dispatch()
global SEND
if SEND:
notif = collectd.Notification(plugin=PLUGIN_NAME,
type_instance="started",
type="objects") # need a valid type for notification
notif.severity = 4 # OKAY
notif.message = "The %s plugin has just started" % PLUGIN_NAME
notif.dispatch()
SEND = False
def init():
"""
This method has been registered as the init callback; this gives the plugin a way to do startup
actions. We'll just log a message.
:return: None
"""
log("Plugin %s initializing..." % PLUGIN_NAME)
def shutdown():
"""
This method has been registered as the shutdown callback. this gives the plugin a way to clean
up after itself before shutting down. We'll just log a message.
:return: None
"""
log("Plugin %s shutting down..." % PLUGIN_NAME)
def write(values):
"""
This method has been registered as the write callback. Let's count the number of datapoints
and emit that as a metric.
:param values: Values object for datapoint
:return: None
"""
global DATAPOINT_COUNT
DATAPOINT_COUNT += len(values.values)
def flush(timeout, identifier):
"""
This method has been registered as the flush callback. Log the two params it is given.
:param timeout: indicates that only data older than timeout seconds is to be flushed
:param identifier: specifies which values are to be flushed
:return: None
"""
log("Plugin %s flushing timeout %s and identifier %s" % PLUGIN_NAME, timeout, identifier)
def log_cb(severity, message):
"""
This method has been registered as the log callback. Don't emit log messages from within this
as you will cause a loop.
:param severity: an integer and small for important messages and high for less important messages
:param message: a string without a newline at the end
:return: None
"""
pass
def notification(notif):
"""
This method has been regstered as the notification callback. Let's count the notifications
we receive and emit that as a metric.
:param notif: a Notification object.
:return: None
"""
global NOTIFICATION_COUNT
NOTIFICATION_COUNT += 1
if __name__ != "__main__":
# when running inside plugin register each callback
collectd.register_config(config)
collectd.register_read(read)
collectd.register_init(init)
collectd.register_shutdown(shutdown)
collectd.register_write(write)
collectd.register_flush(flush)
collectd.register_log(log_cb)
collectd.register_notification(notification)
else:
# outside plugin just collect the info
read()
if len(sys.argv) < 2:
while True:
time.sleep(10)
read()