1 /**
2 * collectd - src/perl.c
3 * Copyright (C) 2007-2009 Sebastian Harl
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Author:
19 * Sebastian Harl <sh at tokkee.org>
20 **/
22 /*
23 * This plugin embeds a Perl interpreter into collectd and provides an
24 * interface for collectd plugins written in perl.
25 */
27 /* do not automatically get the thread specific perl interpreter */
28 #define PERL_NO_GET_CONTEXT
30 #define DONT_POISON_SPRINTF_YET 1
31 #include "collectd.h"
32 #undef DONT_POISON_SPRINTF_YET
34 #include "configfile.h"
36 #include <EXTERN.h>
37 #include <perl.h>
39 #if defined(COLLECT_DEBUG) && COLLECT_DEBUG && defined(__GNUC__) && __GNUC__
40 # pragma GCC poison sprintf
41 #endif
43 #include <XSUB.h>
45 /* Some versions of Perl define their own version of DEBUG... :-/ */
46 #ifdef DEBUG
47 # undef DEBUG
48 #endif /* DEBUG */
50 /* ... while we want the definition found in plugin.h. */
51 #include "plugin.h"
52 #include "common.h"
54 #include "filter_chain.h"
56 #include <pthread.h>
58 #if !defined(USE_ITHREADS)
59 # error "Perl does not support ithreads!"
60 #endif /* !defined(USE_ITHREADS) */
62 /* clear the Perl sub's stack frame
63 * (this should only be used inside an XSUB) */
64 #define CLEAR_STACK_FRAME PL_stack_sp = PL_stack_base + *PL_markstack_ptr
66 #define PLUGIN_INIT 0
67 #define PLUGIN_READ 1
68 #define PLUGIN_WRITE 2
69 #define PLUGIN_SHUTDOWN 3
70 #define PLUGIN_LOG 4
71 #define PLUGIN_NOTIF 5
72 #define PLUGIN_FLUSH 6
74 #define PLUGIN_TYPES 7
76 #define PLUGIN_CONFIG 254
77 #define PLUGIN_DATASET 255
79 #define FC_MATCH 0
80 #define FC_TARGET 1
82 #define FC_TYPES 2
84 #define FC_CB_CREATE 0
85 #define FC_CB_DESTROY 1
86 #define FC_CB_EXEC 2
88 #define FC_CB_TYPES 3
90 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
91 #define log_info(...) INFO ("perl: " __VA_ARGS__)
92 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
93 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
95 /* this is defined in DynaLoader.a */
96 void boot_DynaLoader (PerlInterpreter *, CV *);
98 static XS (Collectd_plugin_register_ds);
99 static XS (Collectd_plugin_unregister_ds);
100 static XS (Collectd_plugin_dispatch_values);
101 static XS (Collectd__plugin_write);
102 static XS (Collectd__plugin_flush);
103 static XS (Collectd_plugin_dispatch_notification);
104 static XS (Collectd_plugin_log);
105 static XS (Collectd__fc_register);
106 static XS (Collectd_call_by_name);
108 /*
109 * private data types
110 */
112 typedef struct c_ithread_s {
113 /* the thread's Perl interpreter */
114 PerlInterpreter *interp;
116 /* double linked list of threads */
117 struct c_ithread_s *prev;
118 struct c_ithread_s *next;
119 } c_ithread_t;
121 typedef struct {
122 c_ithread_t *head;
123 c_ithread_t *tail;
125 #if COLLECT_DEBUG
126 /* some usage stats */
127 int number_of_threads;
128 #endif /* COLLECT_DEBUG */
130 pthread_mutex_t mutex;
131 } c_ithread_list_t;
133 /* name / user_data for Perl matches / targets */
134 typedef struct {
135 char *name;
136 SV *user_data;
137 } pfc_user_data_t;
139 #define PFC_USER_DATA_FREE(data) \
140 do { \
141 sfree ((data)->name); \
142 if (NULL != (data)->user_data) \
143 sv_free ((data)->user_data); \
144 sfree (data); \
145 } while (0)
147 /*
148 * Public variable
149 */
150 extern char **environ;
152 /*
153 * private variables
154 */
156 /* if perl_threads != NULL perl_threads->head must
157 * point to the "base" thread */
158 static c_ithread_list_t *perl_threads = NULL;
160 /* the key used to store each pthread's ithread */
161 static pthread_key_t perl_thr_key;
163 static int perl_argc = 0;
164 static char **perl_argv = NULL;
166 static char base_name[DATA_MAX_NAME_LEN] = "";
168 static struct {
169 char name[64];
170 XS ((*f));
171 } api[] =
172 {
173 { "Collectd::plugin_register_data_set", Collectd_plugin_register_ds },
174 { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
175 { "Collectd::plugin_dispatch_values", Collectd_plugin_dispatch_values },
176 { "Collectd::_plugin_write", Collectd__plugin_write },
177 { "Collectd::_plugin_flush", Collectd__plugin_flush },
178 { "Collectd::plugin_dispatch_notification",
179 Collectd_plugin_dispatch_notification },
180 { "Collectd::plugin_log", Collectd_plugin_log },
181 { "Collectd::_fc_register", Collectd__fc_register },
182 { "Collectd::call_by_name", Collectd_call_by_name },
183 { "", NULL }
184 };
186 struct {
187 char name[64];
188 int value;
189 } constants[] =
190 {
191 { "Collectd::TYPE_INIT", PLUGIN_INIT },
192 { "Collectd::TYPE_READ", PLUGIN_READ },
193 { "Collectd::TYPE_WRITE", PLUGIN_WRITE },
194 { "Collectd::TYPE_SHUTDOWN", PLUGIN_SHUTDOWN },
195 { "Collectd::TYPE_LOG", PLUGIN_LOG },
196 { "Collectd::TYPE_NOTIF", PLUGIN_NOTIF },
197 { "Collectd::TYPE_FLUSH", PLUGIN_FLUSH },
198 { "Collectd::TYPE_CONFIG", PLUGIN_CONFIG },
199 { "Collectd::TYPE_DATASET", PLUGIN_DATASET },
200 { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
201 { "Collectd::DS_TYPE_GAUGE", DS_TYPE_GAUGE },
202 { "Collectd::DS_TYPE_DERIVE", DS_TYPE_DERIVE },
203 { "Collectd::DS_TYPE_ABSOLUTE", DS_TYPE_ABSOLUTE },
204 { "Collectd::LOG_ERR", LOG_ERR },
205 { "Collectd::LOG_WARNING", LOG_WARNING },
206 { "Collectd::LOG_NOTICE", LOG_NOTICE },
207 { "Collectd::LOG_INFO", LOG_INFO },
208 { "Collectd::LOG_DEBUG", LOG_DEBUG },
209 { "Collectd::FC_MATCH", FC_MATCH },
210 { "Collectd::FC_TARGET", FC_TARGET },
211 { "Collectd::FC_CB_CREATE", FC_CB_CREATE },
212 { "Collectd::FC_CB_DESTROY", FC_CB_DESTROY },
213 { "Collectd::FC_CB_EXEC", FC_CB_EXEC },
214 { "Collectd::FC_MATCH_NO_MATCH", FC_MATCH_NO_MATCH },
215 { "Collectd::FC_MATCH_MATCHES", FC_MATCH_MATCHES },
216 { "Collectd::FC_TARGET_CONTINUE", FC_TARGET_CONTINUE },
217 { "Collectd::FC_TARGET_STOP", FC_TARGET_STOP },
218 { "Collectd::FC_TARGET_RETURN", FC_TARGET_RETURN },
219 { "Collectd::NOTIF_FAILURE", NOTIF_FAILURE },
220 { "Collectd::NOTIF_WARNING", NOTIF_WARNING },
221 { "Collectd::NOTIF_OKAY", NOTIF_OKAY },
222 { "", 0 }
223 };
225 struct {
226 char name[64];
227 char *var;
228 } g_strings[] =
229 {
230 { "Collectd::hostname_g", hostname_g },
231 { "", NULL }
232 };
234 struct {
235 char name[64];
236 int *var;
237 } g_integers[] =
238 {
239 { "Collectd::interval_g", &interval_g },
240 { "", NULL }
241 };
243 /*
244 * Helper functions for data type conversion.
245 */
247 /*
248 * data source:
249 * [
250 * {
251 * name => $ds_name,
252 * type => $ds_type,
253 * min => $ds_min,
254 * max => $ds_max
255 * },
256 * ...
257 * ]
258 */
259 static int hv2data_source (pTHX_ HV *hash, data_source_t *ds)
260 {
261 SV **tmp = NULL;
263 if ((NULL == hash) || (NULL == ds))
264 return -1;
266 if (NULL != (tmp = hv_fetch (hash, "name", 4, 0))) {
267 sstrncpy (ds->name, SvPV_nolen (*tmp), sizeof (ds->name));
268 }
269 else {
270 log_err ("hv2data_source: No DS name given.");
271 return -1;
272 }
274 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0))) {
275 ds->type = SvIV (*tmp);
277 if ((DS_TYPE_COUNTER != ds->type)
278 && (DS_TYPE_GAUGE != ds->type)
279 && (DS_TYPE_DERIVE != ds->type)
280 && (DS_TYPE_ABSOLUTE != ds->type)) {
281 log_err ("hv2data_source: Invalid DS type.");
282 return -1;
283 }
284 }
285 else {
286 ds->type = DS_TYPE_COUNTER;
287 }
289 if (NULL != (tmp = hv_fetch (hash, "min", 3, 0)))
290 ds->min = SvNV (*tmp);
291 else
292 ds->min = NAN;
294 if (NULL != (tmp = hv_fetch (hash, "max", 3, 0)))
295 ds->max = SvNV (*tmp);
296 else
297 ds->max = NAN;
298 return 0;
299 } /* static int hv2data_source (HV *, data_source_t *) */
301 static int av2value (pTHX_ char *name, AV *array, value_t *value, int len)
302 {
303 const data_set_t *ds;
305 int i = 0;
307 if ((NULL == name) || (NULL == array) || (NULL == value))
308 return -1;
310 if (av_len (array) < len - 1)
311 len = av_len (array) + 1;
313 if (0 >= len)
314 return -1;
316 ds = plugin_get_ds (name);
317 if (NULL == ds) {
318 log_err ("av2value: Unknown dataset \"%s\"", name);
319 return -1;
320 }
322 if (ds->ds_num < len) {
323 log_warn ("av2value: Value length exceeds data set length.");
324 len = ds->ds_num;
325 }
327 for (i = 0; i < len; ++i) {
328 SV **tmp = av_fetch (array, i, 0);
330 if (NULL != tmp) {
331 if (DS_TYPE_COUNTER == ds->ds[i].type)
332 value[i].counter = SvIV (*tmp);
333 else if (DS_TYPE_GAUGE == ds->ds[i].type)
334 value[i].gauge = SvNV (*tmp);
335 else if (DS_TYPE_DERIVE == ds->ds[i].type)
336 value[i].derive = SvIV (*tmp);
337 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
338 value[i].absolute = SvIV (*tmp);
339 }
340 else {
341 return -1;
342 }
343 }
344 return len;
345 } /* static int av2value (char *, AV *, value_t *, int) */
347 /*
348 * value list:
349 * {
350 * values => [ @values ],
351 * time => $time,
352 * host => $host,
353 * plugin => $plugin,
354 * plugin_instance => $pinstance,
355 * type_instance => $tinstance,
356 * }
357 */
358 static int hv2value_list (pTHX_ HV *hash, value_list_t *vl)
359 {
360 SV **tmp;
362 if ((NULL == hash) || (NULL == vl))
363 return -1;
365 if (NULL == (tmp = hv_fetch (hash, "type", 4, 0))) {
366 log_err ("hv2value_list: No type given.");
367 return -1;
368 }
370 sstrncpy (vl->type, SvPV_nolen (*tmp), sizeof (vl->type));
372 if ((NULL == (tmp = hv_fetch (hash, "values", 6, 0)))
373 || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
374 log_err ("hv2value_list: No valid values given.");
375 return -1;
376 }
378 {
379 AV *array = (AV *)SvRV (*tmp);
380 int len = av_len (array) + 1;
382 if (len <= 0)
383 return -1;
385 vl->values = (value_t *)smalloc (len * sizeof (value_t));
386 vl->values_len = av2value (aTHX_ vl->type, (AV *)SvRV (*tmp),
387 vl->values, len);
389 if (-1 == vl->values_len) {
390 sfree (vl->values);
391 return -1;
392 }
393 }
395 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
396 vl->time = (time_t)SvIV (*tmp);
398 if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0)))
399 vl->interval = SvIV (*tmp);
401 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
402 sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host));
403 else
404 sstrncpy (vl->host, hostname_g, sizeof (vl->host));
406 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
407 sstrncpy (vl->plugin, SvPV_nolen (*tmp), sizeof (vl->plugin));
409 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
410 sstrncpy (vl->plugin_instance, SvPV_nolen (*tmp),
411 sizeof (vl->plugin_instance));
413 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
414 sstrncpy (vl->type_instance, SvPV_nolen (*tmp),
415 sizeof (vl->type_instance));
416 return 0;
417 } /* static int hv2value_list (pTHX_ HV *, value_list_t *) */
419 static int av2data_set (pTHX_ AV *array, char *name, data_set_t *ds)
420 {
421 int len, i;
423 if ((NULL == array) || (NULL == name) || (NULL == ds))
424 return -1;
426 len = av_len (array);
428 if (-1 == len) {
429 log_err ("av2data_set: Invalid data set.");
430 return -1;
431 }
433 ds->ds = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
434 ds->ds_num = len + 1;
436 for (i = 0; i <= len; ++i) {
437 SV **elem = av_fetch (array, i, 0);
439 if (NULL == elem) {
440 log_err ("av2data_set: Failed to fetch data source %i.", i);
441 return -1;
442 }
444 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
445 log_err ("av2data_set: Invalid data source.");
446 return -1;
447 }
449 if (-1 == hv2data_source (aTHX_ (HV *)SvRV (*elem), &ds->ds[i]))
450 return -1;
452 log_debug ("av2data_set: "
453 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
454 ds->ds[i].name, ds->ds[i].type, ds->ds[i].min, ds->ds[i].max);
455 }
457 sstrncpy (ds->type, name, sizeof (ds->type));
458 return 0;
459 } /* static int av2data_set (pTHX_ AV *, data_set_t *) */
461 /*
462 * notification:
463 * {
464 * severity => $severity,
465 * time => $time,
466 * message => $msg,
467 * host => $host,
468 * plugin => $plugin,
469 * type => $type,
470 * plugin_instance => $instance,
471 * type_instance => $type_instance,
472 * meta => [ { name => <name>, value => <value> }, ... ]
473 * }
474 */
475 static int av2notification_meta (pTHX_ AV *array, notification_meta_t **meta)
476 {
477 notification_meta_t **m = meta;
479 int len = av_len (array);
480 int i;
482 for (i = 0; i <= len; ++i) {
483 SV **tmp = av_fetch (array, i, 0);
484 HV *hash;
486 if (NULL == tmp)
487 return -1;
489 if (! (SvROK (*tmp) && (SVt_PVHV == SvTYPE (SvRV (*tmp))))) {
490 log_warn ("av2notification_meta: Skipping invalid "
491 "meta information.");
492 continue;
493 }
495 hash = (HV *)SvRV (*tmp);
497 *m = (notification_meta_t *)smalloc (sizeof (**m));
499 if (NULL == (tmp = hv_fetch (hash, "name", 4, 0))) {
500 log_warn ("av2notification_meta: Skipping invalid "
501 "meta information.");
502 free (*m);
503 continue;
504 }
505 sstrncpy ((*m)->name, SvPV_nolen (*tmp), sizeof ((*m)->name));
507 if (NULL == (tmp = hv_fetch (hash, "value", 5, 0))) {
508 log_warn ("av2notification_meta: Skipping invalid "
509 "meta information.");
510 free ((*m)->name);
511 free (*m);
512 continue;
513 }
515 if (SvNOK (*tmp)) {
516 (*m)->nm_value.nm_double = SvNVX (*tmp);
517 (*m)->type = NM_TYPE_DOUBLE;
518 }
519 else if (SvUOK (*tmp)) {
520 (*m)->nm_value.nm_unsigned_int = SvUVX (*tmp);
521 (*m)->type = NM_TYPE_UNSIGNED_INT;
522 }
523 else if (SvIOK (*tmp)) {
524 (*m)->nm_value.nm_signed_int = SvIVX (*tmp);
525 (*m)->type = NM_TYPE_SIGNED_INT;
526 }
527 else {
528 (*m)->nm_value.nm_string = sstrdup (SvPV_nolen (*tmp));
529 (*m)->type = NM_TYPE_STRING;
530 }
532 (*m)->next = NULL;
533 m = &((*m)->next);
534 }
535 return 0;
536 } /* static int av2notification_meta (AV *, notification_meta_t *) */
538 static int hv2notification (pTHX_ HV *hash, notification_t *n)
539 {
540 SV **tmp = NULL;
542 if ((NULL == hash) || (NULL == n))
543 return -1;
545 if (NULL != (tmp = hv_fetch (hash, "severity", 8, 0)))
546 n->severity = SvIV (*tmp);
547 else
548 n->severity = NOTIF_FAILURE;
550 if (NULL != (tmp = hv_fetch (hash, "time", 4, 0)))
551 n->time = (time_t)SvIV (*tmp);
552 else
553 n->time = time (NULL);
555 if (NULL != (tmp = hv_fetch (hash, "message", 7, 0)))
556 sstrncpy (n->message, SvPV_nolen (*tmp), sizeof (n->message));
558 if (NULL != (tmp = hv_fetch (hash, "host", 4, 0)))
559 sstrncpy (n->host, SvPV_nolen (*tmp), sizeof (n->host));
560 else
561 sstrncpy (n->host, hostname_g, sizeof (n->host));
563 if (NULL != (tmp = hv_fetch (hash, "plugin", 6, 0)))
564 sstrncpy (n->plugin, SvPV_nolen (*tmp), sizeof (n->plugin));
566 if (NULL != (tmp = hv_fetch (hash, "plugin_instance", 15, 0)))
567 sstrncpy (n->plugin_instance, SvPV_nolen (*tmp),
568 sizeof (n->plugin_instance));
570 if (NULL != (tmp = hv_fetch (hash, "type", 4, 0)))
571 sstrncpy (n->type, SvPV_nolen (*tmp), sizeof (n->type));
573 if (NULL != (tmp = hv_fetch (hash, "type_instance", 13, 0)))
574 sstrncpy (n->type_instance, SvPV_nolen (*tmp),
575 sizeof (n->type_instance));
577 n->meta = NULL;
578 while (NULL != (tmp = hv_fetch (hash, "meta", 4, 0))) {
579 if (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp))))) {
580 log_warn ("hv2notification: Ignoring invalid meta information.");
581 break;
582 }
584 if (0 != av2notification_meta (aTHX_ (AV *)SvRV (*tmp), &n->meta)) {
585 plugin_notification_meta_free (n->meta);
586 n->meta = NULL;
587 return -1;
588 }
589 break;
590 }
591 return 0;
592 } /* static int hv2notification (pTHX_ HV *, notification_t *) */
594 static int data_set2av (pTHX_ data_set_t *ds, AV *array)
595 {
596 int i = 0;
598 if ((NULL == ds) || (NULL == array))
599 return -1;
601 av_extend (array, ds->ds_num);
603 for (i = 0; i < ds->ds_num; ++i) {
604 HV *source = newHV ();
606 if (NULL == hv_store (source, "name", 4,
607 newSVpv (ds->ds[i].name, 0), 0))
608 return -1;
610 if (NULL == hv_store (source, "type", 4, newSViv (ds->ds[i].type), 0))
611 return -1;
613 if (! isnan (ds->ds[i].min))
614 if (NULL == hv_store (source, "min", 3,
615 newSVnv (ds->ds[i].min), 0))
616 return -1;
618 if (! isnan (ds->ds[i].max))
619 if (NULL == hv_store (source, "max", 3,
620 newSVnv (ds->ds[i].max), 0))
621 return -1;
623 if (NULL == av_store (array, i, newRV_noinc ((SV *)source)))
624 return -1;
625 }
626 return 0;
627 } /* static int data_set2av (data_set_t *, AV *) */
629 static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash)
630 {
631 AV *values = NULL;
633 int i = 0;
634 int len = 0;
636 if ((NULL == vl) || (NULL == ds) || (NULL == hash))
637 return -1;
639 len = vl->values_len;
641 if (ds->ds_num < len) {
642 log_warn ("value2av: Value length exceeds data set length.");
643 len = ds->ds_num;
644 }
646 values = newAV ();
647 av_extend (values, len - 1);
649 for (i = 0; i < len; ++i) {
650 SV *val = NULL;
652 if (DS_TYPE_COUNTER == ds->ds[i].type)
653 val = newSViv (vl->values[i].counter);
654 else if (DS_TYPE_GAUGE == ds->ds[i].type)
655 val = newSVnv (vl->values[i].gauge);
656 else if (DS_TYPE_DERIVE == ds->ds[i].type)
657 val = newSViv (vl->values[i].derive);
658 else if (DS_TYPE_ABSOLUTE == ds->ds[i].type)
659 val = newSViv (vl->values[i].absolute);
661 if (NULL == av_store (values, i, val)) {
662 av_undef (values);
663 return -1;
664 }
665 }
667 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0))
668 return -1;
670 if (0 != vl->time)
671 if (NULL == hv_store (hash, "time", 4, newSViv (vl->time), 0))
672 return -1;
674 if (NULL == hv_store (hash, "interval", 8, newSViv (vl->interval), 0))
675 return -1;
677 if ('\0' != vl->host[0])
678 if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0))
679 return -1;
681 if ('\0' != vl->plugin[0])
682 if (NULL == hv_store (hash, "plugin", 6, newSVpv (vl->plugin, 0), 0))
683 return -1;
685 if ('\0' != vl->plugin_instance[0])
686 if (NULL == hv_store (hash, "plugin_instance", 15,
687 newSVpv (vl->plugin_instance, 0), 0))
688 return -1;
690 if ('\0' != vl->type[0])
691 if (NULL == hv_store (hash, "type", 4, newSVpv (vl->type, 0), 0))
692 return -1;
694 if ('\0' != vl->type_instance[0])
695 if (NULL == hv_store (hash, "type_instance", 13,
696 newSVpv (vl->type_instance, 0), 0))
697 return -1;
698 return 0;
699 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
701 static int notification_meta2av (pTHX_ notification_meta_t *meta, AV *array)
702 {
703 int meta_num = 0;
704 int i;
706 while (meta) {
707 ++meta_num;
708 meta = meta->next;
709 }
711 av_extend (array, meta_num);
713 for (i = 0; NULL != meta; meta = meta->next, ++i) {
714 HV *m = newHV ();
715 SV *value;
717 if (NULL == hv_store (m, "name", 4, newSVpv (meta->name, 0), 0))
718 return -1;
720 if (NM_TYPE_STRING == meta->type)
721 value = newSVpv (meta->nm_value.nm_string, 0);
722 else if (NM_TYPE_SIGNED_INT == meta->type)
723 value = newSViv (meta->nm_value.nm_signed_int);
724 else if (NM_TYPE_UNSIGNED_INT == meta->type)
725 value = newSVuv (meta->nm_value.nm_unsigned_int);
726 else if (NM_TYPE_DOUBLE == meta->type)
727 value = newSVnv (meta->nm_value.nm_double);
728 else if (NM_TYPE_BOOLEAN == meta->type)
729 value = meta->nm_value.nm_boolean ? &PL_sv_yes : &PL_sv_no;
730 else
731 return -1;
733 if (NULL == hv_store (m, "value", 5, value, 0)) {
734 sv_free (value);
735 return -1;
736 }
738 if (NULL == av_store (array, i, newRV_noinc ((SV *)m))) {
739 hv_clear (m);
740 hv_undef (m);
741 return -1;
742 }
743 }
744 return 0;
745 } /* static int notification_meta2av (notification_meta_t *, AV *) */
747 static int notification2hv (pTHX_ notification_t *n, HV *hash)
748 {
749 if (NULL == hv_store (hash, "severity", 8, newSViv (n->severity), 0))
750 return -1;
752 if (0 != n->time)
753 if (NULL == hv_store (hash, "time", 4, newSViv (n->time), 0))
754 return -1;
756 if ('\0' != *n->message)
757 if (NULL == hv_store (hash, "message", 7, newSVpv (n->message, 0), 0))
758 return -1;
760 if ('\0' != *n->host)
761 if (NULL == hv_store (hash, "host", 4, newSVpv (n->host, 0), 0))
762 return -1;
764 if ('\0' != *n->plugin)
765 if (NULL == hv_store (hash, "plugin", 6, newSVpv (n->plugin, 0), 0))
766 return -1;
768 if ('\0' != *n->plugin_instance)
769 if (NULL == hv_store (hash, "plugin_instance", 15,
770 newSVpv (n->plugin_instance, 0), 0))
771 return -1;
773 if ('\0' != *n->type)
774 if (NULL == hv_store (hash, "type", 4, newSVpv (n->type, 0), 0))
775 return -1;
777 if ('\0' != *n->type_instance)
778 if (NULL == hv_store (hash, "type_instance", 13,
779 newSVpv (n->type_instance, 0), 0))
780 return -1;
782 if (NULL != n->meta) {
783 AV *meta = newAV ();
784 if ((0 != notification_meta2av (aTHX_ n->meta, meta))
785 || (NULL == hv_store (hash, "meta", 4,
786 newRV_noinc ((SV *)meta), 0))) {
787 av_clear (meta);
788 av_undef (meta);
789 return -1;
790 }
791 }
792 return 0;
793 } /* static int notification2hv (notification_t *, HV *) */
795 static int oconfig_item2hv (pTHX_ oconfig_item_t *ci, HV *hash)
796 {
797 int i;
799 AV *values;
800 AV *children;
802 if (NULL == hv_store (hash, "key", 3, newSVpv (ci->key, 0), 0))
803 return -1;
805 values = newAV ();
806 if (0 < ci->values_num)
807 av_extend (values, ci->values_num);
809 if (NULL == hv_store (hash, "values", 6, newRV_noinc ((SV *)values), 0)) {
810 av_clear (values);
811 av_undef (values);
812 return -1;
813 }
815 for (i = 0; i < ci->values_num; ++i) {
816 SV *value;
818 switch (ci->values[i].type) {
819 case OCONFIG_TYPE_STRING:
820 value = newSVpv (ci->values[i].value.string, 0);
821 break;
822 case OCONFIG_TYPE_NUMBER:
823 value = newSVnv ((NV)ci->values[i].value.number);
824 break;
825 case OCONFIG_TYPE_BOOLEAN:
826 value = ci->values[i].value.boolean ? &PL_sv_yes : &PL_sv_no;
827 break;
828 default:
829 log_err ("oconfig_item2hv: Invalid value type %i.",
830 ci->values[i].type);
831 value = &PL_sv_undef;
832 }
834 if (NULL == av_store (values, i, value)) {
835 sv_free (value);
836 return -1;
837 }
838 }
840 /* ignoring 'parent' member which is uninteresting in this case */
842 children = newAV ();
843 if (0 < ci->children_num)
844 av_extend (children, ci->children_num);
846 if (NULL == hv_store (hash, "children", 8, newRV_noinc ((SV *)children), 0)) {
847 av_clear (children);
848 av_undef (children);
849 return -1;
850 }
852 for (i = 0; i < ci->children_num; ++i) {
853 HV *child = newHV ();
855 if (0 != oconfig_item2hv (aTHX_ ci->children + i, child)) {
856 hv_clear (child);
857 hv_undef (child);
858 return -1;
859 }
861 if (NULL == av_store (children, i, newRV_noinc ((SV *)child))) {
862 hv_clear (child);
863 hv_undef (child);
864 return -1;
865 }
866 }
867 return 0;
868 } /* static int oconfig_item2hv (pTHX_ oconfig_item_t *, HV *) */
870 /*
871 * Internal functions.
872 */
874 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
875 int status = 0;
876 if (base_name[0] == '\0')
877 status = ssnprintf (buf, buf_len, "%s", module);
878 else
879 status = ssnprintf (buf, buf_len, "%s::%s", base_name, module);
880 if ((status < 0) || ((unsigned int)status >= buf_len))
881 return (NULL);
882 return (buf);
883 } /* char *get_module_name */
885 /*
886 * Add a plugin's data set definition.
887 */
888 static int pplugin_register_data_set (pTHX_ char *name, AV *dataset)
889 {
890 int ret = 0;
892 data_set_t ds;
894 if ((NULL == name) || (NULL == dataset))
895 return -1;
897 if (0 != av2data_set (aTHX_ dataset, name, &ds))
898 return -1;
900 ret = plugin_register_data_set (&ds);
902 free (ds.ds);
903 return ret;
904 } /* static int pplugin_register_data_set (char *, SV *) */
906 /*
907 * Remove a plugin's data set definition.
908 */
909 static int pplugin_unregister_data_set (char *name)
910 {
911 if (NULL == name)
912 return 0;
913 return plugin_unregister_data_set (name);
914 } /* static int pplugin_unregister_data_set (char *) */
916 /*
917 * Submit the values to the write functions.
918 */
919 static int pplugin_dispatch_values (pTHX_ HV *values)
920 {
921 value_list_t vl = VALUE_LIST_INIT;
923 int ret = 0;
925 if (NULL == values)
926 return -1;
928 if (0 != hv2value_list (aTHX_ values, &vl))
929 return -1;
931 ret = plugin_dispatch_values (&vl);
933 sfree (vl.values);
934 return ret;
935 } /* static int pplugin_dispatch_values (char *, HV *) */
937 /*
938 * Submit the values to a single write function.
939 */
940 static int pplugin_write (pTHX_ const char *plugin, AV *data_set, HV *values)
941 {
942 data_set_t ds;
943 value_list_t vl = VALUE_LIST_INIT;
945 int ret;
947 if (NULL == values)
948 return -1;
950 if (0 != hv2value_list (aTHX_ values, &vl))
951 return -1;
953 if ((NULL != data_set)
954 && (0 != av2data_set (aTHX_ data_set, vl.type, &ds)))
955 return -1;
957 ret = plugin_write (plugin, NULL == data_set ? NULL : &ds, &vl);
958 if (0 != ret)
959 log_warn ("Dispatching value to plugin \"%s\" failed with status %i.",
960 NULL == plugin ? "<any>" : plugin, ret);
962 if (NULL != data_set)
963 sfree (ds.ds);
964 sfree (vl.values);
965 return ret;
966 } /* static int pplugin_write (const char *plugin, HV *, HV *) */
968 /*
969 * Dispatch a notification.
970 */
971 static int pplugin_dispatch_notification (pTHX_ HV *notif)
972 {
973 notification_t n;
975 int ret;
977 if (NULL == notif)
978 return -1;
980 memset (&n, 0, sizeof (n));
982 if (0 != hv2notification (aTHX_ notif, &n))
983 return -1;
985 ret = plugin_dispatch_notification (&n);
986 plugin_notification_meta_free (n.meta);
987 return ret;
988 } /* static int pplugin_dispatch_notification (HV *) */
990 /*
991 * Call all working functions of the given type.
992 */
993 static int pplugin_call_all (pTHX_ int type, ...)
994 {
995 int retvals = 0;
997 va_list ap;
998 int ret = 0;
1000 dSP;
1002 if ((type < 0) || (type >= PLUGIN_TYPES))
1003 return -1;
1005 va_start (ap, type);
1007 ENTER;
1008 SAVETMPS;
1010 PUSHMARK (SP);
1012 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1014 if (PLUGIN_WRITE == type) {
1015 /*
1016 * $_[0] = $plugin_type;
1017 *
1018 * $_[1] =
1019 * [
1020 * {
1021 * name => $ds_name,
1022 * type => $ds_type,
1023 * min => $ds_min,
1024 * max => $ds_max
1025 * },
1026 * ...
1027 * ];
1028 *
1029 * $_[2] =
1030 * {
1031 * values => [ $v1, ... ],
1032 * time => $time,
1033 * host => $hostname,
1034 * plugin => $plugin,
1035 * type => $type,
1036 * plugin_instance => $instance,
1037 * type_instance => $type_instance
1038 * };
1039 */
1040 data_set_t *ds;
1041 value_list_t *vl;
1043 AV *pds = newAV ();
1044 HV *pvl = newHV ();
1046 ds = va_arg (ap, data_set_t *);
1047 vl = va_arg (ap, value_list_t *);
1049 if (-1 == data_set2av (aTHX_ ds, pds)) {
1050 av_clear (pds);
1051 av_undef (pds);
1052 pds = (AV *)&PL_sv_undef;
1053 ret = -1;
1054 }
1056 if (-1 == value_list2hv (aTHX_ vl, ds, pvl)) {
1057 hv_clear (pvl);
1058 hv_undef (pvl);
1059 pvl = (HV *)&PL_sv_undef;
1060 ret = -1;
1061 }
1063 XPUSHs (sv_2mortal (newSVpv (ds->type, 0)));
1064 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1065 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1066 }
1067 else if (PLUGIN_LOG == type) {
1068 /*
1069 * $_[0] = $level;
1070 *
1071 * $_[1] = $message;
1072 */
1073 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1074 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1075 }
1076 else if (PLUGIN_NOTIF == type) {
1077 /*
1078 * $_[0] =
1079 * {
1080 * severity => $severity,
1081 * time => $time,
1082 * message => $msg,
1083 * host => $host,
1084 * plugin => $plugin,
1085 * type => $type,
1086 * plugin_instance => $instance,
1087 * type_instance => $type_instance
1088 * };
1089 */
1090 notification_t *n;
1091 HV *notif = newHV ();
1093 n = va_arg (ap, notification_t *);
1095 if (-1 == notification2hv (aTHX_ n, notif)) {
1096 hv_clear (notif);
1097 hv_undef (notif);
1098 notif = (HV *)&PL_sv_undef;
1099 ret = -1;
1100 }
1102 XPUSHs (sv_2mortal (newRV_noinc ((SV *)notif)));
1103 }
1104 else if (PLUGIN_FLUSH == type) {
1105 /*
1106 * $_[0] = $timeout;
1107 * $_[1] = $identifier;
1108 */
1109 XPUSHs (sv_2mortal (newSViv (va_arg (ap, int))));
1110 XPUSHs (sv_2mortal (newSVpv (va_arg (ap, char *), 0)));
1111 }
1113 PUTBACK;
1115 retvals = call_pv ("Collectd::plugin_call_all", G_SCALAR);
1117 SPAGAIN;
1118 if (0 < retvals) {
1119 SV *tmp = POPs;
1120 if (! SvTRUE (tmp))
1121 ret = -1;
1122 }
1124 PUTBACK;
1125 FREETMPS;
1126 LEAVE;
1128 va_end (ap);
1129 return ret;
1130 } /* static int pplugin_call_all (int, ...) */
1132 /*
1133 * collectd's perl interpreter based thread implementation.
1134 *
1135 * This has been inspired by Perl's ithreads introduced in version 5.6.0.
1136 */
1138 /* must be called with perl_threads->mutex locked */
1139 static void c_ithread_destroy (c_ithread_t *ithread)
1140 {
1141 dTHXa (ithread->interp);
1143 assert (NULL != perl_threads);
1145 PERL_SET_CONTEXT (aTHX);
1146 log_debug ("Shutting down Perl interpreter %p...", aTHX);
1148 #if COLLECT_DEBUG
1149 sv_report_used ();
1151 --perl_threads->number_of_threads;
1152 #endif /* COLLECT_DEBUG */
1154 perl_destruct (aTHX);
1155 perl_free (aTHX);
1157 if (NULL == ithread->prev)
1158 perl_threads->head = ithread->next;
1159 else
1160 ithread->prev->next = ithread->next;
1162 if (NULL == ithread->next)
1163 perl_threads->tail = ithread->prev;
1164 else
1165 ithread->next->prev = ithread->prev;
1167 sfree (ithread);
1168 return;
1169 } /* static void c_ithread_destroy (c_ithread_t *) */
1171 static void c_ithread_destructor (void *arg)
1172 {
1173 c_ithread_t *ithread = (c_ithread_t *)arg;
1174 c_ithread_t *t = NULL;
1176 if (NULL == perl_threads)
1177 return;
1179 pthread_mutex_lock (&perl_threads->mutex);
1181 for (t = perl_threads->head; NULL != t; t = t->next)
1182 if (t == ithread)
1183 break;
1185 /* the ithread no longer exists */
1186 if (NULL == t)
1187 return;
1189 c_ithread_destroy (ithread);
1191 pthread_mutex_unlock (&perl_threads->mutex);
1192 return;
1193 } /* static void c_ithread_destructor (void *) */
1195 /* must be called with perl_threads->mutex locked */
1196 static c_ithread_t *c_ithread_create (PerlInterpreter *base)
1197 {
1198 c_ithread_t *t = NULL;
1199 dTHXa (NULL);
1201 assert (NULL != perl_threads);
1203 t = (c_ithread_t *)smalloc (sizeof (c_ithread_t));
1204 memset (t, 0, sizeof (c_ithread_t));
1206 t->interp = (NULL == base)
1207 ? NULL
1208 : perl_clone (base, CLONEf_KEEP_PTR_TABLE);
1210 aTHX = t->interp;
1212 if ((NULL != base) && (NULL != PL_endav)) {
1213 av_clear (PL_endav);
1214 av_undef (PL_endav);
1215 PL_endav = Nullav;
1216 }
1218 #if COLLECT_DEBUG
1219 ++perl_threads->number_of_threads;
1220 #endif /* COLLECT_DEBUG */
1222 t->next = NULL;
1224 if (NULL == perl_threads->tail) {
1225 perl_threads->head = t;
1226 t->prev = NULL;
1227 }
1228 else {
1229 perl_threads->tail->next = t;
1230 t->prev = perl_threads->tail;
1231 }
1233 perl_threads->tail = t;
1235 pthread_setspecific (perl_thr_key, (const void *)t);
1236 return t;
1237 } /* static c_ithread_t *c_ithread_create (PerlInterpreter *) */
1239 /*
1240 * Filter chains implementation.
1241 */
1243 static int fc_call (pTHX_ int type, int cb_type, pfc_user_data_t *data, ...)
1244 {
1245 int retvals = 0;
1247 va_list ap;
1248 int ret = 0;
1250 notification_meta_t **meta = NULL;
1251 AV *pmeta = NULL;
1253 dSP;
1255 if ((type < 0) || (type >= FC_TYPES))
1256 return -1;
1258 if ((cb_type < 0) || (cb_type >= FC_CB_TYPES))
1259 return -1;
1261 va_start (ap, data);
1263 ENTER;
1264 SAVETMPS;
1266 PUSHMARK (SP);
1268 XPUSHs (sv_2mortal (newSViv ((IV)type)));
1269 XPUSHs (sv_2mortal (newSVpv (data->name, 0)));
1270 XPUSHs (sv_2mortal (newSViv ((IV)cb_type)));
1272 if (FC_CB_CREATE == cb_type) {
1273 /*
1274 * $_[0] = $ci;
1275 * $_[1] = $user_data;
1276 */
1277 oconfig_item_t *ci;
1278 HV *config = newHV ();
1280 ci = va_arg (ap, oconfig_item_t *);
1282 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
1283 hv_clear (config);
1284 hv_undef (config);
1285 config = (HV *)&PL_sv_undef;
1286 ret = -1;
1287 }
1289 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
1290 }
1291 else if (FC_CB_DESTROY == cb_type) {
1292 /*
1293 * $_[1] = $user_data;
1294 */
1296 /* nothing to be done - the user data pointer
1297 * is pushed onto the stack later */
1298 }
1299 else if (FC_CB_EXEC == cb_type) {
1300 /*
1301 * $_[0] = $ds;
1302 * $_[1] = $vl;
1303 * $_[2] = $meta;
1304 * $_[3] = $user_data;
1305 */
1306 data_set_t *ds;
1307 value_list_t *vl;
1309 AV *pds = newAV ();
1310 HV *pvl = newHV ();
1312 ds = va_arg (ap, data_set_t *);
1313 vl = va_arg (ap, value_list_t *);
1314 meta = va_arg (ap, notification_meta_t **);
1316 if (0 != data_set2av (aTHX_ ds, pds)) {
1317 av_clear (pds);
1318 av_undef (pds);
1319 pds = (AV *)&PL_sv_undef;
1320 ret = -1;
1321 }
1323 if (0 != value_list2hv (aTHX_ vl, ds, pvl)) {
1324 hv_clear (pvl);
1325 hv_undef (pvl);
1326 pvl = (HV *)&PL_sv_undef;
1327 ret = -1;
1328 }
1330 if (NULL != meta) {
1331 pmeta = newAV ();
1333 if (0 != notification_meta2av (aTHX_ *meta, pmeta)) {
1334 av_clear (pmeta);
1335 av_undef (pmeta);
1336 pmeta = (AV *)&PL_sv_undef;
1337 ret = -1;
1338 }
1339 }
1340 else {
1341 pmeta = (AV *)&PL_sv_undef;
1342 }
1344 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pds)));
1345 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pvl)));
1346 XPUSHs (sv_2mortal (newRV_noinc ((SV *)pmeta)));
1347 }
1349 XPUSHs (sv_2mortal (newRV_inc (data->user_data)));
1351 PUTBACK;
1353 retvals = call_pv ("Collectd::fc_call", G_SCALAR);
1355 if ((FC_CB_EXEC == cb_type) && (meta != NULL)) {
1356 assert (pmeta != NULL);
1358 plugin_notification_meta_free (*meta);
1359 av2notification_meta (aTHX_ pmeta, meta);
1360 }
1362 SPAGAIN;
1363 if (0 < retvals) {
1364 SV *tmp = POPs;
1366 /* the exec callbacks return a status, while
1367 * the others return a boolean value */
1368 if (FC_CB_EXEC == cb_type)
1369 ret = SvIV (tmp);
1370 else if (! SvTRUE (tmp))
1371 ret = -1;
1372 }
1374 PUTBACK;
1375 FREETMPS;
1376 LEAVE;
1378 va_end (ap);
1379 return ret;
1380 } /* static int fc_call (int, int, pfc_user_data_t *, ...) */
1382 static int fc_create (int type, const oconfig_item_t *ci, void **user_data)
1383 {
1384 pfc_user_data_t *data;
1386 int ret = 0;
1388 dTHX;
1390 if (NULL == perl_threads)
1391 return 0;
1393 if (NULL == aTHX) {
1394 c_ithread_t *t = NULL;
1396 pthread_mutex_lock (&perl_threads->mutex);
1397 t = c_ithread_create (perl_threads->head->interp);
1398 pthread_mutex_unlock (&perl_threads->mutex);
1400 aTHX = t->interp;
1401 }
1403 log_debug ("fc_create: c_ithread: interp = %p (active threads: %i)",
1404 aTHX, perl_threads->number_of_threads);
1406 if ((1 != ci->values_num)
1407 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
1408 log_warn ("A \"%s\" block expects a single string argument.",
1409 (FC_MATCH == type) ? "Match" : "Target");
1410 return -1;
1411 }
1413 data = (pfc_user_data_t *)smalloc (sizeof (*data));
1414 data->name = sstrdup (ci->values[0].value.string);
1415 data->user_data = newSV (0);
1417 ret = fc_call (aTHX_ type, FC_CB_CREATE, data, ci);
1419 if (0 != ret)
1420 PFC_USER_DATA_FREE (data);
1421 else
1422 *user_data = data;
1423 return ret;
1424 } /* static int fc_create (int, const oconfig_item_t *, void **) */
1426 static int fc_destroy (int type, void **user_data)
1427 {
1428 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1430 int ret = 0;
1432 dTHX;
1434 if ((NULL == perl_threads) || (NULL == data))
1435 return 0;
1437 if (NULL == aTHX) {
1438 c_ithread_t *t = NULL;
1440 pthread_mutex_lock (&perl_threads->mutex);
1441 t = c_ithread_create (perl_threads->head->interp);
1442 pthread_mutex_unlock (&perl_threads->mutex);
1444 aTHX = t->interp;
1445 }
1447 log_debug ("fc_destroy: c_ithread: interp = %p (active threads: %i)",
1448 aTHX, perl_threads->number_of_threads);
1450 ret = fc_call (aTHX_ type, FC_CB_DESTROY, data);
1452 PFC_USER_DATA_FREE (data);
1453 *user_data = NULL;
1454 return ret;
1455 } /* static int fc_destroy (int, void **) */
1457 static int fc_exec (int type, const data_set_t *ds, const value_list_t *vl,
1458 notification_meta_t **meta, void **user_data)
1459 {
1460 pfc_user_data_t *data = *(pfc_user_data_t **)user_data;
1462 dTHX;
1464 if (NULL == perl_threads)
1465 return 0;
1467 assert (NULL != data);
1469 if (NULL == aTHX) {
1470 c_ithread_t *t = NULL;
1472 pthread_mutex_lock (&perl_threads->mutex);
1473 t = c_ithread_create (perl_threads->head->interp);
1474 pthread_mutex_unlock (&perl_threads->mutex);
1476 aTHX = t->interp;
1477 }
1479 log_debug ("fc_exec: c_ithread: interp = %p (active threads: %i)",
1480 aTHX, perl_threads->number_of_threads);
1482 return fc_call (aTHX_ type, FC_CB_EXEC, data, ds, vl, meta);
1483 } /* static int fc_exec (int, const data_set_t *, const value_list_t *,
1484 notification_meta_t **, void **) */
1486 static int pmatch_create (const oconfig_item_t *ci, void **user_data)
1487 {
1488 return fc_create (FC_MATCH, ci, user_data);
1489 } /* static int pmatch_create (const oconfig_item_t *, void **) */
1491 static int pmatch_destroy (void **user_data)
1492 {
1493 return fc_destroy (FC_MATCH, user_data);
1494 } /* static int pmatch_destroy (void **) */
1496 static int pmatch_match (const data_set_t *ds, const value_list_t *vl,
1497 notification_meta_t **meta, void **user_data)
1498 {
1499 return fc_exec (FC_MATCH, ds, vl, meta, user_data);
1500 } /* static int pmatch_match (const data_set_t *, const value_list_t *,
1501 notification_meta_t **, void **) */
1503 static match_proc_t pmatch = {
1504 pmatch_create, pmatch_destroy, pmatch_match
1505 };
1507 static int ptarget_create (const oconfig_item_t *ci, void **user_data)
1508 {
1509 return fc_create (FC_TARGET, ci, user_data);
1510 } /* static int ptarget_create (const oconfig_item_t *, void **) */
1512 static int ptarget_destroy (void **user_data)
1513 {
1514 return fc_destroy (FC_TARGET, user_data);
1515 } /* static int ptarget_destroy (void **) */
1517 static int ptarget_invoke (const data_set_t *ds, value_list_t *vl,
1518 notification_meta_t **meta, void **user_data)
1519 {
1520 return fc_exec (FC_TARGET, ds, vl, meta, user_data);
1521 } /* static int ptarget_invoke (const data_set_t *, value_list_t *,
1522 notification_meta_t **, void **) */
1524 static target_proc_t ptarget = {
1525 ptarget_create, ptarget_destroy, ptarget_invoke
1526 };
1528 /*
1529 * Exported Perl API.
1530 */
1532 /*
1533 * Collectd::plugin_register_data_set (type, dataset).
1534 *
1535 * type:
1536 * type of the dataset
1537 *
1538 * dataset:
1539 * dataset to be registered
1540 */
1541 static XS (Collectd_plugin_register_ds)
1542 {
1543 SV *data = NULL;
1544 int ret = 0;
1546 dXSARGS;
1548 log_warn ("Using plugin_register() to register new data-sets is "
1549 "deprecated - add new entries to a custom types.db instead.");
1551 if (2 != items) {
1552 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
1553 XSRETURN_EMPTY;
1554 }
1556 log_debug ("Collectd::plugin_register_data_set: "
1557 "type = \"%s\", dataset = \"%s\"",
1558 SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
1560 data = ST (1);
1562 if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
1563 ret = pplugin_register_data_set (aTHX_ SvPV_nolen (ST (0)),
1564 (AV *)SvRV (data));
1565 }
1566 else {
1567 log_err ("Collectd::plugin_register_data_set: Invalid data.");
1568 XSRETURN_EMPTY;
1569 }
1571 if (0 == ret)
1572 XSRETURN_YES;
1573 else
1574 XSRETURN_EMPTY;
1575 } /* static XS (Collectd_plugin_register_ds) */
1577 /*
1578 * Collectd::plugin_unregister_data_set (type).
1579 *
1580 * type:
1581 * type of the dataset
1582 */
1583 static XS (Collectd_plugin_unregister_ds)
1584 {
1585 dXSARGS;
1587 if (1 != items) {
1588 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
1589 XSRETURN_EMPTY;
1590 }
1592 log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
1593 SvPV_nolen (ST (0)));
1595 if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (0))))
1596 XSRETURN_YES;
1597 else
1598 XSRETURN_EMPTY;
1599 } /* static XS (Collectd_plugin_register_ds) */
1601 /*
1602 * Collectd::plugin_dispatch_values (name, values).
1603 *
1604 * name:
1605 * name of the plugin
1606 *
1607 * values:
1608 * value list to submit
1609 */
1610 static XS (Collectd_plugin_dispatch_values)
1611 {
1612 SV *values = NULL;
1613 int values_idx = 0;
1615 int ret = 0;
1617 dXSARGS;
1619 if (2 == items) {
1620 log_warn ("Collectd::plugin_dispatch_values with two arguments "
1621 "is deprecated - pass the type through values->{type}.");
1622 values_idx = 1;
1623 }
1624 else if (1 != items) {
1625 log_err ("Usage: Collectd::plugin_dispatch_values(values)");
1626 XSRETURN_EMPTY;
1627 }
1629 log_debug ("Collectd::plugin_dispatch_values: values=\"%s\"",
1630 SvPV_nolen (ST (values_idx)));
1632 values = ST (values_idx);
1634 if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
1635 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
1636 XSRETURN_EMPTY;
1637 }
1639 if (((2 == items) && (NULL == ST (0))) || (NULL == values))
1640 XSRETURN_EMPTY;
1642 if ((2 == items) && (NULL == hv_store ((HV *)SvRV (values), "type", 4,
1643 newSVsv (ST (0)), 0))) {
1644 log_err ("Collectd::plugin_dispatch_values: Could not store type.");
1645 XSRETURN_EMPTY;
1646 }
1648 ret = pplugin_dispatch_values (aTHX_ (HV *)SvRV (values));
1650 if (0 == ret)
1651 XSRETURN_YES;
1652 else
1653 XSRETURN_EMPTY;
1654 } /* static XS (Collectd_plugin_dispatch_values) */
1656 /* Collectd::plugin_write (plugin, ds, vl).
1657 *
1658 * plugin:
1659 * name of the plugin to call, may be 'undef'
1660 *
1661 * ds:
1662 * data-set that describes the submitted values, may be 'undef'
1663 *
1664 * vl:
1665 * value-list to be written
1666 */
1667 static XS (Collectd__plugin_write)
1668 {
1669 char *plugin;
1670 SV *ds, *vl;
1671 AV *ds_array;
1673 int ret;
1675 dXSARGS;
1677 if (3 != items) {
1678 log_err ("Usage: Collectd::plugin_write(plugin, ds, vl)");
1679 XSRETURN_EMPTY;
1680 }
1682 log_debug ("Collectd::plugin_write: plugin=\"%s\", ds=\"%s\", vl=\"%s\"",
1683 SvPV_nolen (ST (0)), SvOK (ST (1)) ? SvPV_nolen (ST (1)) : "",
1684 SvPV_nolen (ST (2)));
1686 if (! SvOK (ST (0)))
1687 plugin = NULL;
1688 else
1689 plugin = SvPV_nolen (ST (0));
1691 ds = ST (1);
1692 if (SvROK (ds) && (SVt_PVAV == SvTYPE (SvRV (ds))))
1693 ds_array = (AV *)SvRV (ds);
1694 else if (! SvOK (ds))
1695 ds_array = NULL;
1696 else {
1697 log_err ("Collectd::plugin_write: Invalid data-set.");
1698 XSRETURN_EMPTY;
1699 }
1701 vl = ST (2);
1702 if (! (SvROK (vl) && (SVt_PVHV == SvTYPE (SvRV (vl))))) {
1703 log_err ("Collectd::plugin_write: Invalid value-list.");
1704 XSRETURN_EMPTY;
1705 }
1707 ret = pplugin_write (aTHX_ plugin, ds_array, (HV *)SvRV (vl));
1709 if (0 == ret)
1710 XSRETURN_YES;
1711 else
1712 XSRETURN_EMPTY;
1713 } /* static XS (Collectd__plugin_write) */
1715 /*
1716 * Collectd::_plugin_flush (plugin, timeout, identifier).
1717 *
1718 * plugin:
1719 * name of the plugin to flush
1720 *
1721 * timeout:
1722 * timeout to use when flushing the data
1723 *
1724 * identifier:
1725 * data-set identifier to flush
1726 */
1727 static XS (Collectd__plugin_flush)
1728 {
1729 char *plugin = NULL;
1730 int timeout = -1;
1731 char *id = NULL;
1733 dXSARGS;
1735 if (3 != items) {
1736 log_err ("Usage: Collectd::_plugin_flush(plugin, timeout, id)");
1737 XSRETURN_EMPTY;
1738 }
1740 if (SvOK (ST (0)))
1741 plugin = SvPV_nolen (ST (0));
1743 if (SvOK (ST (1)))
1744 timeout = (int)SvIV (ST (1));
1746 if (SvOK (ST (2)))
1747 id = SvPV_nolen (ST (2));
1749 log_debug ("Collectd::_plugin_flush: plugin = \"%s\", timeout = %i, "
1750 "id = \"%s\"", plugin, timeout, id);
1752 if (0 == plugin_flush (plugin, timeout, id))
1753 XSRETURN_YES;
1754 else
1755 XSRETURN_EMPTY;
1756 } /* static XS (Collectd__plugin_flush) */
1758 /*
1759 * Collectd::plugin_dispatch_notification (notif).
1760 *
1761 * notif:
1762 * notification to dispatch
1763 */
1764 static XS (Collectd_plugin_dispatch_notification)
1765 {
1766 SV *notif = NULL;
1768 int ret = 0;
1770 dXSARGS;
1772 if (1 != items) {
1773 log_err ("Usage: Collectd::plugin_dispatch_notification(notif)");
1774 XSRETURN_EMPTY;
1775 }
1777 log_debug ("Collectd::plugin_dispatch_notification: notif = \"%s\"",
1778 SvPV_nolen (ST (0)));
1780 notif = ST (0);
1782 if (! (SvROK (notif) && (SVt_PVHV == SvTYPE (SvRV (notif))))) {
1783 log_err ("Collectd::plugin_dispatch_notification: Invalid notif.");
1784 XSRETURN_EMPTY;
1785 }
1787 ret = pplugin_dispatch_notification (aTHX_ (HV *)SvRV (notif));
1789 if (0 == ret)
1790 XSRETURN_YES;
1791 else
1792 XSRETURN_EMPTY;
1793 } /* static XS (Collectd_plugin_dispatch_notification) */
1795 /*
1796 * Collectd::plugin_log (level, message).
1797 *
1798 * level:
1799 * log level (LOG_DEBUG, ... LOG_ERR)
1800 *
1801 * message:
1802 * log message
1803 */
1804 static XS (Collectd_plugin_log)
1805 {
1806 dXSARGS;
1808 if (2 != items) {
1809 log_err ("Usage: Collectd::plugin_log(level, message)");
1810 XSRETURN_EMPTY;
1811 }
1813 plugin_log (SvIV (ST (0)), "%s", SvPV_nolen (ST (1)));
1814 XSRETURN_YES;
1815 } /* static XS (Collectd_plugin_log) */
1817 /*
1818 * Collectd::_fc_register (type, name)
1819 *
1820 * type:
1821 * match | target
1822 *
1823 * name:
1824 * name of the match
1825 */
1826 static XS (Collectd__fc_register)
1827 {
1828 int type;
1829 char *name;
1831 int ret = 0;
1833 dXSARGS;
1835 if (2 != items) {
1836 log_err ("Usage: Collectd::_fc_register(type, name)");
1837 XSRETURN_EMPTY;
1838 }
1840 type = SvIV (ST (0));
1841 name = SvPV_nolen (ST (1));
1843 if (FC_MATCH == type)
1844 ret = fc_register_match (name, pmatch);
1845 else if (FC_TARGET == type)
1846 ret = fc_register_target (name, ptarget);
1848 if (0 == ret)
1849 XSRETURN_YES;
1850 else
1851 XSRETURN_EMPTY;
1852 } /* static XS (Collectd_fc_register) */
1854 /*
1855 * Collectd::call_by_name (...).
1856 *
1857 * Call a Perl sub identified by its name passed through $Collectd::cb_name.
1858 */
1859 static XS (Collectd_call_by_name)
1860 {
1861 SV *tmp = NULL;
1862 char *name = NULL;
1864 if (NULL == (tmp = get_sv ("Collectd::cb_name", 0))) {
1865 sv_setpv (get_sv ("@", 1), "cb_name has not been set");
1866 CLEAR_STACK_FRAME;
1867 return;
1868 }
1870 name = SvPV_nolen (tmp);
1872 if (NULL == get_cv (name, 0)) {
1873 sv_setpvf (get_sv ("@", 1), "unknown callback \"%s\"", name);
1874 CLEAR_STACK_FRAME;
1875 return;
1876 }
1878 /* simply pass on the subroutine call without touching the stack,
1879 * thus leaving any arguments and return values in place */
1880 call_pv (name, 0);
1881 } /* static XS (Collectd_call_by_name) */
1883 /*
1884 * Interface to collectd.
1885 */
1887 static int perl_init (void)
1888 {
1889 dTHX;
1891 if (NULL == perl_threads)
1892 return 0;
1894 if (NULL == aTHX) {
1895 c_ithread_t *t = NULL;
1897 pthread_mutex_lock (&perl_threads->mutex);
1898 t = c_ithread_create (perl_threads->head->interp);
1899 pthread_mutex_unlock (&perl_threads->mutex);
1901 aTHX = t->interp;
1902 }
1904 log_debug ("perl_init: c_ithread: interp = %p (active threads: %i)",
1905 aTHX, perl_threads->number_of_threads);
1906 return pplugin_call_all (aTHX_ PLUGIN_INIT);
1907 } /* static int perl_init (void) */
1909 static int perl_read (void)
1910 {
1911 dTHX;
1913 if (NULL == perl_threads)
1914 return 0;
1916 if (NULL == aTHX) {
1917 c_ithread_t *t = NULL;
1919 pthread_mutex_lock (&perl_threads->mutex);
1920 t = c_ithread_create (perl_threads->head->interp);
1921 pthread_mutex_unlock (&perl_threads->mutex);
1923 aTHX = t->interp;
1924 }
1926 log_debug ("perl_read: c_ithread: interp = %p (active threads: %i)",
1927 aTHX, perl_threads->number_of_threads);
1928 return pplugin_call_all (aTHX_ PLUGIN_READ);
1929 } /* static int perl_read (void) */
1931 static int perl_write (const data_set_t *ds, const value_list_t *vl,
1932 user_data_t __attribute__((unused)) *user_data)
1933 {
1934 dTHX;
1936 if (NULL == perl_threads)
1937 return 0;
1939 if (NULL == aTHX) {
1940 c_ithread_t *t = NULL;
1942 pthread_mutex_lock (&perl_threads->mutex);
1943 t = c_ithread_create (perl_threads->head->interp);
1944 pthread_mutex_unlock (&perl_threads->mutex);
1946 aTHX = t->interp;
1947 }
1949 log_debug ("perl_write: c_ithread: interp = %p (active threads: %i)",
1950 aTHX, perl_threads->number_of_threads);
1951 return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl);
1952 } /* static int perl_write (const data_set_t *, const value_list_t *) */
1954 static void perl_log (int level, const char *msg,
1955 user_data_t __attribute__((unused)) *user_data)
1956 {
1957 dTHX;
1959 if (NULL == perl_threads)
1960 return;
1962 if (NULL == aTHX) {
1963 c_ithread_t *t = NULL;
1965 pthread_mutex_lock (&perl_threads->mutex);
1966 t = c_ithread_create (perl_threads->head->interp);
1967 pthread_mutex_unlock (&perl_threads->mutex);
1969 aTHX = t->interp;
1970 }
1972 pplugin_call_all (aTHX_ PLUGIN_LOG, level, msg);
1973 return;
1974 } /* static void perl_log (int, const char *) */
1976 static int perl_notify (const notification_t *notif,
1977 user_data_t __attribute__((unused)) *user_data)
1978 {
1979 dTHX;
1981 if (NULL == perl_threads)
1982 return 0;
1984 if (NULL == aTHX) {
1985 c_ithread_t *t = NULL;
1987 pthread_mutex_lock (&perl_threads->mutex);
1988 t = c_ithread_create (perl_threads->head->interp);
1989 pthread_mutex_unlock (&perl_threads->mutex);
1991 aTHX = t->interp;
1992 }
1993 return pplugin_call_all (aTHX_ PLUGIN_NOTIF, notif);
1994 } /* static int perl_notify (const notification_t *) */
1996 static int perl_flush (int timeout, const char *identifier,
1997 user_data_t __attribute__((unused)) *user_data)
1998 {
1999 dTHX;
2001 if (NULL == perl_threads)
2002 return 0;
2004 if (NULL == aTHX) {
2005 c_ithread_t *t = NULL;
2007 pthread_mutex_lock (&perl_threads->mutex);
2008 t = c_ithread_create (perl_threads->head->interp);
2009 pthread_mutex_unlock (&perl_threads->mutex);
2011 aTHX = t->interp;
2012 }
2013 return pplugin_call_all (aTHX_ PLUGIN_FLUSH, timeout, identifier);
2014 } /* static int perl_flush (const int) */
2016 static int perl_shutdown (void)
2017 {
2018 c_ithread_t *t = NULL;
2020 int ret = 0;
2022 dTHX;
2024 plugin_unregister_complex_config ("perl");
2026 if (NULL == perl_threads)
2027 return 0;
2029 if (NULL == aTHX) {
2030 c_ithread_t *t = NULL;
2032 pthread_mutex_lock (&perl_threads->mutex);
2033 t = c_ithread_create (perl_threads->head->interp);
2034 pthread_mutex_unlock (&perl_threads->mutex);
2036 aTHX = t->interp;
2037 }
2039 log_debug ("perl_shutdown: c_ithread: interp = %p (active threads: %i)",
2040 aTHX, perl_threads->number_of_threads);
2042 plugin_unregister_log ("perl");
2043 plugin_unregister_notification ("perl");
2044 plugin_unregister_init ("perl");
2045 plugin_unregister_read ("perl");
2046 plugin_unregister_write ("perl");
2047 plugin_unregister_flush ("perl");
2049 ret = pplugin_call_all (aTHX_ PLUGIN_SHUTDOWN);
2051 pthread_mutex_lock (&perl_threads->mutex);
2052 t = perl_threads->tail;
2054 while (NULL != t) {
2055 c_ithread_t *thr = t;
2057 /* the pointer has to be advanced before destroying
2058 * the thread as this will free the memory */
2059 t = t->prev;
2061 c_ithread_destroy (thr);
2062 }
2064 pthread_mutex_unlock (&perl_threads->mutex);
2065 pthread_mutex_destroy (&perl_threads->mutex);
2067 sfree (perl_threads);
2069 pthread_key_delete (perl_thr_key);
2071 PERL_SYS_TERM ();
2073 plugin_unregister_shutdown ("perl");
2074 return ret;
2075 } /* static void perl_shutdown (void) */
2077 /*
2078 * Access functions for global variables.
2079 *
2080 * These functions implement the "magic" used to access
2081 * the global variables from Perl.
2082 */
2084 static int g_pv_get (pTHX_ SV *var, MAGIC *mg)
2085 {
2086 char *pv = mg->mg_ptr;
2087 sv_setpv (var, pv);
2088 return 0;
2089 } /* static int g_pv_get (pTHX_ SV *, MAGIC *) */
2091 static int g_pv_set (pTHX_ SV *var, MAGIC *mg)
2092 {
2093 char *pv = mg->mg_ptr;
2094 sstrncpy (pv, SvPV_nolen (var), DATA_MAX_NAME_LEN);
2095 return 0;
2096 } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */
2098 static int g_iv_get (pTHX_ SV *var, MAGIC *mg)
2099 {
2100 int *iv = (int *)mg->mg_ptr;
2101 sv_setiv (var, *iv);
2102 return 0;
2103 } /* static int g_iv_get (pTHX_ SV *, MAGIC *) */
2105 static int g_iv_set (pTHX_ SV *var, MAGIC *mg)
2106 {
2107 int *iv = (int *)mg->mg_ptr;
2108 *iv = (int)SvIV (var);
2109 return 0;
2110 } /* static int g_iv_set (pTHX_ SV *, MAGIC *) */
2112 static MGVTBL g_pv_vtbl = {
2113 g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL
2114 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2115 , NULL
2116 #endif
2117 };
2118 static MGVTBL g_iv_vtbl = {
2119 g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL
2120 #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL
2121 , NULL
2122 #endif
2123 };
2125 /* bootstrap the Collectd module */
2126 static void xs_init (pTHX)
2127 {
2128 HV *stash = NULL;
2129 SV *tmp = NULL;
2130 char *file = __FILE__;
2132 int i = 0;
2134 dXSUB_SYS;
2136 /* enable usage of Perl modules using shared libraries */
2137 newXS ("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2139 /* register API */
2140 for (i = 0; NULL != api[i].f; ++i)
2141 newXS (api[i].name, api[i].f, file);
2143 stash = gv_stashpv ("Collectd", 1);
2145 /* export "constants" */
2146 for (i = 0; '\0' != constants[i].name[0]; ++i)
2147 newCONSTSUB (stash, constants[i].name, newSViv (constants[i].value));
2149 /* export global variables
2150 * by adding "magic" to the SV's representing the globale variables
2151 * perl is able to automagically call the get/set function when
2152 * accessing any such variable (this is basically the same as using
2153 * tie() in Perl) */
2154 /* global strings */
2155 for (i = 0; '\0' != g_strings[i].name[0]; ++i) {
2156 tmp = get_sv (g_strings[i].name, 1);
2157 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_pv_vtbl,
2158 g_strings[i].var, 0);
2159 }
2161 /* global integers */
2162 for (i = 0; '\0' != g_integers[i].name[0]; ++i) {
2163 tmp = get_sv (g_integers[i].name, 1);
2164 sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl,
2165 (char *)g_integers[i].var, 0);
2166 }
2167 return;
2168 } /* static void xs_init (pTHX) */
2170 /* Initialize the global Perl interpreter. */
2171 static int init_pi (int argc, char **argv)
2172 {
2173 dTHXa (NULL);
2175 if (NULL != perl_threads)
2176 return 0;
2178 log_info ("Initializing Perl interpreter...");
2179 #if COLLECT_DEBUG
2180 {
2181 int i = 0;
2183 for (i = 0; i < argc; ++i)
2184 log_debug ("argv[%i] = \"%s\"", i, argv[i]);
2185 }
2186 #endif /* COLLECT_DEBUG */
2188 if (0 != pthread_key_create (&perl_thr_key, c_ithread_destructor)) {
2189 log_err ("init_pi: pthread_key_create failed");
2191 /* this must not happen - cowardly giving up if it does */
2192 return -1;
2193 }
2195 #ifdef __FreeBSD__
2196 /* On FreeBSD, PERL_SYS_INIT3 expands to some expression which
2197 * triggers a "value computed is not used" warning by gcc. */
2198 (void)
2199 #endif
2200 PERL_SYS_INIT3 (&argc, &argv, &environ);
2202 perl_threads = (c_ithread_list_t *)smalloc (sizeof (c_ithread_list_t));
2203 memset (perl_threads, 0, sizeof (c_ithread_list_t));
2205 pthread_mutex_init (&perl_threads->mutex, NULL);
2206 /* locking the mutex should not be necessary at this point
2207 * but let's just do it for the sake of completeness */
2208 pthread_mutex_lock (&perl_threads->mutex);
2210 perl_threads->head = c_ithread_create (NULL);
2211 perl_threads->tail = perl_threads->head;
2213 if (NULL == (perl_threads->head->interp = perl_alloc ())) {
2214 log_err ("init_pi: Not enough memory.");
2215 exit (3);
2216 }
2218 aTHX = perl_threads->head->interp;
2219 pthread_mutex_unlock (&perl_threads->mutex);
2221 perl_construct (aTHX);
2223 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
2225 if (0 != perl_parse (aTHX_ xs_init, argc, argv, NULL)) {
2226 SV *err = get_sv ("@", 1);
2227 log_err ("init_pi: Unable to bootstrap Collectd: %s",
2228 SvPV_nolen (err));
2230 perl_destruct (perl_threads->head->interp);
2231 perl_free (perl_threads->head->interp);
2232 sfree (perl_threads);
2234 pthread_key_delete (perl_thr_key);
2235 return -1;
2236 }
2238 /* Set $0 to "collectd" because perl_parse() has to set it to "-e". */
2239 sv_setpv (get_sv ("0", 0), "collectd");
2241 perl_run (aTHX);
2243 plugin_register_log ("perl", perl_log, /* user_data = */ NULL);
2244 plugin_register_notification ("perl", perl_notify,
2245 /* user_data = */ NULL);
2246 plugin_register_init ("perl", perl_init);
2248 plugin_register_read ("perl", perl_read);
2250 plugin_register_write ("perl", perl_write, /* user_data = */ NULL);
2251 plugin_register_flush ("perl", perl_flush, /* user_data = */ NULL);
2252 plugin_register_shutdown ("perl", perl_shutdown);
2253 return 0;
2254 } /* static int init_pi (const char **, const int) */
2256 /*
2257 * LoadPlugin "<Plugin>"
2258 */
2259 static int perl_config_loadplugin (pTHX_ oconfig_item_t *ci)
2260 {
2261 char module_name[DATA_MAX_NAME_LEN];
2263 char *value = NULL;
2265 if ((0 != ci->children_num) || (1 != ci->values_num)
2266 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2267 log_err ("LoadPlugin expects a single string argument.");
2268 return 1;
2269 }
2271 value = ci->values[0].value.string;
2273 if (NULL == get_module_name (module_name, sizeof (module_name), value)) {
2274 log_err ("Invalid module name %s", value);
2275 return (1);
2276 }
2278 if (0 != init_pi (perl_argc, perl_argv))
2279 return -1;
2281 assert (NULL != perl_threads);
2282 assert (NULL != perl_threads->head);
2284 aTHX = perl_threads->head->interp;
2286 log_debug ("perl_config: loading perl plugin \"%s\"", value);
2287 load_module (PERL_LOADMOD_NOIMPORT,
2288 newSVpv (module_name, strlen (module_name)), Nullsv);
2289 return 0;
2290 } /* static int perl_config_loadplugin (oconfig_item_it *) */
2292 /*
2293 * BaseName "<Name>"
2294 */
2295 static int perl_config_basename (pTHX_ oconfig_item_t *ci)
2296 {
2297 char *value = NULL;
2299 if ((0 != ci->children_num) || (1 != ci->values_num)
2300 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2301 log_err ("BaseName expects a single string argument.");
2302 return 1;
2303 }
2305 value = ci->values[0].value.string;
2307 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
2308 sstrncpy (base_name, value, sizeof (base_name));
2309 return 0;
2310 } /* static int perl_config_basename (oconfig_item_it *) */
2312 /*
2313 * EnableDebugger "<Package>"|""
2314 */
2315 static int perl_config_enabledebugger (pTHX_ oconfig_item_t *ci)
2316 {
2317 char *value = NULL;
2319 if ((0 != ci->children_num) || (1 != ci->values_num)
2320 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2321 log_err ("EnableDebugger expects a single string argument.");
2322 return 1;
2323 }
2325 if (NULL != perl_threads) {
2326 log_warn ("EnableDebugger has no effects if used after LoadPlugin.");
2327 return 1;
2328 }
2330 value = ci->values[0].value.string;
2332 perl_argv = (char **)realloc (perl_argv,
2333 (++perl_argc + 1) * sizeof (char *));
2335 if (NULL == perl_argv) {
2336 log_err ("perl_config: Not enough memory.");
2337 exit (3);
2338 }
2340 if ('\0' == value[0]) {
2341 perl_argv[perl_argc - 1] = "-d";
2342 }
2343 else {
2344 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 4);
2345 sstrncpy (perl_argv[perl_argc - 1], "-d:", 4);
2346 sstrncpy (perl_argv[perl_argc - 1] + 3, value, strlen (value) + 1);
2347 }
2349 perl_argv[perl_argc] = NULL;
2350 return 0;
2351 } /* static int perl_config_enabledebugger (oconfig_item_it *) */
2353 /*
2354 * IncludeDir "<Dir>"
2355 */
2356 static int perl_config_includedir (pTHX_ oconfig_item_t *ci)
2357 {
2358 char *value = NULL;
2360 if ((0 != ci->children_num) || (1 != ci->values_num)
2361 || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2362 log_err ("IncludeDir expects a single string argument.");
2363 return 1;
2364 }
2366 value = ci->values[0].value.string;
2368 if (NULL == aTHX) {
2369 perl_argv = (char **)realloc (perl_argv,
2370 (++perl_argc + 1) * sizeof (char *));
2372 if (NULL == perl_argv) {
2373 log_err ("perl_config: Not enough memory.");
2374 exit (3);
2375 }
2377 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
2378 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
2379 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
2381 perl_argv[perl_argc] = NULL;
2382 }
2383 else {
2384 /* prepend the directory to @INC */
2385 av_unshift (GvAVn (PL_incgv), 1);
2386 av_store (GvAVn (PL_incgv), 0, newSVpv (value, strlen (value)));
2387 }
2388 return 0;
2389 } /* static int perl_config_includedir (oconfig_item_it *) */
2391 /*
2392 * <Plugin> block
2393 */
2394 static int perl_config_plugin (pTHX_ oconfig_item_t *ci)
2395 {
2396 int retvals = 0;
2397 int ret = 0;
2399 char *plugin;
2400 HV *config;
2402 dSP;
2404 if ((1 != ci->values_num) || (OCONFIG_TYPE_STRING != ci->values[0].type)) {
2405 log_err ("LoadPlugin expects a single string argument.");
2406 return 1;
2407 }
2409 plugin = ci->values[0].value.string;
2410 config = newHV ();
2412 if (0 != oconfig_item2hv (aTHX_ ci, config)) {
2413 hv_clear (config);
2414 hv_undef (config);
2416 log_err ("Unable to convert configuration to a Perl hash value.");
2417 config = (HV *)&PL_sv_undef;
2418 }
2420 ENTER;
2421 SAVETMPS;
2423 PUSHMARK (SP);
2425 XPUSHs (sv_2mortal (newSVpv (plugin, 0)));
2426 XPUSHs (sv_2mortal (newRV_noinc ((SV *)config)));
2428 PUTBACK;
2430 retvals = call_pv ("Collectd::_plugin_dispatch_config", G_SCALAR);
2432 SPAGAIN;
2433 if (0 < retvals) {
2434 SV *tmp = POPs;
2435 if (! SvTRUE (tmp))
2436 ret = 1;
2437 }
2438 else
2439 ret = 1;
2441 PUTBACK;
2442 FREETMPS;
2443 LEAVE;
2444 return ret;
2445 } /* static int perl_config_plugin (oconfig_item_it *) */
2447 static int perl_config (oconfig_item_t *ci)
2448 {
2449 int status = 0;
2450 int i = 0;
2452 dTHXa (NULL);
2454 for (i = 0; i < ci->children_num; ++i) {
2455 oconfig_item_t *c = ci->children + i;
2456 int current_status = 0;
2458 if (NULL != perl_threads)
2459 aTHX = PERL_GET_CONTEXT;
2461 if (0 == strcasecmp (c->key, "LoadPlugin"))
2462 current_status = perl_config_loadplugin (aTHX_ c);
2463 else if (0 == strcasecmp (c->key, "BaseName"))
2464 current_status = perl_config_basename (aTHX_ c);
2465 else if (0 == strcasecmp (c->key, "EnableDebugger"))
2466 current_status = perl_config_enabledebugger (aTHX_ c);
2467 else if (0 == strcasecmp (c->key, "IncludeDir"))
2468 current_status = perl_config_includedir (aTHX_ c);
2469 else if (0 == strcasecmp (c->key, "Plugin"))
2470 current_status = perl_config_plugin (aTHX_ c);
2471 else
2472 {
2473 log_warn ("Ignoring unknown config key \"%s\".", c->key);
2474 current_status = 0;
2475 }
2477 /* fatal error - it's up to perl_config_* to clean up */
2478 if (0 > current_status) {
2479 log_err ("Configuration failed with a fatal error - "
2480 "plugin disabled!");
2481 return current_status;
2482 }
2484 status += current_status;
2485 }
2486 return status;
2487 } /* static int perl_config (oconfig_item_t *) */
2489 void module_register (void)
2490 {
2491 perl_argc = 4;
2492 perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
2494 /* default options for the Perl interpreter */
2495 perl_argv[0] = "";
2496 perl_argv[1] = "-MCollectd";
2497 perl_argv[2] = "-e";
2498 perl_argv[3] = "1";
2499 perl_argv[4] = NULL;
2501 plugin_register_complex_config ("perl", perl_config);
2502 return;
2503 } /* void module_register (void) */
2505 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */