X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fipvs.c;h=87eee108a79410853b6f02e107697b7a358f465a;hb=218635ab44a8533ef5b23fd1ae78be761ff0600c;hp=9bf2224a816e9b559fef9fb0ef8a70017268b70e;hpb=4f5787fb7afb14dc1643a94bb2e894af66067bb5;p=collectd.git diff --git a/src/ipvs.c b/src/ipvs.c index 9bf2224a..87eee108 100644 --- a/src/ipvs.c +++ b/src/ipvs.c @@ -52,19 +52,16 @@ #endif /* HAVE_IP_VS_H */ #define log_err(...) ERROR ("ipvs: " __VA_ARGS__) - +#define log_info(...) INFO ("ipvs: " __VA_ARGS__) /* * private variables */ - -static int sockfd = -1; - +static int sockfd = -1; /* * libipvs API */ - static struct ip_vs_get_services *ipvs_get_services (void) { struct ip_vs_getinfo ipvs_info; @@ -133,19 +130,46 @@ static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *se) return ret; } /* ip_vs_get_dests */ - /* * collectd plugin API and helper functions */ - static int cipvs_init (void) { + struct ip_vs_getinfo ipvs_info; + + socklen_t len; + if (-1 == (sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW))) { char errbuf[1024]; log_err ("cipvs_init: socket() failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return -1; } + + len = sizeof (ipvs_info); + + if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO, + (void *)&ipvs_info, &len)) { + char errbuf[1024]; + log_err ("cipvs_init: getsockopt() failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + close (sockfd); + sockfd = -1; + return -1; + } + + /* we need IPVS >= 1.1.4 */ + if (ipvs_info.version < ((1 << 16) + (1 << 8) + 4)) { + log_err ("cipvs_init: IPVS version too old (%d.%d.%d < %d.%d.%d)", + NVERSION (ipvs_info.version), 1, 1, 4); + close (sockfd); + sockfd = -1; + return -1; + } + else { + log_info ("Successfully connected to IPVS %d.%d.%d", + NVERSION (ipvs_info.version)); + } return 0; } /* cipvs_init */ @@ -167,7 +191,7 @@ static int get_pi (struct ip_vs_service_entry *se, char *pi, size_t size) /* inet_ntoa() returns a pointer to a statically allocated buffer * I hope non-glibc systems behave the same */ - len = snprintf (pi, size, "%s_%s%u", inet_ntoa (addr), + len = ssnprintf (pi, size, "%s_%s%u", inet_ntoa (addr), (se->protocol == IPPROTO_TCP) ? "TCP" : "UDP", ntohs (se->port)); @@ -191,7 +215,7 @@ static int get_ti (struct ip_vs_dest_entry *de, char *ti, size_t size) /* inet_ntoa() returns a pointer to a statically allocated buffer * I hope non-glibc systems behave the same */ - len = snprintf (ti, size, "%s_%u", inet_ntoa (addr), + len = ssnprintf (ti, size, "%s_%u", inet_ntoa (addr), ntohs (de->port)); if ((0 > len) || (size <= len)) { @@ -211,15 +235,16 @@ static void cipvs_submit_connections (char *pi, char *ti, counter_t value) vl.values = values; vl.values_len = 1; - vl.time = time (NULL); vl.interval = interval_g; - strcpy (vl.host, hostname_g); - strcpy (vl.plugin, "ipvs"); - strcpy (vl.plugin_instance, pi); - strcpy (vl.type_instance, (NULL != ti) ? ti : "total"); + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "ipvs", sizeof (vl.plugin)); + sstrncpy (vl.plugin_instance, pi, sizeof (vl.plugin_instance)); + sstrncpy (vl.type, "connections", sizeof (vl.type)); + sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total", + sizeof (vl.type_instance)); - plugin_dispatch_values ("connections", &vl); + plugin_dispatch_values (&vl); return; } /* cipvs_submit_connections */ @@ -235,15 +260,16 @@ static void cipvs_submit_if (char *pi, char *t, char *ti, vl.values = values; vl.values_len = 2; - vl.time = time (NULL); vl.interval = interval_g; - strcpy (vl.host, hostname_g); - strcpy (vl.plugin, "ipvs"); - strcpy (vl.plugin_instance, pi); - strcpy (vl.type_instance, (NULL != ti) ? ti : "total"); + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "ipvs", sizeof (vl.plugin)); + sstrncpy (vl.plugin_instance, pi, sizeof (vl.plugin_instance)); + sstrncpy (vl.type, t, sizeof (vl.type)); + sstrncpy (vl.type_instance, (NULL != ti) ? ti : "total", + sizeof (vl.type_instance)); - plugin_dispatch_values (t, &vl); + plugin_dispatch_values (&vl); return; } /* cipvs_submit_if */ @@ -252,7 +278,7 @@ static void cipvs_submit_dest (char *pi, struct ip_vs_dest_entry *de) { char ti[DATA_MAX_NAME_LEN]; - if (0 != get_ti (de, ti, DATA_MAX_NAME_LEN)) + if (0 != get_ti (de, ti, sizeof (ti))) return; cipvs_submit_connections (pi, ti, stats.conns); @@ -270,7 +296,7 @@ static void cipvs_submit_service (struct ip_vs_service_entry *se) int i = 0; - if (0 != get_pi (se, pi, DATA_MAX_NAME_LEN)) + if (0 != get_pi (se, pi, sizeof (pi))) return; cipvs_submit_connections (pi, NULL, stats.conns); @@ -287,9 +313,11 @@ static void cipvs_submit_service (struct ip_vs_service_entry *se) static int cipvs_read (void) { struct ip_vs_get_services *services = NULL; - int i = 0; + if (sockfd < 0) + return (-1); + if (NULL == (services = ipvs_get_services ())) return -1; @@ -302,7 +330,10 @@ static int cipvs_read (void) static int cipvs_shutdown (void) { - close (sockfd); + if (sockfd >= 0) + close (sockfd); + sockfd = -1; + return 0; } /* cipvs_shutdown */ @@ -315,4 +346,3 @@ void module_register (void) } /* module_register */ /* vim: set sw=4 ts=4 tw=78 noexpandtab : */ -