Skip to content

Instantly share code, notes, and snippets.

@mjf
Last active August 29, 2015 14:17
Show Gist options
  • Save mjf/48c94027a87fecc11843 to your computer and use it in GitHub Desktop.
Save mjf/48c94027a87fecc11843 to your computer and use it in GitHub Desktop.
nagios_varnish_plugin - Monitoring for Varnish Cache
// nagios_varnish_plugin - Monitoring for Varnish Cache
// Copyright (C) 2015 Matous Jan Fialka, <http://mjf.cz/>
// Released under the terms of "The MIT License"
package main
import (
"encoding/json"
"fmt"
"log"
"os/exec"
"strconv"
"strings"
)
const prefix = "Varnish_Cache."
func main() {
out, err := exec.Command("varnishstat", "-j").Output()
if err != nil {
log.Fatalf("%v", err)
}
var jdata interface{}
err = json.Unmarshal(out, &jdata)
if err != nil {
log.Fatalf("%v", err)
}
metric := make(map[string]float64)
info := make(map[string]string)
data := jdata.(map[string]interface{})
for key, value := range data {
switch item := value.(type) {
case map[string]interface{}:
for skey, svalue := range item {
switch sitem := svalue.(type) {
case float64:
if strings.Contains(skey, "value") {
metric[prefix+key] = sitem
}
case string:
if strings.Contains(skey, "description") {
info[prefix+key] = sitem
}
}
}
}
}
metric[prefix+"Custom.cache_hit_ratio"] = func() float64 {
hit := metric[prefix+"MAIN.cache_hit"]
total := hit + metric[prefix+"MAIN.cache_miss"]
if total != 0 {
return (hit / total) * 100
}
return 0
}()
metric[prefix+"Custom.sess_accept_ratio"] = func() float64 {
req := metric[prefix+"MAIN.s_req"]
if req != 0 {
return (metric[prefix+"MAIN.sess_conn"] / req) * 100
}
return 0
}()
metric[prefix+"Custom.backend_success_ratio"] = func() float64 {
conn := metric[prefix+"MAIN.backend_conn"]
total := conn + metric[prefix+"MAIN.backend_fail"]
if total != 0 {
return (conn / total) * 100
}
return 0
}()
for key, value := range metric {
svalue := strconv.FormatFloat(value, 'f', 5, 64)
ilen := len(info[key])
var sinfo string = "Undescribed"
if ilen > 0 {
sinfo = strings.ToUpper(string(info[key][0]))
if ilen > 1 {
sinfo += info[key][1:]
}
}
fmt.Println("0", key, "value="+svalue, "OK -", sinfo)
}
}
// vi:ft=go
@mjf
Copy link
Author

mjf commented Mar 23, 2015

Warning

This is just toy code!

TODO

  • Provide support for varnishadm param.show -l
  • Provide support for "counting" values
  • Provide support for "counting" values storage configuration
  • Provide support for metrics configuration
  • Provide support for stats items configuration
  • Provide support for configurable thresholds for both stats items and metrics
  • Modularize and document source code

Also create:

  • collectd_varnish_plugin (plugin for collectd)
  • snmpd_varnish_plugin (support for snmpd)

Compile

$ go build nagios_varnish_plugin.go

Install

$ sftp root@<hostname>:/usr/share/check-mk-agent/local/
sftp> put nagios_varnish_plugin
sftp> quit

Check_MK

Rescan services in the WATO plugin for the given <hostname>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment