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;
29 PyObject_HEAD /* No semicolon! */
36 static void Config_dealloc(PyObject *s) {
37 Config *self = (Config *) s;
39 Py_XDECREF(self->parent);
40 Py_XDECREF(self->key);
41 Py_XDECREF(self->values);
42 Py_XDECREF(self->children);
43 self->ob_type->tp_free(s);
46 static PyObject *Config_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
49 self = (Config *) type->tp_alloc(type, 0);
56 self->children = NULL;
57 return (PyObject *) self;
60 static int Config_init(PyObject *s, PyObject *args, PyObject *kwds) {
61 PyObject *key = NULL, *parent = NULL, *values = NULL, *children = NULL, *tmp;
62 Config *self = (Config *) s;
63 static char *kwlist[] = {"key", "parent", "values", "children", NULL};
65 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|OOO", kwlist,
66 &key, &parent, &values, &children))
70 values = PyTuple_New(0);
73 if (children == NULL) {
74 children = PyTuple_New(0);
84 self->parent = parent;
90 self->values = values;
93 if (children != NULL) {
96 self->children = children;
102 static PyMemberDef Config_members[] = {
103 {"Parent", T_OBJECT, offsetof(Config, parent), 0, "Parent node"},
104 {"Key", T_OBJECT_EX, offsetof(Config, key), 0, "Keyword of this node"},
105 {"Values", T_OBJECT_EX, offsetof(Config, values), 0, "Values after the key"},
106 {"Children", T_OBJECT_EX, offsetof(Config, children), 0, "Childnodes of this node"},
110 static PyTypeObject ConfigType = {
111 PyObject_HEAD_INIT(NULL)
113 "collectd.Config", /* tp_name */
114 sizeof(Config), /* tp_basicsize */
115 0, /* Will be filled in later */
116 Config_dealloc, /* tp_dealloc */
122 0, /* tp_as_number */
123 0, /* tp_as_sequence */
124 0, /* tp_as_mapping */
130 0, /* tp_as_buffer */
131 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
132 "Cool help text later", /* tp_doc */
135 0, /* tp_richcompare */
136 0, /* tp_weaklistoffset */
140 Config_members, /* tp_members */
144 0, /* tp_descr_get */
145 0, /* tp_descr_set */
146 0, /* tp_dictoffset */
147 Config_init, /* tp_init */
149 Config_new /* tp_new */
152 static PyObject *cpy_register_config(PyObject *self, PyObject *args, PyObject *kwds) {
154 const char *name = NULL;
155 PyObject *callback = NULL, *data = NULL;
156 static char *kwlist[] = {"callback", "data", "name", NULL};
158 if (PyArg_ParseTupleAndKeywords(args, kwds, "O|Oz", kwlist, &callback, &data, &name) == 0) return NULL;
159 if (PyCallable_Check(callback) == 0) {
160 PyErr_SetString(PyExc_TypeError, "callback needs a be a callable object.");
166 mod = PyObject_GetAttrString(callback, "__module__");
167 if (mod != NULL) name = PyString_AsString(mod);
169 PyErr_SetString(PyExc_ValueError, "No module name specified and "
170 "callback function does not have a \"__module__\" attribute.");
174 c = malloc(sizeof(*c));
175 c->name = strdup(name);
176 c->callback = callback;
178 c->next = cpy_config_callbacks;
179 cpy_config_callbacks = c;
183 static PyMethodDef cpy_methods[] = {
184 {"register_config", (PyCFunction) cpy_register_config, METH_VARARGS | METH_KEYWORDS, "This is an unhelpful text."},
188 static int cpy_shutdown(void) {
189 /* This can happen if the module was loaded but not configured. */
191 PyEval_RestoreThread(state);
196 static int cpy_init(void) {
197 PyEval_InitThreads();
198 /* Now it's finally OK to use python threads. */
202 static PyObject *cpy_oconfig_to_pyconfig(oconfig_item_t *ci, PyObject *parent) {
204 PyObject *item, *values, *children, *tmp;
209 values = PyTuple_New(ci->values_num); /* New reference. */
210 for (i = 0; i < ci->values_num; ++i) {
211 if (ci->values[i].type == OCONFIG_TYPE_STRING) {
212 PyTuple_SET_ITEM(values, i, PyString_FromString(ci->values[i].value.string));
213 } else if (ci->values[i].type == OCONFIG_TYPE_NUMBER) {
214 PyTuple_SET_ITEM(values, i, PyFloat_FromDouble(ci->values[i].value.number));
215 } else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN) {
216 PyTuple_SET_ITEM(values, i, PyBool_FromLong(ci->values[i].value.boolean));
220 item = PyObject_CallFunction((PyObject *) &ConfigType, "sONO", ci->key, parent, values, Py_None);
223 children = PyTuple_New(ci->children_num); /* New reference. */
224 for (i = 0; i < ci->children_num; ++i) {
225 PyTuple_SET_ITEM(children, i, cpy_oconfig_to_pyconfig(ci->children + i, item));
227 tmp = ((Config *) item)->children;
228 ((Config *) item)->children = children;
233 static int cpy_config(oconfig_item_t *ci) {
239 /* Ok in theory we shouldn't do initialization at this point
240 * but we have to. In order to give python scripts a chance
241 * to register a config callback we need to be able to execute
242 * python code during the config callback so we have to start
243 * the interpreter here. */
244 /* Do *not* use the python "thread" module at this point! */
247 PyType_Ready(&ConfigType);
248 sys = PyImport_ImportModule("sys"); /* New reference. */
250 ERROR("python module: Unable to import \"sys\" module.");
251 /* Just print the default python exception text to stderr. */
255 sys_path = PyObject_GetAttrString(sys, "path"); /* New reference. */
257 if (sys_path == NULL) {
258 ERROR("python module: Unable to read \"sys.path\".");
262 module = Py_InitModule("collectd", cpy_methods); /* Borrowed reference. */
263 PyModule_AddObject(module, "Config", (PyObject *) &ConfigType); /* Steals a reference. */
264 for (i = 0; i < ci->children_num; ++i) {
265 oconfig_item_t *item = ci->children + i;
267 if (strcasecmp(item->key, "ModulePath") == 0) {
269 PyObject *dir_object;
271 if (cf_util_get_string(item, &dir) != 0)
273 dir_object = PyString_FromString(dir); /* New reference. */
274 if (dir_object == NULL) {
275 ERROR("python plugin: Unable to convert \"%s\" to "
276 "a python object.", dir);
281 if (PyList_Append(sys_path, dir_object) != 0) {
282 ERROR("python plugin: Unable to append \"%s\" to "
283 "python module path.", dir);
286 Py_DECREF(dir_object);
288 } else if (strcasecmp(item->key, "Import") == 0) {
289 char *module_name = NULL;
292 if (cf_util_get_string(item, &module_name) != 0)
294 module = PyImport_ImportModule(module_name); /* New reference. */
295 if (module == NULL) {
296 ERROR("python plugin: Error importing module \"%s\".", module_name);
301 } else if (strcasecmp(item->key, "Module") == 0) {
306 if (cf_util_get_string(item, &name) != 0)
308 for (c = cpy_config_callbacks; c; c = c->next) {
309 if (strcasecmp(c->name, name) == 0)
313 WARNING("python plugin: Found a configuration for the \"%s\" plugin, "
314 "but the plugin isn't loaded or didn't register "
315 "a configuration callback.", name);
320 ret = PyObject_CallFunction(c->callback, "N",
321 cpy_oconfig_to_pyconfig(item, NULL)); /* New reference. */
327 WARNING("python plugin: Ignoring unknown config key \"%s\".", item->key);
334 void module_register(void) {
335 plugin_register_complex_config("python", cpy_config);
336 plugin_register_init("python", cpy_init);
337 // plugin_register_read("python", cna_read);
338 plugin_register_shutdown("python", cpy_shutdown);