Skip to content

Instantly share code, notes, and snippets.

@mfournier
Created October 21, 2014 14:43
Show Gist options
  • Save mfournier/ad455a71f9e0772b7ae0 to your computer and use it in GitHub Desktop.
Save mfournier/ad455a71f9e0772b7ae0 to your computer and use it in GitHub Desktop.
initial patch adding varnish4 support to collectd
diff --git configure.ac configure.ac
index 9dd30c7..26c342a 100644
--- configure.ac
+++ configure.ac
@@ -4591,7 +4591,7 @@ if test "x$with_libvarnish" = "xyes"
then
SAVE_CPPFLAGS="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS $with_libvarnish_cflags"
- AC_CHECK_HEADERS(varnish/varnishapi.h, [], [with_libvarnish="no (varnish/varnishapi.h not found)"])
+ #AC_CHECK_HEADERS(varnish/varnishapi.h, [], [with_libvarnish="no (varnish/varnishapi.h not found)"])
CPPFLAGS="$SAVE_CPPFLAGS"
fi
@@ -4603,9 +4603,11 @@ then
CPPFLAGS="$CPPFLAGS $with_libvarnish_cflags"
#LDFLAGS="$LDFLAGS $with_libvarnish_libs"
- AC_CHECK_HEADERS(varnish/vsc.h,
- [AC_DEFINE([HAVE_VARNISH_V3], [1], [Varnish 3 API support])],
- [AC_DEFINE([HAVE_VARNISH_V2], [1], [Varnish 2 API support])])
+ #AC_CHECK_HEADERS(varnish/vsc.h,
+ # [AC_DEFINE([HAVE_VARNISH_V3], [1], [Varnish 3 API support])],
+ # [AC_DEFINE([HAVE_VARNISH_V2], [1], [Varnish 2 API support])])
+
+ AC_DEFINE([HAVE_VARNISH_V4], [1], [Varnish 4 API support])
CPPFLAGS="$SAVE_CPPFLAGS"
#LDFLAGS="$SAVE_LDFLAGS"
diff --git src/varnish.c src/varnish.c
index 3a8dddd..6a6a6b6 100644
--- src/varnish.c
+++ src/varnish.c
@@ -28,14 +28,21 @@
#include "plugin.h"
#include "configfile.h"
-#include <varnish/varnishapi.h>
+
+#if HAVE_VARNISH_V4
+#include <varnish/vapi/vsm.h>
+#include <varnish/vapi/vsc.h>
+typedef struct VSC_C_main c_varnish_stats_t;
+#endif
#if HAVE_VARNISH_V3
-# include <varnish/vsc.h>
+#include <varnish/varnishapi.h>
+#include <varnish/vsc.h>
typedef struct VSC_C_main c_varnish_stats_t;
#endif
#if HAVE_VARNISH_V2
+#include <varnish/varnishapi.h>
typedef struct varnish_stats c_varnish_stats_t;
#endif
@@ -141,10 +148,12 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
if (conf->collect_connections)
{
+#if !HAVE_VARNISH_V4
/* Client connections accepted */
varnish_submit_derive (conf->instance, "connections", "connections", "accepted", stats->client_conn);
/* Connection dropped, no sess */
varnish_submit_derive (conf->instance, "connections", "connections", "dropped" , stats->client_drop);
+#endif
/* Client requests received */
varnish_submit_derive (conf->instance, "connections", "connections", "received", stats->client_req);
}
@@ -263,12 +272,14 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
#endif
/* HTTP header overflows */
varnish_submit_derive (conf->instance, "objects", "total_objects", "header_overflow", stats->losthdr);
+#if !HAVE_VARNISH_V4
/* Objects sent with sendfile */
varnish_submit_derive (conf->instance, "objects", "total_objects", "sent_sendfile", stats->n_objsendfile);
/* Objects sent with write */
varnish_submit_derive (conf->instance, "objects", "total_objects", "sent_write", stats->n_objwrite);
/* Objects overflowing workspace */
varnish_submit_derive (conf->instance, "objects", "total_objects", "workspace_overflow", stats->n_objoverflow);
+#endif
}
#if HAVE_VARNISH_V2
@@ -287,7 +298,8 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
/* N duplicate purges removed */
varnish_submit_derive (conf->instance, "purge", "total_operations", "duplicate", stats->n_purge_dups);
}
-#else
+#endif
+#if HAVE_VARNISH_V3
if (conf->collect_ban)
{
/* N total active bans */
@@ -313,8 +325,10 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
varnish_submit_derive (conf->instance, "session", "total_operations", "pipeline", stats->sess_pipeline);
/* Session Read Ahead */
varnish_submit_derive (conf->instance, "session", "total_operations", "readahead", stats->sess_readahead);
+#if !HAVE_VARNISH_V4
/* Session Linger */
varnish_submit_derive (conf->instance, "session", "total_operations", "linger", stats->sess_linger);
+#endif
/* Session herd */
varnish_submit_derive (conf->instance, "session", "total_operations", "herd", stats->sess_herd);
}
@@ -377,10 +391,12 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
if (conf->collect_struct)
{
+#if !HAVE_VARNISH_V4
/* N struct sess_mem */
varnish_submit_gauge (conf->instance, "struct", "current_sessions", "sess_mem", stats->n_sess_mem);
/* N struct sess */
varnish_submit_gauge (conf->instance, "struct", "current_sessions", "sess", stats->n_sess);
+#endif
/* N struct object */
varnish_submit_gauge (conf->instance, "struct", "objects", "object", stats->n_object);
#ifdef HAVE_VARNISH_V3
@@ -415,10 +431,12 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
varnish_submit_derive (conf->instance, "totals", "total_operations", "pass", stats->s_pass);
/* Total fetch */
varnish_submit_derive (conf->instance, "totals", "total_operations", "fetches", stats->s_fetch);
+#if !HAVE_VARNISH_V4
/* Total header bytes */
varnish_submit_derive (conf->instance, "totals", "total_bytes", "header-bytes", stats->s_hdrbytes);
/* Total body byte */
varnish_submit_derive (conf->instance, "totals", "total_bytes", "body-bytes", stats->s_bodybytes);
+#endif
}
#ifdef HAVE_VARNISH_V3
@@ -441,6 +459,7 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
if (conf->collect_workers)
{
+#if HAVE_VARNISH_V3 || HAVE_VARNISH_V2
/* worker threads */
varnish_submit_gauge (conf->instance, "workers", "threads", "worker", stats->n_wrk);
/* worker threads created */
@@ -462,10 +481,11 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
/* work request queue length */
varnish_submit_derive (conf->instance, "workers", "total_requests", "queue_length", stats->n_wrk_lqueue);
#endif
+#endif
}
} /* }}} void varnish_monitor */
-#if HAVE_VARNISH_V3
+#if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
static int varnish_read (user_data_t *ud) /* {{{ */
{
struct VSM_data *vd;
@@ -479,7 +499,9 @@ static int varnish_read (user_data_t *ud) /* {{{ */
conf = ud->data;
vd = VSM_New();
+#if HAVE_VARNISH_V3
VSC_Setup(vd);
+#endif
if (conf->instance != NULL)
{
@@ -495,14 +517,22 @@ static int varnish_read (user_data_t *ud) /* {{{ */
}
}
+#if HAVE_VARNISH_V3
if (VSC_Open (vd, /* diag = */ 1))
+#else /* if HAVE_VARNISH_V4 */
+ if (VSM_Open (vd))
+#endif
{
ERROR ("varnish plugin: Unable to load statistics.");
return (-1);
}
+#if HAVE_VARNISH_V3
stats = VSC_Main(vd);
+#else /* if HAVE_VARNISH_V4 */
+ stats = VSC_Main(vd, NULL);
+#endif
varnish_monitor (conf, stats);
VSM_Close (vd);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment