Code

varnish: isolate varnish v2 code
[collectd.git] / src / varnish.c
index 3a8dddd78ac05bbf2633c362dda2f90a350e0d8a..45e7f9c6f78797bbf8f7c3c69f2149252089df10 100644 (file)
 #include "plugin.h"
 #include "configfile.h"
 
-#include <varnish/varnishapi.h>
+#if HAVE_VARNISH_V4
+#include <vapi/vsm.h>
+#include <vapi/vsc.h>
+typedef struct VSC_C_main c_varnish_stats_t;
+#endif
 
 #if HAVE_VARNISH_V3
-# include <varnish/vsc.h>
+#include <varnishapi.h>
+#include <vsc.h>
 typedef struct VSC_C_main c_varnish_stats_t;
 #endif
 
 #if HAVE_VARNISH_V2
+#include <varnishapi.h>
 typedef struct varnish_stats c_varnish_stats_t;
 #endif
 
@@ -67,11 +73,14 @@ struct user_config_s {
 #endif
        _Bool collect_struct;
        _Bool collect_totals;
-#ifdef HAVE_VARNISH_V3
+#if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
        _Bool collect_uptime;
 #endif
        _Bool collect_vcl;
        _Bool collect_workers;
+#if HAVE_VARNISH_V4
+       _Bool collect_vsm;
+#endif
 };
 typedef struct user_config_s user_config_t; /* }}} */
 
@@ -126,6 +135,7 @@ static int varnish_submit_derive (const char *plugin_instance, /* {{{ */
        return (varnish_submit (plugin_instance, category, type, type_instance, value));
 } /* }}} int varnish_submit_derive */
 
+#if HAVE_VARNISH_V2
 static void varnish_monitor (const user_config_t *conf, /* {{{ */
                const c_varnish_stats_t *stats)
 {
@@ -149,31 +159,12 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
                varnish_submit_derive (conf->instance, "connections", "connections", "received", stats->client_req);
        }
 
-#ifdef HAVE_VARNISH_V3
-       if (conf->collect_dirdns)
-       {
-               /* DNS director lookups */
-               varnish_submit_derive (conf->instance, "dirdns", "cache_operation", "lookups",    stats->dir_dns_lookups);
-               /* DNS director failed lookups */
-               varnish_submit_derive (conf->instance, "dirdns", "cache_result",    "failed",     stats->dir_dns_failed);
-               /* DNS director cached lookups hit */
-               varnish_submit_derive (conf->instance, "dirdns", "cache_result",    "hits",       stats->dir_dns_hit);
-               /* DNS director full dnscache */
-               varnish_submit_derive (conf->instance, "dirdns", "cache_result",    "cache_full", stats->dir_dns_cache_full);
-       }
-#endif
-
        if (conf->collect_esi)
        {
                /* ESI parse errors (unlock)   */
                varnish_submit_derive (conf->instance, "esi", "total_operations", "error",   stats->esi_errors);
-#if HAVE_VARNISH_V2
                /* Objects ESI parsed (unlock) */
                varnish_submit_derive (conf->instance, "esi", "total_operations", "parsed",  stats->esi_parse);
-#else
-               /* ESI parse warnings (unlock) */
-               varnish_submit_derive (conf->instance, "esi", "total_operations", "warning", stats->esi_warnings);
-#endif
        }
 
        if (conf->collect_backend)
@@ -192,13 +183,8 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
                varnish_submit_derive (conf->instance, "backend", "connections", "was-closed"   , stats->backend_toolate);
                /* Backend conn. recycles      */
                varnish_submit_derive (conf->instance, "backend", "connections", "recycled"     , stats->backend_recycle);
-#if HAVE_VARNISH_V2
                /* Backend conn. unused        */
                varnish_submit_derive (conf->instance, "backend", "connections", "unused"       , stats->backend_unused);
-#else
-               /* Backend conn. retry         */
-               varnish_submit_derive (conf->instance, "backend", "connections", "retries"      , stats->backend_retry);
-#endif
                /* Backend requests mades      */
                varnish_submit_derive (conf->instance, "backend", "http_requests", "requests"   , stats->backend_req);
                /* N backends                  */
@@ -225,14 +211,6 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
                varnish_submit_derive (conf->instance, "fetch", "http_requests", "zero"       , stats->fetch_zero);
                /* Fetch failed              */
                varnish_submit_derive (conf->instance, "fetch", "http_requests", "failed"     , stats->fetch_failed);
-#if HAVE_VARNISH_V3
-               /* Fetch no body (1xx)       */
-               varnish_submit_derive (conf->instance, "fetch", "http_requests", "no_body_1xx", stats->fetch_1xx);
-               /* Fetch no body (204)       */
-               varnish_submit_derive (conf->instance, "fetch", "http_requests", "no_body_204", stats->fetch_204);
-               /* Fetch no body (304)       */
-               varnish_submit_derive (conf->instance, "fetch", "http_requests", "no_body_304", stats->fetch_304);
-#endif
        }
 
        if (conf->collect_hcb)
