2 #include <structmember.h>
13 typedef struct cpy_callback_s {
17 struct cpy_callback_s *next;
20 static int do_interactive = 0;
22 /* This is our global thread state. Python saves some stuff in thread-local
23 * storage. So if we allow the interpreter to run in the background
24 * (the scriptwriters might have created some threads from python), we have
25 * to save the state so we can resume it later after shutdown. */
27 static PyThreadState *state;
29 static PyObject *cpy_format_exception;
31 static cpy_callback_t *cpy_config_callbacks;
32 static cpy_callback_t *cpy_init_callbacks;
33 static cpy_callback_t *cpy_shutdown_callbacks;
35 static void cpy_destroy_user_data(void *data) {
36 cpy_callback_t *c = data;
38 Py_DECREF(c->callback);
43 /* You must hold the GIL to call this function!
44 * But if you managed to extract the callback parameter then you probably already do. */
46 static void cpy_build_name(char *buf, size_t size, PyObject *callback, const char *name, int short_name) {
48 PyObject *mod = NULL, *n = NULL;
50 if (name != NULL && (strchr(name, '.') != NULL || short_name)) {
51 snprintf(buf, size, "python.%s", name);
55 mod = PyObject_GetAttrString(callback, "__module__"); /* New reference. */
57 module = PyString_AsString(mod);
62 snprintf(buf, size, "python.%s", module);
68 snprintf(buf, size, "python.%s.%s", module, name);
73 n = PyObject_GetAttrString(callback, "__name__"); /* New reference. */
75 name = PyString_AsString(n);
78 snprintf(buf, size, "python.%s.%s", module, name);
80 snprintf(buf, size, "python.%s.%p", module, callback);
85 static void cpy_log_exception(const char *context) {
87 const char *typename = NULL, *message = NULL;
88 PyObject *type, *value, *traceback, *tn, *m, *list;
90 PyErr_Fetch(&type, &value, &traceback);
91 PyErr_NormalizeException(&type, &value, &traceback);
92 if (type == NULL) return;
93 tn = PyObject_GetAttrString(type, "__name__"); /* New reference. */
94 m = PyObject_GetAttrString(value, "message"); /* New reference. */
96 typename = PyString_AsString(tn);
98 message = PyString_AsString(m);
100 typename = "NamelessException";
103 ERROR("Unhandled python exception in %s: %s: %s", context, typename, message);
106 if (!cpy_format_exception) {
110 Py_XDECREF(traceback);
117 list = PyObject_CallFunction(cpy_format_exception, "NNN", type, value, traceback);
119 l = PyObject_Length(list);
120 for (i = 0; i < l; ++i) {
124 line = PyList_GET_ITEM(list, i);
125 s = strdup(PyString_AsString(line));
127 if (s[strlen(s) - 1] == '\n')
128 s[strlen(s) - 1] = 0;
135 static int cpy_read_callback(user_data_t *data) {
136 cpy_callback_t *c = data->data;
140 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
142 cpy_log_exception("read callback");
150 static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_list, user_data_t *data) {
152 cpy_callback_t *c = data->data;
153 PyObject *ret, *v, *list;
156 list = PyList_New(value_list->values_len); /* New reference. */
158 cpy_log_exception("write callback");
159 CPY_RETURN_FROM_THREADS 0;
161 for (i = 0; i < value_list->values_len; ++i) {
162 if (ds->ds->type == DS_TYPE_COUNTER) {
163 if ((long) value_list->values[i].counter == value_list->values[i].counter)
164 PyList_SetItem(list, i, PyInt_FromLong(value_list->values[i].counter));
166 PyList_SetItem(list, i, PyLong_FromUnsignedLongLong(value_list->values[i].counter));
167 } else if (ds->ds->type == DS_TYPE_GAUGE) {
168 PyList_SetItem(list, i, PyFloat_FromDouble(value_list->values[i].gauge));
169 } else if (ds->ds->type == DS_TYPE_DERIVE) {
170 if ((long) value_list->values[i].derive == value_list->values[i].derive)
171 PyList_SetItem(list, i, PyInt_FromLong(value_list->values[i].derive));
173 PyList_SetItem(list, i, PyLong_FromLongLong(value_list->values[i].derive));
174 } else if (ds->ds->type == DS_TYPE_ABSOLUTE) {
175 if ((long) value_list->values[i].absolute == value_list->values[i].absolute)
176 PyList_SetItem(list, i, PyInt_FromLong(value_list->values[i].absolute));
178 PyList_SetItem(list, i, PyLong_FromUnsignedLongLong(value_list->values[i].absolute));
180 ERROR("cpy_write_callback: Unknown value type %d.", ds->ds->type);
182 CPY_RETURN_FROM_THREADS 0;
184 if (PyErr_Occurred() != NULL) {
185 cpy_log_exception("value building for write callback");
186 CPY_RETURN_FROM_THREADS 0;
189 v = PyObject_CallFunction((PyObject *) &ValuesType, "sOssssdi", value_list->type, list,
190 value_list->plugin_instance, value_list->type_instance, value_list->plugin,
191 value_list->host, (double) value_list->time, value_list->interval);
193 ret = PyObject_CallFunctionObjArgs(c->callback, v, c->data, (void *) 0); /* New reference. */
195 cpy_log_exception("write callback");
203 static int cpy_notification_callback(const notification_t *notification, user_data_t *data) {
204 cpy_callback_t *c = data->data;
208 n = PyObject_CallFunction((PyObject *) &NotificationType, "ssssssdi", notification->type, notification->message,
209 notification->plugin_instance, notification->type_instance, notification->plugin,
210 notification->host, (double) notification->time, notification->severity);
211 ret = PyObject_CallFunctionObjArgs(c->callback, n, c->data, (void *) 0); /* New reference. */
213 cpy_log_exception("notification callback");
221 static void cpy_log_callback(int severity, const char *message, user_data_t *data) {
222 cpy_callback_t * c = data->data;
227 ret = PyObject_CallFunction(c->callback, "is", severity, message); /* New reference. */
229 ret = PyObject_CallFunction(c->callback, "isO", severity, message, c->data); /* New reference. */
233 /* Do we really want to trigger a log callback because a log callback failed?
242 static void cpy_flush_callback(int timeout, const char *id, user_data_t *data) {
243 cpy_callback_t * c = data->data;
248 ret = PyObject_CallFunction(c->callback, "is", timeout, id); /* New reference. */
250 ret = PyObject_CallFunction(c->callback, "isO", timeout, id, c->data); /* New reference. */
253 cpy_log_exception("flush callback");
260 static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args, PyObject *kwds, int short_name) {
263 const char *name = NULL;
264 PyObject *callback = NULL, *data = NULL, *mod = NULL;
265 static char *kwlist[] = {"callback", "data", "name", NULL};
267 if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oz", kwlist, &callback, &data, &name) == 0) return NULL;
268 if (PyCallable_Check(callback) == 0) {
269 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
272 cpy_build_name(buf, sizeof(buf), callback, name, short_name);
276 c = malloc(sizeof(*c));
277 c->name = strdup(buf);
278 c->callback = callback;
280 c->next = *list_head;
283 return PyString_FromString(buf);
286 static PyObject *cpy_flush(cpy_callback_t **list_head, PyObject *args, PyObject *kwds) {
288 const char *plugin = NULL, *identifier = NULL;
289 static char *kwlist[] = {"plugin", "timeout", "identifier", NULL};
291 if (PyArg_ParseTupleAndKeywords(args, kwds, "|ziz", kwlist, &plugin, &timeout, &identifier) == 0) return NULL;
292 Py_BEGIN_ALLOW_THREADS
293 plugin_flush(plugin, timeout, identifier);
298 static PyObject *cpy_register_config(PyObject *self, PyObject *args, PyObject *kwds) {
299 return cpy_register_generic(&cpy_config_callbacks, args, kwds, 1);
302 static PyObject *cpy_register_init(PyObject *self, PyObject *args, PyObject *kwds) {
303 return cpy_register_generic(&cpy_init_callbacks, args, kwds, 0);
306 typedef int reg_function_t(const char *name, void *callback, void *data);
308 static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObject *args, PyObject *kwds, int short_name) {
310 reg_function_t *register_function = (reg_function_t *) reg;
311 cpy_callback_t *c = NULL;
312 user_data_t *user_data = NULL;
313 const char *name = NULL;
314 PyObject *callback = NULL, *data = NULL;
315 static char *kwlist[] = {"callback", "data", "name", NULL};
317 if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oz", kwlist, &callback, &data, &name) == 0) return NULL;
318 if (PyCallable_Check(callback) == 0) {
319 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
322 cpy_build_name(buf, sizeof(buf), callback, name, short_name);
326 c = malloc(sizeof(*c));
327 c->name = strdup(buf);
328 c->callback = callback;
331 user_data = malloc(sizeof(*user_data));
332 user_data->free_func = cpy_destroy_user_data;
334 register_function(buf, handler, user_data);
335 return PyString_FromString(buf);
338 static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwds) {
340 cpy_callback_t *c = NULL;
341 user_data_t *user_data = NULL;
343 const char *name = NULL;
344 PyObject *callback = NULL, *data = NULL;
346 static char *kwlist[] = {"callback", "interval", "data", "name", NULL};
348 if (PyArg_ParseTupleAndKeywords(args, kwds, "O|dOz", kwlist, &callback, &interval, &data, &name) == 0) return NULL;
349 if (PyCallable_Check(callback) == 0) {
350 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
353 cpy_build_name(buf, sizeof(buf), callback, name, 0);
357 c = malloc(sizeof(*c));
358 c->name = strdup(buf);
359 c->callback = callback;
362 user_data = malloc(sizeof(*user_data));
363 user_data->free_func = cpy_destroy_user_data;
365 ts.tv_sec = interval;
366 ts.tv_nsec = (interval - ts.tv_sec) * 1000000000;
367 plugin_register_complex_read(buf, cpy_read_callback, &ts, user_data);
368 return PyString_FromString(buf);
371 static PyObject *cpy_register_log(PyObject *self, PyObject *args, PyObject *kwds) {
372 return cpy_register_generic_userdata(plugin_register_log, cpy_log_callback, args, kwds, 0);
375 static PyObject *cpy_register_write(PyObject *self, PyObject *args, PyObject *kwds) {
376 return cpy_register_generic_userdata(plugin_register_write, cpy_write_callback, args, kwds, 0);
379 static PyObject *cpy_register_notification(PyObject *self, PyObject *args, PyObject *kwds) {
380 return cpy_register_generic_userdata(plugin_register_notification, cpy_notification_callback, args, kwds, 0);
383 static PyObject *cpy_register_flush(PyObject *self, PyObject *args, PyObject *kwds) {
384 return cpy_register_generic_userdata(plugin_register_flush, cpy_flush_callback, args, kwds, 1);
387 static PyObject *cpy_register_shutdown(PyObject *self, PyObject *args, PyObject *kwds) {
388 return cpy_register_generic(&cpy_shutdown_callbacks, args, kwds, 0);
391 static PyObject *cpy_error(PyObject *self, PyObject *args) {
393 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
394 Py_BEGIN_ALLOW_THREADS
395 plugin_log(LOG_ERR, "%s", text);
400 static PyObject *cpy_warning(PyObject *self, PyObject *args) {
402 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
403 Py_BEGIN_ALLOW_THREADS
404 plugin_log(LOG_WARNING, "%s", text);
409 static PyObject *cpy_notice(PyObject *self, PyObject *args) {
411 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
412 Py_BEGIN_ALLOW_THREADS
413 plugin_log(LOG_NOTICE, "%s", text);
418 static PyObject *cpy_info(PyObject *self, PyObject *args) {
420 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
421 Py_BEGIN_ALLOW_THREADS
422 plugin_log(LOG_INFO, "%s", text);
427 static PyObject *cpy_debug(PyObject *self, PyObject *args) {
430 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
431 plugin_log(LOG_DEBUG, "%s", text);
436 static PyObject *cpy_unregister_generic(cpy_callback_t **list_head, PyObject *arg, const char *desc, int short_name) {
439 cpy_callback_t *prev = NULL, *tmp;
441 if (PyString_Check(arg)) {
442 name = PyString_AsString(arg);
444 if (!PyCallable_Check(arg)) {
445 PyErr_SetString(PyExc_TypeError, "This function needs a string or a callable object as its only parameter.");
448 cpy_build_name(buf, sizeof(buf), arg, NULL, short_name);
451 for (tmp = *list_head; tmp; prev = tmp, tmp = tmp->next)
452 if (strcmp(name, tmp->name) == 0)
456 PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
459 /* Yes, this is actually save. To call this function the calles has to
460 * hold the GIL. Well, save as long as there is only one GIL anyway ... */
462 *list_head = tmp->next;
464 prev->next = tmp->next;
465 cpy_destroy_user_data(tmp);
469 typedef int cpy_unregister_function_t(const char *name);
471 static PyObject *cpy_unregister_generic_userdata(cpy_unregister_function_t *unreg, PyObject *arg, const char *desc, int short_name) {
475 if (PyString_Check(arg)) {
476 name = PyString_AsString(arg);
478 if (!PyCallable_Check(arg)) {
479 PyErr_SetString(PyExc_TypeError, "This function needs a string or a callable object as its only parameter.");
482 cpy_build_name(buf, sizeof(buf), arg, NULL, short_name);
485 if (unreg(name) == 0)
487 PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
491 static PyObject *cpy_unregister_log(PyObject *self, PyObject *arg) {
492 return cpy_unregister_generic_userdata(plugin_unregister_log, arg, "log", 0);
495 static PyObject *cpy_unregister_init(PyObject *self, PyObject *arg) {
496 return cpy_unregister_generic(&cpy_init_callbacks, arg, "init", 0);
499 static PyObject *cpy_unregister_config(PyObject *self, PyObject *arg) {
500 return cpy_unregister_generic(&cpy_config_callbacks, arg, "config", 1);
503 static PyObject *cpy_unregister_read(PyObject *self, PyObject *arg) {
504 return cpy_unregister_generic_userdata(plugin_unregister_read, arg, "read", 0);
507 static PyObject *cpy_unregister_write(PyObject *self, PyObject *arg) {
508 return cpy_unregister_generic_userdata(plugin_unregister_write, arg, "write", 0);
511 static PyObject *cpy_unregister_notification(PyObject *self, PyObject *arg) {
512 return cpy_unregister_generic_userdata(plugin_unregister_notification, arg, "notification", 0);
515 static PyObject *cpy_unregister_flush(PyObject *self, PyObject *arg) {
516 return cpy_unregister_generic_userdata(plugin_unregister_flush, arg, "flush", 1);
519 static PyObject *cpy_unregister_shutdown(PyObject *self, PyObject *arg) {
520 return cpy_unregister_generic(&cpy_shutdown_callbacks, arg, "shutdown", 0);
523 static PyMethodDef cpy_methods[] = {
524 {"debug", cpy_debug, METH_VARARGS, "This is an unhelpful text."},
525 {"info", cpy_info, METH_VARARGS, "This is an unhelpful text."},
526 {"notice", cpy_notice, METH_VARARGS, "This is an unhelpful text."},
527 {"warning", cpy_warning, METH_VARARGS, "This is an unhelpful text."},
528 {"error", cpy_error, METH_VARARGS, "This is an unhelpful text."},
529 {"flush", (PyCFunction) cpy_flush, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
530 {"register_log", (PyCFunction) cpy_register_log, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
531 {"register_init", (PyCFunction) cpy_register_init, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
532 {"register_config", (PyCFunction) cpy_register_config, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
533 {"register_read", (PyCFunction) cpy_register_read, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
534 {"register_write", (PyCFunction) cpy_register_write, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
535 {"register_notification", (PyCFunction) cpy_register_notification, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
536 {"register_flush", (PyCFunction) cpy_register_flush, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
537 {"register_shutdown", (PyCFunction) cpy_register_shutdown, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
538 {"unregister_log", cpy_unregister_log, METH_O, "This is an unhelpful text."},
539 {"unregister_init", cpy_unregister_init, METH_O, "This is an unhelpful text."},
540 {"unregister_config", cpy_unregister_config, METH_O, "This is an unhelpful text."},
541 {"unregister_read", cpy_unregister_read, METH_O, "This is an unhelpful text."},
542 {"unregister_write", cpy_unregister_write, METH_O, "This is an unhelpful text."},
543 {"unregister_notification", cpy_unregister_notification, METH_O, "This is an unhelpful text."},
544 {"unregister_flush", cpy_unregister_flush, METH_O, "This is an unhelpful text."},
545 {"unregister_shutdown", cpy_unregister_shutdown, METH_O, "This is an unhelpful text."},
549 static int cpy_shutdown(void) {
553 /* This can happen if the module was loaded but not configured. */
555 PyEval_RestoreThread(state);
557 for (c = cpy_shutdown_callbacks; c; c = c->next) {
558 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
560 cpy_log_exception("shutdown callback");
568 static void *cpy_interactive(void *data) {
570 if (PyImport_ImportModule("readline") == NULL) {
571 /* This interactive session will suck. */
572 cpy_log_exception("interactive session init");
574 PyRun_InteractiveLoop(stdin, "<stdin>");
576 NOTICE("python: Interactive interpreter exited, stopping collectd ...");
581 static int cpy_init(void) {
584 static pthread_t thread;
586 PyEval_InitThreads();
587 /* Now it's finally OK to use python threads. */
588 for (c = cpy_init_callbacks; c; c = c->next) {
589 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
591 cpy_log_exception("init callback");
595 state = PyEval_SaveThread();
596 if (do_interactive) {
597 if (pthread_create(&thread, NULL, cpy_interactive, NULL)) {
598 ERROR("python: Error creating thread for interactive interpreter.");
605 static PyObject *cpy_oconfig_to_pyconfig(oconfig_item_t *ci, PyObject *parent) {
607 PyObject *item, *values, *children, *tmp;
612 values = PyTuple_New(ci->values_num); /* New reference. */
613 for (i = 0; i < ci->values_num; ++i) {
614 if (ci->values[i].type == OCONFIG_TYPE_STRING) {
615 PyTuple_SET_ITEM(values, i, PyString_FromString(ci->values[i].value.string));
616 } else if (ci->values[i].type == OCONFIG_TYPE_NUMBER) {
617 PyTuple_SET_ITEM(values, i, PyFloat_FromDouble(ci->values[i].value.number));
618 } else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN) {
619 PyTuple_SET_ITEM(values, i, PyBool_FromLong(ci->values[i].value.boolean));
623 item = PyObject_CallFunction((PyObject *) &ConfigType, "sONO", ci->key, parent, values, Py_None);
626 children = PyTuple_New(ci->children_num); /* New reference. */
627 for (i = 0; i < ci->children_num; ++i) {
628 PyTuple_SET_ITEM(children, i, cpy_oconfig_to_pyconfig(ci->children + i, item));
630 tmp = ((Config *) item)->children;
631 ((Config *) item)->children = children;
636 static int cpy_config(oconfig_item_t *ci) {
642 /* Ok in theory we shouldn't do initialization at this point
643 * but we have to. In order to give python scripts a chance
644 * to register a config callback we need to be able to execute
645 * python code during the config callback so we have to start
646 * the interpreter here. */
647 /* Do *not* use the python "thread" module at this point! */
650 PyType_Ready(&ConfigType);
651 PyType_Ready(&PluginDataType);
652 ValuesType.tp_base = &PluginDataType;
653 PyType_Ready(&ValuesType);
654 NotificationType.tp_base = &PluginDataType;
655 PyType_Ready(&NotificationType);
656 sys = PyImport_ImportModule("sys"); /* New reference. */
658 cpy_log_exception("python initialization");
661 sys_path = PyObject_GetAttrString(sys, "path"); /* New reference. */
663 if (sys_path == NULL) {
664 cpy_log_exception("python initialization");
667 module = Py_InitModule("collectd", cpy_methods); /* Borrowed reference. */
668 PyModule_AddObject(module, "Config", (PyObject *) &ConfigType); /* Steals a reference. */
669 PyModule_AddObject(module, "Values", (PyObject *) &ValuesType); /* Steals a reference. */
670 PyModule_AddObject(module, "Notification", (PyObject *) &NotificationType); /* Steals a reference. */
671 PyModule_AddIntConstant(module, "LOG_DEBUG", LOG_DEBUG);
672 PyModule_AddIntConstant(module, "LOG_INFO", LOG_INFO);
673 PyModule_AddIntConstant(module, "LOG_NOTICE", LOG_NOTICE);
674 PyModule_AddIntConstant(module, "LOG_WARNING", LOG_WARNING);
675 PyModule_AddIntConstant(module, "LOG_ERROR", LOG_ERR);
676 PyModule_AddIntConstant(module, "NOTIF_FAILURE", NOTIF_FAILURE);
677 PyModule_AddIntConstant(module, "NOTIF_WARNING", NOTIF_WARNING);
678 PyModule_AddIntConstant(module, "NOTIF_OKAY", NOTIF_OKAY);
679 for (i = 0; i < ci->children_num; ++i) {
680 oconfig_item_t *item = ci->children + i;
682 if (strcasecmp(item->key, "Interactive") == 0) {
683 if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN)
685 do_interactive = item->values[0].value.boolean;
686 } else if (strcasecmp(item->key, "LogTraces") == 0) {
687 if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN)
689 if (!item->values[0].value.boolean) {
690 Py_XDECREF(cpy_format_exception);
691 cpy_format_exception = NULL;
694 if (cpy_format_exception)
696 tb = PyImport_ImportModule("traceback"); /* New reference. */
698 cpy_log_exception("python initialization");
701 cpy_format_exception = PyObject_GetAttrString(tb, "format_exception"); /* New reference. */
703 if (cpy_format_exception == NULL)
704 cpy_log_exception("python initialization");
705 } else if (strcasecmp(item->key, "ModulePath") == 0) {
707 PyObject *dir_object;
709 if (cf_util_get_string(item, &dir) != 0)
711 dir_object = PyString_FromString(dir); /* New reference. */
712 if (dir_object == NULL) {
713 ERROR("python plugin: Unable to convert \"%s\" to "
714 "a python object.", dir);
716 cpy_log_exception("python initialization");
719 if (PyList_Append(sys_path, dir_object) != 0) {
720 ERROR("python plugin: Unable to append \"%s\" to "
721 "python module path.", dir);
722 cpy_log_exception("python initialization");
724 Py_DECREF(dir_object);
726 } else if (strcasecmp(item->key, "Import") == 0) {
727 char *module_name = NULL;
730 if (cf_util_get_string(item, &module_name) != 0)
732 module = PyImport_ImportModule(module_name); /* New reference. */
733 if (module == NULL) {
734 ERROR("python plugin: Error importing module \"%s\".", module_name);
735 cpy_log_exception("python initialization");
740 } else if (strcasecmp(item->key, "Module") == 0) {
745 if (cf_util_get_string(item, &name) != 0)
747 for (c = cpy_config_callbacks; c; c = c->next) {
748 if (strcasecmp(c->name + 7, name) == 0)
752 WARNING("python plugin: Found a configuration for the \"%s\" plugin, "
753 "but the plugin isn't loaded or didn't register "
754 "a configuration callback.", name);
760 ret = PyObject_CallFunction(c->callback, "N",
761 cpy_oconfig_to_pyconfig(item, NULL)); /* New reference. */
763 ret = PyObject_CallFunction(c->callback, "NO",
764 cpy_oconfig_to_pyconfig(item, NULL), c->data); /* New reference. */
766 cpy_log_exception("loading module");
770 WARNING("python plugin: Ignoring unknown config key \"%s\".", item->key);
777 void module_register(void) {
778 plugin_register_complex_config("python", cpy_config);
779 plugin_register_init("python", cpy_init);
780 // plugin_register_read("python", cna_read);
781 plugin_register_shutdown("python", cpy_shutdown);