#endif /* HAVE_NETINET_IN_H */
/* this can probably only be found in the kernel sources */
-#if HAVE_NET_IP_VS_H
+#if HAVE_LINUX_IP_VS_H
+# include <linux/ip_vs.h>
+#elif HAVE_NET_IP_VS_H
# include <net/ip_vs.h>
#elif HAVE_IP_VS_H
# include <ip_vs.h>
#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 void *ipvs_func = NULL;
-
-static struct ip_vs_getinfo ipvs_info;
-
+static int sockfd = -1;
/*
* libipvs API
*/
-
-static struct ip_vs_get_services *ipvs_get_services (void);
-static struct ip_vs_get_dests *ipvs_get_dests (struct ip_vs_service_entry *);
-
-static const char *ipvs_strerror (int err)
-{
- char errbuf[1024];
- unsigned int i;
-
- struct {
- void *func;
- int err;
- const char *message;
- } table [] = {
- { 0, EPERM, "Permission denied (you must be root)" },
- { 0, EINVAL, "Module is wrong version" },
- { 0, ENOPROTOOPT, "Protocol not available" },
- { 0, ENOMEM, "Memory allocation problem" },
- { ipvs_get_services, ESRCH, "No such service" },
- { ipvs_get_dests, ESRCH, "No such service" },
- };
-
- for (i = 0; i < sizeof (table) / sizeof (table[0]); i++) {
- if (((NULL == table[i].func) || (table[i].func == ipvs_func))
- && (table[i].err == err))
- return table[i].message;
- }
- return sstrerror (err, errbuf, sizeof (errbuf));
-} /* ipvs_strerror */
-
static struct ip_vs_get_services *ipvs_get_services (void)
{
+ struct ip_vs_getinfo ipvs_info;
struct ip_vs_get_services *ret;
+
socklen_t len;
+ len = sizeof (ipvs_info);
+
+ if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_INFO,
+ (void *)&ipvs_info, &len)) {
+ char errbuf[1024];
+ log_err ("ip_vs_get_services: getsockopt() failed: %s",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+ return NULL;
+ }
+
len = sizeof (*ret) +
sizeof (struct ip_vs_service_entry) * ipvs_info.num_services;
exit (3);
}
- ipvs_func = ipvs_get_services;
-
ret->num_services = ipvs_info.num_services;
if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_SERVICES,
(void *)ret, &len)) {
+ char errbuf[1024];
log_err ("ipvs_get_services: getsockopt failed: %s",
- ipvs_strerror (errno));
+ sstrerror (errno, errbuf, sizeof (errbuf)));
free(ret);
return NULL;
exit (3);
}
- ipvs_func = ipvs_get_dests;
-
ret->fwmark = se->fwmark;
ret->protocol = se->protocol;
ret->addr = se->addr;
if (0 != getsockopt (sockfd, IPPROTO_IP, IP_VS_SO_GET_DESTS,
(void *)ret, &len)) {
+ char errbuf[1024];
log_err ("ipvs_get_dests: getsockopt() failed: %s",
- ipvs_strerror (errno));
+ sstrerror (errno, errbuf, sizeof (errbuf)));
free (ret);
return NULL;
}
return ret;
} /* ip_vs_get_dests */
-
/*
* collectd plugin API and helper functions
*/
-
static int cipvs_init (void)
{
- socklen_t len;
+ struct ip_vs_getinfo ipvs_info;
- len = sizeof (ipvs_info);
+ socklen_t len;
if (-1 == (sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_RAW))) {
- log_err ("cipvs_init: socket() failed: %s", ipvs_strerror (errno));
+ 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)) {
- log_err ("cipvs_init: getsockopt() failed: %s", ipvs_strerror (errno));
+ 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 */
/* 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));
/* 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)) {
return 0;
} /* get_ti */
-static void cipvs_submit_connections (char *pi, char *ti, counter_t value)
+static void cipvs_submit_connections (char *pi, char *ti, derive_t value)
{
value_t values[1];
value_list_t vl = VALUE_LIST_INIT;
- values[0].counter = value;
+ values[0].derive = 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 */
static void cipvs_submit_if (char *pi, char *t, char *ti,
- counter_t rx, counter_t tx)
+ derive_t rx, derive_t tx)
{
value_t values[2];
value_list_t vl = VALUE_LIST_INIT;
- values[0].counter = rx;
- values[1].counter = tx;
+ values[0].derive = rx;
+ values[1].derive = tx;
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 */
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);
int i = 0;
- if (0 != get_pi (se, pi, DATA_MAX_NAME_LEN))
+ if (0 != get_pi (se, pi, sizeof (pi)))
+ {
+ free (dests);
return;
+ }
cipvs_submit_connections (pi, NULL, stats.conns);
cipvs_submit_if (pi, "if_packets", NULL, stats.inpkts, stats.outpkts);
for (i = 0; i < dests->num_dests; ++i)
cipvs_submit_dest (pi, &dests->entrytable[i]);
+
+ free (dests);
return;
} /* cipvs_submit_service */
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;
static int cipvs_shutdown (void)
{
- close (sockfd);
+ if (sockfd >= 0)
+ close (sockfd);
+ sockfd = -1;
+
return 0;
} /* cipvs_shutdown */
} /* module_register */
/* vim: set sw=4 ts=4 tw=78 noexpandtab : */
-