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 void cpy_log_callback(int severity, const char *message, user_data_t *data) {
204 cpy_callback_t * c = data->data;
209 ret = PyObject_CallFunction(c->callback, "is", severity, message); /* New reference. */
211 ret = PyObject_CallFunction(c->callback, "isO", severity, message, c->data); /* New reference. */
215 /* Do we really want to trigger a log callback because a log callback failed?
224 static void cpy_flush_callback(int timeout, const char *id, user_data_t *data) {
225 cpy_callback_t * c = data->data;
230 ret = PyObject_CallFunction(c->callback, "is", timeout, id); /* New reference. */
232 ret = PyObject_CallFunction(c->callback, "isO", timeout, id, c->data); /* New reference. */
235 cpy_log_exception("flush callback");
242 static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args, PyObject *kwds, int short_name) {
245 const char *name = NULL;
246 PyObject *callback = NULL, *data = NULL, *mod = NULL;
247 static char *kwlist[] = {"callback", "data", "name", NULL};
249 if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oz", kwlist, &callback, &data, &name) == 0) return NULL;
250 if (PyCallable_Check(callback) == 0) {
251 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
254 cpy_build_name(buf, sizeof(buf), callback, name, short_name);
258 c = malloc(sizeof(*c));
259 c->name = strdup(buf);
260 c->callback = callback;
262 c->next = *list_head;
265 return PyString_FromString(buf);
268 static PyObject *cpy_flush(cpy_callback_t **list_head, PyObject *args, PyObject *kwds) {
270 const char *plugin = NULL, *identifier = NULL;
271 static char *kwlist[] = {"plugin", "timeout", "identifier", NULL};
273 if (PyArg_ParseTupleAndKeywords(args, kwds, "|ziz", kwlist, &plugin, &timeout, &identifier) == 0) return NULL;
274 Py_BEGIN_ALLOW_THREADS
275 plugin_flush(plugin, timeout, identifier);
280 static PyObject *cpy_register_config(PyObject *self, PyObject *args, PyObject *kwds) {
281 return cpy_register_generic(&cpy_config_callbacks, args, kwds, 1);
284 static PyObject *cpy_register_init(PyObject *self, PyObject *args, PyObject *kwds) {
285 return cpy_register_generic(&cpy_init_callbacks, args, kwds, 0);
288 typedef int reg_function_t(const char *name, void *callback, void *data);
290 static PyObject *cpy_register_generic_userdata(void *reg, void *handler, PyObject *args, PyObject *kwds, int short_name) {
292 reg_function_t *register_function = (reg_function_t *) reg;
293 cpy_callback_t *c = NULL;
294 user_data_t *user_data = NULL;
295 const char *name = NULL;
296 PyObject *callback = NULL, *data = NULL;
297 static char *kwlist[] = {"callback", "data", "name", NULL};
299 if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oz", kwlist, &callback, &data, &name) == 0) return NULL;
300 if (PyCallable_Check(callback) == 0) {
301 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
304 cpy_build_name(buf, sizeof(buf), callback, name, short_name);
308 c = malloc(sizeof(*c));
309 c->name = strdup(buf);
310 c->callback = callback;
313 user_data = malloc(sizeof(*user_data));
314 user_data->free_func = cpy_destroy_user_data;
316 register_function(buf, handler, user_data);
317 return PyString_FromString(buf);
320 static PyObject *cpy_register_read(PyObject *self, PyObject *args, PyObject *kwds) {
322 cpy_callback_t *c = NULL;
323 user_data_t *user_data = NULL;
325 const char *name = NULL;
326 PyObject *callback = NULL, *data = NULL;
328 static char *kwlist[] = {"callback", "interval", "data", "name", NULL};
330 if (PyArg_ParseTupleAndKeywords(args, kwds, "O|dOz", kwlist, &callback, &interval, &data, &name) == 0) return NULL;
331 if (PyCallable_Check(callback) == 0) {
332 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
335 cpy_build_name(buf, sizeof(buf), callback, name, 0);
339 c = malloc(sizeof(*c));
340 c->name = strdup(buf);
341 c->callback = callback;
344 user_data = malloc(sizeof(*user_data));
345 user_data->free_func = cpy_destroy_user_data;
347 ts.tv_sec = interval;
348 ts.tv_nsec = (interval - ts.tv_sec) * 1000000000;
349 plugin_register_complex_read(buf, cpy_read_callback, &ts, user_data);
350 return PyString_FromString(buf);
353 static PyObject *cpy_register_log(PyObject *self, PyObject *args, PyObject *kwds) {
354 return cpy_register_generic_userdata(plugin_register_log, cpy_log_callback, args, kwds, 0);
357 static PyObject *cpy_register_write(PyObject *self, PyObject *args, PyObject *kwds) {
358 return cpy_register_generic_userdata(plugin_register_write, cpy_write_callback, args, kwds, 0);
361 static PyObject *cpy_register_flush(PyObject *self, PyObject *args, PyObject *kwds) {
362 return cpy_register_generic_userdata(plugin_register_flush, cpy_flush_callback, args, kwds, 1);
365 static PyObject *cpy_register_shutdown(PyObject *self, PyObject *args, PyObject *kwds) {
366 return cpy_register_generic(&cpy_shutdown_callbacks, args, kwds, 0);
369 static PyObject *cpy_error(PyObject *self, PyObject *args) {
371 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
372 Py_BEGIN_ALLOW_THREADS
373 plugin_log(LOG_ERR, "%s", text);
378 static PyObject *cpy_warning(PyObject *self, PyObject *args) {
380 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
381 Py_BEGIN_ALLOW_THREADS
382 plugin_log(LOG_WARNING, "%s", text);
387 static PyObject *cpy_notice(PyObject *self, PyObject *args) {
389 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
390 Py_BEGIN_ALLOW_THREADS
391 plugin_log(LOG_NOTICE, "%s", text);
396 static PyObject *cpy_info(PyObject *self, PyObject *args) {
398 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
399 Py_BEGIN_ALLOW_THREADS
400 plugin_log(LOG_INFO, "%s", text);
405 static PyObject *cpy_debug(PyObject *self, PyObject *args) {
408 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
409 plugin_log(LOG_DEBUG, "%s", text);
414 static PyObject *cpy_unregister_generic(cpy_callback_t **list_head, PyObject *arg, const char *desc, int short_name) {
417 cpy_callback_t *prev = NULL, *tmp;
419 if (PyString_Check(arg)) {
420 name = PyString_AsString(arg);
422 if (!PyCallable_Check(arg)) {
423 PyErr_SetString(PyExc_TypeError, "This function needs a string or a callable object as its only parameter.");
426 cpy_build_name(buf, sizeof(buf), arg, NULL, short_name);
429 for (tmp = *list_head; tmp; prev = tmp, tmp = tmp->next)
430 if (strcmp(name, tmp->name) == 0)
434 PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
437 /* Yes, this is actually save. To call this function the calles has to
438 * hold the GIL. Well, save as long as there is only one GIL anyway ... */
440 *list_head = tmp->next;
442 prev->next = tmp->next;
443 cpy_destroy_user_data(tmp);
447 typedef int cpy_unregister_function_t(const char *name);
449 static PyObject *cpy_unregister_generic_userdata(cpy_unregister_function_t *unreg, PyObject *arg, const char *desc, int short_name) {
453 if (PyString_Check(arg)) {
454 name = PyString_AsString(arg);
456 if (!PyCallable_Check(arg)) {
457 PyErr_SetString(PyExc_TypeError, "This function needs a string or a callable object as its only parameter.");
460 cpy_build_name(buf, sizeof(buf), arg, NULL, short_name);
463 if (unreg(name) == 0)
465 PyErr_Format(PyExc_RuntimeError, "Unable to unregister %s callback '%s'.", desc, name);
469 static PyObject *cpy_unregister_log(PyObject *self, PyObject *arg) {
470 return cpy_unregister_generic_userdata(plugin_unregister_log, arg, "log", 0);
473 static PyObject *cpy_unregister_init(PyObject *self, PyObject *arg) {
474 return cpy_unregister_generic(&cpy_init_callbacks, arg, "init", 0);
477 static PyObject *cpy_unregister_config(PyObject *self, PyObject *arg) {
478 return cpy_unregister_generic(&cpy_config_callbacks, arg, "config", 1);
481 static PyObject *cpy_unregister_read(PyObject *self, PyObject *arg) {
482 return cpy_unregister_generic_userdata(plugin_unregister_read, arg, "read", 0);
485 static PyObject *cpy_unregister_write(PyObject *self, PyObject *arg) {
486 return cpy_unregister_generic_userdata(plugin_unregister_write, arg, "write", 0);
489 static PyObject *cpy_unregister_flush(PyObject *self, PyObject *arg) {
490 return cpy_unregister_generic_userdata(plugin_unregister_flush, arg, "flush", 1);
493 static PyObject *cpy_unregister_shutdown(PyObject *self, PyObject *arg) {
494 return cpy_unregister_generic(&cpy_shutdown_callbacks, arg, "shutdown", 0);
497 static PyMethodDef cpy_methods[] = {
498 {"debug", cpy_debug, METH_VARARGS, "This is an unhelpful text."},
499 {"info", cpy_info, METH_VARARGS, "This is an unhelpful text."},
500 {"notice", cpy_notice, METH_VARARGS, "This is an unhelpful text."},
501 {"warning", cpy_warning, METH_VARARGS, "This is an unhelpful text."},
502 {"error", cpy_error, METH_VARARGS, "This is an unhelpful text."},
503 {"flush", (PyCFunction) cpy_flush, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
504 {"register_log", (PyCFunction) cpy_register_log, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
505 {"register_init", (PyCFunction) cpy_register_init, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
506 {"register_config", (PyCFunction) cpy_register_config, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
507 {"register_read", (PyCFunction) cpy_register_read, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
508 {"register_write", (PyCFunction) cpy_register_write, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
509 {"register_flush", (PyCFunction) cpy_register_flush, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
510 {"register_shutdown", (PyCFunction) cpy_register_shutdown, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
511 {"unregister_log", cpy_unregister_log, METH_O, "This is an unhelpful text."},
512 {"unregister_init", cpy_unregister_init, METH_O, "This is an unhelpful text."},
513 {"unregister_config", cpy_unregister_config, METH_O, "This is an unhelpful text."},
514 {"unregister_read", cpy_unregister_read, METH_O, "This is an unhelpful text."},
515 {"unregister_write", cpy_unregister_write, METH_O, "This is an unhelpful text."},
516 {"unregister_flush", cpy_unregister_flush, METH_O, "This is an unhelpful text."},
517 {"unregister_shutdown", cpy_unregister_shutdown, METH_O, "This is an unhelpful text."},
521 static int cpy_shutdown(void) {
525 /* This can happen if the module was loaded but not configured. */
527 PyEval_RestoreThread(state);
529 for (c = cpy_shutdown_callbacks; c; c = c->next) {
530 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
532 cpy_log_exception("shutdown callback");
540 static void *cpy_interactive(void *data) {
542 if (PyImport_ImportModule("readline") == NULL) {
543 /* This interactive session will suck. */
544 cpy_log_exception("interactive session init");
546 PyRun_InteractiveLoop(stdin, "<stdin>");
548 NOTICE("python: Interactive interpreter exited, stopping collectd ...");
553 static int cpy_init(void) {
556 static pthread_t thread;
558 PyEval_InitThreads();
559 /* Now it's finally OK to use python threads. */
560 for (c = cpy_init_callbacks; c; c = c->next) {
561 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
563 cpy_log_exception("init callback");
567 state = PyEval_SaveThread();
568 if (do_interactive) {
569 if (pthread_create(&thread, NULL, cpy_interactive, NULL)) {
570 ERROR("python: Error creating thread for interactive interpreter.");
577 static PyObject *cpy_oconfig_to_pyconfig(oconfig_item_t *ci, PyObject *parent) {
579 PyObject *item, *values, *children, *tmp;
584 values = PyTuple_New(ci->values_num); /* New reference. */
585 for (i = 0; i < ci->values_num; ++i) {
586 if (ci->values[i].type == OCONFIG_TYPE_STRING) {
587 PyTuple_SET_ITEM(values, i, PyString_FromString(ci->values[i].value.string));
588 } else if (ci->values[i].type == OCONFIG_TYPE_NUMBER) {
589 PyTuple_SET_ITEM(values, i, PyFloat_FromDouble(ci->values[i].value.number));
590 } else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN) {
591 PyTuple_SET_ITEM(values, i, PyBool_FromLong(ci->values[i].value.boolean));
595 item = PyObject_CallFunction((PyObject *) &ConfigType, "sONO", ci->key, parent, values, Py_None);
598 children = PyTuple_New(ci->children_num); /* New reference. */
599 for (i = 0; i < ci->children_num; ++i) {
600 PyTuple_SET_ITEM(children, i, cpy_oconfig_to_pyconfig(ci->children + i, item));
602 tmp = ((Config *) item)->children;
603 ((Config *) item)->children = children;
608 static int cpy_config(oconfig_item_t *ci) {
614 /* Ok in theory we shouldn't do initialization at this point
615 * but we have to. In order to give python scripts a chance
616 * to register a config callback we need to be able to execute
617 * python code during the config callback so we have to start
618 * the interpreter here. */
619 /* Do *not* use the python "thread" module at this point! */
622 PyType_Ready(&ConfigType);
623 PyType_Ready(&PluginDataType);
624 ValuesType.tp_base = &PluginDataType;
625 PyType_Ready(&ValuesType);
626 sys = PyImport_ImportModule("sys"); /* New reference. */
628 cpy_log_exception("python initialization");
631 sys_path = PyObject_GetAttrString(sys, "path"); /* New reference. */
633 if (sys_path == NULL) {
634 cpy_log_exception("python initialization");
637 module = Py_InitModule("collectd", cpy_methods); /* Borrowed reference. */
638 PyModule_AddObject(module, "Config", (PyObject *) &ConfigType); /* Steals a reference. */
639 PyModule_AddObject(module, "Values", (PyObject *) &ValuesType); /* Steals a reference. */
640 PyModule_AddIntConstant(module, "LOG_DEBUG", LOG_DEBUG);
641 PyModule_AddIntConstant(module, "LOG_INFO", LOG_INFO);
642 PyModule_AddIntConstant(module, "LOG_NOTICE", LOG_NOTICE);
643 PyModule_AddIntConstant(module, "LOG_WARNING", LOG_WARNING);
644 PyModule_AddIntConstant(module, "LOG_ERROR", LOG_ERR);
645 for (i = 0; i < ci->children_num; ++i) {
646 oconfig_item_t *item = ci->children + i;
648 if (strcasecmp(item->key, "Interactive") == 0) {
649 if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN)
651 do_interactive = item->values[0].value.boolean;
652 } else if (strcasecmp(item->key, "LogTraces") == 0) {
653 if (item->values_num != 1 || item->values[0].type != OCONFIG_TYPE_BOOLEAN)
655 if (!item->values[0].value.boolean) {
656 Py_XDECREF(cpy_format_exception);
657 cpy_format_exception = NULL;
660 if (cpy_format_exception)
662 tb = PyImport_ImportModule("traceback"); /* New reference. */
664 cpy_log_exception("python initialization");
667 cpy_format_exception = PyObject_GetAttrString(tb, "format_exception"); /* New reference. */
669 if (cpy_format_exception == NULL)
670 cpy_log_exception("python initialization");
671 } else if (strcasecmp(item->key, "ModulePath") == 0) {
673 PyObject *dir_object;
675 if (cf_util_get_string(item, &dir) != 0)
677 dir_object = PyString_FromString(dir); /* New reference. */
678 if (dir_object == NULL) {
679 ERROR("python plugin: Unable to convert \"%s\" to "
680 "a python object.", dir);
682 cpy_log_exception("python initialization");
685 if (PyList_Append(sys_path, dir_object) != 0) {
686 ERROR("python plugin: Unable to append \"%s\" to "
687 "python module path.", dir);
688 cpy_log_exception("python initialization");
690 Py_DECREF(dir_object);
692 } else if (strcasecmp(item->key, "Import") == 0) {
693 char *module_name = NULL;
696 if (cf_util_get_string(item, &module_name) != 0)
698 module = PyImport_ImportModule(module_name); /* New reference. */
699 if (module == NULL) {
700 ERROR("python plugin: Error importing module \"%s\".", module_name);
701 cpy_log_exception("python initialization");
706 } else if (strcasecmp(item->key, "Module") == 0) {
711 if (cf_util_get_string(item, &name) != 0)
713 for (c = cpy_config_callbacks; c; c = c->next) {
714 if (strcasecmp(c->name + 7, name) == 0)
718 WARNING("python plugin: Found a configuration for the \"%s\" plugin, "
719 "but the plugin isn't loaded or didn't register "
720 "a configuration callback.", name);
726 ret = PyObject_CallFunction(c->callback, "N",
727 cpy_oconfig_to_pyconfig(item, NULL)); /* New reference. */
729 ret = PyObject_CallFunction(c->callback, "NO",
730 cpy_oconfig_to_pyconfig(item, NULL), c->data); /* New reference. */
732 cpy_log_exception("loading module");
736 WARNING("python plugin: Ignoring unknown config key \"%s\".", item->key);
743 void module_register(void) {
744 plugin_register_complex_config("python", cpy_config);
745 plugin_register_init("python", cpy_init);
746 // plugin_register_read("python", cna_read);
747 plugin_register_shutdown("python", cpy_shutdown);