Merge pull request #1890 from trenkel/master
[collectd.git] / src / python.c
1 /**
2  * collectd - src/python.c
3  * Copyright (C) 2009  Sven Trenkel
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Sven Trenkel <collectd at semidefinite.de>
25  **/
26
27 #include <Python.h>
28 #include <structmember.h>
29
30 #include <signal.h>
31
32 #include "collectd.h"
33
34 #include "common.h"
35
36 #include "cpython.h"
37
38 typedef struct cpy_callback_s {
39         char *name;
40         PyObject *callback;
41         PyObject *data;
42         struct cpy_callback_s *next;
43 } cpy_callback_t;
44
45 static char log_doc[] = "This function sends a string to all logging plugins.";
46
47 static char get_ds_doc[] = "get_dataset(name) -> definition\n"
48                 "\n"
49                 "Returns the definition of a dataset specified by name.\n"
50                 "\n"
51                 "'name' is a string specifying the dataset to query.\n"
52                 "'definition' is a list of 4-tuples. Every tuple represents a \n"
53                 "    data source within the data set and its 4 values are the \n"
54                 "    name, type, min and max value.\n"
55                 "    'name' is a string.\n"
56                 "    'type' is a string that is equal to either DS_TYPE_COUNTER,\n"
57                 "        DS_TYPE_GAUGE, DS_TYPE_DERIVE or DS_TYPE_ABSOLUTE.\n"
58                 "    'min' and 'max' are either a float or None.";
59
60 static char flush_doc[] = "flush([plugin][, timeout][, identifier]) -> None\n"
61                 "\n"
62                 "Flushes the cache of another plugin.";
63
64 static char unregister_doc[] = "Unregisters a callback. This function needs exactly one parameter either\n"
65                 "the function to unregister or the callback identifier to unregister.";
66
67 static char reg_log_doc[] = "register_log(callback[, data][, name]) -> identifier\n"
68                 "\n"
69                 "Register a callback function for log messages.\n"
70                 "\n"
71                 "'callback' is a callable object that will be called every time something\n"
72                 "    is logged.\n"
73                 "'data' is an optional object that will be passed back to the callback\n"
74                 "    function every time it is called.\n"
75                 "'name' is an optional identifier for this callback. The default name\n"
76                 "    is 'python.<module>'.\n"
77                 "    Every callback needs a unique identifier, so if you want to\n"
78                 "    register this callback multiple time from the same module you need\n"
79                 "    to specify a name here.\n"
80                 "'identifier' is the full identifier assigned to this callback.\n"
81                 "\n"
82                 "The callback function will be called with two or three parameters:\n"
83                 "severity: An integer that should be compared to the LOG_ constants.\n"
84                 "message: The text to be logged.\n"
85                 "data: The optional data parameter passed to the register function.\n"
86                 "    If the parameter was omitted it will be omitted here, too.";
87
88 static char reg_init_doc[] = "register_init(callback[, data][, name]) -> identifier\n"
89                 "\n"
90                 "Register a callback function that will be executed once after the config.\n"
91                 "file has been read, all plugins heve been loaded and the collectd has\n"
92                 "forked into the background.\n"
93                 "\n"
94                 "'callback' is a callable object that will be executed.\n"
95                 "'data' is an optional object that will be passed back to the callback\n"
96                 "    function when it is called.\n"
97                 "'name' is an optional identifier for this callback. The default name\n"
98                 "    is 'python.<module>'.\n"
99                 "    Every callback needs a unique identifier, so if you want to\n"
100                 "    register this callback multiple time from the same module you need\n"
101                 "    to specify a name here.\n"
102                 "'identifier' is the full identifier assigned to this callback.\n"
103                 "\n"
104                 "The callback function will be called without parameters, except for\n"
105                 "data if it was supplied.";
106
107 static char reg_config_doc[] = "register_config(callback[, data][, name]) -> identifier\n"
108                 "\n"
109                 "Register a callback function for config file entries.\n"
110                 "'callback' is a callable object that will be called for every config block.\n"
111                 "'data' is an optional object that will be passed back to the callback\n"
112                 "    function every time it is called.\n"
113                 "'name' is an optional identifier for this callback. The default name\n"
114                 "    is 'python.<module>'.\n"
115                 "    Every callback needs a unique identifier, so if you want to\n"
116                 "    register this callback multiple time from the same module you need\n"
117                 "    to specify a name here.\n"
118                 "'identifier' is the full identifier assigned to this callback.\n"
119                 "\n"
120                 "The callback function will be called with one or two parameters:\n"
121                 "config: A Config object.\n"
122                 "data: The optional data parameter passed to the register function.\n"
123                 "    If the parameter was omitted it will be omitted here, too.";
124
125 static char reg_read_doc[] = "register_read(callback[, interval][, data][, name]) -> identifier\n"
126                 "\n"
127                 "Register a callback function for reading data. It will just be called\n"
128                 "in a fixed interval to signal that it's time to dispatch new values.\n"
129                 "'callback' is a callable object that will be called every time something\n"
130                 "    is logged.\n"
131                 "'interval' is the number of seconds between between calls to the callback\n"
132                 "    function. Full float precision is supported here.\n"
133                 "'data' is an optional object that will be passed back to the callback\n"
134                 "    function every time it is called.\n"
135                 "'name' is an optional identifier for this callback. The default name\n"
136                 "    is 'python.<module>'.\n"
137                 "    Every callback needs a unique identifier, so if you want to\n"
138                 "    register this callback multiple time from the same module you need\n"
139                 "    to specify a name here.\n"
140                 "'identifier' is the full identifier assigned to this callback.\n"
141                 "\n"
142                 "The callback function will be called without parameters, except for\n"
143                 "data if it was supplied.";
144
145 static char reg_write_doc[] = "register_write(callback[, data][, name]) -> identifier\n"
146                 "\n"
147                 "Register a callback function to receive values dispatched by other plugins.\n"
148                 "'callback' is a callable object that will be called every time a value\n"
149                 "    is dispatched.\n"
150                 "'data' is an optional object that will be passed back to the callback\n"
151                 "    function every time it is called.\n"
152                 "'name' is an optional identifier for this callback. The default name\n"
153                 "    is 'python.<module>'.\n"
154                 "    Every callback needs a unique identifier, so if you want to\n"
155                 "    register this callback multiple time from the same module you need\n"
156                 "    to specify a name here.\n"
157                 "'identifier' is the full identifier assigned to this callback.\n"
158                 "\n"
159                 "The callback function will be called with one or two parameters:\n"
160                 "values: A Values object which is a copy of the dispatched values.\n"
161                 "data: The optional data parameter passed to the register function.\n"
162                 "    If the parameter was omitted it will be omitted here, too.";
163
164 static char reg_notification_doc[] = "register_notification(callback[, data][, name]) -> identifier\n"
165                 "\n"
166                 "Register a callback function for notifications.\n"
167                 "'callback' is a callable object that will be called every time a notification\n"
168                 "    is dispatched.\n"
169                 "'data' is an optional object that will be passed back to the callback\n"
170                 "    function every time it is called.\n"
171                 "'name' is an optional identifier for this callback. The default name\n"
172                 "    is 'python.<module>'.\n"
173                 "    Every callback needs a unique identifier, so if you want to\n"
174                 "    register this callback multiple time from the same module you need\n"
175                 "    to specify a name here.\n"
176                 "'identifier' is the full identifier assigned to this callback.\n"
177                 "\n"
178                 "The callback function will be called with one or two parameters:\n"
179                 "notification: A copy of the notification that was dispatched.\n"
180                 "data: The optional data parameter passed to the register function.\n"
181                 "    If the parameter was omitted it will be omitted here, too.";
182
183 static char reg_flush_doc[] = "register_flush(callback[, data][, name]) -> identifier\n"
184                 "\n"
185                 "Register a callback function for flush messages.\n"
186                 "'callback' is a callable object that will be called every time a plugin\n"
187                 "    requests a flush for either this or all plugins.\n"
188                 "'data' is an optional object that will be passed back to the callback\n"
189                 "    function every time it is called.\n"
190                 "'name' is an optional identifier for this callback. The default name\n"
191                 "    is 'python.<module>'.\n"
192                 "    Every callback needs a unique identifier, so if you want to\n"
193                 "    register this callback multiple time from the same module you need\n"
194                 "    to specify a name here.\n"
195                 "'identifier' is the full identifier assigned to this callback.\n"
196                 "\n"
197                 "The callback function will be called with two or three parameters:\n"
198                 "timeout: Indicates that only data older than 'timeout' seconds is to\n"
199                 "    be flushed.\n"
200                 "id: Specifies which values are to be flushed. Might be None.\n"
201                 "data: The optional data parameter passed to the register function.\n"
202                 "    If the parameter was omitted it will be omitted here, too.";
203
204 static char reg_shutdown_doc[] = "register_shutdown(callback[, data][, name]) -> identifier\n"
205                 "\n"
206                 "Register a callback function for collectd shutdown.\n"
207                 "'callback' is a callable object that will be called once collectd is\n"
208                 "    shutting down.\n"
209                 "'data' is an optional object that will be passed back to the callback\n"
210                 "    function if it is called.\n"
211                 "'name' is an optional identifier for this callback. The default name\n"
212                 "    is 'python.<module>'.\n"
213                 "    Every callback needs a unique identifier, so if you want to\n"
214                 "    register this callback multiple time from the same module you need\n"
215                 "    to specify a name here.\n"
216                 "'identifier' is the full identifier assigned to this callback.\n"
217                 "\n"
218                 "The callback function will be called with no parameters except for\n"
219                 "    data if it was supplied.";
220
221
222 static pthread_t main_thread;
223 static PyOS_sighandler_t python_sigint_handler;
224 static _Bool do_interactive = 0;
225
226 /* This is our global thread state. Python saves some stuff in thread-local
227  * storage. So if we allow the interpreter to run in the background
228  * (the scriptwriters might have created some threads from python), we have
229  * to save the state so we can resume it later after shutdown. */
230
231 static PyThreadState *state;
232
233 static PyObject *sys_path, *cpy_format_exception;
234
235 static cpy_callback_t *cpy_config_callbacks;
236 static cpy_callback_t *cpy_init_callbacks;
237 static cpy_callback_t *cpy_shutdown_callbacks;
238
239 static void cpy_destroy_user_data(void *data) {
240         cpy_callback_t *c = data;
241         free(c->name);
242         CPY_LOCK_THREADS
243         Py_DECREF(c->callback);
244         Py_XDECREF(c->data);
245         CPY_RELEASE_THREADS
246         free(c);
247 }
248
249 /* You must hold the GIL to call this function!
250  * But if you managed to extract the callback parameter then you probably already do. */
251
252 static void cpy_build_name(char *buf, size_t size, PyObject *callback, const char *name) {
253         const char *module = NULL;
254         PyObject *mod = NULL;
255
256         if (name != NULL) {
257                 snprintf(buf, size, "python.%s", name);
258                 return;
259         }
260
261         mod = PyObject_GetAttrString(callback, "__module__"); /* New reference. */
262         if (mod != NULL)
263                 module = cpy_unicode_or_bytes_to_string(&mod);
264
265         if (module != NULL) {
266                 snprintf(buf, size, "python.%s", module);
267                 Py_XDECREF(mod);
268                 PyErr_Clear();
269                 return;
270         }
271         Py_XDECREF(mod);
272
273         snprintf(buf, size, "python.%p", callback);
274         PyErr_Clear();
275 }
276
277 void cpy_log_exception(const char *context) {
278         int l = 0;
279         const char *typename = NULL, *message = NULL;
280         PyObject *type, *value, *traceback, *tn, *m, *list;
281
282         PyErr_Fetch(&type, &value, &traceback);
283         PyErr_NormalizeException(&type, &value, &traceback);
284         if (type == NULL) return;
285         tn = PyObject_GetAttrString(type, "__name__"); /* New reference. */
286         m = PyObject_Str(value); /* New reference. */
287         if (tn != NULL)
288                 typename = cpy_unicode_or_bytes_to_string(&tn);
289         if (m != NULL)
290                 message = cpy_unicode_or_bytes_to_string(&m);
291         if (typename == NULL)
292                 typename = "NamelessException";
293         if (message == NULL)
294                 message = "N/A";
295         Py_BEGIN_ALLOW_THREADS
296         ERROR("Unhandled python exception in %s: %s: %s", context, typename, message);
297         Py_END_ALLOW_THREADS
298         Py_XDECREF(tn);
299         Py_XDECREF(m);
300         if (!cpy_format_exception || !traceback) {
301                 PyErr_Clear();
302                 Py_DECREF(type);
303                 Py_XDECREF(value);
304                 Py_XDECREF(traceback);
305                 return;
306         }
307         list = PyObject_CallFunction(cpy_format_exception, "NNN", type, value, traceback); /* New reference. Steals references from "type", "value" and "traceback". */
308         if (list)
309                 l = PyObject_Length(list);
310
311         for (int i = 0; i < l; ++i) {
312                 PyObject *line;
313                 char const *msg;
314                 char *cpy;
315
316                 line = PyList_GET_ITEM(list, i); /* Borrowed reference. */
317                 Py_INCREF(line);
318
319                 msg = cpy_unicode_or_bytes_to_string(&line);
320                 Py_DECREF(line);
321                 if (msg == NULL)
322                         continue;
323
324                 cpy = strdup(msg);
325                 if (cpy == NULL)
326                         continue;
327
328                 if (cpy[strlen(cpy) - 1] == '\n')
329                         cpy[strlen(cpy) - 1] = 0;
330
331                 Py_BEGIN_ALLOW_THREADS
332                 ERROR("%s", cpy);
333                 Py_END_ALLOW_THREADS
334
335                 free(cpy);
336         }
337
338         Py_XDECREF(list);
339         PyErr_Clear();
340 }
341
342 static int cpy_read_callback(user_data_t *data) {
343         cpy_callback_t *c = data->data;
344         PyObject *ret;
345
346         CPY_LOCK_THREADS
347                 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
348                 if (ret == NULL) {
349                         cpy_log_exception("read callback");
350                 } else {
351                         Py_DECREF(ret);
352                 }
353         CPY_RELEASE_THREADS
354         if (ret == NULL)
355                 return 1;
356         return 0;
357 }
358
359 static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_list, user_data_t *data) {
360         cpy_callback_t *c = data->data;
361         PyObject *ret, *list, *temp, *dict = NULL;
362         Values *v;
363
364         CPY_LOCK_THREADS
365                 list = PyList_New(value_list->values_len); /* New reference. */
366                 if (list == NULL) {
367                         cpy_log_exception("write callback");
368                         CPY_RETURN_FROM_THREADS 0;
369                 }
370                 for (size_t i = 0; i < value_list->values_len; ++i) {
371                         if (ds->ds[i].type == DS_TYPE_COUNTER) {
372                                 PyList_SetItem(list, i, PyLong_FromUnsignedLongLong(value_list->values[i].counter));
373                         } else if (ds->ds[i].type == DS_TYPE_GAUGE) {
374                                 PyList_SetItem(list, i, PyFloat_FromDouble(value_list->values[i].gauge));
375                         } else if (ds->ds[i].type == DS_TYPE_DERIVE) {
376                                 PyList_SetItem(list, i, PyLong_FromLongLong(value_list->values[i].derive));
377                         } else if (ds->ds[i].type == DS_TYPE_ABSOLUTE) {
378                                 PyList_SetItem(list, i, PyLong_FromUnsignedLongLong(value_list->values[i].absolute));
379                         } else {
380                                 Py_BEGIN_ALLOW_THREADS
381                                 ERROR("cpy_write_callback: Unknown value type %d.", ds->ds[i].type);
382                                 Py_END_ALLOW_THREADS
383                                 Py_DECREF(list);
384                                 CPY_RETURN_FROM_THREADS 0;
385                         }
386                         if (PyErr_Occurred() != NULL) {
387                                 cpy_log_exception("value building for write callback");
388                                 Py_DECREF(list);
389                                 CPY_RETURN_FROM_THREADS 0;
390                         }
391                 }
392                 dict = PyDict_New();  /* New reference. */
393                 if (value_list->meta) {
394                         char **table;
395                         meta_data_t *meta = value_list->meta;
396
397                         int num = meta_data_toc(meta, &table);
398                         for (int i = 0; i < num; ++i) {
399                                 int type;
400                                 char *string;
401                                 int64_t si;
402                                 uint64_t ui;
403                                 double d;
404                                 _Bool b;
405
406                                 type = meta_data_type(meta, table[i]);
407                                 if (type == MD_TYPE_STRING) {
408                                         if (meta_data_get_string(meta, table[i], &string))
409                                                 continue;
410                                         temp = cpy_string_to_unicode_or_bytes(string);  /* New reference. */
411                                         free(string);
412                                         PyDict_SetItemString(dict, table[i], temp);
413                                         Py_XDECREF(temp);
414                                 } else if (type == MD_TYPE_SIGNED_INT) {
415                                         if (meta_data_get_signed_int(meta, table[i], &si))
416                                                 continue;
417                                         temp = PyObject_CallFunctionObjArgs((void *) &SignedType, PyLong_FromLongLong(si), (void *) 0);  /* New reference. */
418                                         PyDict_SetItemString(dict, table[i], temp);
419                                         Py_XDECREF(temp);
420                                 } else if (type == MD_TYPE_UNSIGNED_INT) {
421                                         if (meta_data_get_unsigned_int(meta, table[i], &ui))
422                                                 continue;
423                                         temp = PyObject_CallFunctionObjArgs((void *) &UnsignedType, PyLong_FromUnsignedLongLong(ui), (void *) 0);  /* New reference. */
424                                         PyDict_SetItemString(dict, table[i], temp);
425                                         Py_XDECREF(temp);
426                                 } else if (type == MD_TYPE_DOUBLE) {
427                                         if (meta_data_get_double(meta, table[i], &d))
428                                                 continue;
429                                         temp = PyFloat_FromDouble(d);  /* New reference. */
430                                         PyDict_SetItemString(dict, table[i], temp);
431                                         Py_XDECREF(temp);
432                                 } else if (type == MD_TYPE_BOOLEAN) {
433                                         if (meta_data_get_boolean(meta, table[i], &b))
434                                                 continue;
435                                         if (b)
436                                                 PyDict_SetItemString(dict, table[i], Py_True);
437                                         else
438                                                 PyDict_SetItemString(dict, table[i], Py_False);
439                                 }
440                                 free(table[i]);
441                         }
442                         free(table);
443                 }
444                 v = (Values *) Values_New(); /* New reference. */
445                 sstrncpy(v->data.host, value_list->host, sizeof(v->data.host));
446                 sstrncpy(v->data.type, value_list->type, sizeof(v->data.type));
447                 sstrncpy(v->data.type_instance, value_list->type_instance, sizeof(v->data.type_instance));
448                 sstrncpy(v->data.plugin, value_list->plugin, sizeof(v->data.plugin));
449                 sstrncpy(v->data.plugin_instance, value_list->plugin_instance, sizeof(v->data.plugin_instance));
450                 v->data.time = CDTIME_T_TO_DOUBLE(value_list->time);
451                 v->interval = CDTIME_T_TO_DOUBLE(value_list->interval);
452                 Py_CLEAR(v->values);
453                 v->values = list;
454                 Py_CLEAR(v->meta);
455                 v->meta = dict;  /* Steals a reference. */
456                 ret = PyObject_CallFunctionObjArgs(c->callback, v, c->data, (void *) 0); /* New reference. */
457                 Py_XDECREF(v);
458                 if (ret == NULL) {
459                         cpy_log_exception("write callback");
460                 } else {
461                         Py_DECREF(ret);
462                 }
463         CPY_RELEASE_THREADS
464         return 0;
465 }
466
467 static int cpy_notification_callback(const notification_t *notification, user_data_t *data) {
468         cpy_callback_t *c = data->data;
469         PyObject *ret, *notify;
470         Notification *n;
471
472         CPY_LOCK_THREADS
473                 notify = Notification_New(); /* New reference. */
474                 n = (Notification *) notify;
475                 sstrncpy(n->data.host, notification->host, sizeof(n->data.host));
476                 sstrncpy(n->data.type, notification->type, sizeof(n->data.type));
477                 sstrncpy(n->data.type_instance, notification->type_instance, sizeof(n->data.type_instance));
478                 sstrncpy(n->data.plugin, notification->plugin, sizeof(n->data.plugin));
479                 sstrncpy(n->data.plugin_instance, notification->plugin_instance, sizeof(n->data.plugin_instance));
480                 n->data.time = CDTIME_T_TO_DOUBLE(notification->time);
481                 sstrncpy(n->message, notification->message, sizeof(n->message));
482                 n->severity = notification->severity;
483                 ret = PyObject_CallFunctionObjArgs(c->callback, n, c->data, (void *) 0); /* New reference. */
484                 Py_XDECREF(notify);
485                 if (ret == NULL) {
486                         cpy_log_exception("notification callback");
487                 } else {
488                         Py_DECREF(ret);
489                 }
490         CPY_RELEASE_THREADS
491         return 0;
492 }
493
494 static void cpy_log_callback(int severity, const char *message, user_data_t *data) {
495         cpy_callback_t * c = data->data;
496         PyObject *ret, *text;
497
498         CPY_LOCK_THREADS
499         text = cpy_string_to_unicode_or_bytes(message);  /* New reference. */
500         if (c->data == NULL)
501                 ret = PyObject_CallFunction(c->callback, "iN", severity, text); /* New reference. Steals a reference from "text". */
502         else
503                 ret = PyObject_CallFunction(c->callback, "iNO", severity, text, c->data); /* New reference. Steals a reference from "text". */
504
505         if (ret == NULL) {
506                 /* FIXME */
507                 /* Do we really want to trigger a log callback because a log callback failed?
508                  * Probably not. */
509                 PyErr_Print();
510                 /* In case someone wanted to be clever, replaced stderr and failed at that. */
511                 PyErr_Clear();
512         } else {
513                 Py_DECREF(ret);
514         }
515         CPY_RELEASE_THREADS
516 }
517
518 static void cpy_flush_callback(int timeout, const char *id, user_data_t *data) {
519         cpy_callback_t * c = data->data;
520         PyObject *ret, *text;
521
522         CPY_LOCK_THREADS
523         if (id) {
524                 text = cpy_string_to_unicode_or_bytes(id);
525         } else {
526                 text = Py_None;
527                 Py_INCREF(text);
528         }
529         if (c->data == NULL)
530                 ret = PyObject_CallFunction(c->callback, "iN", timeout, text); /* New reference. */
531         else
532                 ret = PyObject_CallFunction(c->callback, "iNO", timeout, text, c->data); /* New reference. */
533
534         if (ret == NULL) {
535                 cpy_log_exception("flush callback");
536         } else {
537                 Py_DECREF(ret);
538         }
539         CPY_RELEASE_THREADS
540 }
541
542 static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args, PyObject *kwds) {
543         char buf[512];
544         cpy_callback_t *c;
545         char *name = NULL;
546         PyObject *callback = NULL, *data = NULL, *mod = NULL;
547         static char *kwlist[] = {"callback", "data", "name", NULL};
548
549         if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oet", kwlist, &callback, &data, NULL, &name) == 0) return NULL;
550         if (PyCallable_Check(callback) == 0) {
551                 PyMem_Free(name);
552                 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
553                 return NULL;
554         }
555         cpy_build_name(buf, sizeof(buf), callback, name);
556
557         Py_INCREF(callback);
558         Py_XINCREF(data);
559
560         c = calloc(1, sizeof(*c));
561         if (c == NULL)
562                 return NULL;
563
564         c->name = strdup(buf);
565         c->callback = callback;
566         c->data = data;
567         c->next = *list_head;
568         *list_head = c;
569         Py_XDECREF(mod);
570         PyMem_Free(name);
571         return cpy_string_to_unicode_or_bytes(buf);
572 }
573
574 static PyObject *float_or_none(float number) {
575         if (isnan(number)) {
576                 Py_RETURN_NONE;
577         }
578         return PyFloat_FromDouble(number);
579 }
580
581 static PyObject *cpy_get_dataset(PyObject *self, PyObject *args) {
582         char *name;
583         const data_set_t *ds;
584         PyObject *list, *tuple;
585
586         if (PyArg_ParseTuple(args, "et", NULL, &name) == 0) return NULL;
587         ds = plugin_get_ds(name);
588         PyMem_Free(name);
589         if (ds == NULL) {
590                 PyErr_Format(PyExc_TypeError, "Dataset %s not found", name);
591                 return NULL;
592         }
593         list = PyList_New(ds->ds_num); /* New reference. */
594         for (size_t i = 0; i < ds->ds_num; ++i) {
595                 tuple = PyTuple_New(4);
596                 PyTuple_SET_ITEM(tuple, 0, cpy_string_to_unicode_or_bytes(ds->ds[i].name));
597                 PyTuple_SET_ITEM(tuple, 1, cpy_string_to_unicode_or_bytes(DS_TYPE_TO_STRING(ds->ds[i].type)));
598                 PyTuple_SET_ITEM(tuple, 2, float_or_none(ds->ds[i].min));
599                 PyTuple_SET_ITEM(tuple, 3, float_or_none(ds->ds[i].max));
600                 PyList_SET_ITEM(list, i, tuple);
601         }
602         return list;
603 }
604
605 static PyObject *cpy_flush(PyObject *self, PyObject *args, PyObject *kwds) {
606         int timeout = -1;
607         char *plugin = NULL, *identifier = NULL;
608         static char *kwlist[] = {"plugin", "timeout", "identifier", NULL};
609
610         if (PyArg_ParseTupleAndKeywords(args, kwds, "|etiet", kwlist, NULL, &plugin, &timeout, NULL, &identifier) == 0) return NULL;
611         Py_BEGIN_ALLOW_THREADS
612         plugin_flush(plugin, timeout, identifier);
613         Py_END_ALLOW_THREADS
614         PyMem_Free(plugin);
615         PyMem_Free(identifier);
616         Py_RETURN_NONE;
617 }
618
619 static PyObject *cpy_register_config(PyObject *self, PyObject *args, PyObject *kwds) {
620         return cpy_register_generic(&cpy_config_callbacks, args, kwds);
621 }
622
623 static PyObject *cpy_register_init(PyObject *self, PyObject *args, PyObject *kwds) {
624         return cpy_register_generic(&cpy_init_callbacks, args, kwds);
625 }
626
627 typedef int reg_function_t(const char *name, void *callback, void *data);
628
629 static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObject *args, PyObject *kwds) {
630         char buf[512];
631         reg_function_t *register_function = (reg_function_t *) reg;
632         cpy_callback_t *c = NULL;
633         char *name = NULL;
634         PyObject *callback = NULL, *data = NULL;
635         static char *kwlist[] = {"callback", "data", "name", NULL};
636
637         if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oet", kwlist, &callback, &data, NULL, &name) == 0) return NULL;
638         if (PyCallable_Check(callback) == 0) {
639                 PyMem_Free(name);
640                 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
641                 return NULL;
642         }
643         cpy_build_name(buf, sizeof(buf), callback, name);
644         PyMem_Free(name);
645
646         Py_INCREF(callback);
647         Py_XINCREF(data);
648
649         c = calloc(1, sizeof(*c));
650         if (c == NULL)
651                 return NULL;
652
653         c->name = strdup(buf);
654         c->callback = callback;
655         c->data = data;
656         c->next = NULL;
657
658         user_data_t user_data = {
659                 .data = c,
660                 .free_func = cpy_destroy_user_data
661         };
662
663         register_function(buf, handler, &user_data);
664         return cpy_string_to_unicode_or_bytes(buf);
665 }
666
667 static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwds) {
668         char buf[512];
669         cpy_callback_t *c = NULL;
670         double interval = 0;
671         char *name = NULL;
672         PyObject *callback = NULL, *data = NULL;
673         static char *kwlist[] = {"callback", "interval", "data", "name", NULL};
674
675         if (PyArg_ParseTupleAndKeywords(args, kwds, "O|dOet", kwlist, &callback, &interval, &data, NULL, &name) == 0) return NULL;
676         if (PyCallable_Check(callback) == 0) {
677                 PyMem_Free(name);
678                 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
679                 return NULL;
680         }
681         cpy_build_name(buf, sizeof(buf), callback, name);
682         PyMem_Free(name);
683
684         Py_INCREF(callback);
685         Py_XINCREF(data);
686
687         c = calloc(1, sizeof(*c));
688         if (c == NULL)
689                 return NULL;
690
691         c->name = strdup(buf);
692         c->callback = callback;
693         c->data = data;
694         c->next = NULL;
695
696         user_data_t user_data = {
697                 .data = c,
698                 .free_func = cpy_destroy_user_data
699         };
700
701         plugin_register_complex_read(/* group = */ "python", buf,
702                         cpy_read_callback, DOUBLE_TO_CDTIME_T (interval), &user_data);
703         return cpy_string_to_unicode_or_bytes(buf);
704 }
705
706 static PyObject *cpy_register_log(PyObject *self, PyObject *args, PyObject *kwds) {
707         return cpy_register_generic_userdata((void *) plugin_register_log,
708                         (void *) cpy_log_callback, args, kwds);
709 }
710
711 static PyObject *cpy_register_write(PyObject *self, PyObject *args, PyObject *kwds) {
712         return cpy_register_generic_userdata((void *) plugin_register_write,
713                         (void *) cpy_write_callback, args, kwds);
714 }
715
716 static PyObject *cpy_register_notification(PyObject *self, PyObject *args, PyObject *kwds) {
717         return cpy_register_generic_userdata((void *) plugin_register_notification,
718                         (void *) cpy_notification_callback, args, kwds);
719 }
720
721 static PyObject *cpy_register_flush(PyObject *self, PyObject *args, PyObject *kwds) {
722         return cpy_register_generic_userdata((void *) plugin_register_flush,
723                         (void *) cpy_flush_callback, args, kwds);
724 }
725
726 static PyObject *cpy_register_shutdown(PyObject *self, PyObject *args, PyObject *kwds) {
727         return cpy_register_generic(&cpy_shutdown_callbacks, args, kwds);
728 }
729
730 static PyObject *cpy_error(PyObject *self, PyObject *args) {
731         char *text;
732         if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
733         Py_BEGIN_ALLOW_THREADS
734         plugin_log(LOG_ERR, "%s", text);
735         Py_END_ALLOW_THREADS
736         PyMem_Free(text);
737         Py_RETURN_NONE;
738 }
739
740 static PyObject *cpy_warning(PyObject *self, PyObject *args) {
741         char *text;
742         if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
743         Py_BEGIN_ALLOW_THREADS
744         plugin_log(LOG_WARNING, "%s", text);
745         Py_END_ALLOW_THREADS
746         PyMem_Free(text);
747         Py_RETURN_NONE;
748 }
749
750 static PyObject *cpy_notice(PyObject *self, PyObject *args) {
751         char *text;
752         if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
753         Py_BEGIN_ALLOW_THREADS
754         plugin_log(LOG_NOTICE, "%s", text);
755         Py_END_ALLOW_THREADS
756         PyMem_Free(text);
757         Py_RETURN_NONE;
758 }
759
760 static PyObject *cpy_info(PyObject *self, PyObject *args) {
761         char *text;
762         if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
763         Py_BEGIN_ALLOW_THREADS
764         plugin_log(LOG_INFO, "%s", text);
765         Py_END_ALLOW_THREADS
766         PyMem_Free(text);
767         Py_RETURN_NONE;
768 }
769
770 static PyObject *cpy_debug(PyObject *self, PyObject *args) {
771 #ifdef COLLECT_DEBUG
772         char *text;
773         if (PyArg_ParseTuple(args, "et", NULL, &text) == 0) return NULL;
774         Py_BEGIN_ALLOW_THREADS
775         plugin_log(LOG_DEBUG, "%s", text);
776         Py_END_ALLOW_THREADS
777         PyMem_Free(text);
778 #endif
779         Py_RETURN_NONE;
780 }
781
782 static PyObject *cpy_unregister_generic(cpy_callback_t **list_head, PyObject *arg, const char *desc) {
783         char buf[512];
784         const char *name;
785         cpy_callback_t *prev = NULL, *tmp;
786
787         Py_INCREF(arg);
788         name = cpy_unicode_or_bytes_to_string(&arg);
789         if (name == NULL) {
790                 PyErr_Clear();
791                 if (!PyCallable_Check(arg)) {
792                         PyErr_SetString(PyExc_TypeError, "This function needs a string or a callable object as its only parameter.");
793                         Py_DECREF(arg);
794                         return NULL;
795                 }
796                 cpy_build_name(buf, sizeof(buf), arg, NULL);
797                 name = buf;
798         }
799         for (tmp = *list_head; tmp; prev = tmp, tmp = tmp->next)
800                 if (strcmp(name, tmp->name) == 0)
801                         break;
802
803         Py_DECREF(arg);
804         if (tmp == NULL) {
805                 PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
806                 return NULL;
807         }
808         /* Yes, this is actually safe. To call this function the caller has to
809          * hold the GIL. Well, safe as long as there is only one GIL anyway ... */
810         if (prev == NULL)
811                 *list_head = tmp->next;
812         else
813                 prev->next = tmp->next;
814         cpy_destroy_user_data(tmp);
815         Py_RETURN_NONE;
816 }
817
818 static void cpy_unregister_list(cpy_callback_t **list_head) {
819         cpy_callback_t *cur, *next;
820         for (cur = *list_head; cur; cur = next) {
821                 next = cur->next;
822                 cpy_destroy_user_data(cur);
823         }
824         *list_head = NULL;
825 }
826
827 typedef int cpy_unregister_function_t(const char *name);
828
829 static PyObject *cpy_unregister_generic_userdata(cpy_unregister_function_t *unreg, PyObject *arg, const char *desc) {
830         char buf[512];
831         const char *name;
832
833         Py_INCREF(arg);
834         name = cpy_unicode_or_bytes_to_string(&arg);
835         if (name == NULL) {
836                 PyErr_Clear();
837                 if (!PyCallable_Check(arg)) {
838                         PyErr_SetString(PyExc_TypeError, "This function needs a string or a callable object as its only parameter.");
839                         Py_DECREF(arg);
840                         return NULL;
841                 }
842                 cpy_build_name(buf, sizeof(buf), arg, NULL);
843                 name = buf;
844         }
845         if (unreg(name) == 0) {
846                 Py_DECREF(arg);
847                 Py_RETURN_NONE;
848         }
849         PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
850         Py_DECREF(arg);
851         return NULL;
852 }
853
854 static PyObject *cpy_unregister_log(PyObject *self, PyObject *arg) {
855         return cpy_unregister_generic_userdata(plugin_unregister_log, arg, "log");
856 }
857
858 static PyObject *cpy_unregister_init(PyObject *self, PyObject *arg) {
859         return cpy_unregister_generic(&cpy_init_callbacks, arg, "init");
860 }
861
862 static PyObject *cpy_unregister_config(PyObject *self, PyObject *arg) {
863         return cpy_unregister_generic(&cpy_config_callbacks, arg, "config");
864 }
865
866 static PyObject *cpy_unregister_read(PyObject *self, PyObject *arg) {
867         return cpy_unregister_generic_userdata(plugin_unregister_read, arg, "read");
868 }
869
870 static PyObject *cpy_unregister_write(PyObject *self, PyObject *arg) {
871         return cpy_unregister_generic_userdata(plugin_unregister_write, arg, "write");
872 }
873
874 static PyObject *cpy_unregister_notification(PyObject *self, PyObject *arg) {
875         return cpy_unregister_generic_userdata(plugin_unregister_notification, arg, "notification");
876 }
877
878 static PyObject *cpy_unregister_flush(PyObject *self, PyObject *arg) {
879         return cpy_unregister_generic_userdata(plugin_unregister_flush, arg, "flush");
880 }
881
882 static PyObject *cpy_unregister_shutdown(PyObject *self, PyObject *arg) {
883         return cpy_unregister_generic(&cpy_shutdown_callbacks, arg, "shutdown");
884 }
885
886 static PyMethodDef cpy_methods[] = {
887         {"debug", cpy_debug, METH_VARARGS, log_doc},
888         {"info", cpy_info, METH_VARARGS, log_doc},
889         {"notice", cpy_notice, METH_VARARGS, log_doc},
890         {"warning", cpy_warning, METH_VARARGS, log_doc},
891         {"error", cpy_error, METH_VARARGS, log_doc},
892         {"get_dataset", (PyCFunction) cpy_get_dataset, METH_VARARGS, get_ds_doc},
893         {"flush", (PyCFunction) cpy_flush, METH_VARARGS | METH_KEYWORDS, flush_doc},
894         {"register_log", (PyCFunction) cpy_register_log, METH_VARARGS | METH_KEYWORDS, reg_log_doc},
895         {"register_init", (PyCFunction) cpy_register_init, METH_VARARGS | METH_KEYWORDS, reg_init_doc},
896         {"register_config", (PyCFunction) cpy_register_config, METH_VARARGS | METH_KEYWORDS, reg_config_doc},
897         {"register_read", (PyCFunction) cpy_register_read, METH_VARARGS | METH_KEYWORDS, reg_read_doc},
898         {"register_write", (PyCFunction) cpy_register_write, METH_VARARGS | METH_KEYWORDS, reg_write_doc},
899         {"register_notification", (PyCFunction) cpy_register_notification, METH_VARARGS | METH_KEYWORDS, reg_notification_doc},
900         {"register_flush", (PyCFunction) cpy_register_flush, METH_VARARGS | METH_KEYWORDS, reg_flush_doc},
901         {"register_shutdown", (PyCFunction) cpy_register_shutdown, METH_VARARGS | METH_KEYWORDS, reg_shutdown_doc},
902         {"unregister_log", cpy_unregister_log, METH_O, unregister_doc},
903         {"unregister_init", cpy_unregister_init, METH_O, unregister_doc},
904         {"unregister_config", cpy_unregister_config, METH_O, unregister_doc},
905         {"unregister_read", cpy_unregister_read, METH_O, unregister_doc},
906         {"unregister_write", cpy_unregister_write, METH_O, unregister_doc},
907         {"unregister_notification", cpy_unregister_notification, METH_O, unregister_doc},
908         {"unregister_flush", cpy_unregister_flush, METH_O, unregister_doc},
909         {"unregister_shutdown", cpy_unregister_shutdown, METH_O, unregister_doc},
910         {0, 0, 0, 0}
911 };
912
913 static int cpy_shutdown(void) {
914         PyObject *ret;
915
916         /* This can happen if the module was loaded but not configured. */
917         if (state != NULL)
918                 PyEval_RestoreThread(state);
919
920         for (cpy_callback_t *c = cpy_shutdown_callbacks; c; c = c->next) {
921                 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
922                 if (ret == NULL)
923                         cpy_log_exception("shutdown callback");
924                 else
925                         Py_DECREF(ret);
926         }
927         PyErr_Print();
928
929         cpy_unregister_list(&cpy_config_callbacks);
930         cpy_unregister_list(&cpy_init_callbacks);
931         cpy_unregister_list(&cpy_shutdown_callbacks);
932
933         Py_Finalize();
934         return 0;
935 }
936
937 static void *cpy_interactive(void *data) {
938         PyOS_sighandler_t cur_sig;
939
940         /* Signal handler in a plugin? Bad stuff, but the best way to
941          * handle it I guess. In an interactive session people will
942          * press Ctrl+C at some time, which will generate a SIGINT.
943          * This will cause collectd to shutdown, thus killing the
944          * interactive interpreter, and leaving the terminal in a
945          * mess. Chances are, this isn't what the user wanted to do.
946          *
947          * So this is the plan:
948          * 1. Restore Python's own signal handler
949          * 2. Tell Python we just forked so it will accept this thread
950          *    as the main one. No version of Python will ever handle
951          *    interrupts anywhere but in the main thread.
952          * 3. After the interactive loop is done, restore collectd's
953          *    SIGINT handler.
954          * 4. Raise SIGINT for a clean shutdown. The signal is sent to
955          *    the main thread to ensure it wakes up the main interval
956          *    sleep so that collectd shuts down immediately not in 10
957          *    seconds.
958          *
959          * This will make sure that SIGINT won't kill collectd but
960          * still interrupt syscalls like sleep and pause. */
961
962         PyEval_AcquireThread(state);
963         if (PyImport_ImportModule("readline") == NULL) {
964                 /* This interactive session will suck. */
965                 cpy_log_exception("interactive session init");
966         }
967         cur_sig = PyOS_setsig(SIGINT, python_sigint_handler);
968         /* We totally forked just now. Everyone saw that, right? */
969         PyOS_AfterFork();
970         PyRun_InteractiveLoop(stdin, "<stdin>");
971         PyOS_setsig(SIGINT, cur_sig);
972         PyErr_Print();
973         PyEval_ReleaseThread(state);
974         NOTICE("python: Interactive interpreter exited, stopping collectd ...");
975         pthread_kill(main_thread, SIGINT);
976         return NULL;
977 }
978
979 static int cpy_init(void) {
980         PyObject *ret;
981         static pthread_t thread;
982
983         if (!Py_IsInitialized()) {
984                 WARNING("python: Plugin loaded but not configured.");
985                 plugin_unregister_shutdown("python");
986                 return 0;
987         }
988         PyEval_InitThreads();
989         /* Now it's finally OK to use python threads. */
990         for (cpy_callback_t *c = cpy_init_callbacks; c; c = c->next) {
991                 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
992                 if (ret == NULL)
993                         cpy_log_exception("init callback");
994                 else
995                         Py_DECREF(ret);
996         }
997         state = PyEval_SaveThread();
998         main_thread = pthread_self();
999         if (do_interactive) {
1000                 if (plugin_thread_create(&thread, NULL, cpy_interactive, NULL)) {
1001                         ERROR("python: Error creating thread for interactive interpreter.");
1002                 }
1003         }
1004
1005         return 0;
1006 }
1007
1008 static PyObject *cpy_oconfig_to_pyconfig(oconfig_item_t *ci, PyObject *parent) {
1009         PyObject *item, *values, *children, *tmp;
1010
1011         if (parent == NULL)
1012                 parent = Py_None;
1013
1014         values = PyTuple_New(ci->values_num); /* New reference. */
1015         for (int i = 0; i < ci->values_num; ++i) {
1016                 if (ci->values[i].type == OCONFIG_TYPE_STRING) {
1017                         PyTuple_SET_ITEM(values, i, cpy_string_to_unicode_or_bytes(ci->values[i].value.string));
1018                 } else if (ci->values[i].type == OCONFIG_TYPE_NUMBER) {
1019                         PyTuple_SET_ITEM(values, i, PyFloat_FromDouble(ci->values[i].value.number));
1020                 } else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN) {
1021                         PyTuple_SET_ITEM(values, i, PyBool_FromLong(ci->values[i].value.boolean));
1022                 }
1023         }
1024
1025         tmp = cpy_string_to_unicode_or_bytes(ci->key);
1026         item = PyObject_CallFunction((void *) &ConfigType, "NONO", tmp, parent, values, Py_None);
1027         if (item == NULL)
1028                 return NULL;
1029         children = PyTuple_New(ci->children_num); /* New reference. */
1030         for (int i = 0; i < ci->children_num; ++i) {
1031                 PyTuple_SET_ITEM(children, i, cpy_oconfig_to_pyconfig(ci->children + i, item));
1032         }
1033         tmp = ((Config *) item)->children;
1034         ((Config *) item)->children = children;
1035         Py_XDECREF(tmp);
1036         return item;
1037 }
1038
1039 #ifdef IS_PY3K
1040 static struct PyModuleDef collectdmodule = {
1041         PyModuleDef_HEAD_INIT,
1042         "collectd",   /* name of module */
1043         "The python interface to collectd", /* module documentation, may be NULL */
1044         -1,
1045         cpy_methods
1046 };
1047
1048 PyMODINIT_FUNC PyInit_collectd(void) {
1049         return PyModule_Create(&collectdmodule);
1050 }
1051 #endif
1052
1053 static int cpy_init_python(void) {
1054         PyOS_sighandler_t cur_sig;
1055         PyObject *sys;
1056         PyObject *module;
1057
1058 #ifdef IS_PY3K
1059         wchar_t *argv = L"";
1060         /* Add a builtin module, before Py_Initialize */
1061         PyImport_AppendInittab("collectd", PyInit_collectd);
1062 #else
1063         char *argv = "";
1064 #endif
1065
1066         /* Chances are the current signal handler is already SIG_DFL, but let's make sure. */
1067         cur_sig = PyOS_setsig(SIGINT, SIG_DFL);
1068         Py_Initialize();
1069         python_sigint_handler = PyOS_setsig(SIGINT, cur_sig);
1070
1071         PyType_Ready(&ConfigType);
1072         PyType_Ready(&PluginDataType);
1073         ValuesType.tp_base = &PluginDataType;
1074         PyType_Ready(&ValuesType);
1075         NotificationType.tp_base = &PluginDataType;
1076         PyType_Ready(&NotificationType);
1077         SignedType.tp_base = &PyLong_Type;
1078         PyType_Ready(&SignedType);
1079         UnsignedType.tp_base = &PyLong_Type;
1080         PyType_Ready(&UnsignedType);
1081         sys = PyImport_ImportModule("sys"); /* New reference. */
1082         if (sys == NULL) {
1083                 cpy_log_exception("python initialization");
1084                 return 1;
1085         }
1086         sys_path = PyObject_GetAttrString(sys, "path"); /* New reference. */
1087         Py_DECREF(sys);
1088         if (sys_path == NULL) {
1089                 cpy_log_exception("python initialization");
1090                 return 1;
1091         }
1092         PySys_SetArgv(1, &argv);
1093         PyList_SetSlice(sys_path, 0, 1, NULL);
1094
1095 #ifdef IS_PY3K
1096         module = PyImport_ImportModule("collectd");
1097 #else
1098         module = Py_InitModule("collectd", cpy_methods); /* Borrowed reference. */
1099 #endif
1100         PyModule_AddObject(module, "Config", (void *) &ConfigType); /* Steals a reference. */
1101         PyModule_AddObject(module, "Values", (void *) &ValuesType); /* Steals a reference. */
1102         PyModule_AddObject(module, "Notification", (void *) &NotificationType); /* Steals a reference. */
1103         PyModule_AddObject(module, "Signed", (void *) &SignedType); /* Steals a reference. */
1104         PyModule_AddObject(module, "Unsigned", (void *) &UnsignedType); /* Steals a reference. */
1105         PyModule_AddIntConstant(module, "LOG_DEBUG", LOG_DEBUG);
1106         PyModule_AddIntConstant(module, "LOG_INFO", LOG_INFO);
1107         PyModule_AddIntConstant(module, "LOG_NOTICE", LOG_NOTICE);
1108         PyModule_AddIntConstant(module, "LOG_WARNING", LOG_WARNING);
1109         PyModule_AddIntConstant(module, "LOG_ERROR", LOG_ERR);
1110         PyModule_AddIntConstant(module, "NOTIF_FAILURE", NOTIF_FAILURE);
1111         PyModule_AddIntConstant(module, "NOTIF_WARNING", NOTIF_WARNING);
1112         PyModule_AddIntConstant(module, "NOTIF_OKAY", NOTIF_OKAY);
1113         PyModule_AddStringConstant(module, "DS_TYPE_COUNTER", DS_TYPE_TO_STRING(DS_TYPE_COUNTER));
1114         PyModule_AddStringConstant(module, "DS_TYPE_GAUGE", DS_TYPE_TO_STRING(DS_TYPE_GAUGE));
1115         PyModule_AddStringConstant(module, "DS_TYPE_DERIVE", DS_TYPE_TO_STRING(DS_TYPE_DERIVE));
1116         PyModule_AddStringConstant(module, "DS_TYPE_ABSOLUTE", DS_TYPE_TO_STRING(DS_TYPE_ABSOLUTE));
1117         return 0;
1118 }
1119
1120 static int cpy_config(oconfig_item_t *ci) {
1121         PyObject *tb;
1122         int status = 0;
1123
1124         /* Ok in theory we shouldn't do initialization at this point
1125          * but we have to. In order to give python scripts a chance
1126          * to register a config callback we need to be able to execute
1127          * python code during the config callback so we have to start
1128          * the interpreter here. */
1129         /* Do *not* use the python "thread" module at this point! */
1130
1131         if (!Py_IsInitialized() && cpy_init_python()) return 1;
1132
1133         for (int i = 0; i < ci->children_num; ++i) {
1134                 oconfig_item_t *item = ci->children + i;
1135
1136                 if (strcasecmp(item->key, "Interactive") == 0) {
1137                         if (cf_util_get_boolean(item, &do_interactive) != 0) {
1138                                 status = 1;
1139                                 continue;
1140                         }
1141                 } else if (strcasecmp(item->key, "Encoding") == 0) {
1142                         char *encoding = NULL;
1143                         if (cf_util_get_string(item, &encoding) != 0) {
1144                                 status = 1;
1145                                 continue;
1146                         }
1147 #ifdef IS_PY3K
1148                         ERROR("python: \"Encoding\" was used in the config file but Python3 was used, which does not support changing encodings");
1149                         status = 1;
1150                         sfree(encoding);
1151                         continue;
1152 #else
1153                         /* Why is this even necessary? And undocumented? */
1154                         if (PyUnicode_SetDefaultEncoding(encoding)) {
1155                                 cpy_log_exception("setting default encoding");
1156                                 status = 1;
1157                         }
1158 #endif
1159                         sfree(encoding);
1160                 } else if (strcasecmp(item->key, "LogTraces") == 0) {
1161                         _Bool log_traces;
1162                         if (cf_util_get_boolean(item, &log_traces) != 0) {
1163                                 status = 1;
1164                                 continue;
1165                         }
1166                         if (!log_traces) {
1167                                 Py_XDECREF(cpy_format_exception);
1168                                 cpy_format_exception = NULL;
1169                                 continue;
1170                         }
1171                         if (cpy_format_exception)
1172                                 continue;
1173                         tb = PyImport_ImportModule("traceback"); /* New reference. */
1174                         if (tb == NULL) {
1175                                 cpy_log_exception("python initialization");
1176                                 status = 1;
1177                                 continue;
1178                         }
1179                         cpy_format_exception = PyObject_GetAttrString(tb, "format_exception"); /* New reference. */
1180                         Py_DECREF(tb);
1181                         if (cpy_format_exception == NULL) {
1182                                 cpy_log_exception("python initialization");
1183                                 status = 1;
1184                         }
1185                 } else if (strcasecmp(item->key, "ModulePath") == 0) {
1186                         char *dir = NULL;
1187                         PyObject *dir_object;
1188
1189                         if (cf_util_get_string(item, &dir) != 0) {
1190                                 status = 1;
1191                                 continue;
1192                         }
1193                         dir_object = cpy_string_to_unicode_or_bytes(dir); /* New reference. */
1194                         if (dir_object == NULL) {
1195                                 ERROR("python plugin: Unable to convert \"%s\" to "
1196                                       "a python object.", dir);
1197                                 free(dir);
1198                                 cpy_log_exception("python initialization");
1199                                 status = 1;
1200                                 continue;
1201                         }
1202                         if (PyList_Insert(sys_path, 0, dir_object) != 0) {
1203                                 ERROR("python plugin: Unable to prepend \"%s\" to "
1204                                       "python module path.", dir);
1205                                 cpy_log_exception("python initialization");
1206                                 status = 1;
1207                         }
1208                         Py_DECREF(dir_object);
1209                         free(dir);
1210                 } else if (strcasecmp(item->key, "Import") == 0) {
1211                         char *module_name = NULL;
1212                         PyObject *module;
1213
1214                         if (cf_util_get_string(item, &module_name) != 0) {
1215                                 status = 1;
1216                                 continue;
1217                         }
1218                         module = PyImport_ImportModule(module_name); /* New reference. */
1219                         if (module == NULL) {
1220                                 ERROR("python plugin: Error importing module \"%s\".", module_name);
1221                                 cpy_log_exception("importing module");
1222                                 status = 1;
1223                         }
1224                         free(module_name);
1225                         Py_XDECREF(module);
1226                 } else if (strcasecmp(item->key, "Module") == 0) {
1227                         char *name = NULL;
1228                         cpy_callback_t *c;
1229                         PyObject *ret;
1230
1231                         if (cf_util_get_string(item, &name) != 0) {
1232                                 status = 1;
1233                                 continue;
1234                         }
1235                         for (c = cpy_config_callbacks; c; c = c->next) {
1236                                 if (strcasecmp(c->name + 7, name) == 0)
1237                                         break;
1238                         }
1239                         if (c == NULL) {
1240                                 WARNING("python plugin: Found a configuration for the \"%s\" plugin, "
1241                                         "but the plugin isn't loaded or didn't register "
1242                                         "a configuration callback.", name);
1243                                 free(name);
1244                                 continue;
1245                         }
1246                         free(name);
1247                         if (c->data == NULL)
1248                                 ret = PyObject_CallFunction(c->callback, "N",
1249                                         cpy_oconfig_to_pyconfig(item, NULL)); /* New reference. */
1250                         else
1251                                 ret = PyObject_CallFunction(c->callback, "NO",
1252                                         cpy_oconfig_to_pyconfig(item, NULL), c->data); /* New reference. */
1253                         if (ret == NULL) {
1254                                 cpy_log_exception("loading module");
1255                                 status = 1;
1256                         } else
1257                                 Py_DECREF(ret);
1258                 } else {
1259                         ERROR("python plugin: Unknown config key \"%s\".", item->key);
1260                         status = 1;
1261                 }
1262         }
1263         return (status);
1264 }
1265
1266 void module_register(void) {
1267         plugin_register_complex_config("python", cpy_config);
1268         plugin_register_init("python", cpy_init);
1269         plugin_register_shutdown("python", cpy_shutdown);
1270 }