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