From 7446822df6439e25d04eb456ff085ca2719b6fb1 Mon Sep 17 00:00:00 2001 From: Frederic-Philippe Metz Date: Tue, 3 Feb 2015 13:20:27 +0100 Subject: [PATCH] Forgot the files .. --- daemon/graphite.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++ daemon/graphite.h | 17 ++++++ 2 files changed, 146 insertions(+) create mode 100644 daemon/graphite.c create mode 100644 daemon/graphite.h diff --git a/daemon/graphite.c b/daemon/graphite.c new file mode 100644 index 000000000..496ed1f3d --- /dev/null +++ b/daemon/graphite.c @@ -0,0 +1,129 @@ +/* + * graphite.c + * + * Created on: Jan 19, 2015 + * Author: fmetz + */ +#include +#include +#include +#include +#include + +#include "log.h" +#include "call.h" + +int graphite_sock=0; +u_int32_t graphite_ipaddress; +int graphite_port=0; +struct callmaster* cm=0; +struct totalstats totalstats_prev; + +int connect_to_graphite_server(u_int32_t ipaddress, int port) { + + graphite_sock=0; + int reconnect=0, MAXRECONNECTS=5; + int rc=0; + struct sockaddr_in sin; + memset(&sin,0,sizeof(sin)); + int val=1; + + graphite_ipaddress = ipaddress; + graphite_port = port; + + rc = graphite_sock = socket(AF_INET, SOCK_STREAM,0); + if(rc<0) { + ilog(LOG_ERROR,"Couldn't make socket for connecting to graphite."); + return -1; + } + + sin.sin_family=AF_INET; + sin.sin_addr.s_addr=graphite_ipaddress; + sin.sin_port=htons(graphite_port); + + rc = setsockopt(graphite_sock,SOL_SOCKET,SO_REUSEADDR, &val,sizeof(val)); + if(rc<0) { + ilog(LOG_ERROR,"Couldn't set sockopt for graphite descriptor."); + return -1; + } + + while (reconnecttotalstats_interval.total_average_call_dur.tv_sec,(unsigned)time(NULL)); ptr += rc; + rc = sprintf(ptr,"%s.totals.average_call_dur.tv_usec %i %u\n",hostname, cm->totalstats_interval.total_average_call_dur.tv_usec,(unsigned)time(NULL)); ptr += rc; + rc = sprintf(ptr,"%s.totals.forced_term_sess %i %u\n",hostname, cm->totalstats_interval.total_forced_term_sess,(unsigned)time(NULL)); ptr += rc; + rc = sprintf(ptr,"%s.totals.managed_sess %i %u\n",hostname, cm->totalstats_interval.total_managed_sess,(unsigned)time(NULL)); ptr += rc; + rc = sprintf(ptr,"%s.totals.nopacket_relayed_sess %i %u\n",hostname, cm->totalstats_interval.total_nopacket_relayed_sess,(unsigned)time(NULL)); ptr += rc; + rc = sprintf(ptr,"%s.totals.oneway_stream_sess %i %u\n",hostname, cm->totalstats_interval.total_oneway_stream_sess,(unsigned)time(NULL)); ptr += rc; + rc = sprintf(ptr,"%s.totals.regular_term_sess %i %u\n",hostname, cm->totalstats_interval.total_regular_term_sess,(unsigned)time(NULL)); ptr += rc; + rc = sprintf(ptr,"%s.totals.relayed_errors %i %u\n",hostname, cm->totalstats_interval.total_relayed_errors,(unsigned)time(NULL)); ptr += rc; + rc = sprintf(ptr,"%s.totals.relayed_packets %i %u\n",hostname, cm->totalstats_interval.total_relayed_packets,(unsigned)time(NULL)); ptr += rc; + rc = sprintf(ptr,"%s.totals.silent_timeout_sess %i %u\n",hostname, cm->totalstats_interval.total_silent_timeout_sess,(unsigned)time(NULL)); ptr += rc; + rc = sprintf(ptr,"%s.totals.timeout_sess %i %u\n",hostname, cm->totalstats_interval.total_timeout_sess,(unsigned)time(NULL)); ptr += rc; + + ZERO(cm->totalstats_interval); + + rc = write(graphite_sock, data_to_send, strlen(data_to_send)); + if (rc<0) { + ilog(LOG_ERROR,"Could not write to graphite socket. Disconnecting graphite server."); + close(graphite_sock); graphite_sock=0; + return -1; + } + return 0; +} + +void graphite_loop_run(struct callmaster* callmaster, int seconds) { + + int rc=0; + + if (!cm) + cm = callmaster; + + rc = send_graphite_data(); + if (rc<0) { + rc = connect_to_graphite_server(graphite_ipaddress, graphite_port); + if (rc<0) { + return; + } + } + sleep(seconds); +} diff --git a/daemon/graphite.h b/daemon/graphite.h new file mode 100644 index 000000000..0b51e7b9a --- /dev/null +++ b/daemon/graphite.h @@ -0,0 +1,17 @@ +/* + * graphite.h + * + * Created on: Jan 19, 2015 + * Author: fmetz + */ + +#ifndef GRAPHITE_H_ +#define GRAPHITE_H_ + +#include "call.h" + +int connect_to_graphite_server(u_int32_t ipaddress, int port); +int send_graphite_data(); +void graphite_loop_run(struct callmaster* cm, int seconds); + +#endif /* GRAPHITE_H_ */