Ema has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/326132 )

Change subject: varnish: add varnish hitrate dstat plugin
......................................................................


varnish: add varnish hitrate dstat plugin

dstat_varnish_hit is a plugin for dstat allowing to display varnish
hitrate statistics in dstat.

Usage: dstat --varnish-hit

Change-Id: I40684205b4b6dd28afe62d5aae8419712fe5c29d
---
A modules/varnish/files/dstat_varnish_hit.py
M modules/varnish/manifests/common.pp
2 files changed, 93 insertions(+), 0 deletions(-)

Approvals:
  Ema: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/modules/varnish/files/dstat_varnish_hit.py 
b/modules/varnish/files/dstat_varnish_hit.py
new file mode 100644
index 0000000..94e446e
--- /dev/null
+++ b/modules/varnish/files/dstat_varnish_hit.py
@@ -0,0 +1,85 @@
+"""
+Display varnish hitrate in dstat
+
+Usage: dstat --varnish-hit
+
+  Copyright 2016 Emanuele Rocca <e...@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 os
+
+
+class dstat_plugin(dstat):  # noqa F821 undefined name 'dstat'
+    """
+    Display varnish cache miss/hit percentages in dstat
+    """
+    def __init__(self):
+        self.name = 'hitrate'
+        self.vars = ('fe-hitrate', 'be-hitrate')
+        self.nick = ('fe%', 'be%')
+        self.type = 'd'
+        self.scale = 10
+
+        # Initial values
+        self.values = {
+            'be': self.varnishstat(frontend=False),
+            'fe': self.varnishstat(frontend=True),
+        }
+
+    def check(self):
+        if os.system("varnishstat -1 > /dev/null") != 0:
+            raise Exception('Non-zero exit code from varnishstat')
+
+    def varnishstat(self, frontend=False):
+        cmd = "varnishstat -1 -f MAIN.cache_hit -f MAIN.cache_miss"
+
+        if frontend:
+            cmd += " -n frontend"
+
+        data = os.popen(cmd)
+        total = {}
+        for line in data.readlines():
+            row = line.split()
+            if not row:
+                continue
+
+            item = row[0].replace("MAIN.", "")
+            # Use third column, change per second
+            value = float(row[1])
+            total[item] = value
+
+        return total
+
+    def hitrate(self, frontend=False):
+        if frontend:
+            label = "fe"
+        else:
+            label = "be"
+
+        values = self.varnishstat(frontend)
+        hit = values['cache_hit'] - self.values[label]['cache_hit']
+        miss = values['cache_miss'] - self.values[label]['cache_miss']
+        self.val[label + "-hitrate"] = 100 * (hit / (0.0001 + hit + miss))
+
+        if frontend:
+            self.values['fe'] = values
+        else:
+            self.values['be'] = values
+
+    def extract(self):
+        self.hitrate(frontend=False)
+        self.hitrate(frontend=True)
diff --git a/modules/varnish/manifests/common.pp 
b/modules/varnish/manifests/common.pp
index a7a10b1..033a76c 100644
--- a/modules/varnish/manifests/common.pp
+++ b/modules/varnish/manifests/common.pp
@@ -50,6 +50,14 @@
         require => File['/usr/local/share/dstat'],
     }
 
+    file { '/usr/local/share/dstat/dstat_varnish_hit.py':
+        owner   => 'root',
+        group   => 'root',
+        mode    => '0444',
+        source  => 'puppet:///modules/varnish/dstat_varnish_hit.py',
+        require => File['/usr/local/share/dstat'],
+    }
+
     # `vlogdump` is a small tool to filter the output of varnishlog
     # See <https://github.com/cosimo/vlogdump> for more.
     file { '/usr/local/bin/vlogdump':

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I40684205b4b6dd28afe62d5aae8419712fe5c29d
Gerrit-PatchSet: 3
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Ema <e...@wikimedia.org>
Gerrit-Reviewer: Ema <e...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to