varnish v4 compatibility fix (#6168)
* do not add '-t -1' to varnishtat if varnish version <=4 * minor * minor
Ilya Mashchenko committed
May 31, 2019 at 13:56 UTC
00a7734e550156495d01abbfd32acee0faa51304
1 file changed
+52
-6
collectors/python.d.plugin/varnish/varnish.chart.py
+52
-6
@@ -5,9 +5,8 @@
5
6
import re
7
8
-from bases.collection import find_binary
8
from bases.FrameworkServices.ExecutableService import ExecutableService
10
-
9
+from bases.collection import find_binary
10
11
ORDER = [
12
'session_connections',
@@ -138,6 +137,18 @@ CHARTS = {
137
138
VARNISHSTAT = 'varnishstat'
139
140
+re_version = re.compile(r'varnish-(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)')
141
+
142
+
143
+class VarnishVersion:
144
+ def __init__(self, major, minor, patch):
145
+ self.major = major
146
+ self.minor = minor
147
+ self.patch = patch
148
+
149
+ def __str__(self):
150
+ return '{0}.{1}.{2}'.format(self.major, self.minor, self.patch)
151
+
152
153
class Parser:
154
_backend_new = re.compile(r'VBE.([\d\w_.]+)\(.*?\).(beresp[\w_]+)\s+(\d+)')
@@ -185,10 +196,32 @@ class Service(ExecutableService):
196
self.error("can't locate '{0}' binary or binary is not executable by user netdata".format(VARNISHSTAT))
197
return False
198
199
+ command = [varnishstat, '-V']
200
+ reply = self._get_raw_data(stderr=True, command=command)
201
+ if not reply:
202
+ self.error(
203
+ "no output from '{0}'. Is varnish running? Not enough privileges?".format(' '.join(self.command)))
204
+ return False
205
+
206
+ ver = parse_varnish_version(reply)
207
+ if not ver:
208
+ self.error("failed to parse reply from '{0}', used regex :'{1}', reply : {2}".format(
209
+ ' '.join(command),
210
+ re_version.pattern,
211
+ reply,
212
+ ))
213
+ return False
214
+
215
if self.instance_name:
189
- self.command = [varnishstat, '-1', '-n', self.instance_name, '-t', '1']
216
+ self.command = [varnishstat, '-1', '-n', self.instance_name]
217
else:
191
- self.command = [varnishstat, '-1', '-t', '1']
218
+ self.command = [varnishstat, '-1']
219
+
220
+ if ver.major > 4:
221
+ self.command.extend(['-t', '1'])
222
+
223
+ self.info("varnish version: {0}, will use command: '{1}'".format(ver, ' '.join(self.command)))
224
+
225
return True
226
227
def check(self):
@@ -198,14 +231,14 @@ class Service(ExecutableService):
231
# STDOUT is not empty
232
reply = self._get_raw_data()
233
if not reply:
201
- self.error("No output from 'varnishstat'. Is it running? Not enough privileges?")
234
+ self.error("no output from '{0}'. Is it running? Not enough privileges?".format(' '.join(self.command)))
235
return False
236
237
self.parser.init(reply)
238
239
# Output is parsable
240
if not self.parser.re_default:
208
- self.error('Cant parse the output...')
241
+ self.error('cant parse the output...')
242
return False
243
244
if self.parser.re_backend:
@@ -260,3 +293,16 @@ class Service(ExecutableService):
293
294
self.order.insert(0, chart_name)
295
self.definitions.update(chart)
296
+
297
+
298
+def parse_varnish_version(lines):
299
+ m = re_version.search(lines[0])
300
+ if not m:
301
+ return None
302
+
303
+ m = m.groupdict()
304
+ return VarnishVersion(
305
+ int(m['major']),
306
+ int(m['minor']),
307
+ int(m['patch']),
308
+ )