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