| 1 |
/******************************************************************************* |
| 2 |
* 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 |
******************************************************************************/ |
| 32 |
|
| 33 |
/* |
| 34 |
* The ganglia metric "C" interface, required for building DSO modules. |
| 35 |
*/ |
| 36 |
#include <gm_metric.h> |
| 37 |
|
| 38 |
#include <stdlib.h> |
| 39 |
#include <strings.h> |
| 40 |
#include <time.h> |
| 41 |
|
| 42 |
/* |
| 43 |
* Declare ourselves so the configuration routines can find and know us. |
| 44 |
* We'll fill it in at the end of the module. |
| 45 |
*/ |
| 46 |
extern mmodule example_module; |
| 47 |
|
| 48 |
static unsigned int random_max = 50; |
| 49 |
static unsigned int constant_value = 20; |
| 50 |
|
| 51 |
static int ex_metric_init ( apr_pool_t *p ) |
| 52 |
{ |
| 53 |
const char* str_params = example_module.module_params; |
| 54 |
apr_array_header_t *list_params = example_module.module_params_list; |
| 55 |
mmparam *params; |
| 56 |
int i; |
| 57 |
|
| 58 |
srand(time(NULL)%99); |
| 59 |
|
| 60 |
/* Read the parameters from the gmond.conf file. */ |
| 61 |
/* Single raw string parameter */ |
| 62 |
if (str_params) { |
| 63 |
debug_msg("[mod_example]Received string params: %s", str_params); |
| 64 |
} |
| 65 |
/* Multiple name/value pair parameters. */ |
| 66 |
if (list_params) { |
| 67 |
debug_msg("[mod_example]Received following params list: "); |
| 68 |
params = (mmparam*) list_params->elts; |
| 69 |
for(i=0; i< list_params->nelts; i++) { |
| 70 |
debug_msg("\tParam: %s = %s", params[i].name, params[i].value); |
| 71 |
if (!strcasecmp(params[i].name, "RandomMax")) { |
| 72 |
random_max = atoi(params[i].value); |
| 73 |
} |
| 74 |
if (!strcasecmp(params[i].name, "ConstantValue")) { |
| 75 |
constant_value = atoi(params[i].value); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/* Initialize the metadata storage for each of the metrics and then |
| 81 |
* store one or more key/value pairs. The define MGROUPS defines |
| 82 |
* the key for the grouping attribute. */ |
| 83 |
MMETRIC_INIT_METADATA(&(example_module.metrics_info[0]),p); |
| 84 |
MMETRIC_ADD_METADATA(&(example_module.metrics_info[0]),MGROUP,"random"); |
| 85 |
MMETRIC_ADD_METADATA(&(example_module.metrics_info[0]),MGROUP,"example"); |
| 86 |
/* |
| 87 |
* Usually a metric will be part of one group, but you can add more |
| 88 |
* if needed as shown above where Random_Numbers is both in the random |
| 89 |
* and example groups. |
| 90 |
*/ |
| 91 |
|
| 92 |
MMETRIC_INIT_METADATA(&(example_module.metrics_info[1]),p); |
| 93 |
MMETRIC_ADD_METADATA(&(example_module.metrics_info[1]),MGROUP,"example"); |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
static void ex_metric_cleanup ( void ) |
| 99 |
{ |
| 100 |
} |
| 101 |
|
| 102 |
static g_val_t ex_metric_handler ( int metric_index ) |
| 103 |
{ |
| 104 |
g_val_t val; |
| 105 |
|
| 106 |
/* The metric_index corresponds to the order in which |
| 107 |
the metrics appear in the metric_info array |
| 108 |
*/ |
| 109 |
switch (metric_index) { |
| 110 |
case 0: |
| 111 |
val.uint32 = rand()%random_max; |
| 112 |
break; |
| 113 |
case 1: |
| 114 |
val.uint32 = constant_value; |
| 115 |
break; |
| 116 |
default: |
| 117 |
val.uint32 = 0; /* default fallback */ |
| 118 |
} |
| 119 |
|
| 120 |
return val; |
| 121 |
} |
| 122 |
|
| 123 |
static Ganglia_25metric ex_metric_info[] = |
| 124 |
{ |
| 125 |
{0, "Random_Numbers", 90, GANGLIA_VALUE_UNSIGNED_INT, "Num", "both", "%u", UDP_HEADER_SIZE+8, "Example module metric (random numbers)"}, |
| 126 |
{0, "Constant_Number", 90, GANGLIA_VALUE_UNSIGNED_INT, "Num", "zero", "%u", UDP_HEADER_SIZE+8, "Example module metric (constant number)"}, |
| 127 |
{0, NULL} |
| 128 |
}; |
| 129 |
|
| 130 |
mmodule example_module = |
| 131 |
{ |
| 132 |
STD_MMODULE_STUFF, |
| 133 |
ex_metric_init, |
| 134 |
ex_metric_cleanup, |
| 135 |
ex_metric_info, |
| 136 |
ex_metric_handler, |
| 137 |
}; |