@@ -251,16 +229,12 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
                varnish_submit_derive (conf->instance, "objects", "total_objects", "expired",            stats->n_expired);
                /* N LRU nuked objects           */
                varnish_submit_derive (conf->instance, "objects", "total_objects", "lru_nuked",          stats->n_lru_nuked);
-#if HAVE_VARNISH_V2
                /* N LRU saved objects           */
                varnish_submit_derive (conf->instance, "objects", "total_objects", "lru_saved",          stats->n_lru_saved);
-#endif
                /* N LRU moved objects           */
                varnish_submit_derive (conf->instance, "objects", "total_objects", "lru_moved",          stats->n_lru_moved);
-#if HAVE_VARNISH_V2
                /* N objects on deathrow         */
                varnish_submit_derive (conf->instance, "objects", "total_objects", "deathrow",           stats->n_deathrow);
-#endif
                /* HTTP header overflows         */
                varnish_submit_derive (conf->instance, "objects", "total_objects", "header_overflow",    stats->losthdr);
                /* Objects sent with sendfile    */
@@ -271,7 +245,6 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
                varnish_submit_derive (conf->instance, "objects", "total_objects", "workspace_overflow", stats->n_objoverflow);
        }
 
-#if HAVE_VARNISH_V2
        if (conf->collect_purge)
        {
                /* N total active purges      */
@@ -287,23 +260,6 @@ 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
-       if (conf->collect_ban)
-       {
-               /* N total active bans      */
-               varnish_submit_derive (conf->instance, "ban", "total_operations", "total",          stats->n_ban);
-               /* N new bans added         */
-               varnish_submit_derive (conf->instance, "ban", "total_operations", "added",          stats->n_ban_add);
-               /* N old bans deleted       */
-               varnish_submit_derive (conf->instance, "ban", "total_operations", "deleted",        stats->n_ban_retire);
-               /* N objects tested         */
-               varnish_submit_derive (conf->instance, "ban", "total_operations", "objects_tested", stats->n_ban_obj_test);
-               /* N regexps tested against */
-               varnish_submit_derive (conf->instance, "ban", "total_operations", "regexps_tested", stats->n_ban_re_test);
-               /* N duplicate bans removed */
-               varnish_submit_derive (conf->instance, "ban", "total_operations", "duplicate",      stats->n_ban_dups);
-       }
-#endif
 
        if (conf->collect_session)
        {
@@ -333,7 +289,6 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
                varnish_submit_derive (conf->instance, "shm", "total_operations", "cycles"    , stats->shm_cycles);
        }
 
-#if HAVE_VARNISH_V2
        if (conf->collect_sm)
        {
                /* allocator requests */
@@ -359,7 +314,6 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
                /* SMA bytes free */
                varnish_submit_derive (conf->instance,  "sma", "total_bytes", "free" ,     stats->sma_bfree);
        }
-#endif
 
        if (conf->collect_sms)
        {
@@ -383,15 +337,8 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
                varnish_submit_gauge (conf->instance, "struct", "current_sessions", "sess",      stats->n_sess);
                /* N struct object         */
                varnish_submit_gauge (conf->instance, "struct", "objects", "object",             stats->n_object);
-#ifdef HAVE_VARNISH_V3
-               /* N unresurrected objects */
-               varnish_submit_gauge (conf->instance, "struct", "objects", "vampireobject",      stats->n_vampireobject);
-               /* N struct objectcore     */
-               varnish_submit_gauge (conf->instance, "struct", "objects", "objectcore",         stats->n_objectcore);
-#endif
                /* N struct objecthead     */
                varnish_submit_gauge (conf->instance, "struct", "objects", "objecthead",         stats->n_objecthead);
-#ifdef HAVE_VARNISH_V2
                /* N struct smf            */
                varnish_submit_gauge (conf->instance, "struct", "objects", "smf",                stats->n_smf);
                /* N small free smf         */
@@ -400,7 +347,6 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
                varnish_submit_gauge (conf->instance, "struct", "objects", "smf_large",          stats->n_smf_large);
                /* N struct vbe_conn        */
                varnish_submit_gauge (conf->instance, "struct", "objects", "vbe_conn",           stats->n_vbe_conn);
-#endif
        }
 
        if (conf->collect_totals)
@@ -421,14 +367,6 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
                varnish_submit_derive (conf->instance, "totals", "total_bytes", "body-bytes",   stats->s_bodybytes);
        }
 
