| 1 | package mainscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/ |
| 2 | |
| 3 | import ( |
| 4 | "bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/" |
| 5 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 6 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/" |
| 7 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 8 | "log/slogscip-go gomod github.com/golang/go/src go1.25.0 `log/slog`/" |
| 9 | "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/" |
| 10 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 11 | "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/" |
| 12 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 13 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 14 | "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/" |
| 15 | |
| 16 | "github.com/prometheus/client_golang/prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/" |
| 17 | ) |
| 18 | |
| 19 | const ( |
| 20 | vbeReloadscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/vbeReload. = "VBE.reload_" |
| 21 | ) |
| 22 | |
| 23 | var ( |
| 24 | DescCachescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/DescCache. = &descCachescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#{ |
| 25 | descsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#descs.: make(map[string]*prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc#), |
| 26 | } |
| 27 | ) |
| 28 | |
| 29 | type descCachescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache# struct { |
| 30 | syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.RWMutexscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#RWMutex. |
| 31 | |
| 32 | descsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#descs. map[string]*prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc# |
| 33 | } |
| 34 | |
| 35 | func (dc *descCachescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#) Descscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#Desc().(key string) *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc# { |
| 36 | dc.RLockscip-go gomod github.com/golang/go/src go1.25.0 sync/RWMutex#RLock().() |
| 37 | desc := dc.descsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#descs.[key] |
| 38 | dc.RUnlockscip-go gomod github.com/golang/go/src go1.25.0 sync/RWMutex#RUnlock().() |
| 39 | return desc |
| 40 | } |
| 41 | |
| 42 | func (dc *descCachescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#) Setscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#Set().(key string, desc *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc#) *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc# { |
| 43 | dc.Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/RWMutex#Lock().() |
| 44 | dc.descsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#descs.[key] = desc |
| 45 | dc.Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/RWMutex#Unlock().() |
| 46 | return desc |
| 47 | } |
| 48 | |
| 49 | func ScrapeVarnishscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/ScrapeVarnish().(ch chan<- prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Metricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#) ([]byte, error) { |
| 50 | params := []string{"-j"} |
| 51 | if VarnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/VarnishVersion..EqualsOrGreaterscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#EqualsOrGreater().(4, 1) { |
| 52 | // 4.1 started to support timeout to exit immediately on connection errors. |
| 53 | // Before that varnishstat exits immediately on faulty params or connection errors. |
| 54 | params = append(params, "-t", "0") |
| 55 | } |
| 56 | if !StartParamsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/StartParams..Paramsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/startParams#Params..isEmptyscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishstatParams#isEmpty().() { |
| 57 | params = append(params, StartParamsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/StartParams..Paramsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/startParams#Params..makescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishstatParams#make().()...) |
| 58 | } |
| 59 | buf, errExec := executeVarnishstatscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/executeVarnishstat().(StartParamsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/StartParams..VarnishstatExescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/startParams#VarnishstatExe., params...) |
| 60 | if errExec != nil { |
| 61 | return buf.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().(), fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%s scrape failed: %s", StartParamsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/StartParams..VarnishstatExescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/startParams#VarnishstatExe., errExec) |
| 62 | } |
| 63 | return ScrapeVarnishFromscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/ScrapeVarnishFrom().(buf.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().(), ch) |
| 64 | } |
| 65 | |
| 66 | func ScrapeVarnishFromscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/ScrapeVarnishFrom().(buf []byte, ch chan<- prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Metricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#) ([]byte, error) { |
| 67 | // The output JSON annoyingly is not structured so that we could make a nice map[string]struct for it. |
| 68 | metricsJSON := make(map[string]any) |
| 69 | dec := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.NewDecoderscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/NewDecoder().(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.NewBufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/NewBuffer().(buf)) |
| 70 | dec.UseNumberscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Decoder#UseNumber().() |
| 71 | if err := dec.Decodescip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Decoder#Decode().(&metricsJSON); err != nil { |
| 72 | return buf, err |
| 73 | } |
| 74 | |
| 75 | countersJSON := make(map[string]any) |
| 76 | // From Varnish 6.5 https://varnish-cache.org/docs/6.5/whats-new/upgrading-6.5.html#varnishstat |
| 77 | if metricsJSON["version"] != nil { |
| 78 | version_raw, ok := metricsJSON["version"].(jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Numberscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Number#) |
| 79 | if !ok { |
| 80 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("Unhandled json stats version type: %T %#v", metricsJSON["version"], metricsJSON["version"]) |
| 81 | } |
| 82 | version, err := version_raw.Int64scip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Number#Int64().() |
| 83 | if err != nil { |
| 84 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("Unhandled json stats version type: %s", err) |
| 85 | } |
| 86 | switch version { |
| 87 | case 1: |
| 88 | countersJSON = metricsJSON["counters"].(map[string]any) |
| 89 | default: |
| 90 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("Unimplemented json stats version %d", version) |
| 91 | } |
| 92 | } else { |
| 93 | countersJSON = metricsJSON |
| 94 | } |
| 95 | |
| 96 | mostRecentVbeReloadPrefix := findMostRecentVbeReloadPrefixscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/findMostRecentVbeReloadPrefix().(countersJSON) |
| 97 | |
| 98 | for vName, raw := range countersJSON { |
| 99 | |
| 100 | if isOutdatedVbescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/isOutdatedVbe().(vName, mostRecentVbeReloadPrefix) { |
| 101 | continue |
| 102 | } |
| 103 | if vName == "timestamp" { |
| 104 | continue |
| 105 | } |
| 106 | if dt := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.TypeOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/TypeOf().(raw); dt.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Type#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Mapscip-go gomod github.com/golang/go/src go1.25.0 reflect/Map. { |
| 107 | slogscip-go gomod github.com/golang/go/src go1.25.0 `log/slog`/.Warnscip-go gomod github.com/golang/go/src go1.25.0 `log/slog`/Warn().("Found unexpected data from json", "name", vName, "raw", raw) |
| 108 | continue |
| 109 | } |
| 110 | data, ok := raw.(map[string]any) |
| 111 | if !ok { |
| 112 | slogscip-go gomod github.com/golang/go/src go1.25.0 `log/slog`/.Warnscip-go gomod github.com/golang/go/src go1.25.0 `log/slog`/Warn().("Failed to cast to map[string]any", "name", vName, "raw", raw) |
| 113 | continue |
| 114 | } |
| 115 | var ( |
| 116 | vGroup = prometheusGroupscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/prometheusGroup().(vName) |
| 117 | vDescription string |
| 118 | vIdentifier string |
| 119 | vValue float64 |
| 120 | iValue uint64 |
| 121 | vErr error |
| 122 | ) |
| 123 | flag, _ := stringPropertyscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/stringProperty().(data, "flag") |
| 124 | |
| 125 | if value, ok := data["description"]; ok { |
| 126 | if vDescription, ok = value.(string); !ok { |
| 127 | vErr = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%s description it not a string", vName) |
| 128 | } |
| 129 | } |
| 130 | if value, ok := data["ident"]; ok && vErr == nil { |
| 131 | if vIdentifier, ok = value.(string); !ok { |
| 132 | vErr = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%s ident it not a string", vName) |
| 133 | } |
| 134 | } |
| 135 | if value, ok := data["value"]; ok && vErr == nil { |
| 136 | if number, ok := value.(jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Numberscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Number#); ok { |
| 137 | if vValue, vErr = number.Float64scip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Number#Float64().(); vErr != nil { |
| 138 | vErr = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%s value float64 error: %s", vName, vErr) |
| 139 | } |
| 140 | if flag == "b" { |
| 141 | if iValue, vErr = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseUintscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseUint().(number.Stringscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Number#String().(), 10, 64); vErr != nil { |
| 142 | vErr = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%s value uint64 error: %s", vName, vErr) |
| 143 | } |
| 144 | } |
| 145 | } else { |
| 146 | vErr = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%s value it not a float64", vName) |
| 147 | } |
| 148 | } |
| 149 | if vErr != nil { |
| 150 | slogscip-go gomod github.com/golang/go/src go1.25.0 `log/slog`/.Warnscip-go gomod github.com/golang/go/src go1.25.0 `log/slog`/Warn().(vErr.Error()) |
| 151 | continue |
| 152 | } |
| 153 | |
| 154 | pName, pDescription, pLabelKeys, pLabelValues := computePrometheusInfoscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/computePrometheusInfo().(vName, vGroup, vIdentifier, vDescription) |
| 155 | |
| 156 | descKey := pName + "_" + stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(pLabelKeys, "_") |
| 157 | pDesc := DescCachescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/DescCache..Descscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#Desc().(descKey) |
| 158 | if pDesc == nil { |
| 159 | pDesc = DescCachescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/DescCache..Setscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#Set().(descKey, prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.NewDescscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/NewDesc().( |
| 160 | pName, |
| 161 | pDescription, |
| 162 | pLabelKeys, |
| 163 | nil, |
| 164 | )) |
| 165 | } |
| 166 | |
| 167 | var metricType prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.ValueTypescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/ValueType# |
| 168 | switch flag { |
| 169 | case "c", "a": |
| 170 | metricType = prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.CounterValuescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterValue. |
| 171 | case "g": |
| 172 | metricType = prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.GaugeValuescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/GaugeValue. |
| 173 | default: |
| 174 | metricType = prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.GaugeValuescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/GaugeValue. |
| 175 | } |
| 176 | |
| 177 | ch <- prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.MustNewConstMetricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/MustNewConstMetric().(pDesc, metricType, vValue, pLabelValues...) |
| 178 | |
| 179 | // augment varnish_backend_up from _happy varnish bitmap value |
| 180 | // we are only interested in the latest happy value (up or down) on each scrape |
| 181 | // see draw_line_bitmap function from https://github.com/varnishcache/varnish-cache/blob/master/bin/varnishstat/varnishstat_curses.c |
| 182 | if pName == "varnish_backend_happy" { |
| 183 | upName := "varnish_backend_up" |
| 184 | upDesc := "Backend up as per the latest health probe" |
| 185 | upValue := 0.0 |
| 186 | if iValue > 0 && (iValue&uint64(1)) > 0 { |
| 187 | upValue = 1.0 |
| 188 | } |
| 189 | |
| 190 | descKey = upName + "_" + stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(pLabelKeys, "_") |
| 191 | pDesc = DescCachescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/DescCache..Descscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#Desc().(descKey) |
| 192 | if pDesc == nil { |
| 193 | pDesc = DescCachescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/DescCache..Setscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/descCache#Set().(descKey, prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.NewDescscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/NewDesc().( |
| 194 | upName, |
| 195 | upDesc, |
| 196 | pLabelKeys, |
| 197 | nil, |
| 198 | )) |
| 199 | } |
| 200 | ch <- prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.MustNewConstMetricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/MustNewConstMetric().(pDesc, prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.GaugeValuescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/GaugeValue., upValue, pLabelValues...) |
| 201 | } |
| 202 | } |
| 203 | return buf, nil |
| 204 | } |
| 205 | |
| 206 | // Returns the result of 'varnishtat' with optional command line params. |
| 207 | func executeVarnishstatscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/executeVarnishstat().(varnishstatExe string, params ...string) (*bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#, error) { |
| 208 | buf := &bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#{} |
| 209 | var cmd *execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Cmdscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd# |
| 210 | if len(StartParamsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/StartParams..VarnishDockerContainerscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/startParams#VarnishDockerContainer.) == 0 { |
| 211 | cmd = execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().(varnishstatExe, params...) |
| 212 | } else { |
| 213 | cmd = execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().("docker", append([]string{"exec", "-t", StartParamsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/StartParams..VarnishDockerContainerscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/startParams#VarnishDockerContainer., varnishstatExe}, params...)...) |
| 214 | } |
| 215 | cmd.Stdoutscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stdout. = buf |
| 216 | cmd.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stderr. = buf |
| 217 | return buf, cmd.Runscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Run().() |
| 218 | } |
| 219 | |
| 220 | // Returns the most recent prefix for 'VBE.reload_' stats. Empty until first reload. |
| 221 | // 'VBE.reload_2019-08-29T100458' as by varnish_reload_vcl in 4.1+ |
| 222 | // 'VBE.reload_20191014_091124_78599' as by varnishreload in 6+ |
| 223 | func findMostRecentVbeReloadPrefixscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/findMostRecentVbeReloadPrefix().(countersJSON map[string]any) string { |
| 224 | var mostRecentVbeReloadPrefix string |
| 225 | for vName := range countersJSON { |
| 226 | // Checking only the required ".happy" stat |
| 227 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(vName, vbeReloadscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/vbeReload.) && stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(vName, ".happy") { |
| 228 | dotAfterPrefixIndex := len(vbeReloadscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/vbeReload.) + stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.0 strings/Index().(vName[len(vbeReloadscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/vbeReload.):], ".") |
| 229 | vbeReloadPrefix := vName[:dotAfterPrefixIndex] |
| 230 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Comparescip-go gomod github.com/golang/go/src go1.25.0 strings/Compare().(vbeReloadPrefix, mostRecentVbeReloadPrefix) > 0 { |
| 231 | mostRecentVbeReloadPrefix = vbeReloadPrefix |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | return mostRecentVbeReloadPrefix |
| 236 | } |
| 237 | |
| 238 | // Returns true if the given 'VBE.' statistic refers to an outdated VCL version |
| 239 | func isOutdatedVbescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/isOutdatedVbe().(vName string, mostRecentVbeReloadPrefix string) bool { |
| 240 | return len(mostRecentVbeReloadPrefix) > 0 && |
| 241 | stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(vName, "VBE.") && |
| 242 | !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(vName, mostRecentVbeReloadPrefix) |
| 243 | } |
| 244 | |
| 245 | // varnishVersion |
| 246 | |
| 247 | type varnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion# struct { |
| 248 | Majorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Major. int |
| 249 | Minorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Minor. int |
| 250 | Patchscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Patch. int |
| 251 | Revisionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Revision. string |
| 252 | } |
| 253 | |
| 254 | func NewVarnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/NewVarnishVersion().() *varnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion# { |
| 255 | return &varnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#{ |
| 256 | Majorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Major.: -1, Minorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Minor.: -1, Patchscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Patch.: -1, |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | func (v *varnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#) EqualsOrGreaterscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#EqualsOrGreater().(major, minor int) bool { |
| 261 | if v.Majorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Major. > major { |
| 262 | return true |
| 263 | } else if v.Majorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Major. == major && v.Minorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Minor. >= minor { |
| 264 | return true |
| 265 | } |
| 266 | return false |
| 267 | } |
| 268 | |
| 269 | func (v *varnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#) Validscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Valid().() bool { |
| 270 | return v.Majorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Major. != -1 |
| 271 | } |
| 272 | |
| 273 | func (v *varnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#) Initializescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Initialize().() error { |
| 274 | return v.queryVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#queryVersion().() |
| 275 | } |
| 276 | |
| 277 | func (v *varnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#) queryVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#queryVersion().() error { |
| 278 | buf, err := executeVarnishstatscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/executeVarnishstat().(StartParamsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/StartParams..VarnishstatExescip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/startParams#VarnishstatExe., "-V") |
| 279 | if err != nil { |
| 280 | return err |
| 281 | } |
| 282 | if scanner := bufioscip-go gomod github.com/golang/go/src go1.25.0 bufio/.NewScannerscip-go gomod github.com/golang/go/src go1.25.0 bufio/NewScanner().(buf); scanner.Scanscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Scan().() { |
| 283 | return v.parseVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#parseVersion().(scanner.Textscip-go gomod github.com/golang/go/src go1.25.0 bufio/Scanner#Text().()) |
| 284 | } |
| 285 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("Failed to get varnishstat -V output") |
| 286 | } |
| 287 | |
| 288 | func (v *varnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#) parseVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#parseVersion().(version string) error { |
| 289 | r := regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile().(`(?P<major>\d+)(\.(?P<minor>\d+))?(\.(?P<patch>\d+))?(.*revision\s(?P<revision>[0-9a-f]*)\))?`) |
| 290 | match := r.FindStringSubmatchscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#FindStringSubmatch().(version) |
| 291 | |
| 292 | parts := make(map[string]string) |
| 293 | for i, name := range r.SubexpNamesscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#SubexpNames().() { |
| 294 | if i != 0 && name != "" { |
| 295 | parts[name] = match[i] |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if len(parts) > 1 { |
| 300 | if err := v.setscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#set().(parts); err != nil { |
| 301 | return err |
| 302 | } |
| 303 | } |
| 304 | if !v.Validscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Valid().() { |
| 305 | return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("Failed to resolve version from %q", version) |
| 306 | } |
| 307 | return nil |
| 308 | } |
| 309 | |
| 310 | func (v *varnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#) Labelsscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Labels().() map[string]string { |
| 311 | labels := make(map[string]string) |
| 312 | if v.Majorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Major. != -1 { |
| 313 | labels["major"] = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Itoascip-go gomod github.com/golang/go/src go1.25.0 strconv/Itoa().(v.Majorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Major.) |
| 314 | } |
| 315 | if v.Minorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Minor. != -1 { |
| 316 | labels["minor"] = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Itoascip-go gomod github.com/golang/go/src go1.25.0 strconv/Itoa().(v.Minorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Minor.) |
| 317 | } |
| 318 | if v.Patchscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Patch. != -1 { |
| 319 | labels["patch"] = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Itoascip-go gomod github.com/golang/go/src go1.25.0 strconv/Itoa().(v.Patchscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Patch.) |
| 320 | } |
| 321 | if v.Revisionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Revision. != "" { |
| 322 | labels["revision"] = v.Revisionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Revision. |
| 323 | } |
| 324 | labels["version"] = v.VersionStringscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#VersionString().() |
| 325 | return labels |
| 326 | } |
| 327 | |
| 328 | func (v *varnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#) setscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#set().(parts map[string]string) error { |
| 329 | for name, value := range parts { |
| 330 | // skip empty value |
| 331 | if len(value) == 0 { |
| 332 | continue |
| 333 | } |
| 334 | |
| 335 | // save revision as-is (string) |
| 336 | if name == "revision" { |
| 337 | v.Revisionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Revision. = value |
| 338 | continue |
| 339 | } |
| 340 | |
| 341 | // convert semver parts to integer and save it |
| 342 | num, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(value) |
| 343 | if err != nil { |
| 344 | return err |
| 345 | } |
| 346 | switch name { |
| 347 | case "major": |
| 348 | v.Majorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Major. = num |
| 349 | case "minor": |
| 350 | v.Minorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Minor. = num |
| 351 | case "patch": |
| 352 | v.Patchscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Patch. = num |
| 353 | } |
| 354 | } |
| 355 | return nil |
| 356 | } |
| 357 | |
| 358 | // Version string with numbers only, no revision. |
| 359 | func (v *varnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#) VersionStringscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#VersionString().() string { |
| 360 | parts := []string{} |
| 361 | for _, num := range []int{v.Majorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Major., v.Minorscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Minor., v.Patchscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Patch.} { |
| 362 | if num != -1 { |
| 363 | parts = append(parts, strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Itoascip-go gomod github.com/golang/go/src go1.25.0 strconv/Itoa().(num)) |
| 364 | } |
| 365 | } |
| 366 | return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(parts, ".") |
| 367 | } |
| 368 | |
| 369 | // Full version string, including revision. |
| 370 | func (v *varnishVersionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#) Stringscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#String().() string { |
| 371 | version := v.VersionStringscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#VersionString().() |
| 372 | if v.Revisionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Revision. != "" { |
| 373 | version += " " + v.Revisionscip-go gomod github.com/otto-de/prometheus_varnish_exporter . `github.com/otto-de/prometheus_varnish_exporter`/varnishVersion#Revision. |
| 374 | } |
| 375 | return version |
| 376 | } |