00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <unistd.h>
00005 #include <confuse.h>
00006
00007 #include <apr-1/apr.h>
00008 #include <apr-1/apr_strings.h>
00009 #include <apr-1/apr_pools.h>
00010
00011 #include "send_gmetric.h"
00012 #include "ganglia.h"
00013
00014 Ganglia_pool global_context;
00015 Ganglia_gmond_config gmond_config;
00016 Ganglia_udp_send_channels send_channels;
00017
00018 int init_gmetric(const char* conf)
00019 {
00020 char* conf_local = (char *)conf;
00021
00022
00023 global_context = Ganglia_pool_create(NULL);
00024 if (!global_context)
00025 {
00026 fprintf(stderr, "Unable to create global context. Exiting.\n");
00027 return 1;
00028 }
00029
00030 int use_default_config = 1;
00031 if (strcmp(conf, "") != 0)
00032 {
00033 use_default_config = 0;
00034 }
00035 gmond_config = Ganglia_gmond_config_create(conf_local, use_default_config);
00036
00037
00038 send_channels = Ganglia_udp_send_channels_create(global_context, gmond_config);
00039 if (!send_channels)
00040 {
00041 fprintf(stderr, "Unable to create ganglia send channels. Exiting.\n");
00042 return 1;
00043 }
00044
00045 return 0;
00046 }
00047
00048 void destroy_gmetric()
00049 {
00050 Ganglia_pool_destroy(global_context);
00051 }
00052
00053 int send_gmetric(const char* name, const char* value, const char* type, const char* units,
00054 const char* slope, int tmax, int dmax, const char* group, const char* cluster,
00055 const char* desc, const char* title)
00056 {
00057 Ganglia_metric gmetric;
00058
00059 int rval;
00060
00061
00062 char* name_local = (char *)name;
00063 char* value_local = (char *)value;
00064 char* type_local = (char *)type;
00065 char* units_local = (char *)units;
00066 char* slope_local = (char *)slope;
00067 char* group_local = (char *)group;
00068 char* cluster_local = (char *)cluster;
00069 char* desc_local = (char *)desc;
00070 char* title_local = (char *)title;
00071
00072
00073 gmetric = Ganglia_metric_create(global_context);
00074 if (!gmetric)
00075 {
00076 fprintf(stderr, "Unable to allocate gmetric structure. Exiting.\n");
00077 return 1;
00078 }
00079
00080
00081 if (!(strcmp(name, "") != 0 && strcmp(value, "") != 0 && strcmp(type, "") != 0))
00082 {
00083 fprintf(stderr, "Incorrect options supplied, exiting.\n");
00084 return 1;
00085 }
00086 rval = Ganglia_metric_set(gmetric, name_local, value_local,
00087 type_local, units_local, cstr_to_slope(slope_local),
00088 tmax, dmax);
00089
00090
00091 switch (rval)
00092 {
00093 case 1:
00094 fprintf(stderr, "gmetric parameters invalid. exiting.\n");
00095 return 1;
00096 case 2:
00097 fprintf(stderr, "one of your parameters has an invalid character '\"'. exiting.\n");
00098 return 1;
00099 case 3:
00100 fprintf(stderr, "the type parameter \"%s\" is not a valid type. exiting.\n", type);
00101 return 1;
00102 case 4:
00103 fprintf(stderr, "the value parameter \"%s\" does not represent a number. exiting.\n", value);
00104 return 1;
00105 }
00106
00107 if (strcmp(cluster, "") != 0)
00108 Ganglia_metadata_add(gmetric, "CLUSTER", cluster_local);
00109 if (strcmp(group, "") != 0)
00110 {
00111 char* last;
00112 for (char* groupArg = apr_strtok(group_local, ", ", &last); groupArg != NULL; groupArg = apr_strtok(NULL, ", ", &last))
00113 {
00114 Ganglia_metadata_add(gmetric, "GROUP", groupArg);
00115 }
00116 }
00117 if (strcmp(desc, "") != 0)
00118 Ganglia_metadata_add(gmetric, "DESC", desc_local);
00119 if (strcmp(title, "") != 0)
00120 Ganglia_metadata_add(gmetric, "TITLE", title_local);
00121
00122
00123 rval = Ganglia_metric_send(gmetric, send_channels);
00124 if (rval)
00125 {
00126 fprintf(stderr, "There was an error sending to %d of the send channels.\n", rval);
00127 }
00128
00129
00130
00131
00132 Ganglia_metric_destroy(gmetric);
00133
00134 return rval;
00135 }