old_contents: #!/usr/bin/env python
  #
  # igcollect - Memcached
  #
  # Copyright (c) 2016, InnoGames GmbH
  #
  
  import re, telnetlib, sys, socket, time
  
  class MemcachedStats:
  
      _client = None
      _key_regex = re.compile(ur'ITEM (.*) \[(.*); (.*)\]')
      _slab_regex = re.compile(ur'STAT items:(.*):number')
      _stat_regex = re.compile(ur"STAT (.*) (.*)\r")
  
      def __init__(self, host='localhost', port='11211'):
          self._host = host
          self._port = port
  
      @property
      def client(self):
          if self._client is None:
              self._client = telnetlib.Telnet(self._host, self._port)
          return self._client
  
      def command(self, cmd):
          ' Write a command to telnet and return the response '
          self.client.write("%s\n" % cmd)
          return self.client.read_until('END')
  
      def stats(self):
          ' Return a dict containing memcached stats '
          return dict(self._stat_regex.findall(self.command('stats')))
  
  def main(argv=None):
      if not argv:
          argv = sys.argv
      host = argv[1] if len(argv) >= 2 else '127.0.0.1'
      port = argv[2] if len(argv) >= 3 else '11211'
      m = MemcachedStats(host, port)
      memcached = {}
      memcached=m.stats()
      hostname = socket.gethostname().replace('.', '_')
      timestamp = str(int(time.time()))
      template = 'servers.' + hostname + '.software.memcached.{1} {2} ' + timestamp
  
      for key in memcached:
        print(template.format(hostname,key, memcached[key]))
  
  if __name__ == '__main__':
      main()
  
new_contents: #!/usr/bin/env python
  #
  # igcollect - Memcached
  #
  # Copyright (c) 2016, InnoGames GmbH
  #
  
  import re
  import telnetlib
  import sys
  import socket
  import time
  
  stat_regex = re.compile(ur'STAT (.*) (.*)\r')
  
  
  def main(host='127.0.0.1', port='11211'):
      stats = dict(stat_regex.findall(command(host, port, 'stats')))
      hostname = socket.gethostname().replace('.', '_')
      ts = str(int(time.time()))
      template = 'servers.' + hostname + '.software.memcached.{1} {2} ' + ts
  
      for key in stats:
          print(template.format(hostname, key, stats[key]))
  
  
  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')
  
  
  if __name__ == '__main__':
      main(*sys.argv[1:])
  
subject: Simplify memcached.py