Last active
November 11, 2017 00:57
-
-
Save vane/d41bd8ca8b1ffaa74f34d175fa1d49bf to your computer and use it in GitHub Desktop.
python redis_monitor.py 127.0.0.1 6379
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys | |
import socket | |
import threading | |
from collections import Counter | |
HOST = "127.0.0.1" | |
PORT = 6379 | |
INTERVAL = 5 | |
C1 = Counter() | |
C2 = Counter() | |
def connect(host, port): | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((host, port)) | |
s.send("MONITOR\r\n") | |
connected = True | |
next = "" | |
while connected: | |
data = s.recv(1024*1024) | |
if data.endswith("\r\n"): | |
parse(next+data) | |
else: | |
n = data.rfind('\r\n') + 2 | |
parse(next+data[:n]) | |
next = data[n:] | |
# print data | |
def parse(data): | |
for el in data.split('\r\n')[:-1]: | |
ev = el.split(' ') | |
if len(ev) > 3: | |
C1[ev[3][1:-1]] += 1 | |
C2[ev[4][1:-1]] += 1 | |
def sysout(): | |
print("="*30) | |
print(C1.most_common()) | |
print(C2.most_common()) | |
t = threading.Timer(INTERVAL, sysout) | |
t.start() | |
if __name__ == "__main__": | |
sysout() | |
if len(sys.argv) == 3: | |
connect(sys.argv[1], int(sys.argv[2])) | |
else: | |
print("redis_monitor.py HOST PORT") | |
connect(HOST, PORT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment