artdaq_ganglia_plugin  v1_02_14a
send_gmetric.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <confuse.h> /* header for libconfuse */
6 
7 #include <apr-1/apr.h>
8 #include <apr-1/apr_strings.h>
9 #include <apr-1/apr_pools.h>
10 
11 #include "send_gmetric.h"
12 #include "ganglia.h"
13 
14 Ganglia_pool global_context;
15 Ganglia_gmond_config gmond_config;
16 Ganglia_udp_send_channels send_channels;
17 
18 int init_gmetric(const char* conf)
19 {
20  char* conf_local = (char *)conf;
21 
22  /* create the global context */
23  global_context = Ganglia_pool_create(NULL);
24  if (!global_context)
25  {
26  fprintf(stderr, "Unable to create global context. Exiting.\n");
27  return 1;
28  }
29  /* parse the configuration file */
30  int use_default_config = 1;
31  if (strcmp(conf, "") != 0)
32  {
33  use_default_config = 0;
34  }
35  gmond_config = Ganglia_gmond_config_create(conf_local, use_default_config);
36 
37  /* build the udp send channels */
38  send_channels = Ganglia_udp_send_channels_create(global_context, gmond_config);
39  if (!send_channels)
40  {
41  fprintf(stderr, "Unable to create ganglia send channels. Exiting.\n");
42  return 1;
43  }
44 
45  return 0;
46 }
47 
49 {
50  Ganglia_pool_destroy(global_context);
51 }
52 
53 int send_gmetric(const char* name, const char* value, const char* type, const char* units,
54  const char* slope, int tmax, int dmax, const char* group, const char* cluster,
55  const char* desc, const char* title)
56 {
57  Ganglia_metric gmetric;
58 
59  int rval;
60 
61  /* Create local char* versions of the parameters */
62  char* name_local = (char *)name;
63  char* value_local = (char *)value;
64  char* type_local = (char *)type;
65  char* units_local = (char *)units;
66  char* slope_local = (char *)slope;
67  char* group_local = (char *)group;
68  char* cluster_local = (char *)cluster;
69  char* desc_local = (char *)desc;
70  char* title_local = (char *)title;
71 
72  /* create the message */
73  gmetric = Ganglia_metric_create(global_context);
74  if (!gmetric)
75  {
76  fprintf(stderr, "Unable to allocate gmetric structure. Exiting.\n");
77  return 1;
78  }
79  //apr_pool_t *gm_pool = (apr_pool_t*)gmetric->pool;
80 
81  if (!(strcmp(name, "") != 0 && strcmp(value, "") != 0 && strcmp(type, "") != 0))
82  {
83  fprintf(stderr, "Incorrect options supplied, exiting.\n");
84  return 1;
85  }
86  rval = Ganglia_metric_set(gmetric, name_local, value_local,
87  type_local, units_local, cstr_to_slope(slope_local),
88  tmax, dmax);
89 
90  /* TODO: make this less ugly later */
91  switch (rval)
92  {
93  case 1:
94  fprintf(stderr, "gmetric parameters invalid. exiting.\n");
95  return 1;
96  case 2:
97  fprintf(stderr, "one of your parameters has an invalid character '\"'. exiting.\n");
98  return 1;
99  case 3:
100  fprintf(stderr, "the type parameter \"%s\" is not a valid type. exiting.\n", type);
101  return 1;
102  case 4:
103  fprintf(stderr, "the value parameter \"%s\" does not represent a number. exiting.\n", value);
104  return 1;
105  }
106 
107  if (strcmp(cluster, "") != 0)
108  Ganglia_metadata_add(gmetric, "CLUSTER", cluster_local);
109  if (strcmp(group, "") != 0)
110  {
111  char* last;
112  for (char* groupArg = apr_strtok(group_local, ", ", &last); groupArg != NULL; groupArg = apr_strtok(NULL, ", ", &last))
113  {
114  Ganglia_metadata_add(gmetric, "GROUP", groupArg);
115  }
116  }
117  if (strcmp(desc, "") != 0)
118  Ganglia_metadata_add(gmetric, "DESC", desc_local);
119  if (strcmp(title, "") != 0)
120  Ganglia_metadata_add(gmetric, "TITLE", title_local);
121 
122  /* send the message */
123  rval = Ganglia_metric_send(gmetric, send_channels);
124  if (rval)
125  {
126  fprintf(stderr, "There was an error sending to %d of the send channels.\n", rval);
127  }
128 
129  /* cleanup */
130  // Ganglia_udp_send_channels_destroy(send_channels);
131  // Ganglia_gmond_config_destroy(gmond_config);
132  Ganglia_metric_destroy(gmetric); /* not really necessary but for symmetry */
133 
134  return rval;
135 }
int init_gmetric(const char *conf)
Initialize Ganglia.
Definition: send_gmetric.c:18
void destroy_gmetric()
Close connection to gmond.
Definition: send_gmetric.c:48
int send_gmetric(const char *name, const char *value, const char *type, const char *units, const char *slope, int tmax, int dmax, const char *group, const char *cluster, const char *desc, const char *title)
Send a metric to gmond.
Definition: send_gmetric.c:53