Debian Codegraph @

gmond/modules/perl/mod_perl.c

raw
1 /*******************************************************************************
2 *
3 * Copyright (C) 2010 Bernard Li <[email protected]>
4 * All Rights Reserved.
5 *
6 * Borrowed heavily from gmond/modules/mod_python.c
7 *
8 * This code is part of a perl module for ganglia.
9 *
10 * Contributors : Nicolas Brousse <[email protected]>
11 *
12 * Portions Copyright (C) 2007 Novell, Inc. All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are met:
16 *
17 * - Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above copyright notice,
21 * this list of conditions and the following disclaimer in the documentation
22 * and/or other materials provided with the distribution.
23 *
24 * - Neither the name of Novell, Inc. nor the names of its
25 * contributors may be used to endorse or promote products derived from this
26 * software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL Novell, Inc. OR THE CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 *
40 ******************************************************************************/
41
42 #include <EXTERN.h>
43 #include <perl.h>
44 #include <XSUB.h>
45 #include <gm_metric.h>
46 #include <gm_msg.h>
47 #include <string.h>
48
49 #include <apr_tables.h>
50 #include <apr_strings.h>
51 #include <apr_lib.h>
52 #include <dirent.h>
53
54 /*
55 * Declare ourselves so the configuration routines can find and know us.
56 * We'll fill it in at the end of the module.
57 */
58 mmodule perl_module;
59
60 typedef struct
61 {
62 char *pcb; /* The metric call back function */
63 char *mod_name; /* Name of the module */
64 PerlInterpreter *perl; /* Perl interpreter */
65 }
66 mapped_info_t;
67
68 typedef struct
69 {
70 char mname[128];
71 int tmax;
72 char vtype[32];
73 char units[64];
74 char slope[32];
75 char format[64];
76 char desc[512];
77 char groups[512];
78 apr_table_t *extra_data;
79 char pcb[128];
80 }
81 pl_metric_init_t;
82
83 static apr_pool_t *pool;
84
85 static apr_array_header_t *metric_info = NULL;
86 static apr_array_header_t *metric_mapping_info = NULL;
87 static apr_status_t perl_metric_cleanup ( void *data);
88
89 char modname_bfr[PATH_MAX];
90 static char* is_perl_module(const char* fname)
91 {
92 char* p = strrchr(fname, '.');
93 if (!p) {
94 return NULL;
95 }
96
97 if (strcmp(p, ".pl")) {
98 return NULL;
99 }
100
101 strncpy(modname_bfr, fname, p-fname);
102 modname_bfr[p-fname] = 0;
103 return modname_bfr;
104 }
105
106 static cfg_t* find_module_config(char *modname)
107 {
108 cfg_t *modules_cfg;
109 int j;
110
111 modules_cfg = cfg_getsec(perl_module.config_file, "modules");
112 for (j = 0; j < cfg_size(modules_cfg, "module"); j++) {
113 char *modName, *modLanguage;
114 int modEnabled;
115
116 cfg_t *plmodule = cfg_getnsec(modules_cfg, "module", j);
117
118 /* Check the module language to make sure that
119 the language designation is perl. */
120 modLanguage = cfg_getstr(plmodule, "language");
121 if (!modLanguage || strcasecmp(modLanguage, "perl"))
122 continue;
123
124 modName = cfg_getstr(plmodule, "name");
125 if (strcasecmp(modname, modName))
126 continue;
127
128 /* Check to make sure that the module is enabled. */
129 modEnabled = cfg_getbool(plmodule, "enabled");
130 if (!modEnabled)
131 continue;
132
133 return plmodule;
134 }
135 return NULL;
136 }
137
138 static HV* build_params_hash(cfg_t *plmodule)
139 {
140 int k;
141 HV *params_hash;
142
143 params_hash = newHV();
144
145 if (plmodule && params_hash) {
146 for (k = 0; k < cfg_size(plmodule, "param"); k++) {
147 cfg_t *param;
148 char *name, *value;
149 SV *sv_value;
150
151 param = cfg_getnsec(plmodule, "param", k);
152 name = apr_pstrdup(pool, param->title);
153 value = apr_pstrdup(pool, cfg_getstr(param, "value"));
154 sv_value = newSVpv(value, 0);
155 if (name && sv_value) {
156 /* Silence "value computed is not used" warning */
157 (void)hv_store(params_hash, name, strlen(name), sv_value, 0);
158 }
159 }
160 }
161 return params_hash;
162 }
163
164 static void fill_metric_info(HV* plhash, pl_metric_init_t* minfo, char *modname, apr_pool_t *pool)
165 {
166 char *metric_name = "";
167 char *key;
168 SV* sv_value;
169 I32 ret;
170
171 memset(minfo, 0, sizeof(*minfo));
172
173 /* create the apr table here */
174 minfo->extra_data = apr_table_make(pool, 2);
175
176 hv_iterinit(plhash);
177 while ((sv_value = hv_iternextsv(plhash, &key, &ret))) {
178 if (!strcasecmp(key, "name")) {
179 STRLEN len;
180 char *value = SvPV(sv_value, len);
181 if (!strncpy(minfo->mname, value, sizeof(minfo->mname))) {
182 err_msg("[PERL] No metric name given in module [%s].\n", modname);
183 }
184 else
185 metric_name = minfo->mname;
186 continue;
187 }
188
189 if (!strcasecmp(key, "call_back")) {
190 STRLEN len;
191 char *value = SvPV(sv_value, len);
192 if (!strncpy(minfo->pcb, value, sizeof(minfo->pcb))) {
193 err_msg("[PERL] No perl call back given for metric [%s] in module [%s]. Will not call\n",
194 metric_name, modname);
195 }
196 continue;
197 }
198 if (!strcasecmp(key, "time_max")) {
199 int value = SvIV(sv_value);
200 if (!(minfo->tmax = value)) {
201 minfo->tmax = 60;
202 err_msg("[PERL] No time max given for metric [%s] in module [%s]. Using %d.\n",
203 metric_name, modname, minfo->tmax);
204 }
205 continue;
206 }
207 if (!strcasecmp(key, "value_type")) {
208 STRLEN len;
209 char *value = SvPV(sv_value, len);
210 if (!strncpy(minfo->vtype, value, sizeof(minfo->vtype))) {
211 strcpy (minfo->vtype, "uint");
212 err_msg("[PERL] No value type given for metric [%s] in module [%s]. Using %s.\n",
213 metric_name, modname, minfo->vtype);
214 }
215 continue;
216 }
217 if (!strcasecmp(key, "units")) {
218 STRLEN len;
219 char *value = SvPV(sv_value, len);
220 if (!strncpy(minfo->units, value, sizeof(minfo->units))) {
221 strcpy (minfo->units, "unknown");
222 err_msg("[PERL] No metric units given for metric [%s] in module [%s]. Using %s.\n",
223 metric_name, modname, minfo->units);
224 }
225 continue;
226 }
227 if (!strcasecmp(key, "slope")) {
228 STRLEN len;
229 char *value = SvPV(sv_value, len);
230 if (!strncpy(minfo->slope, value, sizeof(minfo->slope))) {
231 strcpy (minfo->slope, "both");
232 err_msg("[PERL] No slope given for metric [%s] in module [%s]. Using %s.\n",
233 metric_name, modname, minfo->slope);
234 }
235 continue;
236 }
237 if (!strcasecmp(key, "format")) {
238 STRLEN len;
239 char *value = SvPV(sv_value, len);
240 if (!strncpy(minfo->format, value, sizeof(minfo->format))) {
241 strcpy (minfo->format, "%u");
242 err_msg("[PERL] No format given for metric [%s] in module [%s]. Using %s.\n",
243 metric_name, modname, minfo->format);
244 }
245 continue;
246 }
247 if (!strcasecmp(key, "description")) {
248 STRLEN len;
249 char *value = SvPV(sv_value, len);
250 if (!strncpy(minfo->desc, value, sizeof(minfo->desc))) {
251 strcpy (minfo->desc, "unknown metric");
252 err_msg("[PERL] No description given for metric [%s] in module [%s]. Using %s.\n",
253 metric_name, modname, minfo->desc);
254 }
255 continue;
256 }
257 if (!strcasecmp(key, "groups")) {
258 STRLEN len;
259 char *value = SvPV(sv_value, len);
260 if (!strncpy(minfo->groups, value, sizeof(minfo->groups))) {
261 strcpy (minfo->groups, "");
262 }
263 continue;
264 }
265
266 STRLEN len;
267 char *value;
268 if (!(value = SvPV(sv_value, len))) {
269 err_msg("[PERL] Extra data key [%s] could not be processed.\n", key);
270 }
271 else {
272 apr_table_add(minfo->extra_data, key, value);
273 }
274 }
275
276 /*err_msg("name: %s", minfo->mname);
277 err_msg("callback: %s", minfo->pcb);
278 err_msg("time_max: %d", minfo->tmax);
279 err_msg("value_type: %s", minfo->vtype);
280 err_msg("units: %s", minfo->units);
281 err_msg("slope: %s", minfo->slope);
282 err_msg("format: %s", minfo->format);
283 err_msg("description: %s", minfo->desc);
284 err_msg("groups: %s", minfo->groups);*/
285 }
286
287 static void fill_gmi(Ganglia_25metric* gmi, pl_metric_init_t* minfo)
288 {
289 char *s, *lasts;
290 int i;
291 const apr_array_header_t *arr = apr_table_elts(minfo->extra_data);
292 const apr_table_entry_t *elts = (const apr_table_entry_t *)arr->elts;
293
294 /* gmi->key will be automatically assigned by gmond */
295 gmi->name = apr_pstrdup(pool, minfo->mname);
296 gmi->tmax = minfo->tmax;
297 if (!strcasecmp(minfo->vtype, "string")) {
298 gmi->type = GANGLIA_VALUE_STRING;
299 gmi->msg_size = UDP_HEADER_SIZE+MAX_G_STRING_SIZE;
300 }
301 else if (!strcasecmp(minfo->vtype, "uint")) {
302 gmi->type = GANGLIA_VALUE_UNSIGNED_INT;
303 gmi->msg_size = UDP_HEADER_SIZE+8;
304 }
305 else if (!strcasecmp(minfo->vtype, "int")) {
306 gmi->type = GANGLIA_VALUE_INT;
307 gmi->msg_size = UDP_HEADER_SIZE+8;
308 }
309 else if (!strcasecmp(minfo->vtype, "float")) {
310 gmi->type = GANGLIA_VALUE_FLOAT;
311 gmi->msg_size = UDP_HEADER_SIZE+8;
312 }
313 else if (!strcasecmp(minfo->vtype, "double")) {
314 gmi->type = GANGLIA_VALUE_DOUBLE;
315 gmi->msg_size = UDP_HEADER_SIZE+16;
316 }
317 else {
318 gmi->type = GANGLIA_VALUE_UNKNOWN;
319 gmi->msg_size = UDP_HEADER_SIZE+8;
320 }
321
322 gmi->units = apr_pstrdup(pool, minfo->units);
323 gmi->slope = apr_pstrdup(pool, minfo->slope);
324 gmi->fmt = apr_pstrdup(pool, minfo->format);
325 gmi->desc = apr_pstrdup(pool, minfo->desc);
326
327 MMETRIC_INIT_METADATA(gmi, pool);
328 for (s=(char *)apr_strtok(minfo->groups, ",", &lasts);
329 s!=NULL; s=(char *)apr_strtok(NULL, ",", &lasts)) {
330 char *d = s;
331 /* Strip the leading white space */
332 while (d && *d && apr_isspace(*d)) {
333 d++;
334 }
335 MMETRIC_ADD_METADATA(gmi,MGROUP,d);
336 }
337
338 /* transfer any extra data as metric metadata */
339 for (i = 0; i < arr->nelts; ++i) {
340 if (elts[i].key == NULL)
341 continue;
342 MMETRIC_ADD_METADATA(gmi, elts[i].key, elts[i].val);
343 }
344 }
345
346 static int perl_metric_init(apr_pool_t *p)
347 {
348 DIR *dp;
349 struct dirent *entry;
350 int i, size;
351 char* modname;
352 char *modpath;
353 HV *pparamhash;
354 pl_metric_init_t minfo;
355 Ganglia_25metric *gmi;
356 mapped_info_t *mi;
357 const char* path = perl_module.module_params;
358 cfg_t *module_cfg;
359 PerlInterpreter *perl = NULL;
360 int argc = 0;
361 char *argv[] = { };
362 char *env[] = { };
363 char *embedding[] = {"", ""};
364
365 /* Allocate a pool that will be used by this module */
366 apr_pool_create(&pool, p);
367
368 metric_info = apr_array_make(pool, 10, sizeof(Ganglia_25metric));
369 metric_mapping_info = apr_array_make(pool, 10, sizeof(mapped_info_t));
370
371 /* Verify path exists and can be read */
372
373 if (!path) {
374 err_msg("[PERL] Missing perl module path.\n");
375 return -1;
376 }
377
378 if (access(path, F_OK)) {
379 /* 'path' does not exist */
380 err_msg("[PERL] Can't open the perl module path %s.\n", path);
381 return -1;
382 }
383
384 if (access(path, R_OK)) {
385 /* Don't have read access to 'path' */
386 err_msg("[PERL] Can't read from the perl module path %s.\n", path);
387 return -1;
388 }
389
390 /* Initialize each perl module */
391 if ((dp = opendir(path)) == NULL) {
392 /* Error: Cannot open the directory - Shouldn't happen */
393 /* Log? */
394 err_msg("[PERL] Can't open the perl module path %s.\n", path);
395 return -1;
396 }
397
398 PERL_SYS_INIT3(&argc, (char ***) &argv, (char ***) &env);
399
400 i = 0;
401
402 while ((entry = readdir(dp)) != NULL) {
403 modname = is_perl_module(entry->d_name);
404 if (modname == NULL)
405 continue;
406
407 /* Find the specified module configuration in gmond.conf
408 If this return NULL then either the module config
409 doesn't exist or the module is disabled. */
410 module_cfg = find_module_config(modname);
411 if (!module_cfg)
412 continue;
413
414 size_t path_len = strlen(path) + strlen(modname) + 5;
415 modpath = malloc(path_len);
416 modpath = strncpy(modpath, path, path_len);
417 modpath = strcat(modpath, "/");
418 modpath = strcat(modpath, modname);
419 modpath = strcat(modpath, ".pl");
420
421 embedding[1] = modpath ;
422
423 perl = perl_alloc();
424 PL_perl_destruct_level = 0;
425
426 PERL_SET_CONTEXT(perl);
427 perl_construct(perl);
428 PL_origalen = 1;
429
430 PERL_SET_CONTEXT(perl);
431 perl_parse(perl, NULL, 1, embedding, NULL);
432
433 /* Run the perl script so that global variables can be accessed */
434 perl_run(perl);
435
436 free(modpath);
437
438 /* Build a parameter dictionary to pass to the module */
439 pparamhash = build_params_hash(module_cfg);
440 if (!pparamhash) {
441 err_msg("[PERL] Can't build the parameters hash for [%s].\n", modname);
442 continue;
443 }
444
445 dSP;
446 ENTER;
447 SAVETMPS;
448 PUSHMARK(SP);
449 /* Push a reference to the pparamhash to the Perl stack */
450 XPUSHs(sv_2mortal(newRV_noinc((SV*)pparamhash)));
451 PUTBACK;
452 size = call_pv("metric_init", G_ARRAY|G_EVAL);
453 SPAGAIN;
454 /*SP -= size;
455 ax = (SP - PL_stack_base) + 1;
456 SvGETMAGIC(plarray); */
457
458 if (SvTRUE(ERRSV)) {
459 /* failed calling metric_init */
460 err_msg("[PERL] Can't call the metric_init function in the perl module [%s].\n", modname);
461 continue;
462 }
463 else {
464 if (size) {
465 int j;
466 for (j = 0; j < size; j++) {
467 SV* sref = POPs;
468 if (!SvROK(sref)) {
469 err_msg("[PERL] No descriptors returned from metric_init call in the perl module [%s].\n", modname);
470 continue;
471 }
472
473 /* Dereference the reference */
474 HV* plhash = (HV*)(SvRV(sref));
475 if (plhash != NULL) {
476 fill_metric_info(plhash, &minfo, modname, pool);
477 gmi = (Ganglia_25metric*)apr_array_push(metric_info);
478 fill_gmi(gmi, &minfo);
479 mi = (mapped_info_t*)apr_array_push(metric_mapping_info);
480 mi->mod_name = apr_pstrdup(pool, modname);
481 mi->pcb = apr_pstrdup(pool, minfo.pcb);
482 mi->perl = perl;
483 }
484 }
485 }
486 }
487
488 PUTBACK;
489 FREETMPS;
490 LEAVE;
491
492 }
493 closedir(dp);
494
495 apr_pool_cleanup_register(pool, NULL,
496 perl_metric_cleanup,
497 apr_pool_cleanup_null);
498
499 /* Replace the empty static metric definition array with the
500 dynamic array that we just created
501 */
502 /*XXX Need to put this into a finalize MACRO. This is just pushing
503 a NULL entry onto the array so that the looping logic can
504 determine the end if the array. We should probably give back
505 a ready APR array rather than a pointer to a Ganglia_25metric
506 array. */
507 gmi = apr_array_push(metric_info);
508 memset (gmi, 0, sizeof(*gmi));
509 mi = apr_array_push(metric_mapping_info);
510 memset (mi, 0, sizeof(*mi));
511
512 perl_module.metrics_info = (Ganglia_25metric *)metric_info->elts;
513 return 0;
514 }
515
516 static apr_status_t perl_metric_cleanup(void *data)
517 {
518 mapped_info_t *mi, *smi;
519 int i, j;
520
521 mi = (mapped_info_t*) metric_mapping_info->elts;
522 for (i = 0; i < metric_mapping_info->nelts; i++) {
523 if (mi[i].mod_name) {
524
525 /* XXX: Should work but segfault...
526 if (mi[i].perl != NULL) {
527 PERL_SET_CONTEXT(mi[i].perl);
528 perl_destruct(mi[i].perl);
529
530 PERL_SET_CONTEXT(mi[i].perl);
531 perl_free(mi[i].perl);
532 }
533 */
534
535 /* Set all modules that fall after this once with the same
536 * module pointer to NULL so metric_cleanup only gets called
537 * once on the module.
538 */
539 smi = (mapped_info_t*) metric_mapping_info->elts;
540 for (j = i+1; j < metric_mapping_info->nelts; j++) {
541 if (smi[j].mod_name == mi[i].mod_name) {
542 smi[j].mod_name = NULL;
543 }
544 }
545 }
546 }
547
548 PERL_SYS_TERM();
549
550 return APR_SUCCESS;
551 }
552
553 static g_val_t perl_metric_handler(int metric_index)
554 {
555 g_val_t val;
556 Ganglia_25metric *gmi = (Ganglia_25metric *) metric_info->elts;
557 mapped_info_t *mi = (mapped_info_t*) metric_mapping_info->elts;
558 int size;
559
560 memset(&val, 0, sizeof(val));
561 if (!mi[metric_index].pcb) {
562 /* No call back provided for this metric */
563 return val;
564 }
565
566 if (!mi[metric_index].perl) {
567 err_msg("No perl interpreter found.");
568 return val;
569 }
570
571 PERL_SET_CONTEXT(mi[metric_index].perl);
572
573 dSP;
574 ENTER;
575 SAVETMPS;
576 PUSHMARK(SP);
577 PUTBACK;
578
579 /* Call the metric handler call back for this metric */
580 size = call_pv(mi[metric_index].pcb, G_NOARGS);
581 SPAGAIN;
582
583 if (SvTRUE(ERRSV)) {
584 err_msg("[PERL] Can't call the metric handler function for [%s] in the perl module [%s].\n",
585 gmi[metric_index].name, mi[metric_index].mod_name);
586 /* return what? */
587 return val;
588 }
589 else {
590 /*SV* sref = POPs;
591 if (!SvROK(sref)) {
592 err_msg("[PERL] No values returned from metric handler function for [%s] in the perl module [%s].\n",
593 gmi[metric_index].name, mi[metric_index]. modname);
594 return val;
595 }*/
596 if (size) {
597 switch (gmi[metric_index].type) {
598 case GANGLIA_VALUE_STRING:
599 {
600 snprintf(val.str, sizeof(val.str), "%s", POPpx);
601 break;
602 }
603 case GANGLIA_VALUE_UNSIGNED_INT:
604 {
605 val.uint32 = POPl;
606 break;
607 }
608 case GANGLIA_VALUE_INT:
609 {
610 val.int32 = POPi;
611 break;
612 }
613 case GANGLIA_VALUE_FLOAT:
614 {
615 /* XXX FIXEME */
616 val.f = POPn;
617 break;
618 }
619 case GANGLIA_VALUE_DOUBLE:
620 {
621 /* XXX FIXEME */
622 val.d = POPn;
623 break;
624 }
625 default:
626 {
627 memset(&val, 0, sizeof(val));
628 break;
629 }
630 }
631 }
632 }
633
634 PUTBACK;
635 FREETMPS;
636 LEAVE;
637
638 return val;
639 }
640
641 mmodule perl_module =
642 {
643 STD_MMODULE_STUFF,
644 perl_metric_init,
645 NULL,
646 NULL, /* defined dynamically */
647 perl_metric_handler,
648 };