33
44import argparse
55import errno
6+ import os
67try :
78 import simplejson as json
89except ImportError :
@@ -33,7 +34,7 @@ def abstract_unix_addr(arg):
3334 return sfamily , addr , socket .gethostname ()
3435
3536
36- def run (stats , freq , node , role , f ):
37+ def run (stats , node , role , freq , f ):
3738 if ':' in stats :
3839 sfamily , addr , host = inet_addr (stats )
3940 elif stats .startswith ('@' ):
@@ -117,13 +118,16 @@ def run(stats, freq, node, role, f):
117118
118119
119120def main ():
120- default_format = 'uwsgi,node={node},role={role} req={req}i,rps={rps}i,avg={avg},lq={lq}i,busy={busy}i,idle={idle}i,rss={rss}i'
121+ DEFAULT_NODE = socket .gethostname ()
122+ DEFAULT_ROLE = 'uwsgi'
123+ DEFAULT_FREQ = 5
124+ DEFAULT_FORMAT = 'uwsgi,node={node},role={role} req={req}i,rps={rps}i,avg={avg},lq={lq}i,busy={busy}i,idle={idle}i,rss={rss}i'
121125
122126 parser = argparse .ArgumentParser (formatter_class = argparse .RawTextHelpFormatter )
123- parser .add_argument ('--node' , '-n' , dest = 'node' , default = socket . gethostname () , help = 'uWSGI node name, default: "{}"' .format (socket . gethostname () ))
124- parser .add_argument ('--role' , '-r' , dest = 'role' , default = 'uwsgi' , help = 'uWSGI role name, default: "uwsgi"' )
125- parser .add_argument ('--frequency' , '-q' , dest = 'freq' , default = 1 , type = float , help = 'uWSGI stats refresh frequency, in seconds' )
126- parser .add_argument ('--format' , '-f' , dest = 'format' , default = default_format ,
127+ parser .add_argument ('--node' , '-n' , dest = 'node' , default = None , help = 'uWSGI node name, default: "{}"' .format (DEFAULT_NODE ))
128+ parser .add_argument ('--role' , '-r' , dest = 'role' , default = None , help = 'uWSGI role name, default: "{}"' . format ( DEFAULT_ROLE ) )
129+ parser .add_argument ('--frequency' , '-q' , dest = 'freq' , default = None , type = float , help = 'uWSGI stats refresh frequency in seconds, default: {}' . format ( DEFAULT_FREQ ) )
130+ parser .add_argument ('--format' , '-f' , dest = 'format' , default = None ,
127131 help = '''output format, available variables:
128132 ver - uWSGI version
129133 node - uWSGI node name
@@ -136,15 +140,18 @@ def main():
136140 idle - idle workers count
137141 rss - average RSS (Resident Set Size)
138142 vsz - average VSZ (Virtual Memory Size)
139-
140- e.g. "{}"''' .format (default_format ))
141-
143+ e.g. "{}"''' .format (DEFAULT_FORMAT ))
142144 parser .add_argument ('stats' , help = 'uWSGI stats address' )
143145
144146 args = parser .parse_args ()
145147
148+ node = args .node or os .getenv ('UWSGIMON_NODE' ) or DEFAULT_NODE
149+ role = args .role or os .getenv ('UWSGIMON_ROLE' ) or DEFAULT_ROLE
150+ freq = args .freq or os .getenv ('UWSGIMON_FREQ' ) or DEFAULT_FREQ
151+ format_ = args .format or os .getenv ('UWSGIMON_FORMAT' ) or DEFAULT_FORMAT
152+
146153 try :
147- run (stats = args .stats , freq = args . freq , node = args . node , role = args . role , f = args . format )
154+ run (stats = args .stats , node = node , role = role , freq = float ( freq ), f = format_ )
148155 except IOError as e :
149156 if e .errno == errno .EPIPE :
150157 sys .exit (0 )
0 commit comments