forked from innogames/igcollect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcached.py
executable file
·44 lines (34 loc) · 983 Bytes
/
memcached.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
#
# igcollect - Memcached
#
# Copyright (c) 2016, InnoGames GmbH
#
import re
import socket
import sys
import telnetlib
import time
def main(host='127.0.0.1', port='11211'):
hostname = socket.gethostname().replace('.', '_')
ts = str(int(time.time()))
template = 'servers.' + hostname + '.software.memcached.{1} {2} ' + ts
pattern = re.compile('STAT \w+ \d+(?:.\d+)?$')
for line in command(host, port, 'stats').splitlines():
if pattern.match(line):
header, key, value = line.split()
print(template.format(hostname, key, value))
def command(host, port, cmd):
"""Write a command to telnet and return the response"""
client = telnetlib.Telnet(host, port)
client.write(cmd + '\n')
return client.read_until('END')
def is_float(value):
try:
float(value)
except ValueError:
return False
else:
return True
if __name__ == '__main__':
main(*sys.argv[1:])