perl plugin: Fixed an "unused variable" warning.
[collectd.git] / src / perl.c
1 /**
2  * collectd - src/perl.c
3  * Copyright (C) 2007  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  **/
21
22 /*
23  * This plugin embeds a Perl interpreter into collectd and provides an
24  * interface for collectd plugins written in perl.
25  */
26
27 #include "collectd.h"
28
29 #include "configfile.h"
30
31 #include <EXTERN.h>
32 #include <perl.h>
33
34 #include <XSUB.h>
35
36 /* Some versions of Perl define their own version of DEBUG... :-/ */
37 #ifdef DEBUG
38 # undef DEBUG
39 #endif /* DEBUG */
40
41 /* ... while we want the definition found in plugin.h. */
42 #include "plugin.h"
43 #include "common.h"
44
45 #define PLUGIN_INIT     0
46 #define PLUGIN_READ     1
47 #define PLUGIN_WRITE    2
48 #define PLUGIN_SHUTDOWN 3
49 #define PLUGIN_LOG      4
50
51 #define PLUGIN_TYPES    5
52
53 #define PLUGIN_DATASET  255
54
55 #define log_debug(...) DEBUG ("perl: " __VA_ARGS__)
56 #define log_info(...) INFO ("perl: " __VA_ARGS__)
57 #define log_warn(...) WARNING ("perl: " __VA_ARGS__)
58 #define log_err(...) ERROR ("perl: " __VA_ARGS__)
59
60
61 /* this is defined in DynaLoader.a */
62 void boot_DynaLoader (PerlInterpreter *, CV *);
63
64 static XS (Collectd_plugin_register_ds);
65 static XS (Collectd_plugin_unregister_ds);
66 static XS (Collectd_plugin_dispatch_values);
67 static XS (Collectd_plugin_log);
68
69
70 /*
71  * private data types
72  */
73
74 typedef struct {
75         int len;
76         int *values;
77 } ds_types_t;
78
79
80 /*
81  * private variables
82  */
83
84 /* valid configuration file keys */
85 static const char *config_keys[] =
86 {
87         "LoadPlugin",
88         "BaseName",
89         "IncludeDir"
90 };
91 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
92
93 static PerlInterpreter *perl = NULL;
94
95 static int  perl_argc   = 0;
96 static char **perl_argv = NULL;
97
98 static char base_name[DATA_MAX_NAME_LEN] = "";
99
100 static HV   *data_sets;
101
102 static struct {
103         char name[64];
104         XS ((*f));
105 } api[] =
106 {
107         { "Collectd::plugin_register_data_set",   Collectd_plugin_register_ds },
108         { "Collectd::plugin_unregister_data_set", Collectd_plugin_unregister_ds },
109         { "Collectd::plugin_dispatch_values",     Collectd_plugin_dispatch_values },
110         { "Collectd::plugin_log",                 Collectd_plugin_log },
111         { "", NULL }
112 };
113
114 struct {
115         char name[64];
116         int  value;
117 } constants[] =
118 {
119         { "Collectd::TYPE_INIT",       PLUGIN_INIT },
120         { "Collectd::TYPE_READ",       PLUGIN_READ },
121         { "Collectd::TYPE_WRITE",      PLUGIN_WRITE },
122         { "Collectd::TYPE_SHUTDOWN",   PLUGIN_SHUTDOWN },
123         { "Collectd::TYPE_LOG",        PLUGIN_LOG },
124         { "Collectd::TYPE_DATASET",    PLUGIN_DATASET },
125         { "Collectd::DS_TYPE_COUNTER", DS_TYPE_COUNTER },
126         { "Collectd::DS_TYPE_GAUGE",   DS_TYPE_GAUGE },
127         { "Collectd::LOG_ERR",         LOG_ERR },
128         { "Collectd::LOG_WARNING",     LOG_WARNING },
129         { "Collectd::LOG_NOTICE",      LOG_NOTICE },
130         { "Collectd::LOG_INFO",        LOG_INFO },
131         { "Collectd::LOG_DEBUG",       LOG_DEBUG },
132         { "", 0 }
133 };
134
135
136 /*
137  * Helper functions for data type conversion.
138  */
139
140 /*
141  * data source:
142  * [
143  *   {
144  *     name => $ds_name,
145  *     type => $ds_type,
146  *     min  => $ds_min,
147  *     max  => $ds_max
148  *   },
149  *   ...
150  * ]
151  */
152 static int hv2data_source (HV *hash, data_source_t *ds)
153 {
154         SV **tmp = NULL;
155
156         if ((NULL == hash) || (NULL == ds))
157                 return -1;
158
159         if (NULL != (tmp = Perl_hv_fetch (perl, hash, "name", 4, 0))) {
160                 strncpy (ds->name, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
161                 ds->name[DATA_MAX_NAME_LEN - 1] = '\0';
162         }
163         else {
164                 log_err ("hv2data_source: No DS name given.");
165                 return -1;
166         }
167
168         if (NULL != (tmp = Perl_hv_fetch (perl, hash, "type", 4, 0))) {
169                 ds->type = SvIV (*tmp);
170
171                 if ((DS_TYPE_COUNTER != ds->type) && (DS_TYPE_GAUGE != ds->type)) {
172                         log_err ("hv2data_source: Invalid DS type.");
173                         return -1;
174                 }
175         }
176         else {
177                 ds->type = DS_TYPE_COUNTER;
178         }
179
180         if (NULL != (tmp = Perl_hv_fetch (perl, hash, "min", 3, 0)))
181                 ds->min = SvNV (*tmp);
182         else
183                 ds->min = NAN;
184
185         if (NULL != (tmp = Perl_hv_fetch (perl, hash, "max", 3, 0)))
186                 ds->max = SvNV (*tmp);
187         else
188                 ds->max = NAN;
189         return 0;
190 } /* static data_source_t *hv2data_source (HV *) */
191
192 static int av2value (char *name, AV *array, value_t *value, int len)
193 {
194         SV **tmp = NULL;
195
196         ds_types_t *ds = NULL;
197
198         int i = 0;
199
200         if ((NULL == name) || (NULL == array) || (NULL == value))
201                 return -1;
202
203         if (Perl_av_len (perl, array) < len - 1)
204                 len = Perl_av_len (perl, array) + 1;
205
206         if (0 >= len)
207                 return -1;
208
209         tmp = Perl_hv_fetch (perl, data_sets, name, strlen (name), 0);
210         if (NULL == tmp) {
211                 log_err ("av2value: No dataset for \"%s\".", name);
212                 return -1;
213         }
214         ds = (ds_types_t *)SvIV ((SV *)SvRV (*tmp));
215
216         if (ds->len < len) {
217                 log_warn ("av2value: Value length exceeds data set length.");
218                 len = ds->len;
219         }
220
221         for (i = 0; i < len; ++i) {
222                 SV **tmp = Perl_av_fetch (perl, array, i, 0);
223
224                 if (NULL != tmp) {
225                         if (DS_TYPE_COUNTER == ds->values[i])
226                                 value[i].counter = SvIV (*tmp);
227                         else
228                                 value[i].gauge = SvNV (*tmp);
229                 }
230                 else {
231                         return -1;
232                 }
233         }
234         return len;
235 } /* static int av2value (char *, AV *, value_t *, int) */
236
237 static int data_set2av (data_set_t *ds, AV *array)
238 {
239         int i = 0;
240
241         if ((NULL == ds) || (NULL == array))
242                 return -1;
243
244         Perl_av_extend (perl, array, ds->ds_num);
245
246         for (i = 0; i < ds->ds_num; ++i) {
247                 HV *source = Perl_newHV (perl);
248
249                 if (NULL == Perl_hv_store (perl, source, "name", 4,
250                                 Perl_newSVpv (perl, ds->ds[i].name, 0), 0))
251                         return -1;
252
253                 if (NULL == Perl_hv_store (perl, source, "type", 4,
254                                 Perl_newSViv (perl, ds->ds[i].type), 0))
255                         return -1;
256
257                 if (! isnan (ds->ds[i].min))
258                         if (NULL == Perl_hv_store (perl, source, "min", 3,
259                                         Perl_newSVnv (perl, ds->ds[i].min), 0))
260                                 return -1;
261
262                 if (! isnan (ds->ds[i].max))
263                         if (NULL == Perl_hv_store (perl, source, "max", 3,
264                                         Perl_newSVnv (perl, ds->ds[i].max), 0))
265                                 return -1;
266
267                 if (NULL == Perl_av_store (perl, array, i,
268                                 Perl_newRV_noinc (perl, (SV *)source)))
269                         return -1;
270         }
271         return 0;
272 } /* static int data_set2av (data_set_t *, AV *) */
273
274 static int value_list2hv (value_list_t *vl, data_set_t *ds, HV *hash)
275 {
276         AV *values = NULL;
277
278         int i   = 0;
279         int len = 0;
280
281         if ((NULL == vl) || (NULL == ds) || (NULL == hash))
282                 return -1;
283
284         len = vl->values_len;
285
286         if (ds->ds_num < len) {
287                 log_warn ("value2av: Value length exceeds data set length.");
288                 len = ds->ds_num;
289         }
290
291         values = Perl_newAV (perl);
292         Perl_av_extend (perl, values, len - 1);
293
294         for (i = 0; i < len; ++i) {
295                 SV *val = NULL;
296
297                 if (DS_TYPE_COUNTER == ds->ds[i].type)
298                         val = Perl_newSViv (perl, vl->values[i].counter);
299                 else
300                         val = Perl_newSVnv (perl, vl->values[i].gauge);
301
302                 if (NULL == Perl_av_store (perl, values, i, val)) {
303                         Perl_av_undef (perl, values);
304                         return -1;
305                 }
306         }
307
308         if (NULL == Perl_hv_store (perl, hash, "values", 6,
309                         Perl_newRV_noinc (perl, (SV *)values), 0))
310                 return -1;
311
312         if (0 != vl->time)
313                 if (NULL == Perl_hv_store (perl, hash, "time", 4,
314                                 Perl_newSViv (perl, vl->time), 0))
315                         return -1;
316
317         if ('\0' != vl->host[0])
318                 if (NULL == Perl_hv_store (perl, hash, "host", 4,
319                                 Perl_newSVpv (perl, vl->host, 0), 0))
320                         return -1;
321
322         if ('\0' != vl->plugin[0])
323                 if (NULL == Perl_hv_store (perl, hash, "plugin", 6,
324                                 Perl_newSVpv (perl, vl->plugin, 0), 0))
325                         return -1;
326
327         if ('\0' != vl->plugin_instance[0])
328                 if (NULL == Perl_hv_store (perl, hash, "plugin_instance", 15,
329                                 Perl_newSVpv (perl, vl->plugin_instance, 0), 0))
330                         return -1;
331
332         if ('\0' != vl->type_instance[0])
333                 if (NULL == Perl_hv_store (perl, hash, "type_instance", 13,
334                                 Perl_newSVpv (perl, vl->type_instance, 0), 0))
335                         return -1;
336         return 0;
337 } /* static int value2av (value_list_t *, data_set_t *, HV *) */
338
339
340 /*
341  * Internal functions.
342  */
343
344 static char *get_module_name (char *buf, size_t buf_len, const char *module) {
345         int status = 0;
346         if (base_name[0] == '\0')
347                 status = snprintf (buf, buf_len, "%s", module);
348         else
349                 status = snprintf (buf, buf_len, "%s::%s", base_name, module);
350         if ((status < 0) || (status >= buf_len))
351                 return (NULL);
352         buf[buf_len - 1] = '\0';
353         return (buf);
354 } /* char *get_module_name */
355
356 /*
357  * Add a plugin's data set definition.
358  */
359 static int pplugin_register_data_set (char *name, AV *dataset)
360 {
361         int len = -1;
362         int i   = 0;
363
364         data_source_t *ds  = NULL;
365         data_set_t    *set = NULL;
366
367         ds_types_t *types = NULL;
368
369         if ((NULL == name) || (NULL == dataset))
370                 return -1;
371
372         len = Perl_av_len (perl, dataset);
373
374         if (-1 == len)
375                 return -1;
376
377         ds  = (data_source_t *)smalloc ((len + 1) * sizeof (data_source_t));
378         set = (data_set_t *)smalloc (sizeof (data_set_t));
379
380         types = (ds_types_t *)smalloc (sizeof (ds_types_t));
381         types->len = len + 1;
382         types->values = (int *)smalloc ((types->len) * sizeof (int));
383
384         for (i = 0; i <= len; ++i) {
385                 SV **elem = Perl_av_fetch (perl, dataset, i, 0);
386
387                 if (NULL == elem)
388                         return -1;
389
390                 if (! (SvROK (*elem) && (SVt_PVHV == SvTYPE (SvRV (*elem))))) {
391                         log_err ("pplugin_register_data_set: Invalid data source.");
392                         return -1;
393                 }
394
395                 if (-1 == hv2data_source ((HV *)SvRV (*elem), &ds[i]))
396                         return -1;
397
398                 types->values[i] = ds[i].type;
399                 log_debug ("pplugin_register_data_set: "
400                                 "DS.name = \"%s\", DS.type = %i, DS.min = %f, DS.max = %f",
401                                 ds[i].name, ds[i].type, ds[i].min, ds[i].max);
402         }
403
404         if (NULL == Perl_hv_store (perl, data_sets, name, strlen (name),
405                         Perl_sv_setref_pv (perl, Perl_newSV (perl, 0), 0, types), 0))
406                 return -1;
407
408         strncpy (set->type, name, DATA_MAX_NAME_LEN);
409         set->type[DATA_MAX_NAME_LEN - 1] = '\0';
410
411         set->ds_num = len + 1;
412         set->ds = ds;
413         return plugin_register_data_set (set);
414 } /* static int pplugin_register_data_set (char *, SV *) */
415
416 /*
417  * Remove a plugin's data set definition.
418  */
419 static int pplugin_unregister_data_set (char *name)
420 {
421         SV *tmp = NULL;
422
423         if (NULL == name)
424                 return 0;
425
426         /* freeing the allocated memory of the element itself (ds_types_t *)
427          * causes a segfault during perl_destruct () thus I assume perl somehow
428          * takes care of this... */
429
430         tmp = Perl_hv_delete (perl, data_sets, name, strlen (name), 0);
431         if (NULL != tmp) {
432                 ds_types_t *ds = (ds_types_t *)SvIV ((SV *)SvRV (tmp));
433                 sfree (ds->values);
434         }
435         return plugin_unregister_data_set (name);
436 } /* static int pplugin_unregister_data_set (char *) */
437
438 /*
439  * Submit the values to the write functions.
440  *
441  * value list:
442  * {
443  *   values => [ @values ],
444  *   time   => $time,
445  *   host   => $host,
446  *   plugin => $plugin,
447  *   plugin_instance => $pinstance,
448  *   type_instance   => $tinstance,
449  * }
450  */
451 static int pplugin_dispatch_values (char *name, HV *values)
452 {
453         value_list_t list = VALUE_LIST_INIT;
454         value_t      *val = NULL;
455
456         SV **tmp = NULL;
457
458         int ret = 0;
459
460         if ((NULL == name) || (NULL == values))
461                 return -1;
462
463         if ((NULL == (tmp = Perl_hv_fetch (perl, values, "values", 6, 0)))
464                         || (! (SvROK (*tmp) && (SVt_PVAV == SvTYPE (SvRV (*tmp)))))) {
465                 log_err ("pplugin_dispatch_values: No valid values given.");
466                 return -1;
467         }
468
469         {
470                 AV  *array = (AV *)SvRV (*tmp);
471                 int len    = Perl_av_len (perl, array) + 1;
472
473                 val = (value_t *)smalloc (len * sizeof (value_t));
474
475                 list.values_len = av2value (name, (AV *)SvRV (*tmp), val, len);
476                 list.values = val;
477
478                 if (-1 == list.values_len) {
479                         sfree (val);
480                         return -1;
481                 }
482         }
483
484         if (NULL != (tmp = Perl_hv_fetch (perl, values, "time", 4, 0))) {
485                 list.time = (time_t)SvIV (*tmp);
486         }
487         else {
488                 list.time = time (NULL);
489         }
490
491         if (NULL != (tmp = Perl_hv_fetch (perl, values, "host", 4, 0))) {
492                 strncpy (list.host, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
493                 list.host[DATA_MAX_NAME_LEN - 1] = '\0';
494         }
495         else {
496                 strcpy (list.host, hostname_g);
497         }
498
499         if (NULL != (tmp = Perl_hv_fetch (perl, values, "plugin", 6, 0))) {
500                 strncpy (list.plugin, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
501                 list.plugin[DATA_MAX_NAME_LEN - 1] = '\0';
502         }
503
504         if (NULL != (tmp = Perl_hv_fetch (perl, values,
505                         "plugin_instance", 15, 0))) {
506                 strncpy (list.plugin_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
507                 list.plugin_instance[DATA_MAX_NAME_LEN - 1] = '\0';
508         }
509
510         if (NULL != (tmp = Perl_hv_fetch (perl, values, "type_instance", 13, 0))) {
511                 strncpy (list.type_instance, SvPV_nolen (*tmp), DATA_MAX_NAME_LEN);
512                 list.type_instance[DATA_MAX_NAME_LEN - 1] = '\0';
513         }
514
515         ret = plugin_dispatch_values (name, &list);
516
517         sfree (val);
518         return ret;
519 } /* static int pplugin_dispatch_values (char *, HV *) */
520
521 /*
522  * Call all working functions of the given type.
523  */
524 static int pplugin_call_all (int type, ...)
525 {
526         int retvals = 0;
527
528         va_list ap;
529         int ret = 0;
530
531         dSP;
532
533         if ((type < 0) || (type >= PLUGIN_TYPES))
534                 return -1;
535
536         va_start (ap, type);
537
538         ENTER;
539         SAVETMPS;
540
541         PUSHMARK (SP);
542
543         XPUSHs (sv_2mortal (Perl_newSViv (perl, (IV)type)));
544
545         if (PLUGIN_WRITE == type) {
546                 /*
547                  * $_[0] = $plugin_type;
548                  *
549                  * $_[1] =
550                  * [
551                  *   {
552                  *     name => $ds_name,
553                  *     type => $ds_type,
554                  *     min  => $ds_min,
555                  *     max  => $ds_max
556                  *   },
557                  *   ...
558                  * ];
559                  *
560                  * $_[2] =
561                  * {
562                  *   values => [ $v1, ... ],
563                  *   time   => $time,
564                  *   host   => $hostname,
565                  *   plugin => $plugin,
566                  *   plugin_instance => $instance,
567                  *   type_instance   => $type_instance
568                  * };
569                  */
570                 data_set_t   *ds;
571                 value_list_t *vl;
572
573                 AV *pds = Perl_newAV (perl);
574                 HV *pvl = Perl_newHV (perl);
575
576                 ds = va_arg (ap, data_set_t *);
577                 vl = va_arg (ap, value_list_t *);
578
579                 if (-1 == data_set2av (ds, pds))
580                         return -1;
581
582                 if (-1 == value_list2hv (vl, ds, pvl))
583                         return -1;
584
585                 XPUSHs (sv_2mortal (Perl_newSVpv (perl, ds->type, 0)));
586                 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pds)));
587                 XPUSHs (sv_2mortal (Perl_newRV_noinc (perl, (SV *)pvl)));
588         }
589         else if (PLUGIN_LOG == type) {
590                 /*
591                  * $_[0] = $level;
592                  *
593                  * $_[1] = $message;
594                  */
595                 XPUSHs (sv_2mortal (Perl_newSViv (perl, va_arg (ap, int))));
596                 XPUSHs (sv_2mortal (Perl_newSVpv (perl, va_arg (ap, char *), 0)));
597         }
598
599         PUTBACK;
600
601         retvals = Perl_call_pv (perl, "Collectd::plugin_call_all", G_SCALAR);
602
603         SPAGAIN;
604         if (0 < retvals) {
605                 SV *tmp = POPs;
606                 if (! SvTRUE (tmp))
607                         ret = -1;
608         }
609
610         PUTBACK;
611         FREETMPS;
612         LEAVE;
613
614         va_end (ap);
615         return ret;
616 } /* static int pplugin_call_all (int, ...) */
617
618
619 /*
620  * Exported Perl API.
621  */
622
623 /*
624  * Collectd::plugin_register_data_set (type, dataset).
625  *
626  * type:
627  *   type of the dataset
628  *
629  * dataset:
630  *   dataset to be registered
631  */
632 static XS (Collectd_plugin_register_ds)
633 {
634         SV  *data = NULL;
635         int ret   = 0;
636
637         dXSARGS;
638
639         if (2 != items) {
640                 log_err ("Usage: Collectd::plugin_register_data_set(type, dataset)");
641                 XSRETURN_EMPTY;
642         }
643
644         log_debug ("Collectd::plugin_register_data_set: "
645                         "type = \"%s\", dataset = \"%s\"",
646                         SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
647
648         data = ST (1);
649
650         if (SvROK (data) && (SVt_PVAV == SvTYPE (SvRV (data)))) {
651                 ret = pplugin_register_data_set (SvPV_nolen (ST (0)),
652                                 (AV *)SvRV (data));
653         }
654         else {
655                 log_err ("Collectd::plugin_register_data_set: Invalid data.");
656                 XSRETURN_EMPTY;
657         }
658
659         if (0 == ret)
660                 XSRETURN_YES;
661         else
662                 XSRETURN_EMPTY;
663 } /* static XS (Collectd_plugin_register_ds) */
664
665 /*
666  * Collectd::plugin_unregister_data_set (type).
667  *
668  * type:
669  *   type of the dataset
670  */
671 static XS (Collectd_plugin_unregister_ds)
672 {
673         dXSARGS;
674
675         if (1 != items) {
676                 log_err ("Usage: Collectd::plugin_unregister_data_set(type)");
677                 XSRETURN_EMPTY;
678         }
679
680         log_debug ("Collectd::plugin_unregister_data_set: type = \"%s\"",
681                         SvPV_nolen (ST (0)));
682
683         if (0 == pplugin_unregister_data_set (SvPV_nolen (ST (1))))
684                 XSRETURN_YES;
685         else
686                 XSRETURN_EMPTY;
687 } /* static XS (Collectd_plugin_register_ds) */
688
689 /*
690  * Collectd::plugin_dispatch_values (name, values).
691  *
692  * name:
693  *   name of the plugin
694  *
695  * values:
696  *   value list to submit
697  */
698 static XS (Collectd_plugin_dispatch_values)
699 {
700         SV *values = NULL;
701
702         int ret = 0;
703
704         dXSARGS;
705
706         if (2 != items) {
707                 log_err ("Usage: Collectd::plugin_dispatch_values(name, values)");
708                 XSRETURN_EMPTY;
709         }
710
711         log_debug ("Collectd::plugin_dispatch_values: "
712                         "name = \"%s\", values=\"%s\"",
713                         SvPV_nolen (ST (0)), SvPV_nolen (ST (1)));
714
715         values = ST (1);
716
717         if (! (SvROK (values) && (SVt_PVHV == SvTYPE (SvRV (values))))) {
718                 log_err ("Collectd::plugin_dispatch_values: Invalid values.");
719                 XSRETURN_EMPTY;
720         }
721
722         if ((NULL == ST (0)) || (NULL == values))
723                 XSRETURN_EMPTY;
724
725         ret = pplugin_dispatch_values (SvPV_nolen (ST (0)), (HV *)SvRV (values));
726
727         if (0 == ret)
728                 XSRETURN_YES;
729         else
730                 XSRETURN_EMPTY;
731 } /* static XS (Collectd_plugin_dispatch_values) */
732
733 /*
734  * Collectd::plugin_log (level, message).
735  *
736  * level:
737  *   log level (LOG_DEBUG, ... LOG_ERR)
738  *
739  * message:
740  *   log message
741  */
742 static XS (Collectd_plugin_log)
743 {
744         dXSARGS;
745
746         if (2 != items) {
747                 log_err ("Usage: Collectd::plugin_log(level, message)");
748                 XSRETURN_EMPTY;
749         }
750
751         plugin_log (SvIV (ST (0)), SvPV_nolen (ST (1)));
752         XSRETURN_YES;
753 } /* static XS (Collectd_plugin_log) */
754
755
756 /*
757  * Interface to collectd.
758  */
759
760 static int perl_init (void)
761 {
762         if (NULL == perl)
763                 return 0;
764
765         PERL_SET_CONTEXT (perl);
766         return pplugin_call_all (PLUGIN_INIT);
767 } /* static int perl_init (void) */
768
769 static int perl_read (void)
770 {
771         if (NULL == perl)
772                 return 0;
773
774         PERL_SET_CONTEXT (perl);
775         return pplugin_call_all (PLUGIN_READ);
776 } /* static int perl_read (void) */
777
778 static int perl_write (const data_set_t *ds, const value_list_t *vl)
779 {
780         if (NULL == perl)
781                 return 0;
782
783         PERL_SET_CONTEXT (perl);
784         return pplugin_call_all (PLUGIN_WRITE, ds, vl);
785 } /* static int perl_write (const data_set_t *, const value_list_t *) */
786
787 static void perl_log (int level, const char *msg)
788 {
789         if (NULL == perl)
790                 return;
791
792         PERL_SET_CONTEXT (perl);
793         pplugin_call_all (PLUGIN_LOG, level, msg);
794         return;
795 } /* static void perl_log (int, const char *) */
796
797 static int perl_shutdown (void)
798 {
799         int ret = 0;
800
801         plugin_unregister_config ("perl");
802
803         if (NULL == perl)
804                 return 0;
805
806         plugin_unregister_log ("perl");
807         plugin_unregister_init ("perl");
808         plugin_unregister_read ("perl");
809         plugin_unregister_write ("perl");
810
811         PERL_SET_CONTEXT (perl);
812         ret = pplugin_call_all (PLUGIN_SHUTDOWN);
813
814         if (0 < Perl_hv_iterinit (perl, data_sets)) {
815                 char *k = NULL;
816                 I32  l  = 0;
817
818                 while (NULL != Perl_hv_iternextsv (perl, data_sets, &k, &l)) {
819                         pplugin_unregister_data_set (k);
820                 }
821         }
822
823         Perl_hv_undef (perl, data_sets);
824
825 #if COLLECT_DEBUG
826         Perl_sv_report_used (perl);
827 #endif /* COLLECT_DEBUG */
828
829         perl_destruct (perl);
830         perl_free (perl);
831         perl = NULL;
832
833         PERL_SYS_TERM ();
834
835         plugin_unregister_shutdown ("perl");
836         return ret;
837 } /* static void perl_shutdown (void) */
838
839 /* bootstrap the Collectd module */
840 static void xs_init (pTHX)
841 {
842         HV   *stash = NULL;
843         char *file  = __FILE__;
844
845         int i = 0;
846
847         dXSUB_SYS;
848
849         /* enable usage of Perl modules using shared libraries */
850         Perl_newXS (perl, "DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
851
852         /* register API */
853         for (i = 0; NULL != api[i].f; ++i)
854                 Perl_newXS (perl, api[i].name, api[i].f, file);
855
856         stash = Perl_gv_stashpv (perl, "Collectd", 1);
857
858         /* export "constants" */
859         for (i = 0; '\0' != constants[i].name[0]; ++i)
860                 Perl_newCONSTSUB (perl, stash, constants[i].name,
861                                 Perl_newSViv (perl, constants[i].value));
862         return;
863 } /* static void xs_init (pTHX) */
864
865 /* Initialize the global Perl interpreter. */
866 static int init_pi (int argc, char **argv)
867 {
868         if (NULL != perl)
869                 return 0;
870
871         log_info ("Initializing Perl interpreter...");
872 #if COLLECT_DEBUG
873         {
874                 int i = 0;
875
876                 for (i = 0; i < argc; ++i)
877                         log_debug ("argv[%i] = \"%s\"", i, argv[i]);
878         }
879 #endif /* COLLECT_DEBUG */
880
881         PERL_SYS_INIT3 (&argc, &argv, &environ);
882
883         if (NULL == (perl = perl_alloc ())) {
884                 log_err ("module_register: Not enough memory.");
885                 exit (3);
886         }
887         perl_construct (perl);
888
889         PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
890
891         if (0 != perl_parse (perl, xs_init, argc, argv, NULL)) {
892                 log_err ("module_register: Unable to bootstrap Collectd.");
893                 exit (1);
894         }
895         perl_run (perl);
896
897         data_sets = Perl_newHV (perl);
898
899         plugin_register_log ("perl", perl_log);
900         plugin_register_init ("perl", perl_init);
901
902         plugin_register_read ("perl", perl_read);
903
904         plugin_register_write ("perl", perl_write);
905         plugin_register_shutdown ("perl", perl_shutdown);
906         return 0;
907 } /* static int init_pi (const char **, const int) */
908
909 static int perl_config (const char *key, const char *value)
910 {
911         log_debug ("perl_config: key = \"%s\", value=\"%s\"", key, value);
912
913         if (0 == strcasecmp (key, "LoadPlugin")) {
914                 char module_name[DATA_MAX_NAME_LEN];
915
916                 if (get_module_name (module_name, sizeof (module_name), value)
917                                 == NULL) {
918                         log_err ("Invalid module name %s", value);
919                         return (1);
920                 } /* if (get_module_name == NULL) */
921
922                 init_pi (perl_argc, perl_argv);
923
924                 log_debug ("perl_config: loading perl plugin \"%s\"", value);
925                 Perl_load_module (perl, PERL_LOADMOD_NOIMPORT,
926                                 Perl_newSVpv (perl, module_name, strlen (module_name)),
927                                 Nullsv);
928         }
929         else if (0 == strcasecmp (key, "BaseName")) {
930                 log_debug ("perl_config: Setting plugin basename to \"%s\"", value);
931                 strncpy (base_name, value, sizeof (base_name));
932                 base_name[sizeof (base_name) - 1] = '\0';
933         }
934         else if (0 == strcasecmp (key, "IncludeDir")) {
935                 perl_argv = (char **)realloc (perl_argv,
936                                 (++perl_argc + 1) * sizeof (char *));
937
938                 if (NULL == perl_argv) {
939                         log_err ("perl_config: Not enough memory.");
940                         exit (3);
941                 }
942
943                 perl_argv[perl_argc - 1] = (char *)smalloc (strlen (value) + 3);
944                 sstrncpy(perl_argv[perl_argc - 1], "-I", 3);
945                 sstrncpy(perl_argv[perl_argc - 1] + 2, value, strlen (value) + 1);
946
947                 perl_argv[perl_argc] = NULL;
948         }
949         else {
950                 return -1;
951         }
952         return 0;
953 } /* static int perl_config (char *, char *) */
954
955 void module_register (void)
956 {
957         perl_argc = 4;
958         perl_argv = (char **)smalloc ((perl_argc + 1) * sizeof (char *));
959
960         /* default options for the Perl interpreter */
961         perl_argv[0] = "";
962         perl_argv[1] = "-MCollectd";
963         perl_argv[2] = "-e";
964         perl_argv[3] = "1";
965         perl_argv[4] = NULL;
966
967         plugin_register_config ("perl", perl_config, config_keys, config_keys_num);
968         return;
969 } /* void module_register (void) */
970
971 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
972