Re: [collectd] problem with importing collectd python module
Hi, 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 <colle...@daork.net> Sent: Monday, February 8, 2016 5:27 AM To: Pawel Akonom Cc: Joshua J. Kugler; collectd@verplant.org; Derek Palma Subject: Re: [collectd] problem with importing collectd python module > On 7/02/2016, at 02:54, Pawel Akonom <pawel.ako...@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. Pawel, 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: https://gist.github.com/nward/a931f38c5a8b34b8a1cd 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 ___ collectd mailing list collectd@verplant.org http://mailman.verplant.org/listinfo/collectd
Re: [collectd] problem with importing collectd python module
Hi Nils, Yes I am loading the plugin from collectd config file. Here is the python plugin part of my /etc/collectd.conf file: LoadPlugin python ModulePath "/var/lib/collectd/mongodb.py" Import "mongodb" Host "127.0.0.1" Database "virtunomic" mongodb.py file is the one from https://github.com/sebest/collectd-mongodb/blob/master/mongodb.py collectd can't load the plugin and when I run python script manually - just as a python script I got error: can't load collectd module. The error is in line 5 of the python script "import collectd". In collectd log file there is different error so maybe it could load collectd module when is run from collectd daemon. The error in log file is: ImportError: No module named mongodb [root@cisco_aci_dev184 ~]# /etc/init.d/collectd restart ; tail -f /var/log/collectd.log Stopping collectd: [ OK ] Starting collectd: [ OK ] [2016-02-07 04:50:13] python plugin: Error importing module "mongodb". [2016-02-07 04:50:13] Unhandled python exception in importing module: ImportError: No module named mongodb [2016-02-07 04:50:13] python plugin: Found a configuration for the "mongodb" plugin, but the plugin isn't loaded or didn't register a configuration callback. [2016-02-07 04:50:13] logfile: invalid loglevel [debug] defaulting to 'info' [2016-02-07 04:50:14] python plugin: Error importing module "mongodb". [2016-02-07 04:50:14] Unhandled python exception in importing module: ImportError: No module named mongodb [2016-02-07 04:50:14] python plugin: Found a configuration for the "mongodb" plugin, but the plugin isn't loaded or didn't register a configuration callback. [2016-02-07 04:50:14] logfile: invalid loglevel [debug] defaulting to 'info' [2016-02-07 04:50:14] python plugin: Error importing module "mongodb". [2016-02-07 04:50:14] Unhandled python exception in importing module: ImportError: No module named mongodb [2016-02-07 04:50:14] python plugin: Found a configuration for the "mongodb" plugin, but the plugin isn't loaded or didn't register a configuration callback. [2016-02-07 04:50:14] Initialization complete, entering read-loop. ^C [root@cisco_aci_dev184 ~]# ls -la /var/lib/collectd/mongodb.py -rwxr-xr-x. 1 root root 5408 Feb 5 08:48 /var/lib/collectd/mongodb.py [root@cisco_aci_dev184 ~]# python /var/lib/collectd/mongodb.py Traceback (most recent call last): File "/var/lib/collectd/mongodb.py", line 5, in import collectd ImportError: No module named collectd Thanks in advance for any hints about the problem. Best Regards, Pawel From: Nils Steinger <nrg_colle...@voidptr.de> Sent: Saturday, February 6, 2016 5:13 PM To: Pawel Akonom; collectd@verplant.org Subject: Re: [collectd] problem with importing collectd python module Hi Pawel, are you loading the plugin from collectd's config file as described in its documentation [1]? Python scripts importing the collectd module are meant to be run by a Python interpreter included in collectd (by loading and configuring the "Python" plugin in collectd.conf) to avoid the overhead of running multiple Python interpreters. Running those scripts manually from the commandline won't work. To add to the confusion, there's also a Python wrapper around collectd's Unix socket interface, but that one isn't used here. Regards, Nils [1]: https://github.com/sebest/collectd-mongodb#configuration On 06.02.2016 14:54, Pawel Akonom 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. > > Br, > Pawel > ____________ > From: Joshua J. Kugler <jos...@azariah.com> > Sent: Saturday, February 6, 2016 1:13 AM > To: collectd@verplant.org > Cc: Pawel Akonom; Derek Palma > Subject: Re: [collectd] problem with importing collectd python module > > On Friday, February 05, 2016 19:39:50 Pawel Akonom wrote: >> The first line of mongodb.py plugin is: >> >> >> "import collectd" and the plugin can't load it. When I run python shell and >> try to import collectd manually I get: >> >> >> [root@cisco_aci_dev184 ~]# python >> Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) >> [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2 >> Type "help", "copyright", "credi
Re: [collectd] problem with importing collectd python module
> On 7/02/2016, at 02:54, Pawel Akonomwrote: > > 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. Pawel, 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: https://gist.github.com/nward/a931f38c5a8b34b8a1cd 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 ___ collectd mailing list collectd@verplant.org http://mailman.verplant.org/listinfo/collectd
Re: [collectd] problem with importing collectd python module
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. Br, Pawel From: Joshua J. Kugler <jos...@azariah.com> Sent: Saturday, February 6, 2016 1:13 AM To: collectd@verplant.org Cc: Pawel Akonom; Derek Palma Subject: Re: [collectd] problem with importing collectd python module On Friday, February 05, 2016 19:39:50 Pawel Akonom wrote: > The first line of mongodb.py plugin is: > > > "import collectd" and the plugin can't load it. When I run python shell and > try to import collectd manually I get: > > > [root@cisco_aci_dev184 ~]# python > Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) > [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > > >>> import collectd > > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named collectd Can you clarify what you mean by "the plugin can't load it?" Do you can an error? Or is the Python plugin not finding the mongodb.py plugin? Per the wiki page here: https://collectd.org/wiki/index.php/Plugin:Python you will need to provide a path to the module you're trying load. Can you post your collectd config? j -- Joshua J. Kugler - Fairbanks, Alaska Azariah Enterprises - Programming and Website Design jos...@azariah.com - Jabber: pedah...@gmail.com PGP Key: http://pgp.mit.edu/ ID 0x73B13B6A ___ collectd mailing list collectd@verplant.org http://mailman.verplant.org/listinfo/collectd
Re: [collectd] problem with importing collectd python module
On Friday, February 05, 2016 19:39:50 Pawel Akonom wrote: > The first line of mongodb.py plugin is: > > > "import collectd" and the plugin can't load it. When I run python shell and > try to import collectd manually I get: > > > [root@cisco_aci_dev184 ~]# python > Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) > [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > > >>> import collectd > > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named collectd Can you clarify what you mean by "the plugin can't load it?" Do you can an error? Or is the Python plugin not finding the mongodb.py plugin? Per the wiki page here: https://collectd.org/wiki/index.php/Plugin:Python you will need to provide a path to the module you're trying load. Can you post your collectd config? j -- Joshua J. Kugler - Fairbanks, Alaska Azariah Enterprises - Programming and Website Design jos...@azariah.com - Jabber: pedah...@gmail.com PGP Key: http://pgp.mit.edu/ ID 0x73B13B6A ___ collectd mailing list collectd@verplant.org http://mailman.verplant.org/listinfo/collectd