88import gzip
99import logging
1010import json
11+ import math
1112import os
1213import re
1314import statistics
@@ -21,6 +22,21 @@ def num(x):
2122 return float (x )
2223 return int (x )
2324
25+ def hunum (x ):
26+ if x >= 10000000000 :
27+ return '{:.1f}G' .format (x / 1000000000.0 )
28+ if x >= 1000000000 :
29+ return '{:.2f}G' .format (x / 1000000000.0 )
30+ if x >= 10000000 :
31+ return '{:.1f}M' .format (x / 1000000.0 )
32+ if x >= 1000000 :
33+ return '{:.2f}M' .format (x / 1000000.0 )
34+ if x >= 10000 :
35+ return '{:.1f}k' .format (x / 1000.0 )
36+ if x >= 1000 :
37+ return '{:.2f}k' .format (x / 1000.0 )
38+ return '{:.2f}x' .format (x )
39+
2440metric_line_re = re .compile (r'(\S+\{[^}]*\})\s+(.*)' )
2541
2642def test_metric_line_re ():
@@ -118,7 +134,8 @@ def meanOrZero(seq):
118134 return statistics .mean (seq )
119135
120136class summary :
121- def __init__ (self ):
137+ def __init__ (self , label = None ):
138+ self .label = label or ""
122139 self .tpsMeanSum = 0
123140 self .txBpsMeanSum = 0
124141 self .rxBpsMeanSum = 0
@@ -179,12 +196,23 @@ def txPool(self):
179196 mins .append (min (txp ))
180197 maxs .append (max (txp ))
181198 means .append (statistics .mean (txp ))
182- return 'txnpool({} {} {} {} {})' .format (
199+ if not means or not maxs or not mins :
200+ return 'txnpool(no stats)'
201+ return 'txnpool({:.0f} {:.0f} {:.0f} {:.0f} {:.0f})' .format (
183202 min (mins ), min (means ), statistics .mean (means ), max (means ), max (maxs )
184203 )
185204
186205 def __str__ (self ):
187- return '{}\n {}\n summary: {:0.2f} TPS, {:0.0f} tx B/s, {:0.0f} rx B/s' .format (self .byMsg (), self .txPool (), self .tpsMeanSum / self .sumsCount , self .txBpsMeanSum / self .sumsCount , self .rxBpsMeanSum / self .sumsCount )
206+ if not self .sumsCount :
207+ tps , txbps , rxbps = math .nan , math .nan , math .nan
208+ else :
209+ tps = self .tpsMeanSum / self .sumsCount
210+ txbps = self .txBpsMeanSum / self .sumsCount
211+ rxbps = self .rxBpsMeanSum / self .sumsCount
212+ labelspace = ""
213+ if self .label :
214+ labelspace = self .label + " "
215+ return '{byMsg}\n {labelspace}{txPool}\n {labelspace}summary: {TPS:0.2f} TPS, tx {txBps}B/s, rx {rxBps}B/s' .format (labelspace = labelspace , byMsg = self .byMsg (), txPool = self .txPool (), TPS = tps , txBps = hunum (txbps ), rxBps = hunum (rxbps ))
188216
189217def anynickre (nick_re , nicks ):
190218 if not nick_re :
@@ -216,7 +244,14 @@ def gather_metrics_files_by_nick(metrics_files, metrics_dirs=None):
216244 continue
217245 nick = m .group (1 )
218246 dapp (filesByNick , nick , path )
219- return filesByNick
247+ return tf_inventory_path , filesByNick , nonick
248+
249+ def process_nick_re (nre , filesByNick , nick_to_tfname , rsum , args ):
250+ nretup = (nre ,)
251+ for rnick , paths in filesByNick .items ():
252+ nick = nick_to_tfname .get (rnick , rnick )
253+ if anynickre (nretup , (rnick ,nick )):
254+ rsum (process_files (args , nick , paths ), nick )
220255
221256def main ():
222257 test_metric_line_re ()
@@ -227,6 +262,7 @@ def main():
227262 ap .add_argument ('--deltas' , default = None , help = 'path to write csv deltas' )
228263 ap .add_argument ('--report' , default = None , help = 'path to write csv report' )
229264 ap .add_argument ('--nick-re' , action = 'append' , default = [], help = 'regexp to filter node names, may be repeated' )
265+ ap .add_argument ('--nick-lre' , action = 'append' , default = [], help = 'label:regexp to filter node names, may be repeated' )
230266 ap .add_argument ('--verbose' , default = False , action = 'store_true' )
231267 args = ap .parse_args ()
232268
@@ -240,7 +276,7 @@ def main():
240276 if args .dir :
241277 metrics_dirs .add (args .dir )
242278 metrics_files += glob .glob (os .path .join (args .dir , '*.metrics' ))
243- filesByNick = gather_metrics_files_by_nick (metrics_files , metrics_dirs )
279+ tf_inventory_path , filesByNick , nonick = gather_metrics_files_by_nick (metrics_files , metrics_dirs )
244280 if not tf_inventory_path :
245281 for md in metrics_dirs :
246282 tp = os .path .join (md , 'terraform-inventory.host' )
@@ -270,21 +306,26 @@ def main():
270306 elif len (found ) > 1 :
271307 logger .warning ('ip %s (%s) found in nicks: %r' , ip , name , found )
272308 else :
273- logger .warning ('ip %s no nick' )
309+ logger .warning ('ip %s (%s) no nick' , ip , name )
274310 #logger.debug('nick_to_tfname %r', nick_to_tfname)
275311
276312 if args .nick_re :
277313 # use each --nick-re=foo as a group
278314 for nre in args .nick_re :
279315 rsum = summary ()
280- nretup = (nre ,)
281- for rnick , paths in filesByNick .items ():
282- nick = nick_to_tfname .get (rnick , rnick )
283- if anynickre (nretup , (rnick ,nick )):
284- rsum (process_files (args , nick , paths ), nick )
316+ process_nick_re (nre , filesByNick , nick_to_tfname , rsum , args )
285317 print (rsum )
286318 print ('\n ' )
287319 return 0
320+ if args .nick_lre :
321+ for lnre in args .nick_lre :
322+ label , nre = lnre .split (':' , maxsplit = 1 )
323+ rsum = summary (label )
324+ process_nick_re (nre , filesByNick , nick_to_tfname , rsum , args )
325+ print (rsum )
326+ print ('\n ' )
327+ return 0
328+
288329
289330 # no filters, glob it all up
290331 rsum = summary ()
0 commit comments