-#ifdef HAVE_VARNISH_V3
-       if (conf->collect_uptime)
-       {
-               /* Client uptime */
-               varnish_submit_gauge (conf->instance, "uptime", "uptime", "client_uptime", stats->uptime);
-       }
-#endif
-
        if (conf->collect_vcl)
        {
                /* N vcl total     */
@@ -450,22 +388,17 @@ static void varnish_monitor (const user_config_t *conf, /* {{{ */
                /* worker threads limited */
                varnish_submit_derive (conf->instance, "workers", "total_threads", "limited",     stats->n_wrk_max);
                /* dropped work requests */
-               varnish_submit_derive (conf->instance, "workers", "total_requests", "dropped",    stats->n_wrk_drop);
-#ifdef HAVE_VARNISH_V2
+               varnish_submit_derive (conf->instance, "workers", "total_threads", "dropped",     stats->n_wrk_drop);
                /* queued work requests */
                varnish_submit_derive (conf->instance, "workers", "total_requests", "queued",     stats->n_wrk_queue);
                /* overflowed work requests */
                varnish_submit_derive (conf->instance, "workers", "total_requests", "overflowed", stats->n_wrk_overflow);
-#else
-               /* queued work requests */
-               varnish_submit_derive (conf->instance, "workers", "total_requests", "queued",       stats->n_wrk_queued);
-               /* work request queue length */
-               varnish_submit_derive (conf->instance, "workers", "total_requests", "queue_length", stats->n_wrk_lqueue);
-#endif
        }
+
 } /* }}} void varnish_monitor */
+#endif
 
-#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 +412,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)
        {
@@ -488,6 +423,7 @@ static int varnish_read (user_data_t *ud) /* {{{ */
                status = VSM_n_Arg (vd, conf->instance);
                if (status < 0)
                {
+                       VSM_Delete (vd);
                        ERROR ("varnish plugin: VSM_n_Arg (\"%s\") failed "
                                        "with status %i.",
                                        conf->instance, status);
@@ -495,17 +431,34 @@ 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.");
+               VSM_Delete (vd);
+               ERROR ("varnish plugin: Unable to open connection.");
 
                return (-1);
        }
 
+#if HAVE_VARNISH_V3
        stats = VSC_Main(vd);
+#else /* if HAVE_VARNISH_V4 */
+       stats = VSC_Main(vd, NULL);
+#endif
+       if (!stats)
+       {
+               VSM_Delete (vd);
+               ERROR ("varnish plugin: Unable to get statistics.");
+
+               return (-1);
+       }
+
 
        varnish_monitor (conf, stats);
-       VSM_Close (vd);
+       VSM_Delete (vd);
 
        return (0);
 } /* }}} */
@@ -575,11 +528,14 @@ static int varnish_config_apply_default (user_config_t *conf) /* {{{ */
        conf->collect_sms         = 0;
        conf->collect_struct      = 0;
        conf->collect_totals      = 0;
-#ifdef HAVE_VARNISH_V3
+#if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
        conf->collect_uptime      = 0;
 #endif
        conf->collect_vcl         = 0;
        conf->collect_workers     = 0;
+#if HAVE_VARNISH_V4
+       conf->collect_vsm         = 0;
+#endif
 
        return (0);
 } /* }}} int varnish_config_apply_default */
@@ -699,7 +655,7 @@ static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */
                        cf_util_get_boolean (child, &conf->collect_struct);
                else if (strcasecmp ("CollectTotals", child->key) == 0)
                        cf_util_get_boolean (child, &conf->collect_totals);
-#ifdef HAVE_VARNISH_V3
+#if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
                else if (strcasecmp ("CollectUptime", child->key) == 0)
                        cf_util_get_boolean (child, &conf->collect_uptime);
 #endif
@@ -707,6 +663,10 @@ static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */
                        cf_util_get_boolean (child, &conf->collect_vcl);
                else if (strcasecmp ("CollectWorkers", child->key) == 0)
                        cf_util_get_boolean (child, &conf->collect_workers);
+#if HAVE_VARNISH_V4
+               else if (strcasecmp ("CollectVSM", child->key) == 0)
+                       cf_util_get_boolean (child, &conf->collect_vsm);
+#endif
                else
                {
                        WARNING ("Varnish plugin: Ignoring unknown "
@@ -741,11 +701,15 @@ static int varnish_config_instance (const oconfig_item_t *ci) /* {{{ */
 #endif
                        && !conf->collect_struct
                        && !conf->collect_totals
-#ifdef HAVE_VARNISH_V3
+#if HAVE_VARNISH_V3 || HAVE_VARNISH_V4
                        && !conf->collect_uptime
 #endif
                        && !conf->collect_vcl
-                       && !conf->collect_workers)
+                       && !conf->collect_workers
+#if HAVE_VARNISH_V4
+                       && !conf->collect_vsm
+#endif
+       )
        {
                WARNING ("Varnish plugin: No metric has been configured for "
                                "instance \"%s\". Disabling this instance.",