[collectd] problem with importing collectd python module

Pawel Akonom pawel.akonom at vnomic.com
Mon Feb 8 16:38:48 CET 2016


Thank you Nils for solving the collectd python plugin configuration problem. After I changed path to the python script with the path to the directory, where python script is placed, I was able to run it and I am sending mongodb metrics. The problem is solved.

Thank you Nathan for explanation how does collectd module is imported in python. I know is possible to write python module as a c code but I didn't suppose it can be included in the collectd python plugin itself. It is very sophisticated solution. The advantage of keeping pythons modules as a .so shared library is speed - it will run much faster than python script. As mongodb python script is finally working I don't need to test is from python prompt but maybe it would be possible to load it somehow using python ctypes https://docs.python.org/2/library/ctypes.html

Thanks for great support,
Pawel Akonom

From: Nathan WArd <collectd at daork.net>
Sent: Monday, February 8, 2016 5:27 AM
To: Pawel Akonom
Cc: Joshua J. Kugler; collectd at verplant.org; Derek Palma
Subject: Re: [collectd] problem with importing collectd python module

> On 7/02/2016, at 02:54, Pawel Akonom <pawel.akonom at vnomic.com> wrote:
> Hi,
> The problem is with collectd python module - python script can't load it. It can't find it in default python modules path. Is collectd python module included in collectd source code? If so please write with file it is because I couldn't find it. If it's not included in collectd source code from where can I get it? In collectd python plugin documentation there is example with "import collectd" which is loading collectd python module.


The collectd python stuff is a bit confusing, the module that you import is defined in collectd’s python embedding code - see https://github.com/collectd/collectd/blob/42a7c90f4478e98dc970927cfda7ec2e1081f364/src/python.c line 1049 - rather than in a python collectd module that gets loaded in the normal python ways.

This means you can’t run it outside of collectd without modification.

Perhaps it would be possible to move the code in to a python module that can be loaded in the normal python ways, so you can test a module on the command line - I’m not sure.

For testing, you can run your module directly by mocking up the collectd bits, here’s some very stripped down code to give you an idea. Essentially, if the script runs directly then mock up the collectd module, otherwise run it normally - note that this probably doesn’t work as is, you’ll want to use the technique to build a solution that fits your code:


I am no python expert, but, this seems to work well enough for my purposes. Apologies if it’s nasty, suggestions for better alternatives are welcome :-)

Nathan Ward

More information about the collectd mailing list