<s> import os <EOL> import re <EOL> import socket <EOL> from munin import MuninPlugin <EOL> worker_re = re . compile ( r'<STR_LIT>' ) <EOL> class MuninGearmanPlugin ( MuninPlugin ) : <EOL> category = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> super ( MuninGearmanPlugin , self ) . __init__ ( ) <EOL> addr = os . environ . get ( '<STR_LIT>' ) or "<STR_LIT:127.0.0.1>" <EOL> port = int ( addr . split ( '<STR_LIT::>' ) [ - <NUM_LIT:1> ] ) if '<STR_LIT::>' in addr else <NUM_LIT> <EOL> host = addr . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> self . addr = ( host , port ) <EOL> self . _sock = None <EOL> def connect ( self ) : <EOL> if not self . _sock : <EOL> self . _sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . _sock . connect ( self . addr ) <EOL> return self . _sock <EOL> def disconnect ( self ) : <EOL> if self . _sock : <EOL> self . _sock . close ( ) <EOL> def get_workers ( self ) : <EOL> sock = self . connect ( ) <EOL> sock . send ( "<STR_LIT>" ) <EOL> buf = "<STR_LIT>" <EOL> while "<STR_LIT>" not in buf : <EOL> buf += sock . recv ( <NUM_LIT> ) <EOL> info = [ ] <EOL> for l in buf . split ( '<STR_LIT:\n>' ) : <EOL> if l . strip ( ) == '<STR_LIT:.>' : <EOL> break <EOL> m = worker_re . match ( l ) <EOL> i = m . groupdict ( ) <EOL> i [ '<STR_LIT>' ] = [ x for x in i [ '<STR_LIT>' ] . split ( '<STR_LIT:U+0020>' ) if x ] <EOL> info . append ( i ) <EOL> return info <EOL> def get_status ( self ) : <EOL> sock = self . connect ( ) <EOL> sock . send ( "<STR_LIT>" ) <EOL> buf = "<STR_LIT>" <EOL> while "<STR_LIT>" not in buf : <EOL> buf += sock . recv ( <NUM_LIT> ) <EOL> info = { } <EOL> for l in buf . split ( '<STR_LIT:\n>' ) : <EOL> l = l . strip ( ) <EOL> if l == '<STR_LIT:.>' : <EOL> break <EOL> counts = l . split ( '<STR_LIT:\t>' ) <EOL> info [ counts [ <NUM_LIT:0> ] ] = dict ( <EOL> total = int ( counts [ <NUM_LIT:1> ] ) , <EOL> running = int ( counts [ <NUM_LIT:2> ] ) , <EOL> workers = int ( counts [ <NUM_LIT:3> ] ) , <EOL> ) <EOL> return info </s>
