Last active
August 29, 2015 14:17
-
-
Save mjf/48c94027a87fecc11843 to your computer and use it in GitHub Desktop.
nagios_varnish_plugin - Monitoring for Varnish Cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Warning
This is just toy code!
TODO
varnishadm param.show -l
Also create:
collectd_varnish_plugin
(plugin forcollectd
)snmpd_varnish_plugin
(support forsnmpd
)Compile
Install
Check_MK
Rescan services in the WATO plugin for the given
<hostname>
.