2 * collectd - src/pyvalues.c
3 * Copyright (C) 2009 Sven Trenkel
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Sven Trenkel <collectd at semidefinite.de>
28 #include <structmember.h>
35 #define FreeAll() do {\
37 PyMem_Free(plugin_instance);\
38 PyMem_Free(type_instance);\
43 static PyObject *cpy_common_repr(PyObject *s) {
45 static PyObject *l_type = NULL, *l_type_instance = NULL, *l_plugin = NULL, *l_plugin_instance = NULL;
46 static PyObject *l_host = NULL, *l_time = NULL;
47 PluginData *self = (PluginData *) s;
50 l_type = cpy_string_to_unicode_or_bytes("(type=");
51 if (l_type_instance == NULL)
52 l_type_instance = cpy_string_to_unicode_or_bytes(",type_instance=");
54 l_plugin = cpy_string_to_unicode_or_bytes(",plugin=");
55 if (l_plugin_instance == NULL)
56 l_plugin_instance = cpy_string_to_unicode_or_bytes(",plugin_instance=");
58 l_host = cpy_string_to_unicode_or_bytes(",host=");
60 l_time = cpy_string_to_unicode_or_bytes(",time=");
62 if (!l_type || !l_type_instance || !l_plugin || !l_plugin_instance || !l_host || !l_time)
65 ret = cpy_string_to_unicode_or_bytes(s->ob_type->tp_name);
67 CPY_STRCAT(&ret, l_type);
68 tmp = cpy_string_to_unicode_or_bytes(self->type);
69 CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
70 CPY_STRCAT_AND_DEL(&ret, tmp);
72 if (self->type_instance[0] != 0) {
73 CPY_STRCAT(&ret, l_type_instance);
74 tmp = cpy_string_to_unicode_or_bytes(self->type_instance);
75 CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
76 CPY_STRCAT_AND_DEL(&ret, tmp);
79 if (self->plugin[0] != 0) {
80 CPY_STRCAT(&ret, l_plugin);
81 tmp = cpy_string_to_unicode_or_bytes(self->plugin);
82 CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
83 CPY_STRCAT_AND_DEL(&ret, tmp);
86 if (self->plugin_instance[0] != 0) {
87 CPY_STRCAT(&ret, l_plugin_instance);
88 tmp = cpy_string_to_unicode_or_bytes(self->plugin_instance);
89 CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
90 CPY_STRCAT_AND_DEL(&ret, tmp);
93 if (self->host[0] != 0) {
94 CPY_STRCAT(&ret, l_host);
95 tmp = cpy_string_to_unicode_or_bytes(self->host);
96 CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
97 CPY_STRCAT_AND_DEL(&ret, tmp);
100 if (self->time != 0) {
101 CPY_STRCAT(&ret, l_time);
102 tmp = PyFloat_FromDouble(self->time);
103 CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
104 CPY_STRCAT_AND_DEL(&ret, tmp);
109 static char time_doc[] = "This is the Unix timestap of the time this value was read.\n"
110 "For dispatching values this can be set to 0 which means \"now\".\n"
111 "This means the time the value is actually dispatched, not the time\n"
114 static char host_doc[] = "The hostname of the host this value was read from.\n"
115 "For dispatching this can be set to an empty string which means\n"
116 "the local hostname as defined in the collectd.conf.";
118 static char type_doc[] = "The type of this value. This type has to be defined\n"
119 "in your types.db. Attempting to set it to any other value will\n"
120 "raise a TypeError exception.\n"
121 "Assigning a type is mandetory, calling dispatch without doing\n"
122 "so will raise a RuntimeError exception.";
124 static char type_instance_doc[] = "";
126 static char plugin_doc[] = "The name of the plugin that read the data. Setting this\n"
127 "member to an empty string will insert \"python\" upon dispatching.";
129 static char plugin_instance_doc[] = "";
131 static char PluginData_doc[] = "This is an internal class that is the base for Values\n"
132 "and Notification. It is pretty useless by itself and was therefore not\n"
133 "exported to the collectd module.";
135 static PyObject *PluginData_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
138 self = (PluginData *) type->tp_alloc(type, 0);
145 self->plugin_instance[0] = 0;
147 self->type_instance[0] = 0;
148 return (PyObject *) self;
151 static int PluginData_init(PyObject *s, PyObject *args, PyObject *kwds) {
152 PluginData *self = (PluginData *) s;
154 char *type = NULL, *plugin_instance = NULL, *type_instance = NULL, *plugin = NULL, *host = NULL;
155 static char *kwlist[] = {"type", "plugin_instance", "type_instance",
156 "plugin", "host", "time", NULL};
158 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etetetetetd", kwlist, NULL, &type,
159 NULL, &plugin_instance, NULL, &type_instance, NULL, &plugin, NULL, &host, &time))
162 if (type && plugin_get_ds(type) == NULL) {
163 PyErr_Format(PyExc_TypeError, "Dataset %s not found", type);
168 sstrncpy(self->host, host ? host : "", sizeof(self->host));
169 sstrncpy(self->plugin, plugin ? plugin : "", sizeof(self->plugin));
170 sstrncpy(self->plugin_instance, plugin_instance ? plugin_instance : "", sizeof(self->plugin_instance));
171 sstrncpy(self->type, type ? type : "", sizeof(self->type));
172 sstrncpy(self->type_instance, type_instance ? type_instance : "", sizeof(self->type_instance));
180 static PyObject *PluginData_repr(PyObject *s) {
182 static PyObject *l_closing = NULL;
184 if (l_closing == NULL)
185 l_closing = cpy_string_to_unicode_or_bytes(")");
187 if (l_closing == NULL)
190 ret = cpy_common_repr(s);
191 CPY_STRCAT(&ret, l_closing);
195 static PyMemberDef PluginData_members[] = {
196 {"time", T_DOUBLE, offsetof(PluginData, time), 0, time_doc},
200 static PyObject *PluginData_getstring(PyObject *self, void *data) {
201 const char *value = ((char *) self) + (intptr_t) data;
203 return cpy_string_to_unicode_or_bytes(value);
206 static int PluginData_setstring(PyObject *self, PyObject *value, void *data) {
211 PyErr_SetString(PyExc_TypeError, "Cannot delete this attribute");
215 new = cpy_unicode_or_bytes_to_string(&value);
220 old = ((char *) self) + (intptr_t) data;
221 sstrncpy(old, new, DATA_MAX_NAME_LEN);
226 static int PluginData_settype(PyObject *self, PyObject *value, void *data) {
231 PyErr_SetString(PyExc_TypeError, "Cannot delete this attribute");
235 new = cpy_unicode_or_bytes_to_string(&value);
241 if (plugin_get_ds(new) == NULL) {
242 PyErr_Format(PyExc_TypeError, "Dataset %s not found", new);
247 old = ((char *) self) + (intptr_t) data;
248 sstrncpy(old, new, DATA_MAX_NAME_LEN);
253 static PyGetSetDef PluginData_getseters[] = {
254 {"host", PluginData_getstring, PluginData_setstring, host_doc, (void *) offsetof(PluginData, host)},
255 {"plugin", PluginData_getstring, PluginData_setstring, plugin_doc, (void *) offsetof(PluginData, plugin)},
256 {"plugin_instance", PluginData_getstring, PluginData_setstring, plugin_instance_doc, (void *) offsetof(PluginData, plugin_instance)},
257 {"type_instance", PluginData_getstring, PluginData_setstring, type_instance_doc, (void *) offsetof(PluginData, type_instance)},
258 {"type", PluginData_getstring, PluginData_settype, type_doc, (void *) offsetof(PluginData, type)},
262 PyTypeObject PluginDataType = {
264 "collectd.PluginData", /* tp_name */
265 sizeof(PluginData), /* tp_basicsize */
266 0, /* Will be filled in later */
272 PluginData_repr, /* tp_repr */
273 0, /* tp_as_number */
274 0, /* tp_as_sequence */
275 0, /* tp_as_mapping */
281 0, /* tp_as_buffer */
282 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE /*| Py_TPFLAGS_HAVE_GC*/, /*tp_flags*/
283 PluginData_doc, /* tp_doc */
286 0, /* tp_richcompare */
287 0, /* tp_weaklistoffset */
291 PluginData_members, /* tp_members */
292 PluginData_getseters, /* tp_getset */
295 0, /* tp_descr_get */
296 0, /* tp_descr_set */
297 0, /* tp_dictoffset */
298 PluginData_init, /* tp_init */
300 PluginData_new /* tp_new */
303 static char interval_doc[] = "The interval is the timespan in seconds between two submits for\n"
304 "the same data source. This value has to be a positive integer, so you can't\n"
305 "submit more than one value per second. If this member is set to a\n"
306 "non-positive value, the default value as specified in the config file will\n"
307 "be used (default: 10).\n"
309 "If you submit values more often than the specified interval, the average\n"
310 "will be used. If you submit less values, your graphs will have gaps.";
312 static char values_doc[] = "These are the actual values that get dispatched to collectd.\n"
313 "It has to be a sequence (a tuple or list) of numbers.\n"
314 "The size of the sequence and the type of its content depend on the type\n"
315 "member your types.db file. For more information on this read the types.db\n"
318 "If the sequence does not have the correct size upon dispatch a RuntimeError\n"
319 "exception will be raised. If the content of the sequence is not a number,\n"
320 "a TypeError exception will be raised.";
322 static char meta_doc[] = "These are the meta data for this Value object.\n"
323 "It has to be a dictionary of numbers, strings or bools. All keys must be\n"
324 "strings. int and long objects will be dispatched as signed integers unless\n"
325 "they are between 2**63 and 2**64-1, which will result in a unsigned integer.\n"
326 "You can force one of these storage classes by using the classes\n"
327 "collectd.Signed and collectd.Unsigned. A meta object received by a write\n"
328 "callback will always contain Signed or Unsigned objects.";
330 static char dispatch_doc[] = "dispatch([type][, values][, plugin_instance][, type_instance]"
331 "[, plugin][, host][, time][, interval]) -> None. Dispatch a value list.\n"
333 "Dispatch this instance to the collectd process. The object has members\n"
334 "for each of the possible arguments for this method. For a detailed explanation\n"
335 "of these parameters see the member of the same same.\n"
337 "If you do not submit a parameter the value saved in its member will be submitted.\n"
338 "If you do provide a parameter it will be used instead, without altering the member.";
340 static char write_doc[] = "write([destination][, type][, values][, plugin_instance][, type_instance]"
341 "[, plugin][, host][, time][, interval]) -> None. Dispatch a value list.\n"
343 "Write this instance to a single plugin or all plugins if 'destination' is obmitted.\n"
344 "This will bypass the main collectd process and all filtering and caching.\n"
345 "Other than that it works similar to 'dispatch'. In most cases 'dispatch' should be\n"
346 "used instead of 'write'.\n";
348 static char Values_doc[] = "A Values object used for dispatching values to collectd and receiving values from write callbacks.";
350 static PyObject *Values_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
353 self = (Values *) PluginData_new(type, args, kwds);
357 self->values = PyList_New(0);
358 self->meta = PyDict_New();
360 return (PyObject *) self;
363 static int Values_init(PyObject *s, PyObject *args, PyObject *kwds) {
364 Values *self = (Values *) s;
365 double interval = 0, time = 0;
366 PyObject *values = NULL, *meta = NULL, *tmp;
367 char *type = NULL, *plugin_instance = NULL, *type_instance = NULL, *plugin = NULL, *host = NULL;
368 static char *kwlist[] = {"type", "values", "plugin_instance", "type_instance",
369 "plugin", "host", "time", "interval", "meta", NULL};
371 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etOetetetetddO", kwlist,
372 NULL, &type, &values, NULL, &plugin_instance, NULL, &type_instance,
373 NULL, &plugin, NULL, &host, &time, &interval, &meta))
376 if (type && plugin_get_ds(type) == NULL) {
377 PyErr_Format(PyExc_TypeError, "Dataset %s not found", type);
382 sstrncpy(self->data.host, host ? host : "", sizeof(self->data.host));
383 sstrncpy(self->data.plugin, plugin ? plugin : "", sizeof(self->data.plugin));
384 sstrncpy(self->data.plugin_instance, plugin_instance ? plugin_instance : "", sizeof(self->data.plugin_instance));
385 sstrncpy(self->data.type, type ? type : "", sizeof(self->data.type));
386 sstrncpy(self->data.type_instance, type_instance ? type_instance : "", sizeof(self->data.type_instance));
387 self->data.time = time;
391 if (values == NULL) {
392 values = PyList_New(0);
406 self->values = values;
413 self->interval = interval;
417 static meta_data_t *cpy_build_meta(PyObject *meta) {
419 meta_data_t *m = NULL;
422 if ((meta == NULL) || (meta == Py_None))
425 l = PyDict_Items(meta); /* New reference. */
427 cpy_log_exception("building meta data");
436 m = meta_data_create();
437 for (i = 0; i < s; ++i) {
438 const char *string, *keystring;
439 PyObject *key, *value, *item, *tmp;
441 item = PyList_GET_ITEM(l, i);
442 key = PyTuple_GET_ITEM(item, 0);
444 keystring = cpy_unicode_or_bytes_to_string(&key);
450 value = PyTuple_GET_ITEM(item, 1);
452 if (value == Py_True) {
453 meta_data_add_boolean(m, keystring, 1);
454 } else if (value == Py_False) {
455 meta_data_add_boolean(m, keystring, 0);
456 } else if (PyFloat_Check(value)) {
457 meta_data_add_double(m, keystring, PyFloat_AsDouble(value));
458 } else if (PyObject_TypeCheck(value, &SignedType)) {
460 lli = PyLong_AsLongLong(value);
461 if (!PyErr_Occurred() && (lli == (int64_t) lli))
462 meta_data_add_signed_int(m, keystring, lli);
463 } else if (PyObject_TypeCheck(value, &UnsignedType)) {
464 long long unsigned llu;
465 llu = PyLong_AsUnsignedLongLong(value);
466 if (!PyErr_Occurred() && (llu == (uint64_t) llu))
467 meta_data_add_unsigned_int(m, keystring, llu);
468 } else if (PyNumber_Check(value)) {
470 long long unsigned llu;
471 tmp = PyNumber_Long(value);
472 lli = PyLong_AsLongLong(tmp);
473 if (!PyErr_Occurred() && (lli == (int64_t) lli)) {
474 meta_data_add_signed_int(m, keystring, lli);
477 llu = PyLong_AsUnsignedLongLong(tmp);
478 if (!PyErr_Occurred() && (llu == (uint64_t) llu))
479 meta_data_add_unsigned_int(m, keystring, llu);
483 string = cpy_unicode_or_bytes_to_string(&value);
485 meta_data_add_string(m, keystring, string);
488 tmp = PyObject_Str(value);
489 string = cpy_unicode_or_bytes_to_string(&tmp);
491 meta_data_add_string(m, keystring, string);
495 if (PyErr_Occurred())
496 cpy_log_exception("building meta data");
504 static PyObject *Values_dispatch(Values *self, PyObject *args, PyObject *kwds) {
506 const data_set_t *ds;
509 value_list_t value_list = VALUE_LIST_INIT;
510 PyObject *values = self->values, *meta = self->meta;
511 double time = self->data.time, interval = self->interval;
512 char *host = NULL, *plugin = NULL, *plugin_instance = NULL, *type = NULL, *type_instance = NULL;
514 static char *kwlist[] = {"type", "values", "plugin_instance", "type_instance",
515 "plugin", "host", "time", "interval", "meta", NULL};
516 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etOetetetetddO", kwlist,
517 NULL, &type, &values, NULL, &plugin_instance, NULL, &type_instance,
518 NULL, &plugin, NULL, &host, &time, &interval, &meta))
521 sstrncpy(value_list.host, host ? host : self->data.host, sizeof(value_list.host));
522 sstrncpy(value_list.plugin, plugin ? plugin : self->data.plugin, sizeof(value_list.plugin));
523 sstrncpy(value_list.plugin_instance, plugin_instance ? plugin_instance : self->data.plugin_instance, sizeof(value_list.plugin_instance));
524 sstrncpy(value_list.type, type ? type : self->data.type, sizeof(value_list.type));
525 sstrncpy(value_list.type_instance, type_instance ? type_instance : self->data.type_instance, sizeof(value_list.type_instance));
527 if (value_list.type[0] == 0) {
528 PyErr_SetString(PyExc_RuntimeError, "type not set");
532 ds = plugin_get_ds(value_list.type);
534 PyErr_Format(PyExc_TypeError, "Dataset %s not found", value_list.type);
537 if (values == NULL || (PyTuple_Check(values) == 0 && PyList_Check(values) == 0)) {
538 PyErr_Format(PyExc_TypeError, "values must be list or tuple");
541 if (meta != NULL && meta != Py_None && !PyDict_Check(meta)) {
542 PyErr_Format(PyExc_TypeError, "meta must be a dict");
545 size = (int) PySequence_Length(values);
546 if (size != ds->ds_num) {
547 PyErr_Format(PyExc_RuntimeError, "type %s needs %d values, got %i", value_list.type, ds->ds_num, size);
550 value = malloc(size * sizeof(*value));
551 for (i = 0; i < size; ++i) {
552 PyObject *item, *num;
553 item = PySequence_Fast_GET_ITEM(values, i); /* Borrowed reference. */
554 if (ds->ds->type == DS_TYPE_COUNTER) {
555 num = PyNumber_Long(item); /* New reference. */
557 value[i].counter = PyLong_AsUnsignedLongLong(num);
560 } else if (ds->ds->type == DS_TYPE_GAUGE) {
561 num = PyNumber_Float(item); /* New reference. */
563 value[i].gauge = PyFloat_AsDouble(num);
566 } else if (ds->ds->type == DS_TYPE_DERIVE) {
567 /* This might overflow without raising an exception.
568 * Not much we can do about it */
569 num = PyNumber_Long(item); /* New reference. */
571 value[i].derive = PyLong_AsLongLong(num);
574 } else if (ds->ds->type == DS_TYPE_ABSOLUTE) {
575 /* This might overflow without raising an exception.
576 * Not much we can do about it */
577 num = PyNumber_Long(item); /* New reference. */
579 value[i].absolute = PyLong_AsUnsignedLongLong(num);
584 PyErr_Format(PyExc_RuntimeError, "unknown data type %d for %s", ds->ds->type, value_list.type);
587 if (PyErr_Occurred() != NULL) {
592 value_list.values = value;
593 value_list.meta = cpy_build_meta(meta);
594 value_list.values_len = size;
595 value_list.time = DOUBLE_TO_CDTIME_T(time);
596 value_list.interval = DOUBLE_TO_CDTIME_T(interval);
597 if (value_list.host[0] == 0)
598 sstrncpy(value_list.host, hostname_g, sizeof(value_list.host));
599 if (value_list.plugin[0] == 0)
600 sstrncpy(value_list.plugin, "python", sizeof(value_list.plugin));
601 Py_BEGIN_ALLOW_THREADS;
602 ret = plugin_dispatch_values(&value_list);
603 Py_END_ALLOW_THREADS;
604 meta_data_destroy(value_list.meta);
607 PyErr_SetString(PyExc_RuntimeError, "error dispatching values, read the logs");
613 static PyObject *Values_write(Values *self, PyObject *args, PyObject *kwds) {
615 const data_set_t *ds;
618 value_list_t value_list = VALUE_LIST_INIT;
619 PyObject *values = self->values, *meta = self->meta;
620 double time = self->data.time, interval = self->interval;
621 char *host = NULL, *plugin = NULL, *plugin_instance = NULL, *type = NULL, *type_instance = NULL, *dest = NULL;
623 static char *kwlist[] = {"destination", "type", "values", "plugin_instance", "type_instance",
624 "plugin", "host", "time", "interval", "meta", NULL};
625 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|etOetetetetdiO", kwlist, NULL, &dest,
626 NULL, &type, &values, NULL, &plugin_instance, NULL, &type_instance,
627 NULL, &plugin, NULL, &host, &time, &interval, &meta))
630 sstrncpy(value_list.host, host ? host : self->data.host, sizeof(value_list.host));
631 sstrncpy(value_list.plugin, plugin ? plugin : self->data.plugin, sizeof(value_list.plugin));
632 sstrncpy(value_list.plugin_instance, plugin_instance ? plugin_instance : self->data.plugin_instance, sizeof(value_list.plugin_instance));
633 sstrncpy(value_list.type, type ? type : self->data.type, sizeof(value_list.type));
634 sstrncpy(value_list.type_instance, type_instance ? type_instance : self->data.type_instance, sizeof(value_list.type_instance));
636 if (value_list.type[0] == 0) {
637 PyErr_SetString(PyExc_RuntimeError, "type not set");
640 ds = plugin_get_ds(value_list.type);
642 PyErr_Format(PyExc_TypeError, "Dataset %s not found", value_list.type);
645 if (values == NULL || (PyTuple_Check(values) == 0 && PyList_Check(values) == 0)) {
646 PyErr_Format(PyExc_TypeError, "values must be list or tuple");
649 size = (int) PySequence_Length(values);
650 if (size != ds->ds_num) {
651 PyErr_Format(PyExc_RuntimeError, "type %s needs %d values, got %i", value_list.type, ds->ds_num, size);
654 value = malloc(size * sizeof(*value));
655 for (i = 0; i < size; ++i) {
656 PyObject *item, *num;
657 item = PySequence_Fast_GET_ITEM(values, i); /* Borrowed reference. */
658 if (ds->ds->type == DS_TYPE_COUNTER) {
659 num = PyNumber_Long(item); /* New reference. */
661 value[i].counter = PyLong_AsUnsignedLongLong(num);
664 } else if (ds->ds->type == DS_TYPE_GAUGE) {
665 num = PyNumber_Float(item); /* New reference. */
667 value[i].gauge = PyFloat_AsDouble(num);
670 } else if (ds->ds->type == DS_TYPE_DERIVE) {
671 /* This might overflow without raising an exception.
672 * Not much we can do about it */
673 num = PyNumber_Long(item); /* New reference. */
675 value[i].derive = PyLong_AsLongLong(num);
678 } else if (ds->ds->type == DS_TYPE_ABSOLUTE) {
679 /* This might overflow without raising an exception.
680 * Not much we can do about it */
681 num = PyNumber_Long(item); /* New reference. */
683 value[i].absolute = PyLong_AsUnsignedLongLong(num);
688 PyErr_Format(PyExc_RuntimeError, "unknown data type %d for %s", ds->ds->type, value_list.type);
691 if (PyErr_Occurred() != NULL) {
696 value_list.values = value;
697 value_list.values_len = size;
698 value_list.time = DOUBLE_TO_CDTIME_T(time);
699 value_list.interval = DOUBLE_TO_CDTIME_T(interval);
700 value_list.meta = cpy_build_meta(meta);;
701 if (value_list.host[0] == 0)
702 sstrncpy(value_list.host, hostname_g, sizeof(value_list.host));
703 if (value_list.plugin[0] == 0)
704 sstrncpy(value_list.plugin, "python", sizeof(value_list.plugin));
705 Py_BEGIN_ALLOW_THREADS;
706 ret = plugin_write(dest, NULL, &value_list);
707 Py_END_ALLOW_THREADS;
708 meta_data_destroy(value_list.meta);
711 PyErr_SetString(PyExc_RuntimeError, "error dispatching values, read the logs");
717 static PyObject *Values_repr(PyObject *s) {
719 static PyObject *l_interval = NULL, *l_values = NULL, *l_meta = NULL, *l_closing = NULL;
720 Values *self = (Values *) s;
722 if (l_interval == NULL)
723 l_interval = cpy_string_to_unicode_or_bytes(",interval=");
724 if (l_values == NULL)
725 l_values = cpy_string_to_unicode_or_bytes(",values=");
727 l_meta = cpy_string_to_unicode_or_bytes(",meta=");
728 if (l_closing == NULL)
729 l_closing = cpy_string_to_unicode_or_bytes(")");
731 if (l_interval == NULL || l_values == NULL || l_meta == NULL || l_closing == NULL)
734 ret = cpy_common_repr(s);
735 if (self->interval != 0) {
736 CPY_STRCAT(&ret, l_interval);
737 tmp = PyFloat_FromDouble(self->interval);
738 CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
739 CPY_STRCAT_AND_DEL(&ret, tmp);
741 if (self->values && (!PyList_Check(self->values) || PySequence_Length(self->values) > 0)) {
742 CPY_STRCAT(&ret, l_values);
743 tmp = PyObject_Repr(self->values);
744 CPY_STRCAT_AND_DEL(&ret, tmp);
746 if (self->meta && (!PyDict_Check(self->meta) || PyDict_Size(self->meta) > 0)) {
747 CPY_STRCAT(&ret, l_meta);
748 tmp = PyObject_Repr(self->meta);
749 CPY_STRCAT_AND_DEL(&ret, tmp);
751 CPY_STRCAT(&ret, l_closing);
755 static int Values_traverse(PyObject *self, visitproc visit, void *arg) {
756 Values *v = (Values *) self;
762 static int Values_clear(PyObject *self) {
763 Values *v = (Values *) self;
769 static void Values_dealloc(PyObject *self) {
771 self->ob_type->tp_free(self);
774 static PyMemberDef Values_members[] = {
775 {"interval", T_DOUBLE, offsetof(Values, interval), 0, interval_doc},
776 {"values", T_OBJECT_EX, offsetof(Values, values), 0, values_doc},
777 {"meta", T_OBJECT_EX, offsetof(Values, meta), 0, meta_doc},
781 static PyMethodDef Values_methods[] = {
782 {"dispatch", (PyCFunction) Values_dispatch, METH_VARARGS | METH_KEYWORDS, dispatch_doc},
783 {"write", (PyCFunction) Values_write, METH_VARARGS | METH_KEYWORDS, write_doc},
787 PyTypeObject ValuesType = {
789 "collectd.Values", /* tp_name */
790 sizeof(Values), /* tp_basicsize */
791 0, /* Will be filled in later */
792 Values_dealloc, /* tp_dealloc */
797 Values_repr, /* tp_repr */
798 0, /* tp_as_number */
799 0, /* tp_as_sequence */
800 0, /* tp_as_mapping */
806 0, /* tp_as_buffer */
807 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
808 Values_doc, /* tp_doc */
809 Values_traverse, /* tp_traverse */
810 Values_clear, /* tp_clear */
811 0, /* tp_richcompare */
812 0, /* tp_weaklistoffset */
815 Values_methods, /* tp_methods */
816 Values_members, /* tp_members */
820 0, /* tp_descr_get */
821 0, /* tp_descr_set */
822 0, /* tp_dictoffset */
823 Values_init, /* tp_init */
825 Values_new /* tp_new */
828 static char severity_doc[] = "The severity of this notification. Assign or compare to\n"
829 "NOTIF_FAILURE, NOTIF_WARNING or NOTIF_OKAY.";
831 static char message_doc[] = "Some kind of description what's going on and why this Notification was generated.";
833 static char Notification_doc[] = "The Notification class is a wrapper around the collectd notification.\n"
834 "It can be used to notify other plugins about bad stuff happening. It works\n"
835 "similar to Values but has a severity and a message instead of interval\n"
837 "Notifications can be dispatched at any time and can be received with register_notification.";
839 static int Notification_init(PyObject *s, PyObject *args, PyObject *kwds) {
840 Notification *self = (Notification *) s;
843 char *message = NULL;
844 char *type = NULL, *plugin_instance = NULL, *type_instance = NULL, *plugin = NULL, *host = NULL;
845 static char *kwlist[] = {"type", "message", "plugin_instance", "type_instance",
846 "plugin", "host", "time", "severity", NULL};
848 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etetetetetetdi", kwlist,
849 NULL, &type, NULL, &message, NULL, &plugin_instance, NULL, &type_instance,
850 NULL, &plugin, NULL, &host, &time, &severity))
853 if (type && plugin_get_ds(type) == NULL) {
854 PyErr_Format(PyExc_TypeError, "Dataset %s not found", type);
860 sstrncpy(self->data.host, host ? host : "", sizeof(self->data.host));
861 sstrncpy(self->data.plugin, plugin ? plugin : "", sizeof(self->data.plugin));
862 sstrncpy(self->data.plugin_instance, plugin_instance ? plugin_instance : "", sizeof(self->data.plugin_instance));
863 sstrncpy(self->data.type, type ? type : "", sizeof(self->data.type));
864 sstrncpy(self->data.type_instance, type_instance ? type_instance : "", sizeof(self->data.type_instance));
865 sstrncpy(self->message, message ? message : "", sizeof(self->message));
866 self->data.time = time;
867 self->severity = severity;
874 static PyObject *Notification_dispatch(Notification *self, PyObject *args, PyObject *kwds) {
876 const data_set_t *ds;
877 notification_t notification;
878 double t = self->data.time;
879 int severity = self->severity;
880 char *host = NULL, *plugin = NULL, *plugin_instance = NULL, *type = NULL, *type_instance = NULL;
881 char *message = NULL;
883 static char *kwlist[] = {"type", "message", "plugin_instance", "type_instance",
884 "plugin", "host", "time", "severity", NULL};
885 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|etetetetetetdi", kwlist,
886 NULL, &type, NULL, &message, NULL, &plugin_instance, NULL, &type_instance,
887 NULL, &plugin, NULL, &host, &t, &severity))
890 notification.time = DOUBLE_TO_CDTIME_T(t);
891 notification.severity = severity;
892 sstrncpy(notification.message, message ? message : self->message, sizeof(notification.message));
893 sstrncpy(notification.host, host ? host : self->data.host, sizeof(notification.host));
894 sstrncpy(notification.plugin, plugin ? plugin : self->data.plugin, sizeof(notification.plugin));
895 sstrncpy(notification.plugin_instance, plugin_instance ? plugin_instance : self->data.plugin_instance, sizeof(notification.plugin_instance));
896 sstrncpy(notification.type, type ? type : self->data.type, sizeof(notification.type));
897 sstrncpy(notification.type_instance, type_instance ? type_instance : self->data.type_instance, sizeof(notification.type_instance));
898 notification.meta = NULL;
902 if (notification.type[0] == 0) {
903 PyErr_SetString(PyExc_RuntimeError, "type not set");
906 ds = plugin_get_ds(notification.type);
908 PyErr_Format(PyExc_TypeError, "Dataset %s not found", notification.type);
912 if (notification.time == 0)
913 notification.time = cdtime();
914 if (notification.host[0] == 0)
915 sstrncpy(notification.host, hostname_g, sizeof(notification.host));
916 if (notification.plugin[0] == 0)
917 sstrncpy(notification.plugin, "python", sizeof(notification.plugin));
918 Py_BEGIN_ALLOW_THREADS;
919 ret = plugin_dispatch_notification(¬ification);
920 Py_END_ALLOW_THREADS;
922 PyErr_SetString(PyExc_RuntimeError, "error dispatching notification, read the logs");
928 static PyObject *Notification_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
931 self = (Notification *) PluginData_new(type, args, kwds);
935 self->message[0] = 0;
937 return (PyObject *) self;
940 static int Notification_setstring(PyObject *self, PyObject *value, void *data) {
945 PyErr_SetString(PyExc_TypeError, "Cannot delete this attribute");
949 new = cpy_unicode_or_bytes_to_string(&value);
954 old = ((char *) self) + (intptr_t) data;
955 sstrncpy(old, new, NOTIF_MAX_MSG_LEN);
960 static PyObject *Notification_repr(PyObject *s) {
962 static PyObject *l_severity = NULL, *l_message = NULL, *l_closing = NULL;
963 Notification *self = (Notification *) s;
965 if (l_severity == NULL)
966 l_severity = cpy_string_to_unicode_or_bytes(",severity=");
967 if (l_message == NULL)
968 l_message = cpy_string_to_unicode_or_bytes(",message=");
969 if (l_closing == NULL)
970 l_closing = cpy_string_to_unicode_or_bytes(")");
972 if (l_severity == NULL || l_message == NULL || l_closing == NULL)
975 ret = cpy_common_repr(s);
976 if (self->severity != 0) {
977 CPY_STRCAT(&ret, l_severity);
978 tmp = PyInt_FromLong(self->severity);
979 CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
980 CPY_STRCAT_AND_DEL(&ret, tmp);
982 if (self->message[0] != 0) {
983 CPY_STRCAT(&ret, l_message);
984 tmp = cpy_string_to_unicode_or_bytes(self->message);
985 CPY_SUBSTITUTE(PyObject_Repr, tmp, tmp);
986 CPY_STRCAT_AND_DEL(&ret, tmp);
988 CPY_STRCAT(&ret, l_closing);
992 static PyMethodDef Notification_methods[] = {
993 {"dispatch", (PyCFunction) Notification_dispatch, METH_VARARGS | METH_KEYWORDS, dispatch_doc},
997 static PyMemberDef Notification_members[] = {
998 {"severity", T_INT, offsetof(Notification, severity), 0, severity_doc},
1002 static PyGetSetDef Notification_getseters[] = {
1003 {"message", PluginData_getstring, Notification_setstring, message_doc, (void *) offsetof(Notification, message)},
1007 PyTypeObject NotificationType = {
1009 "collectd.Notification", /* tp_name */
1010 sizeof(Notification), /* tp_basicsize */
1011 0, /* Will be filled in later */
1017 Notification_repr, /* tp_repr */
1018 0, /* tp_as_number */
1019 0, /* tp_as_sequence */
1020 0, /* tp_as_mapping */
1024 0, /* tp_getattro */
1025 0, /* tp_setattro */
1026 0, /* tp_as_buffer */
1027 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1028 Notification_doc, /* tp_doc */
1029 0, /* tp_traverse */
1031 0, /* tp_richcompare */
1032 0, /* tp_weaklistoffset */
1034 0, /* tp_iternext */
1035 Notification_methods, /* tp_methods */
1036 Notification_members, /* tp_members */
1037 Notification_getseters, /* tp_getset */
1040 0, /* tp_descr_get */
1041 0, /* tp_descr_set */
1042 0, /* tp_dictoffset */
1043 Notification_init, /* tp_init */
1045 Notification_new /* tp_new */
1048 static char Signed_doc[] = "This is a long by another name. Use it in meta data dicts\n"
1049 "to choose the way it is stored in the meta data.";
1051 PyTypeObject SignedType = {
1053 "collectd.Signed", /* tp_name */
1054 sizeof(Signed), /* tp_basicsize */
1055 0, /* Will be filled in later */
1062 0, /* tp_as_number */
1063 0, /* tp_as_sequence */
1064 0, /* tp_as_mapping */
1068 0, /* tp_getattro */
1069 0, /* tp_setattro */
1070 0, /* tp_as_buffer */
1071 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1072 Signed_doc /* tp_doc */
1075 static char Unsigned_doc[] = "This is a long by another name. Use it in meta data dicts\n"
1076 "to choose the way it is stored in the meta data.";
1078 PyTypeObject UnsignedType = {
1080 "collectd.Unsigned", /* tp_name */
1081 sizeof(Unsigned), /* tp_basicsize */
1082 0, /* Will be filled in later */
1089 0, /* tp_as_number */
1090 0, /* tp_as_sequence */
1091 0, /* tp_as_mapping */
1095 0, /* tp_getattro */
1096 0, /* tp_setattro */
1097 0, /* tp_as_buffer */
1098 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
1099 Unsigned_doc /* tp_doc */