Merge remote branch 'trenkel/st/python'
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Thu, 22 Apr 2010 08:45:58 +0000 (10:45 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Thu, 22 Apr 2010 08:45:58 +0000 (10:45 +0200)
1  2 
src/collectd-python.pod
src/python.c

diff --combined src/collectd-python.pod
@@@ -38,8 -38,8 +38,8 @@@ At least python I<version 2.3> is requi
  Loads the Python plugin I<Plugin>. Unlike most other LoadPlugin lines, this one
  should be a block containing the line "Globals true". This will cause collectd
  to export the name of all objects in the python interpreter for all plugins to
 -see. If you don't do this or your platform does not support it, the embeded
 -interpreter will start anywa but you won't be able to load certain python
 +see. If you don't do this or your platform does not support it, the embedded
 +interpreter will start anyway but you won't be able to load certain python
  modules, e.g. "time".
  
  =item B<Encoding> I<Name>
@@@ -105,6 -105,15 +105,15 @@@ environment variable, e.g. I<export PAG
  Depending on your version of python this might or might not result in an
  B<OSError> exception which can be ignored.
  
+ If you really need to spawn new processes from python you can register an init
+ callback and reset the action for SIGCHLD to the default behavior. Please note
+ that this I<will> break the exec plugin. Do not even load the exec plugin if
+ you intend to do this!
+ There is an example script located in B<contrib/python/getsigchld.py>  to do
+ this. If you import this from I<collectd.conf> SIGCHLD will be handled
+ normally and spawning processes from python will work as intended.
  =back
  
  =item E<lt>B<Module> I<Name>E<gt> block
@@@ -231,9 -240,31 +240,31 @@@ collectd you're done
  The following complex types are used to pass values between the Python plugin
  and collectd:
  
+ =head2 Signed
+ The Signed class is just a long. It has all its methods and behaves exactly
+ like any other long object. It is used to indicate if an integer was or should
+ be stored as a signed or unsigned integer object.
+  class Signed(long)
+ This is a long by another name. Use it in meta data dicts
+ to choose the way it is stored in the meta data.
+ =head2 Unsigned
+ The Unsigned class is just a long. It has all its methods and behaves exactly
+ like any other long object. It is used to indicate if an integer was or should
+ be stored as a signed or unsigned integer object.
+  class Unsigned(long)
+ This is a long by another name. Use it in meta data dicts
+ to choose the way it is stored in the meta data.
  =head2 Config
  
 -The Config class is an object which keeps the informations provided in the
 +The Config class is an object which keeps the information provided in the
  configuration file. The sequence of children keeps one entry for each
  configuration option. Each such entry is another Config instance, which
  may nest further if nested blocks are used.
@@@ -394,6 -425,15 +425,15 @@@ If the sequence does not have the corre
  exception will be raised. If the content of the sequence is not a number, a
  I<TypeError> exception will be raised.
  
+ =item meta
+ These are the meta data for this Value object.
+ It has to be a dictionary of numbers, strings or bools. All keys must be
+ strings. I<int> and <long> objects will be dispatched as signed integers unless
+ they are between 2**63 and 2**64-1, which will result in a unsigned integer.
+ You can force one of these storage classes by using the classes
+ B<collectd.Signed> and B<collectd.Unsigned>. A meta object received by a write
+ callback will always contain B<Signed> or B<Unsigned> objects.
  =back
  
  =head2 Notification
@@@ -653,7 -693,7 +693,7 @@@ dispatched by the python plugin after u
  =item
  
  This plugin is not compatible with python3. Trying to compile it with python3
 -will fail because of the ways string, unicode and bytearray bahavior was
 +will fail because of the ways string, unicode and bytearray behavior was
  changed.
  
  =item
diff --combined src/python.c
@@@ -259,7 -259,7 +259,7 @@@ static void cpy_build_name(char *buf, s
        PyErr_Clear();
  }
  
static void cpy_log_exception(const char *context) {
+ void cpy_log_exception(const char *context) {
        int l = 0, i;
        const char *typename = NULL, *message = NULL;
        PyObject *type, *value, *traceback, *tn, *m, *list;
@@@ -335,7 -335,7 +335,7 @@@ static int cpy_read_callback(user_data_
  static int cpy_write_callback(const data_set_t *ds, const value_list_t *value_list, user_data_t *data) {
        int i;
        cpy_callback_t *c = data->data;
-       PyObject *ret, *list;
+       PyObject *ret, *list, *temp, *dict = NULL;
        Values *v;
  
        CPY_LOCK_THREADS
                                CPY_RETURN_FROM_THREADS 0;
                        }
                }
+               dict = PyDict_New();
+               if (value_list->meta) {
+                       int i, num;
+                       char **table;
+                       meta_data_t *meta = value_list->meta;
+                       num = meta_data_toc(meta, &table);
+                       for (i = 0; i < num; ++i) {
+                               int type;
+                               char *string;
+                               int64_t si;
+                               uint64_t ui;
+                               double d;
+                               _Bool b;
+                               
+                               type = meta_data_type(meta, table[i]);
+                               if (type == MD_TYPE_STRING) {
+                                       if (meta_data_get_string(meta, table[i], &string))
+                                               continue;
+                                       temp = cpy_string_to_unicode_or_bytes(string);
+                                       free(string);
+                                       PyDict_SetItemString(dict, table[i], temp);
+                                       Py_XDECREF(temp);
+                               } else if (type == MD_TYPE_SIGNED_INT) {
+                                       if (meta_data_get_signed_int(meta, table[i], &si))
+                                               continue;
+                                       temp = PyObject_CallFunctionObjArgs((void *) &SignedType, PyLong_FromLongLong(si), (void *) 0);
+                                       PyDict_SetItemString(dict, table[i], temp);
+                                       Py_XDECREF(temp);
+                               } else if (type == MD_TYPE_UNSIGNED_INT) {
+                                       if (meta_data_get_unsigned_int(meta, table[i], &ui))
+                                               continue;
+                                       temp = PyObject_CallFunctionObjArgs((void *) &UnsignedType, PyLong_FromUnsignedLongLong(ui), (void *) 0);
+                                       PyDict_SetItemString(dict, table[i], temp);
+                                       Py_XDECREF(temp);
+                               } else if (type == MD_TYPE_DOUBLE) {
+                                       if (meta_data_get_double(meta, table[i], &d))
+                                               continue;
+                                       temp = PyFloat_FromDouble(d);
+                                       PyDict_SetItemString(dict, table[i], temp);
+                                       Py_XDECREF(temp);
+                               } else if (type == MD_TYPE_BOOLEAN) {
+                                       if (meta_data_get_boolean(meta, table[i], &b))
+                                               continue;
+                                       if (b)
+                                               temp = Py_True;
+                                       else
+                                               temp = Py_False;
+                                       PyDict_SetItemString(dict, table[i], temp);
+                               }
+                               free(table[i]);
+                       }
+                       free(table);
+               }
                v = PyObject_New(Values, (void *) &ValuesType);
                sstrncpy(v->data.host, value_list->host, sizeof(v->data.host));
                sstrncpy(v->data.type, value_list->type, sizeof(v->data.type));
                v->data.time = value_list->time;
                v->interval = value_list->interval;
                v->values = list;
+               v->meta = dict;
                ret = PyObject_CallFunctionObjArgs(c->callback, v, c->data, (void *) 0); /* New reference. */
                if (ret == NULL) {
                        cpy_log_exception("write callback");
@@@ -568,8 -623,7 +623,8 @@@ static PyObject *cpy_register_read(PyOb
        user_data->data = c;
        ts.tv_sec = interval;
        ts.tv_nsec = (interval - ts.tv_sec) * 1000000000;
 -      plugin_register_complex_read(buf, cpy_read_callback, &ts, user_data);
 +      plugin_register_complex_read(/* group = */ NULL, buf,
 +                      cpy_read_callback, &ts, user_data);
        return cpy_string_to_unicode_or_bytes(buf);
  }
  
@@@ -936,6 -990,10 +991,10 @@@ static int cpy_config(oconfig_item_t *c
        PyType_Ready(&ValuesType);
        NotificationType.tp_base = &PluginDataType;
        PyType_Ready(&NotificationType);
+       SignedType.tp_base = &PyLong_Type;
+       PyType_Ready(&SignedType);
+       UnsignedType.tp_base = &PyLong_Type;
+       PyType_Ready(&UnsignedType);
        sys = PyImport_ImportModule("sys"); /* New reference. */
        if (sys == NULL) {
                cpy_log_exception("python initialization");
        PyModule_AddObject(module, "Config", (void *) &ConfigType); /* Steals a reference. */
        PyModule_AddObject(module, "Values", (void *) &ValuesType); /* Steals a reference. */
        PyModule_AddObject(module, "Notification", (void *) &NotificationType); /* Steals a reference. */
+       PyModule_AddObject(module, "Signed", (void *) &SignedType); /* Steals a reference. */
+       PyModule_AddObject(module, "Unsigned", (void *) &UnsignedType); /* Steals a reference. */
        PyModule_AddIntConstant(module, "LOG_DEBUG", LOG_DEBUG);
        PyModule_AddIntConstant(module, "LOG_INFO", LOG_INFO);
        PyModule_AddIntConstant(module, "LOG_NOTICE", LOG_NOTICE);