Debian Codegraph @

gmond/modules/python/mod_python.c

raw
1 /*******************************************************************************
2 * Portions Copyright (C) 2007 Novell, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * - Neither the name of Novell, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL Novell, Inc. OR THE CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Author: Brad Nicholes (bnicholes novell.com)
31 * Jon Carey (jcarey novell.com)
32 ******************************************************************************/
33
34 #include <Python.h>
35 #include <gm_metric.h>
36 #include <gm_msg.h>
37 #include <stdio.h>
38 #include <time.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <unistd.h>
43
44 #include <apr_tables.h>
45 #include <apr_strings.h>
46 #include <apr_lib.h>
47
48 #include <dirent.h>
49 #include <sys/stat.h>
50
51 /*
52 * Backward compatibility for 2.1 to 2.4
53 */
54 #if PY_MINOR_VERSION < 5
55 #define Py_ssize_t int
56 #if PY_MINOR_VERSION < 3
57 #define PyInt_AsUnsignedLongMask PyInt_AsLong
58 #endif
59 #endif
60
61 /*
62 * Declare ourselves so the configuration routines can find and know us.
63 * We'll fill it in at the end of the module.
64 */
65 mmodule python_module;
66
67 typedef struct
68 {
69 PyObject* pmod; /* The python metric module object */
70 PyObject* pcb; /* The metric call back function */
71 char *mod_name; /* The name */
72 }
73 mapped_info_t;
74
75 typedef struct
76 {
77 char mname[251]; // 255 (a fairly common filename max) - 4 (".rrd")
78 int tmax;
79 char vtype[32];
80 char units[64];
81 char slope[32];
82 char format[64];
83 char desc[512];
84 char groups[512];
85 apr_table_t *extra_data;
86 PyObject* pcb;
87 }
88 py_metric_init_t;
89
90
91 static apr_pool_t *pool;
92 static PyThreadState* gtstate = NULL;
93
94 static apr_array_header_t *metric_info = NULL;
95 static apr_array_header_t *metric_mapping_info = NULL;
96 static apr_status_t pyth_metric_cleanup ( void *data);
97
98 char modname_bfr[PATH_MAX];
99 static char* is_python_module(const char* fname)
100 {
101 char* p = strrchr(fname, '.');
102 if (!p) {
103 return NULL;
104 }
105
106 if (strcmp(p, ".py")) {
107 return NULL;
108 }
109
110 strncpy(modname_bfr, fname, p-fname);
111 modname_bfr[p-fname] = 0;
112 return modname_bfr;
113 }
114
115 int
116 get_python_string_value(PyObject* dv, char* bfr, int len)
117 {
118 int cc = 1;
119 if (PyLong_Check(dv)) {
120 long v = PyLong_AsLong(dv);
121 snprintf(bfr, len, "%ld", v);
122 }
123 else if (PyInt_Check(dv)) {
124 long v = PyInt_AsLong(dv);
125 snprintf(bfr, len, "%ld", v);
126 }
127 else if (PyString_Check(dv)) {
128 char* v = PyString_AsString(dv);
129 snprintf(bfr, len, "%s", v);
130 }
131 else if (PyFloat_Check(dv)) {
132 double v = PyFloat_AsDouble(dv);
133 snprintf(bfr, len, "%f", v);
134 }
135 else {
136 /* Don't know how to convert */
137 cc = -1;
138 }
139 return cc;
140 }
141
142
143 /* Get a string value from a python dictionary
144 * Return:
145 * 0 - Item not in dictionary
146 * -1 - Invalid data type for key in dictionary
147 * 1 - Success
148 */
149 int
150 get_pydict_string_value(PyObject* pdict, char* key, char* bfr, int len)
151 {
152 PyObject* dv;
153 int cc = 1;
154 if (!PyMapping_HasKeyString(pdict, key))
155 return 0; /* Key not in dictionary */
156 dv = PyMapping_GetItemString(pdict, key);
157 if (!dv)
158 return 0; /* shouldn't happen */
159 cc = get_python_string_value(dv, bfr, len);
160 Py_DECREF(dv);
161 return cc;
162 }
163
164
165 int
166 get_python_uint_value(PyObject* dv, unsigned int* pint)
167 {
168 int cc = 1;
169 if (PyInt_Check(dv) || PyLong_Check(dv)) {
170 unsigned long v = PyInt_AsUnsignedLongMask(dv);
171 *pint = (unsigned int)v;
172 }
173 else if (PyString_Check(dv)) {
174 /* Convert from string to int */
175 unsigned long tid;
176 char *endptr;
177 char* p = PyString_AsString(dv);
178 tid = strtoul(p, &endptr, 10);
179 if(endptr == p || *endptr)
180 cc = -1; /* Invalid numeric format */
181 else
182 *pint = (unsigned int)tid;
183 }
184 else {
185 cc = -1; /* Don't know how to convert this */
186 }
187 return cc;
188 }
189
190 int
191 get_python_int_value(PyObject* dv, int* pint)
192 {
193 int cc = 1;
194 if (PyLong_Check(dv)) {
195 long v = PyLong_AsLong(dv);
196 *pint = (int)v;
197 }
198 else if (PyInt_Check(dv)) {
199 long v = PyInt_AsLong(dv);
200 *pint = (int)v;
201 }
202 else if (PyString_Check(dv)) {
203 /* Convert from string to int */
204 long tid;
205 char *endptr;
206 char* p = PyString_AsString(dv);
207 tid = strtol(p, &endptr, 10);
208 if(endptr == p || *endptr)
209 cc = -1; /* Invalid numeric format */
210 else
211 *pint = (int)tid;
212 }
213 else {
214 cc = -1; /* Don't know how to convert this */
215 }
216 return cc;
217 }
218
219 /* Return:
220 * 0 - Item not in dictionary
221 * -1 - Invalid data type for key in dictionary
222 * 1 - Success
223 */
224 int
225 get_pydict_int_value(PyObject* pdict, char* key, int* pint)
226 {
227 PyObject* dv;
228 int cc = 1;
229 if (!PyMapping_HasKeyString(pdict, key))
230 return 0;
231 dv = PyMapping_GetItemString(pdict, key);
232 if (!dv)
233 return 0; /* shouldn't happen */
234 cc = get_python_int_value(dv, pint);
235 Py_DECREF(dv);
236 return cc;
237 }
238
239 int
240 get_python_float_value(PyObject* dv, double* pnum)
241 {
242 int cc = 1;
243 if (PyFloat_Check(dv)) {
244 *pnum = PyFloat_AsDouble(dv);
245 }
246 else if (PyLong_Check(dv)) {
247 long v = PyLong_AsLong(dv);
248 *pnum = (double)v;
249 }
250 else if (PyInt_Check(dv)) {
251 long v = PyInt_AsLong(dv);
252 *pnum = (double)v;
253 }
254 else if (PyString_Check(dv)) {
255 /* Convert from string to int */
256 double tid;
257 char *endptr;
258 char* p = PyString_AsString(dv);
259 tid = strtod(p, &endptr);
260 if(endptr == p || *endptr)
261 cc = -1; /* Invalid format for double */
262 else
263 *pnum = tid;
264 }
265 else {
266 cc = -1; /* Don't know how to convert this */
267 }
268 return cc;
269 }
270
271 /* Return:
272 * 0 - Item not in dictionary
273 * -1 - Invalid data type for key in dictionary
274 * 1 - Success
275 */
276 int
277 get_pydict_float_value(PyObject* pdict, char* key, double* pnum)
278 {
279 int cc = 1;
280 PyObject* dv;
281 if (!PyMapping_HasKeyString(pdict, key))
282 return 0;
283 dv = PyMapping_GetItemString(pdict, key);
284 if (!dv)
285 return 0; /* shouldn't happen */
286 cc = get_python_float_value(dv, pnum);
287 Py_DECREF(dv);
288 return cc;
289 }
290
291 /* Return:
292 * 0 - Item not in dictionary
293 * -1 - Invalid data type for key in dictionary
294 * 1 - Success
295 */
296 int
297 get_pydict_callable_value(PyObject* pdict, char* key, PyObject** pobj)
298 {
299 PyObject* dv;
300 *pobj = NULL;
301 if (!PyMapping_HasKeyString(pdict, key))
302 return 0;
303 dv = PyMapping_GetItemString(pdict, key);
304 if (!dv)
305 return 0; /* shouldn't happen */
306 if (!PyCallable_Check(dv))
307 {
308 Py_DECREF(dv);
309 return -1;
310 }
311 *pobj = dv;
312 return 1;
313 }
314
315 static void fill_metric_info(PyObject* pdict, py_metric_init_t* minfo, char *modname, apr_pool_t *pool)
316 {
317 char *metric_name = "";
318 PyObject *key, *value;
319 Py_ssize_t pos = 0;
320 int ret;
321 char strkey[1024], strvalue[1024];
322
323 memset(minfo, 0, sizeof(*minfo));
324
325 /* create the apr table here */
326 minfo->extra_data = apr_table_make(pool, 2);
327
328 while (PyDict_Next(pdict, &pos, &key, &value)) {
329 ret = get_python_string_value(key, strkey, sizeof(strkey));
330 if (ret < 0)
331 continue;
332
333 if (!strcasecmp(strkey, "name")) {
334 if (get_python_string_value(value, minfo->mname, sizeof(minfo->mname)) < 1) {
335 err_msg("[PYTHON] No metric name given in module [%s].\n", modname);
336 }
337 else
338 metric_name = minfo->mname;
339 continue;
340 }
341
342 if (!strcasecmp(strkey, "call_back")) {
343 if (get_pydict_callable_value(pdict, "call_back", &minfo->pcb) < 1) {
344 err_msg("[PYTHON] No python call back given for metric [%s] in module [%s]. Will not call\n",
345 metric_name, modname);
346 }
347 continue;
348 }
349 if (!strcasecmp(strkey, "time_max")) {
350 if (get_python_int_value(value, &(minfo->tmax)) < 1) {
351 minfo->tmax = 60;
352 err_msg("[PYTHON] No time max given for metric [%s] in module [%s]. Using %d.\n",
353 metric_name, modname, minfo->tmax);
354 }
355 continue;
356 }
357 if (!strcasecmp(strkey, "value_type")) {
358 if (get_python_string_value(value, minfo->vtype, sizeof(minfo->vtype)) < 1) {
359 strcpy (minfo->vtype, "uint");
360 err_msg("[PYTHON] No value type given for metric [%s] in module [%s]. Using %s.\n",
361 metric_name, modname, minfo->vtype);
362 }
363 continue;
364 }
365 if (!strcasecmp(strkey, "units")) {
366 if (get_python_string_value(value, minfo->units, sizeof(minfo->units)) < 1) {
367 strcpy (minfo->units, "unknown");
368 err_msg("[PYTHON] No metric units given for metric [%s] in module [%s]. Using %s.\n",
369 metric_name, modname, minfo->units);
370 }
371 continue;
372 }
373 if (!strcasecmp(strkey, "slope")) {
374 if (get_python_string_value(value, minfo->slope, sizeof(minfo->slope)) < 1) {
375 strcpy (minfo->slope, "both");
376 err_msg("[PYTHON] No slope given for metric [%s] in module [%s]. Using %s.\n",
377 metric_name, modname, minfo->slope);
378 }
379 continue;
380 }
381 if (!strcasecmp(strkey, "format")) {
382 if (get_python_string_value(value, minfo->format, sizeof(minfo->format)) < 1) {
383 strcpy (minfo->format, "%u");
384 err_msg("[PYTHON] No format given for metric [%s] in module [%s]. Using %s.\n",
385 metric_name, modname, minfo->format);
386 }
387 continue;
388 }
389 if (!strcasecmp(strkey, "description")) {
390 if (get_python_string_value(value, minfo->desc, sizeof(minfo->desc)) < 1) {
391 strcpy (minfo->desc, "unknown metric");
392 err_msg("[PYTHON] No description given for metric [%s] in module [%s]. Using %s.\n",
393 metric_name, modname, minfo->desc);
394 }
395 continue;
396 }
397 if (!strcasecmp(strkey, "groups")) {
398 if (get_python_string_value(value, minfo->groups, sizeof(minfo->groups)) < 1) {
399 strcpy (minfo->groups, "");
400 }
401 continue;
402 }
403
404 if (get_python_string_value(value, strvalue, sizeof(strvalue)) < 1) {
405 err_msg("[PYTHON] Extra data key [%s] could not be processed.\n", strkey);
406 }
407 else {
408 apr_table_add(minfo->extra_data, strkey, strvalue);
409 }
410 }
411 }
412
413 static void fill_gmi(Ganglia_25metric* gmi, py_metric_init_t* minfo)
414 {
415 char *s, *lasts;
416 int i;
417 const apr_array_header_t *arr = apr_table_elts(minfo->extra_data);
418 const apr_table_entry_t *elts = (const apr_table_entry_t *)arr->elts;
419
420 /* gmi->key will be automatically assigned by gmond */
421 gmi->name = apr_pstrdup (pool, minfo->mname);
422 gmi->tmax = minfo->tmax;
423 if (!strcasecmp(minfo->vtype, "string")) {
424 gmi->type = GANGLIA_VALUE_STRING;
425 gmi->msg_size = UDP_HEADER_SIZE+MAX_G_STRING_SIZE;
426 }
427 else if (!strcasecmp(minfo->vtype, "uint")) {
428 gmi->type = GANGLIA_VALUE_UNSIGNED_INT;
429 gmi->msg_size = UDP_HEADER_SIZE+8;
430 }
431 else if (!strcasecmp(minfo->vtype, "int")) {
432 gmi->type = GANGLIA_VALUE_INT;
433 gmi->msg_size = UDP_HEADER_SIZE+8;
434 }
435 else if (!strcasecmp(minfo->vtype, "float")) {
436 gmi->type = GANGLIA_VALUE_FLOAT;
437 gmi->msg_size = UDP_HEADER_SIZE+8;
438 }
439 else if (!strcasecmp(minfo->vtype, "double")) {
440 gmi->type = GANGLIA_VALUE_DOUBLE;
441 gmi->msg_size = UDP_HEADER_SIZE+16;
442 }
443 else {
444 gmi->type = GANGLIA_VALUE_UNKNOWN;
445 gmi->msg_size = UDP_HEADER_SIZE+8;
446 }
447
448 gmi->units = apr_pstrdup(pool, minfo->units);
449 gmi->slope = apr_pstrdup(pool, minfo->slope);
450 gmi->fmt = apr_pstrdup(pool, minfo->format);
451 gmi->desc = apr_pstrdup(pool, minfo->desc);
452
453 MMETRIC_INIT_METADATA(gmi, pool);
454 for (s=(char *)apr_strtok(minfo->groups, ",", &lasts);
455 s!=NULL; s=(char *)apr_strtok(NULL, ",", &lasts)) {
456 char *d = s;
457 /* Strip the leading white space */
458 while (d && *d && apr_isspace(*d)) {
459 d++;
460 }
461 MMETRIC_ADD_METADATA(gmi,MGROUP,d);
462 }
463
464 /* transfer any extra data as metric metadata */
465 for (i = 0; i < arr->nelts; ++i) {
466 if (elts[i].key == NULL)
467 continue;
468 MMETRIC_ADD_METADATA(gmi, elts[i].key, elts[i].val);
469 }
470 }
471
472 static cfg_t* find_module_config(char *modname)
473 {
474 cfg_t *modules_cfg;
475 int j;
476
477 modules_cfg = cfg_getsec(python_module.config_file, "modules");
478 for (j = 0; j < cfg_size(modules_cfg, "module"); j++) {
479 char *modName, *modLanguage;
480 int modEnabled;
481
482 cfg_t *pymodule = cfg_getnsec(modules_cfg, "module", j);
483
484 /* Check the module language to make sure that
485 the language designation is python.
486 */
487 modLanguage = cfg_getstr(pymodule, "language");
488 if (!modLanguage || strcasecmp(modLanguage, "python"))
489 continue;
490
491 modName = cfg_getstr(pymodule, "name");
492 if (strcasecmp(modname, modName)) {
493 continue;
494 }
495
496 /* Check to make sure that the module is enabled.
497 */
498 modEnabled = cfg_getbool(pymodule, "enabled");
499 if (!modEnabled)
500 continue;
501
502 return pymodule;
503 }
504 return NULL;
505 }
506
507 static PyObject* build_params_dict(cfg_t *pymodule)
508 {
509 int k;
510 PyObject *params_dict = PyDict_New();
511
512 if (pymodule && params_dict) {
513 for (k = 0; k < cfg_size(pymodule, "param"); k++) {
514 cfg_t *param;
515 char *name, *value;
516 PyObject *pyvalue;
517
518 param = cfg_getnsec(pymodule, "param", k);
519 name = apr_pstrdup(pool, param->title);
520 value = apr_pstrdup(pool, cfg_getstr(param, "value"));
521 pyvalue = PyString_FromString(value);
522 if (name && pyvalue) {
523 PyDict_SetItemString(params_dict, name, pyvalue);
524 Py_DECREF(pyvalue);
525 }
526 }
527 }
528 return params_dict;
529 }
530
531 static PyObject*
532 ganglia_get_debug_msg_level(PyObject *self, PyObject *args)
533 {
534 return Py_BuildValue("i", get_debug_msg_level());
535 }
536
537 static PyMethodDef GangliaMethods[] = {
538 {"get_debug_msg_level", ganglia_get_debug_msg_level, METH_NOARGS,
539 "Return the debug level used by ganglia."},
540 {NULL, NULL, 0, NULL}
541 };
542
543 static int pyth_metric_init (apr_pool_t *p)
544 {
545 DIR *dp;
546 struct dirent *entry;
547 int i;
548 char* modname;
549 PyObject *pmod, *pinitfunc, *pobj, *pparamdict;
550 py_metric_init_t minfo;
551 Ganglia_25metric *gmi;
552 mapped_info_t *mi;
553 const char* path = python_module.module_params;
554 cfg_t *module_cfg;
555
556 /* Allocate a pool that will be used by this module */
557 apr_pool_create(&pool, p);
558
559 metric_info = apr_array_make(pool, 10, sizeof(Ganglia_25metric));
560 metric_mapping_info = apr_array_make(pool, 10, sizeof(mapped_info_t));
561
562 /* Verify path exists and can be read */
563
564 if (!path) {
565 err_msg("[PYTHON] Missing python module path.\n");
566 return -1;
567 }
568
569 if (access(path, F_OK))
570 {
571 /* 'path' does not exist */
572 err_msg("[PYTHON] Can't open the python module path %s.\n", path);
573 return -1;
574 }
575
576 if (access(path, R_OK))
577 {
578 /* Don't have read access to 'path' */
579 err_msg("[PYTHON] Can't read from the python module path %s.\n", path);
580 return -1;
581 }
582
583 /* Init Python environment */
584
585 /* Set up the python path to be able to load module from our module path */
586 Py_Initialize();
587 Py_InitModule("ganglia", GangliaMethods);
588
589 PyObject *sys_path = PySys_GetObject("path");
590 PyObject *addpath = PyString_FromString(path);
591 PyList_Append(sys_path, addpath);
592
593 PyEval_InitThreads();
594 gtstate = PyEval_SaveThread();
595
596 /* Initialize each python module */
597 if ((dp = opendir(path)) == NULL) {
598 /* Error: Cannot open the directory - Shouldn't happen */
599 /* Log? */
600 err_msg("[PYTHON] Can't open the python module path %s.\n", path);
601 return -1;
602 }
603
604 i = 0;
605
606 while ((entry = readdir(dp)) != NULL) {
607 modname = is_python_module(entry->d_name);
608
609 if (modname == NULL)
610 continue;
611
612 /* Find the specified module configuration in gmond.conf
613 If this return NULL then either the module config
614 doesn't exist or the module is disabled. */
615 module_cfg = find_module_config(modname);
616 if (!module_cfg)
617 continue;
618
619 PyEval_RestoreThread(gtstate);
620
621 pmod = PyImport_ImportModule(modname);
622 if (!pmod) {
623 /* Failed to import module. Log? */
624 err_msg("[PYTHON] Can't import the metric module [%s].\n", modname);
625 if (PyErr_Occurred()) {
626 PyErr_Print();
627 }
628 gtstate = PyEval_SaveThread();
629 continue;
630 }
631
632 pinitfunc = PyObject_GetAttrString(pmod, "metric_init");
633 if (!pinitfunc || !PyCallable_Check(pinitfunc)) {
634 /* No metric_init function. */
635 err_msg("[PYTHON] Can't find the metric_init function in the python module [%s].\n", modname);
636 Py_DECREF(pmod);
637 gtstate = PyEval_SaveThread();
638 continue;
639 }
640
641 /* Build a parameter dictionary to pass to the module */
642 pparamdict = build_params_dict(module_cfg);
643 if (!pparamdict || !PyDict_Check(pparamdict)) {
644 /* No metric_init function. */
645 err_msg("[PYTHON] Can't build the parameters dictionary for [%s].\n", modname);
646 Py_DECREF(pmod);
647 gtstate = PyEval_SaveThread();
648 continue;
649 }
650
651 /* Now call the metric_init method of the python module */
652 pobj = PyObject_CallFunction(pinitfunc, "(N)", pparamdict);
653
654 if (!pobj) {
655 /* failed calling metric_init */
656 err_msg("[PYTHON] Can't call the metric_init function in the python module [%s].\n", modname);
657 if (PyErr_Occurred()) {
658 PyErr_Print();
659 }
660 Py_DECREF(pinitfunc);
661 Py_DECREF(pmod);
662 gtstate = PyEval_SaveThread();
663 continue;
664 }
665
666 if (PyList_Check(pobj)) {
667 int j;
668 int size = PyList_Size(pobj);
669 for (j = 0; j < size; j++) {
670 PyObject* plobj = PyList_GetItem(pobj, j);
671 if (PyMapping_Check(plobj)) {
672 fill_metric_info(plobj, &minfo, modname, pool);
673 gmi = (Ganglia_25metric*)apr_array_push(metric_info);
674 fill_gmi(gmi, &minfo);
675 mi = (mapped_info_t*)apr_array_push(metric_mapping_info);
676 mi->pmod = pmod;
677 mi->mod_name = apr_pstrdup(pool, modname);
678 mi->pcb = minfo.pcb;
679 }
680 }
681 }
682 else if (PyMapping_Check(pobj)) {
683 fill_metric_info(pobj, &minfo, modname, pool);
684 gmi = (Ganglia_25metric*)apr_array_push(metric_info);
685 fill_gmi(gmi, &minfo);
686 mi = (mapped_info_t*)apr_array_push(metric_mapping_info);
687 mi->pmod = pmod;
688 mi->mod_name = apr_pstrdup(pool, modname);
689 mi->pcb = minfo.pcb;
690 }
691 Py_DECREF(pobj);
692 Py_DECREF(pinitfunc);
693 gtstate = PyEval_SaveThread();
694 }
695 closedir(dp);
696
697 apr_pool_cleanup_register(pool, NULL,
698 pyth_metric_cleanup,
699 apr_pool_cleanup_null);
700
701 /* Replace the empty static metric definition array with the
702 dynamic array that we just created
703 */
704 /*XXX Need to put this into a finalize MACRO. This is just pushing
705 a NULL entry onto the array so that the looping logic can
706 determine the end if the array. We should probably give back
707 a ready APR array rather than a pointer to a Ganglia_25metric
708 array. */
709 gmi = apr_array_push(metric_info);
710 memset (gmi, 0, sizeof(*gmi));
711 mi = apr_array_push(metric_mapping_info);
712 memset (mi, 0, sizeof(*mi));
713
714 python_module.metrics_info = (Ganglia_25metric *)metric_info->elts;
715 return 0;
716 }
717
718 static apr_status_t pyth_metric_cleanup ( void *data)
719 {
720 PyObject *pcleanup, *pobj;
721 mapped_info_t *mi, *smi;
722 int i, j;
723
724 mi = (mapped_info_t*) metric_mapping_info->elts;
725 for (i = 0; i < metric_mapping_info->nelts; i++) {
726 if (mi[i].pmod) {
727 PyEval_RestoreThread(gtstate);
728 pcleanup = PyObject_GetAttrString(mi[i].pmod, "metric_cleanup");
729 if (pcleanup && PyCallable_Check(pcleanup)) {
730 pobj = PyObject_CallFunction(pcleanup, NULL);
731 Py_XDECREF(pobj);
732 if (PyErr_Occurred()) {
733 PyErr_Print();
734 }
735 }
736 Py_XDECREF(pcleanup);
737 Py_DECREF(mi[i].pmod);
738 Py_XDECREF(mi[i].pcb);
739 gtstate = PyEval_SaveThread();
740
741 /* Set all modules that fall after this once with the same
742 * module pointer to NULL so metric_cleanup only gets called
743 * once on the module.
744 */
745 smi = (mapped_info_t*) metric_mapping_info->elts;
746 for (j = i+1; j < metric_mapping_info->nelts; j++) {
747 if (smi[j].pmod == mi[i].pmod) {
748 smi[j].pmod = NULL;
749 }
750 }
751 }
752 }
753
754 PyEval_RestoreThread(gtstate);
755 Py_Finalize();
756 return APR_SUCCESS;
757 }
758
759 static g_val_t pyth_metric_handler( int metric_index )
760 {
761 g_val_t val;
762 PyObject *pobj;
763 Ganglia_25metric *gmi = (Ganglia_25metric *) metric_info->elts;
764 mapped_info_t *mi = (mapped_info_t*) metric_mapping_info->elts;
765
766 memset(&val, 0, sizeof(val));
767 if (!mi[metric_index].pcb) {
768 /* No call back provided for this metric */
769 return val;
770 }
771
772 PyEval_RestoreThread(gtstate);
773
774 /* Call the metric handler call back for this metric */
775 pobj = PyObject_CallFunction(mi[metric_index].pcb, "s", gmi[metric_index].name);
776 if (!pobj) {
777 err_msg("[PYTHON] Can't call the metric handler function for [%s] in the python module [%s].\n",
778 gmi[metric_index].name, mi[metric_index].mod_name);
779 if (PyErr_Occurred()) {
780 PyErr_Print();
781 }
782 gtstate = PyEval_SaveThread();
783 /* return what? */
784 return val;
785 }
786
787 switch (gmi[metric_index].type) {
788 case GANGLIA_VALUE_STRING:
789 {
790 get_python_string_value(pobj, val.str, sizeof(val.str));
791 break;
792 }
793 case GANGLIA_VALUE_UNSIGNED_INT:
794 {
795 unsigned int v = 0;
796 get_python_uint_value(pobj, &v);
797 val.uint32 = v;
798 break;
799 }
800 case GANGLIA_VALUE_INT:
801 {
802 int v = 0;
803 get_python_int_value(pobj, &v);
804 val.int32 = v;
805 break;
806 }
807 case GANGLIA_VALUE_FLOAT:
808 {
809 double v = 0.0;
810 get_python_float_value(pobj, &v);
811 val.f = v;
812 break;
813 }
814 case GANGLIA_VALUE_DOUBLE:
815 {
816 double v = 0.0;
817 get_python_float_value(pobj, &v);
818 val.d = v;
819 break;
820 }
821 default:
822 {
823 memset(&val, 0, sizeof(val));
824 break;
825 }
826 }
827 Py_DECREF(pobj);
828 gtstate = PyEval_SaveThread();
829 return val;
830 }
831
832 mmodule python_module =
833 {
834 STD_MMODULE_STUFF,
835 pyth_metric_init,
836 NULL,
837 NULL, /* defined dynamically */
838 pyth_metric_handler,
839 };