Ema has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/282887

Change subject: Port varnishxcps to new VSL API
......................................................................

Port varnishxcps to new VSL API

Bug: T131353
Change-Id: I157ec0bcaf77625526410170a5a56a41c3317d42
---
A modules/varnish/files/varnishxcps4
M modules/varnish/manifests/logging/xcps.pp
2 files changed, 103 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/87/282887/1

diff --git a/modules/varnish/files/varnishxcps4 
b/modules/varnish/files/varnishxcps4
new file mode 100755
index 0000000..4008d6f
--- /dev/null
+++ b/modules/varnish/files/varnishxcps4
@@ -0,0 +1,95 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+  varnishxcps
+  ~~~~~~~~~~~
+
+  Accumulate X-Client-Connection stats and report them to StatsD.
+
+  Usage: varnishxcps [--statsd-server SERVER] [--key-prefix PREFIX]
+
+    --statsd-server SERVER  statsd server (default: none; echo to stdout)
+    --key-prefix PREFIX     metric key prefix (default: varnish.clients)
+
+  Copyright 2015 Ori Livneh <o...@wikimedia.org>
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+"""
+import argparse
+import io
+import re
+import socket
+import threading
+import urlparse
+
+import varnishlog
+
+
+def parse_statsd_server_string(server_string):
+    parsed = urlparse.urlparse('//' + server_string)
+    return parsed.hostname, parsed.port or 8125
+
+
+def parse_prefix_string(key_prefix):
+    key_prefix = key_prefix.strip('.')
+    if not key_prefix:
+        raise ValueError('Key prefix must not be empty')
+    return key_prefix
+
+
+ap = argparse.ArgumentParser(
+    description='X-Connection-Properties StatsD reporter',
+    epilog='If no statsd server is specified, prints stats to stdout instead.'
+)
+ap.add_argument('--statsd-server', help='statsd server',
+                type=parse_statsd_server_string, default=None)
+ap.add_argument('--key-prefix', help='metric key prefix',
+                type=parse_prefix_string, default='varnish.clients')
+args = ap.parse_args()
+
+sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+key_value_pairs = re.compile('([A-Z]+)=([^;]+)')
+stats = {}
+
+
+def vsl_callback(transaction_id, tag, record, remote_party):
+    for k, v in key_value_pairs.findall(record):
+        if k == 'SSR':
+            k = 'ssl_sessions'
+            v = 'reused' if v == '1' else 'negotiated'
+        elif k == 'C':
+            k = 'ssl_cipher'
+        v = v.replace('.', '_')
+        s = '.'.join((args.key_prefix, k, v)).lower()
+        stats[s] = stats.get(s, 0) + 1
+
+    if sum(stats.values()) > 1e4:
+        buf = io.BytesIO()
+        while stats:
+            metric = '%s:%s|c\n' % stats.popitem()
+            buf.write(metric.encode('utf-8'))
+        buf.seek(io.SEEK_SET)
+        if args.statsd_server:
+            sock.sendto(buf.read(), args.statsd_server)
+        else:
+            print(buf.read().decode('utf-8', errors='replace').rstrip())
+
+    return 0
+
+
+varnishlog.varnishlog((
+    ('i', 'BerespHeader'),
+    ('I', 'X-Connection-Properties'),
+    ('n', 'frontend'),
+), vsl_callback)
diff --git a/modules/varnish/manifests/logging/xcps.pp 
b/modules/varnish/manifests/logging/xcps.pp
index f77eeb1..f4bc187 100644
--- a/modules/varnish/manifests/logging/xcps.pp
+++ b/modules/varnish/manifests/logging/xcps.pp
@@ -18,8 +18,15 @@
 define varnish::logging::xcps( $statsd_server = 'statsd' ) {
     include varnish::common
 
+    if (hiera('varnish_version4', false)) {
+        # Use v4 version of varnishxcps
+        $varnish4_python_suffix = '4'
+    } else {
+        $varnish4_python_suffix = ''
+    }
+
     file { '/usr/local/bin/varnishxcps':
-        source  => 'puppet:///modules/varnish/varnishxcps',
+        source  => 
"puppet:///modules/varnish/varnishxcps${varnish4_python_suffix}",
         owner   => 'root',
         group   => 'root',
         mode    => '0555',

-- 
To view, visit https://gerrit.wikimedia.org/r/282887
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I157ec0bcaf77625526410170a5a56a41c3317d42
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Ema <e...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to