/usr/bin/flow-rptfmt is in flow-tools 1:0.68-12.1+b4.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | #!/usr/bin/python
import sys
import string
import getopt
import signal
#
# flow-rptfmt - pretty print flow-report output
#
# $Id: flow-rptfmt,v 1.3 2005/05/11 12:38:37 maf Exp $
#
# TODO:
# speed up sorting by first mapping array to long/floats.
# better support for older flow-reports with internal list of
# key fields.
# use the 'totals' line if available. Then don't have to
# load entire report for percent totals.
#
# Class: ftsym
# load a symbol table from a file in value symbol format, provide
# access methods findbyname and findbyval
#
class ftsym:
#
# load symbols
#
def __init__(self,field):
self.sv = {}
self.vs = {}
__symbol_lookup = { 'ip-source-port' : 'tcp-port.sym',
'ip-destination-port' : 'tcp-port.sym',
'ip-protocol' : 'ip-prot.sym',
'source-as' : 'asn.sym',
'destination-as' : 'asn.sym',
'source-tag' : 'tag.sym',
'destination-tag' : 'tag.sym',
'ip-address-type' : 'ip-type.sym',
}
fname = "/usr/local/netflow/var/sym/%s" % __symbol_lookup[field]
f = open(fname, "r")
line = f.readline()
while line:
line.strip()
(v,s) = line.split();
self.vs[v] = s
self.sv[s] = v
line = f.readline()
f.close()
#
# access by name, return value. If the name does not exist return the name.
#
def findbyname(self, name):
return self.sv.get(name,name)
#
# access by value, return name. If the value does not exist return the value.
#
def findbyval(self, val):
return self.vs.get(val,val)
#
# Class: ftrptfmt
#
# Read in output of flow-report.
#
# load(stream) - read from the stream
# pickdisplay(fields) - configure a comma seperated list of fields to display
# sort(field) - sort report -descending or +ascending on field
# mapsym() - replace key values with symbols
# percent() - replace non key fields with percent totals
# dump(format, header, max_lines) - dump output in format (ascii/html).
# if header is != 0, then list the header values too.
#
class ftrptfmt:
#
#
#
def __init__(self):
# not in data area
self.in_data = 0
# no header info yet
self.header_info = []
self.field_totals = {}
self.field_lens = {}
self.field_names = {}
self.field_vals = {}
self.field_avals = []
self.field_idx = {}
self.field_iter = []
self.field_total = 0
self.field_use = {'all' : 1}
self.field_display = {}
self.field_keys = {}
self.recno = int(0)
self.display_summary_detail = 0
self.max_lines = 0
self.display_header = 0
self.got_totals = 0
self.soft_field = ''
#
#
#
def _fmt_summary_detail(self):
if self.got_totals:
print "Ignores: %s" % self.field_vals['ignores']
print "Total Flows: %s" % self.field_vals['flows']
print "Total Octets: %s" % self.field_vals['octets']
print "Total Packets: %s" % self.field_vals['packets']
print "Total Duration (ms): %s" % self.field_vals['duration']
print "Real Time: %s" % self.field_vals['time_real']
print "Average Flow Time: %s" % self.field_vals['aflowtime']
print "Average Packets/Second: %s" % self.field_vals['aps']
print "Average Flows/Second: %s" % self.field_vals['afs']
print "Average Packets/Flow: %s" % self.field_vals['apf']
print "Flows/Second: %s" % self.field_vals['fps']
print "Flows/Second (real): %s" % self.field_vals['fps_real']
print "\nAverage IP packet size distribution:"
print "\n 1-32 64 96 128 160 192 224 256 288 320 352 384 416 448 480\n ",
for x in range(7,22):
print ("%-3.3f" % float(self.field_vals[x]))[1:],
print "\n\n 512 544 576 1024 1536 2048 2560 3072 3584 4096 4608\n ",
for x in range(22,33):
print ("%-3.3f" % float(self.field_vals[x]))[1:],
print "\n\nPackets per flow distribution:"
print "\n 1 2 4 8 12 16 20 24 28 32 36 40 44 48 52\n ",
for x in range(33,48):
print ("%-3.3f" % float(self.field_vals[x]))[1:],
print "\n\n 60 100 200 300 400 500 600 700 800 900 >900\n ",
for x in range(48,59):
print ("%-3.3f" % float(self.field_vals[x]))[1:],
print "\n\nOctets per flow distribution:"
print "\n 32 64 128 256 512 1280 2048 2816 3584 4352 5120 5888 6656 7424 8192\n ",
for x in range(59,74):
print ("%-3.3f" % float(self.field_vals[x]))[1:],
print "\n\n 8960 9728 10496 11264 12032 12800 13568 14336 15104 15872 >15872\n ",
print ("%-3.3f" % float(self.field_vals[74]))[1:],
for x in range(75,85):
print ("%-3.3f " % float(self.field_vals[x]))[1:],
print "\n\nFlow Time Distribution (ms):"
print "\n 10 50 100 200 500 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000\n ",
for x in range(85,100):
print ("%-3.3f" % float(self.field_vals[x]))[1:],
print "\n\n 12000 14000 16000 18000 20000 22000 24000 26000 28000 30000 >30000\n ",
for x in range(100,111):
print ("%-3.3f " % float(self.field_vals[x]))[1:],
print
#
#
#
def fmt_ascii_lines(self) :
# summary-detail is a special format
if self.display_summary_detail != 1:
line = int(self.max_lines)
for x in self.field_iter:
for y in xrange(self.field_total):
if self.field_display[y]:
# print "x=",x,"y=",y,"#"
i = self.field_avals[x][y]
print i.ljust(self.field_lens[y]),
if line:
line = line - 1
if not line:
break
print
else:
self._fmt_summary_detail()
#
#
#
def fmt_ascii_header(self) :
if not self.display_header:
return
for i in self.header_info:
if i[0:7] == '# rec1:':
break
print i;
print "# ", sys.argv
# summary-detail is a special format
if self.display_summary_detail != 1:
for x in xrange(self.field_total):
if self.field_display[x]:
i = self.field_names[x]
l = self.field_lens[x]
print i.ljust(l),
print
#
#
#
def fmt_ascii_footer(self) :
print "\n",
#
# HTML
#
def fmt_html_header(self) :
if self.display_header:
print '''<pre>'''
for i in self.header_info:
if i[0:7] == '#rec1:':
break
print i
print '''</pre>'''
print "<table border cellspacing=0 cellpadding=5>"
print "<tr>"
if self.display_summary_detail != 1:
for x in xrange(self.field_total):
if self.field_display[x]:
print " <th>", self.field_names[x], '''</th>'''
print '''</tr>'''
#
#
#
def fmt_html_lines(self) :
if self.display_summary_detail != 1:
line = int(self.max_lines)
for x in self.field_iter:
print "<tr>"
for y in xrange(self.field_total):
if self.field_display[y]:
i = self.field_avals[x][y]
print " <td>", i, '''</td>'''
if line:
line = line - 1
if not line:
break
print '''</tr>'''
else:
print '''<pre>'''
self._fmt_summary_detail()
print '''</pre>'''
#
#
#
def fmt_html_footer(self) :
print '''</table>'''
#
# load data in flow-report format
#
def load(self, f, sort_field, max_lines, opt_percent):
self.max_lines = int(max_lines)
self.sort_field = sort_field
# first line
line = f.readline()
while line:
# lop off trailing and extra whitespace
line = line.strip()
# report data starts after recn comment
if (not self.in_data) :
if line[:5] == '# rec' and line[:10] != '# records:':
# special totals record
if line[:6] == '# rec1':
self.got_totals = 1
line2 = f.readline()
line2 = line2.strip()
splt = string.split(line2,',')
x = 0
for i in string.split(line[8:],','):
self.field_vals[i] = splt[x]
x += 1
elif line[:6] == '# rec2':
self.display_summary_detail = 1
line2 = f.readline()
line2 = line2.strip()
splt = string.split(line2,',')
x = 0
for i in string.split(line[8:],','):
self.field_vals[i] = splt[x]
self.field_vals[x] = splt[x]
x += 1
else:
if line[:6] == '# recn':
self.in_data = 1
# foreach element in field names
x = 0
for i in string.split(line[8:],','):
# remove key designators
if i[-1:] == '*':
i = i[:-1]
self.field_keys[self.field_total] = 1
i = string.lower(i)
self.field_idx[i] = self.field_total
# default to the length of the header
self.field_lens[self.field_total] = len(i)
# append the header name
self.field_names[self.field_total] = i
self.field_total += 1
else:
self.header_info.append(line)
else :
# if in the data area and not a comment, store it
if self.in_data and line [:1] != '#':
splt = string.split(line, ',')
for i in xrange(self.field_total):
if len(splt[i]) > self.field_lens[i]:
self.field_lens[i] = len(splt[i])
self.field_avals.append(splt)
self.recno += 1
# next line
line = f.readline()
# short cut, if not sorting and max lines is defined then can stop early
if self.max_lines and (self.sort_field == '') and (opt_percent == 0) and (self.recno == self.max_lines):
break;
# default to sequentially as read
self.field_iter = range(self.recno)
#
#
#
def pickdisplay(self, f):
self.field_use['all'] = 0
for i in string.split(f, ','):
self.field_use[i] = 1
# figure out which fields are okay to print
if self.field_use['all'] == 1:
for x in xrange(self.field_total):
self.field_display[x] = 1
else:
for x in xrange(self.field_total):
self.field_display[x] = self.field_use.get(self.field_names[x],0)
#
#
#
def sort(self):
sort_field = self.sort_field
if self.sort_field != '':
i = self.field_idx[sort_field[1:]]
if sort_field[0] == '+':
if sort_field[1:] == 'pps' or sort_field[1:] == 'bps':
self.field_iter.sort(lambda a,b: cmp(float(self.field_avals[a][i]),
float(self.field_avals[b][i])))
elif sort_field[1:] == 'ip-next-hop-address' or sort_field[1:] == 'ip-source-address' or sort_field[1:] == 'ip-destination-address' or sort_field[1:] == 'ip-exporter-address':
self.field_iter.sort(lambda a,b: cmp(self.field_avals[a][i],
self.field_avals[b][i]))
else:
self.field_iter.sort(lambda a,b: cmp(long(self.field_avals[a][i]),
long(self.field_avals[b][i])))
elif sort_field[0] == '-':
if sort_field[1:] == 'pps' or sort_field[1:] == 'bps':
self.field_iter.sort(lambda b,a: cmp(float(self.field_avals[a][i]),
float(self.field_avals[b][i])))
elif sort_field[1:] == 'ip-next-hop-address' or sort_field[1:] == 'ip-source-address' or sort_field[1:] == 'ip-destination-address' or sort_field[1:] == 'ip-exporter-address':
self.field_iter.sort(lambda b,a: cmp(self.field_avals[a][i],
self.field_avals[b][i]))
else:
self.field_iter.sort(lambda b,a: cmp(long(self.field_avals[a][i]),
long(self.field_avals[b][i])))
#
#
#
def mapsym(self):
# foreach key field try to map in symbols
for i in self.field_keys.keys():
if self.field_display[i] != 1:
continue
n = self.field_names[i]
sym = ftsym(n)
for j in self.field_iter:
self.field_avals[j][i] = sym.findbyval(self.field_avals[j][i])
# readjust field len
if len(self.field_avals[j][i]) > self.field_lens[i]:
self.field_lens[i] = len(self.field_avals[j][i])
#
#
#
def percent(self):
# foreach field
for i in xrange(self.field_total):
# skip key fields
if self.field_keys.get(i,0) == 1:
continue
# for each value for the field
self.field_totals[i] = 0
for j in self.field_iter:
self.field_totals[i] += long(self.field_avals[j][i])
for j in self.field_iter:
self.field_avals[j][i] = "%5.6f" % (float(self.field_avals[j][i]) / float(self.field_totals[i]) * 100)
# readjust field len
if len(self.field_avals[j][i]) > self.field_lens[i]:
self.field_lens[i] = len(self.field_avals[j][i])
#
#
#
def dump(self, format, display_header):
self.display_header = display_header
if format == 'ascii':
fmt_header = ftrptfmt.fmt_ascii_header
fmt_lines = ftrptfmt.fmt_ascii_lines
fmt_footer = ftrptfmt.fmt_ascii_footer
if format == 'html':
fmt_header = ftrptfmt.fmt_html_header
fmt_lines = ftrptfmt.fmt_html_lines
fmt_footer = ftrptfmt.fmt_html_footer
fmt_header(self)
fmt_lines(self)
fmt_footer(self)
#
# main
#
(opts,rags) = getopt.getopt(sys.argv[1:], "a:f:F:ghHps:m:n")
opt_display_header = 1
opt_display = 'all'
opt_format = 'ascii'
opt_sort_field = ''
opt_mapsym = 0
opt_percent = 0
opt_max_lines = 0
opt_alarm = int(0)
for o,v in opts:
if o == '-a':
opt_alarm = int(v)
elif o == '-f':
opt_format = v
elif o == '-F':
opt_display = v
elif o == '-H':
opt_display_header = 0
elif o == '-m':
opt_max_lines = v
elif o == '-n':
opt_mapsym = 1
elif o == '-p':
opt_percent = 1
elif o == '-s':
opt_sort_field = v
elif o == '-h':
print "Usage: flow-rptfmt [-hHnp] [-a alarm] [-f format] [-F display_fields]"
print " [-m max_lines] [-s sort_field]"
sys.exit(0)
def sig_alarm(sig, stack):
print "flow-rptfmt exceeded CPU time, consider running report offline."
sys.exit(0)
signal.signal(signal.SIGALRM, sig_alarm)
if opt_alarm:
signal.alarm(opt_alarm)
rpt = ftrptfmt()
rpt.load(sys.stdin, opt_sort_field, opt_max_lines, opt_percent)
rpt.pickdisplay(opt_display)
rpt.sort()
if (opt_mapsym == 1):
rpt.mapsym()
if (opt_percent == 1):
rpt.percent()
try:
rpt.dump(opt_format, opt_display_header)
except IOError:
sys.exit(0)
|