[collectd] perl plugin: callback only for one of the thread

Pavel V. pavel2000 at ngs.ru
Mon Feb 27 22:50:56 CET 2017


Hi, Andreas.

> my perl plugin queries two hosts, and two threads are created, with their
> respective configurations from the config files. 

> However, by virtue of print statements, i can tell that only one of the
> threads receive read callbacks from collectd.

How do you implemented read callback registering?

If you call plugin_register (TYPE_READ, "FooBar", "foobar_read") twice with same
plugin name, the plugin will be registered only once (with warning at logs)!

There is two ways to get your needs:

1) Register perl plugin multiple times with different plugin names, like this:

   plugin_register (TYPE_READ, "FooBar/$hostname", "foobar_read");

I.e. a separate registration for each host/thread.

That will be equal to plugin_register_complex_read() of C plugins, just without
'userdata'.

To distinguish hosts, you may try to use sub AUTOLOAD {...} and $AUTOLOAD variable:

our $AUTOLOAD;
sub AUTOLOAD {
   #print $AUTOLOAD."\n";  #Expected output:  package::foobar_read_$hostname

   if ($AUTOLOAD =~ /\:\:foobar_read_(.*)$/) {
     foobar_read($1);
   }
   else {
     die;
   }
}

#Many times, for each $hostname
plugin_register (TYPE_READ, "FooBar/$hostname", "foobar_read_$hostname");


OR pregenerate required subs:

#Many times, for each $hostname
eval "sub foobar_read_$hostname { foobar_read('$hostname');}";
plugin_register (TYPE_READ, "FooBar/$hostname", "foobar_read_$hostname");

(I don't checked these under Collectd ever)

In this case, your threads can be polled by Collectd in parallel,
simultaneously, which can be better for performance / if there is many hosts
polled. 

Preferred way, IMHO.

2) Register plugin once and implement reading data from  your threads yourself.
In this case, threads will be polled consequentially by one Collectd thread
which can be slow.

> the init and configuration callbacks were called for both threads. 


-- 
Regards,
 Pavel                          mailto:pavel2000 at ngs.ru




More information about the collectd mailing list