Skip to content

Instantly share code, notes, and snippets.

@wido
Created February 3, 2017 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wido/69d1aa5381bc68459f23fca9d7a250b9 to your computer and use it in GitHub Desktop.
Save wido/69d1aa5381bc68459f23fca9d7a250b9 to your computer and use it in GitHub Desktop.
Varnish Object Banner
#!/usr/bin/env python3
"""
Very simple Proof of Concept to read lines coming from
varnishncsa and send them out as events using Serf.
In this case Varnish is used in front of Ceph RGW and
we want to achieve cache consistency.
Send out a event using Serf when a PUT, POST or DELETE
is received so that all nodes can issue a varnish ban.
In the future this should probably be a script with threads
and Queues where incoming lines are put in a queue, combined
and send out as a single Serf event every 500ms or so to prevent
way to many forking and events in Serf.
Not of concern now for this PoC.
varnishncsa -F '%m %{Host}i %U %{Bucket}o %s'|varnishbanner
Author: Wido den Hollander <wido@widodh.nl>
"""
import sys
import json
import subprocess
SERF_BINARY = "/usr/local/bin/serf"
while True:
try:
line = sys.stdin.readline().strip()
method, host, url, bucket, status = line.split()
if int(status) > 400:
pass
if method == "PUT":
ban = {'host': host, 'url': url, 'bucket': bucket}
subprocess.call([SERF_BINARY, "event", "varnishban", json.dumps(ban)])
except KeyboardInterrupt:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment