forked from jonnenauha/prometheus_varnish_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
varnish.go
241 lines (220 loc) · 5.27 KB
/
varnish.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os/exec"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"github.com/prometheus/client_golang/prometheus"
)
const (
varnishstatExe = "varnishstat"
)
var (
descCache = make(map[string]*prometheus.Desc)
mDescCache sync.RWMutex
)
func scrapeVarnish(ch chan<- prometheus.Metric) (*bytes.Buffer, error) {
params := []string{"-j"}
if VarnishVersion.Major >= 4 && VarnishVersion.Minor >= 1 {
// timeout to not hang for a long time if instance is not found.
// Varnish 3.x exits immediately on faulty params
params = append(params, "-t", "2")
}
if !StartParams.Params.isEmpty() {
params = append(params, StartParams.Params.make()...)
}
buf, errExec := executeVarnishstat(params...)
if errExec != nil {
return buf, errExec
}
// The output JSON annoyingly is not stuctured so that we could make a nice map[string]struct for it.
metricsJSON := make(map[string]interface{})
dec := json.NewDecoder(buf)
if err := dec.Decode(&metricsJSON); err != nil {
return buf, err
}
// This is a bit broad but better than locking on each desc query below.
mDescCache.Lock()
defer mDescCache.Unlock()
for vName, raw := range metricsJSON {
if vName == "timestamp" {
continue
}
if dt := reflect.TypeOf(raw); dt.Kind() != reflect.Map {
if StartParams.Verbose {
logWarn("Found unexpected data from json: %s: %#v", vName, raw)
}
continue
}
data, ok := raw.(map[string]interface{})
if !ok {
if StartParams.Verbose {
logWarn("Failed to cast to map[string]interface{}: %s: %#v", vName, raw)
}
continue
}
var (
vGroup = prometheusGroup(vName)
vDescription string
vIdentifier string
vValue float64
vErr error
)
if value, ok := data["description"]; ok && vErr == nil {
if vDescription, ok = value.(string); !ok {
vErr = fmt.Errorf("%s description it not a string", vName)
}
}
if value, ok := data["ident"]; ok && vErr == nil {
if vIdentifier, ok = value.(string); !ok {
vErr = fmt.Errorf("%s ident it not a string", vName)
}
}
if value, ok := data["value"]; ok && vErr == nil {
if vValue, ok = value.(float64); !ok {
vErr = fmt.Errorf("%s value it not a float64", vName)
}
}
if vErr != nil {
if StartParams.Verbose {
logWarn(vErr.Error())
}
continue
}
pName, pDescription, pLabelKeys, pLabelValues := computePrometheusInfo(vName, vGroup, vIdentifier, vDescription)
descKey := pName + "_" + strings.Join(pLabelKeys, "_")
pDesc, ok := descCache[descKey]
if !ok {
pDesc = prometheus.NewDesc(
pName,
pDescription,
pLabelKeys,
nil,
)
descCache[descKey] = pDesc
}
ch <- prometheus.MustNewConstMetric(pDesc, prometheus.GaugeValue, vValue, pLabelValues...)
}
return buf, nil
}
// Returns the result of 'varnishtat' with optional command line params.
func executeVarnishstat(params ...string) (*bytes.Buffer, error) {
buf := bytes.Buffer{}
cmd := exec.Command(varnishstatExe, params...)
cmd.Stdout = &buf
cmd.Stderr = &buf
if err := cmd.Start(); err != nil {
return &buf, err
}
if err := cmd.Wait(); err != nil {
return &buf, err
}
return &buf, nil
}
// varnishVersion
type varnishVersion struct {
Major int
Minor int
Patch int
Revision string
}
func NewVarnishVersion() *varnishVersion {
return &varnishVersion{
Major: -1, Minor: -1, Patch: -1,
}
}
func (v *varnishVersion) Initialize() error {
return v.queryVersion()
}
func (v *varnishVersion) queryVersion() error {
buf, err := executeVarnishstat("-V")
if err != nil {
return err
}
scanner := bufio.NewScanner(buf)
for scanner.Scan() {
return v.parseVersion(scanner.Text())
}
return nil
}
func (v *varnishVersion) parseVersion(version string) error {
r := regexp.MustCompile(`(\d)\.?(\d)?\.?(\d)?(?:.*revision\s(.*)\))?`)
parts := r.FindStringSubmatch(version)
if len(parts) > 1 {
if err := v.set(parts[1:]); err != nil {
return err
}
}
if !v.isValid() {
return fmt.Errorf("Failed to resolve version from %q", version)
}
return nil
}
func (v *varnishVersion) Labels() map[string]string {
labels := make(map[string]string)
if v.Major != -1 {
labels["major"] = strconv.Itoa(v.Major)
}
if v.Minor != -1 {
labels["minor"] = strconv.Itoa(v.Minor)
}
if v.Patch != -1 {
labels["patch"] = strconv.Itoa(v.Patch)
}
if v.Revision != "" {
labels["revision"] = v.Revision
}
labels["version"] = v.VersionString()
return labels
}
func (v *varnishVersion) set(parts []string) error {
for i, part := range parts {
if len(part) == 0 {
continue
}
if i == 3 {
v.Revision = part
break
}
num, err := strconv.Atoi(part)
if err != nil {
return err
}
switch i {
case 0:
v.Major = num
case 1:
v.Minor = num
case 2:
v.Patch = num
}
}
return nil
}
func (v *varnishVersion) isValid() bool {
return v.Major != -1
}
// Version string with numbers only, no revision.
func (v *varnishVersion) VersionString() string {
parts := []string{}
for _, num := range []int{v.Major, v.Minor, v.Patch} {
if num != -1 {
parts = append(parts, strconv.Itoa(num))
}
}
return strings.Join(parts, ".")
}
// Full version string, including revision.
func (v *varnishVersion) String() string {
version := v.VersionString()
if v.Revision != "" {
version += " " + v.Revision
}
return version
}