2 #include <structmember.h>
7 typedef struct cpy_callback_s {
11 struct cpy_callback_s *next;
14 /* This is our global thread state. Python saves some stuff in thread-local
15 * storage. So if we allow the interpreter to run in the background
16 * (the scriptwriters might have created some threads from python), we have
17 * to save the state so we can resume it later from a different thread.
19 * Technically the Global Interpreter Lock (GIL) and thread states can be
20 * manipulated independently. But to keep stuff from getting too complex
21 * we'll just use PyEval_SaveTread and PyEval_RestoreThreas which takes
22 * care of the thread states as well as the GIL. */
24 static PyThreadState *state;
26 static cpy_callback_t *cpy_config_callbacks;
27 static cpy_callback_t *cpy_init_callbacks;
28 static cpy_callback_t *cpy_shutdown_callbacks;
31 PyObject_HEAD /* No semicolon! */
38 static void Config_dealloc(PyObject *s) {
39 Config *self = (Config *) s;
41 Py_XDECREF(self->parent);
42 Py_XDECREF(self->key);
43 Py_XDECREF(self->values);
44 Py_XDECREF(self->children);
45 self->ob_type->tp_free(s);
48 static PyObject *Config_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
51 self = (Config *) type->tp_alloc(type, 0);
58 self->children = NULL;
59 return (PyObject *) self;
62 static int Config_init(PyObject *s, PyObject *args, PyObject *kwds) {
63 PyObject *key = NULL, *parent = NULL, *values = NULL, *children = NULL, *tmp;
64 Config *self = (Config *) s;
65 static char *kwlist[] = {"key", "parent", "values", "children", NULL};
67 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|OOO", kwlist,
68 &key, &parent, &values, &children))
72 values = PyTuple_New(0);
75 if (children == NULL) {
76 children = PyTuple_New(0);
86 self->parent = parent;
92 self->values = values;
95 if (children != NULL) {
98 self->children = children;
104 static PyMemberDef Config_members[] = {
105 {"Parent", T_OBJECT, offsetof(Config, parent), 0, "Parent node"},
106 {"Key", T_OBJECT_EX, offsetof(Config, key), 0, "Keyword of this node"},
107 {"Values", T_OBJECT_EX, offsetof(Config, values), 0, "Values after the key"},
108 {"Children", T_OBJECT_EX, offsetof(Config, children), 0, "Childnodes of this node"},
112 static PyTypeObject ConfigType = {
113 PyObject_HEAD_INIT(NULL)
115 "collectd.Config", /* tp_name */
116 sizeof(Config), /* tp_basicsize */
117 0, /* Will be filled in later */
118 Config_dealloc, /* tp_dealloc */
124 0, /* tp_as_number */
125 0, /* tp_as_sequence */
126 0, /* tp_as_mapping */
132 0, /* tp_as_buffer */
133 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
134 "Cool help text later", /* tp_doc */
137 0, /* tp_richcompare */
138 0, /* tp_weaklistoffset */
142 Config_members, /* tp_members */
146 0, /* tp_descr_get */
147 0, /* tp_descr_set */
148 0, /* tp_dictoffset */
149 Config_init, /* tp_init */
151 Config_new /* tp_new */
154 static PyObject *cpy_register_generic(cpy_callback_t **list_head, PyObject *args, PyObject *kwds) {
156 const char *name = NULL;
157 PyObject *callback = NULL, *data = NULL;
158 static char *kwlist[] = {"callback", "data", "name", NULL};
160 if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oz", kwlist, &callback, &data, &name) == 0) return NULL;
161 if (PyCallable_Check(callback) == 0) {
162 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
168 mod = PyObject_GetAttrString(callback, "__module__");
169 if (mod != NULL) name = PyString_AsString(mod);
171 PyErr_SetString(PyExc_ValueError, "No module name specified and "
172 "callback function does not have a \"__module__\" attribute.");
178 c = malloc(sizeof(*c));
179 c->name = strdup(name);
180 c->callback = callback;
182 c->next = *list_head;
187 static PyObject *cpy_register_config(PyObject *self, PyObject *args, PyObject *kwds) {
188 return cpy_register_generic(&cpy_config_callbacks, args, kwds);
191 static PyObject *cpy_register_init(PyObject *self, PyObject *args, PyObject *kwds) {
192 return cpy_register_generic(&cpy_init_callbacks, args, kwds);
195 static PyObject *cpy_register_shutdown(PyObject *self, PyObject *args, PyObject *kwds) {
196 return cpy_register_generic(&cpy_shutdown_callbacks, args, kwds);
199 static PyObject *cpy_Error(PyObject *self, PyObject *args) {
201 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
202 plugin_log(LOG_ERR, "%s", text);
206 static PyObject *cpy_Warning(PyObject *self, PyObject *args) {
208 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
209 plugin_log(LOG_WARNING, "%s", text);
213 static PyObject *cpy_Notice(PyObject *self, PyObject *args) {
215 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
216 plugin_log(LOG_NOTICE, "%s", text);
220 static PyObject *cpy_Info(PyObject *self, PyObject *args) {
222 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
223 plugin_log(LOG_INFO, "%s", text);
227 static PyObject *cpy_Debug(PyObject *self, PyObject *args) {
230 if (PyArg_ParseTuple(args, "s", &text) == 0) return NULL;
231 plugin_log(LOG_DEBUG, "%s", text);
236 static PyMethodDef cpy_methods[] = {
237 {"Debug", cpy_Debug, METH_VARARGS, "This is an unhelpful text."},
238 {"Info", cpy_Info, METH_VARARGS, "This is an unhelpful text."},
239 {"Notice", cpy_Notice, METH_VARARGS, "This is an unhelpful text."},
240 {"Warning", cpy_Warning, METH_VARARGS, "This is an unhelpful text."},
241 {"Error", cpy_Error, METH_VARARGS, "This is an unhelpful text."},
242 {"register_init", (PyCFunction) cpy_register_init, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
243 {"register_config", (PyCFunction) cpy_register_config, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
244 {"register_shutdown", (PyCFunction) cpy_register_shutdown, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
248 static int cpy_shutdown(void) {
252 /* This can happen if the module was loaded but not configured. */
254 PyEval_RestoreThread(state);
256 for (c = cpy_shutdown_callbacks; c; c = c->next) {
258 ret = PyObject_CallObject(c->callback, NULL); /* New reference. */
260 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
262 PyErr_Print(); /* FIXME */
270 static int cpy_init(void) {
274 PyEval_InitThreads();
275 /* Now it's finally OK to use python threads. */
276 for (c = cpy_init_callbacks; c; c = c->next) {
278 ret = PyObject_CallObject(c->callback, NULL); /* New reference. */
280 ret = PyObject_CallFunctionObjArgs(c->callback, c->data, (void *) 0); /* New reference. */
282 PyErr_Print(); /* FIXME */
286 state = PyEval_SaveThread();
290 static PyObject *cpy_oconfig_to_pyconfig(oconfig_item_t *ci, PyObject *parent) {
292 PyObject *item, *values, *children, *tmp;
297 values = PyTuple_New(ci->values_num); /* New reference. */
298 for (i = 0; i < ci->values_num; ++i) {
299 if (ci->values[i].type == OCONFIG_TYPE_STRING) {
300 PyTuple_SET_ITEM(values, i, PyString_FromString(ci->values[i].value.string));
301 } else if (ci->values[i].type == OCONFIG_TYPE_NUMBER) {
302 PyTuple_SET_ITEM(values, i, PyFloat_FromDouble(ci->values[i].value.number));
303 } else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN) {
304 PyTuple_SET_ITEM(values, i, PyBool_FromLong(ci->values[i].value.boolean));
308 item = PyObject_CallFunction((PyObject *) &ConfigType, "sONO", ci->key, parent, values, Py_None);
311 children = PyTuple_New(ci->children_num); /* New reference. */
312 for (i = 0; i < ci->children_num; ++i) {
313 PyTuple_SET_ITEM(children, i, cpy_oconfig_to_pyconfig(ci->children + i, item));
315 tmp = ((Config *) item)->children;
316 ((Config *) item)->children = children;
321 static int cpy_config(oconfig_item_t *ci) {
327 /* Ok in theory we shouldn't do initialization at this point
328 * but we have to. In order to give python scripts a chance
329 * to register a config callback we need to be able to execute
330 * python code during the config callback so we have to start
331 * the interpreter here. */
332 /* Do *not* use the python "thread" module at this point! */
335 PyType_Ready(&ConfigType);
336 sys = PyImport_ImportModule("sys"); /* New reference. */
338 ERROR("python module: Unable to import \"sys\" module.");
339 /* Just print the default python exception text to stderr. */
343 sys_path = PyObject_GetAttrString(sys, "path"); /* New reference. */
345 if (sys_path == NULL) {
346 ERROR("python module: Unable to read \"sys.path\".");
350 module = Py_InitModule("collectd", cpy_methods); /* Borrowed reference. */
351 PyModule_AddObject(module, "Config", (PyObject *) &ConfigType); /* Steals a reference. */
352 for (i = 0; i < ci->children_num; ++i) {
353 oconfig_item_t *item = ci->children + i;
355 if (strcasecmp(item->key, "ModulePath") == 0) {
357 PyObject *dir_object;
359 if (cf_util_get_string(item, &dir) != 0)
361 dir_object = PyString_FromString(dir); /* New reference. */
362 if (dir_object == NULL) {
363 ERROR("python plugin: Unable to convert \"%s\" to "
364 "a python object.", dir);
369 if (PyList_Append(sys_path, dir_object) != 0) {
370 ERROR("python plugin: Unable to append \"%s\" to "
371 "python module path.", dir);
374 Py_DECREF(dir_object);
376 } else if (strcasecmp(item->key, "Import") == 0) {
377 char *module_name = NULL;
380 if (cf_util_get_string(item, &module_name) != 0)
382 module = PyImport_ImportModule(module_name); /* New reference. */
383 if (module == NULL) {
384 ERROR("python plugin: Error importing module \"%s\".", module_name);
389 } else if (strcasecmp(item->key, "Module") == 0) {
394 if (cf_util_get_string(item, &name) != 0)
396 for (c = cpy_config_callbacks; c; c = c->next) {
397 if (strcasecmp(c->name, name) == 0)
401 WARNING("python plugin: Found a configuration for the \"%s\" plugin, "
402 "but the plugin isn't loaded or didn't register "
403 "a configuration callback.", name);
409 ret = PyObject_CallFunction(c->callback, "N",
410 cpy_oconfig_to_pyconfig(item, NULL)); /* New reference. */
412 ret = PyObject_CallFunction(c->callback, "NO",
413 cpy_oconfig_to_pyconfig(item, NULL), c->data); /* New reference. */
419 WARNING("python plugin: Ignoring unknown config key \"%s\".", item->key);
426 void module_register(void) {
427 plugin_register_complex_config("python", cpy_config);
428 plugin_register_init("python", cpy_init);
429 // plugin_register_read("python", cna_read);
430 plugin_register_shutdown("python", cpy_shutdown);