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