コード例 #1
0
def do(sshc, cache, history, hz, raw, show_cpu):
	frags = ParserFragmentation(sshc).get()
	usage = ParserProcessCPU(sshc).get([])
	etime = ParserCurrentTime(sshc).get()
	
	if 'last' not in cache:
		cache['last'] = {
			'collected_on': etime.as_timestamp(),
			'frags': frags,
			'cpu' : usage,
		}
		return

	time_difference = etime.as_timestamp() - cache['last']['collected_on']

	overall_cpus = {}
	for tmp in ['user', 'system', 'idle', 'iowait', 'irq', 'softirq']:
		overall_cpus[tmp] = int(round(((usage['global'][tmp] - cache['last']['cpu']['global'][tmp])*100)/(time_difference*hz)))

	pdiff = {}
	for p in frags['frags']:
		if p not in frags['frags']:
			print >>sys.stderr, 'Error: fragmentation key %s missing in current statistics' % (p,)
			return
		elif p not in cache['last']['frags']['frags']:
			print >>sys.stderr, 'Error: fragmentation key %s missing in previous statistics' % (p,)
			return

		if raw:
			pdiff[p] = frags['frags'][p] - cache['last']['frags']['frags'][p]
		else:
			pdiff[p] = int(round((((frags['frags'][p] - cache['last']['frags']['frags'][p]))/(time_difference))))

	if os.name == 'nt':
		os.system('cls')
		print "Packet fragmentation    (written by Ondrej Holecek <*****@*****.**>)"
	else:
		print "\x1b[2J\x1b[H\033[1mPacket fragmentation   (written by Ondrej Holecek <*****@*****.**>)\033[0m"

	
	filters_applied = "Applied filters: "
	if raw: filters_applied += "CNTS[raw] "
	else: filters_applied += "CNTS[diff] "
	filters_applied += "HIST[%i] " % (history,)

	print prepend_timestamp("Overall CPU utilization: %3.1f %% user, %3.1f %% system, %3.1f %% idle" % (
		overall_cpus['user'], overall_cpus['system'], overall_cpus['idle'],
	), etime, 'fragtop')
	print prepend_timestamp("Overall CPU utilization: %3.1f %% iowait, %3.1f %% irq, %3.1f %% softirq" % (
		overall_cpus['iowait'], overall_cpus['irq'], overall_cpus['softirq'],
	), etime, 'fragtop')
	print prepend_timestamp(filters_applied, etime, 'fragtop')

	prehdr = "         |     Received fragments reassembly counters    |  Outgoing fragmentation counters  |"
	if show_cpu: prehdr += "   Historical CPU percentage    |"
	print prepend_timestamp(prehdr, etime, 'fragtop')
	hdr = " %7s | %9s | %9s | %9s | %9s | %9s | %9s | %9s |" % ("history", "fragments", "packets", "timeout", "error", "packets", "fragments", "unable",)
	if show_cpu: hdr += " %8s | %8s | %8s |" % ("system%", "irq%", "softirq%",)
	print prepend_timestamp(hdr, etime, 'fragtop')

	# current line
	current_line = " %7i " % ( 0, )
	for k in ('ReasmReqds', 'ReasmOKs', 'ReasmTimeout', 'ReasmFails', 'FragOKs', 'FragCreates', 'FragFails'):
		current_line += "| %9i " % (pdiff[k],)
	current_line += "|"
	if show_cpu: current_line += " %8i | %8i | %8i |" % (overall_cpus['system'], overall_cpus['irq'], overall_cpus['softirq'],)
	print prepend_timestamp(current_line, etime, 'fragtop')

	# older lines
	for odata in cache['history']:
		old_line = " %7i " % ( -int(round(etime.as_timestamp()-odata[0])),)
		for k in ('ReasmReqds', 'ReasmOKs', 'ReasmTimeout', 'ReasmFails', 'FragOKs', 'FragCreates', 'FragFails'):
			old_line += "| %9i " % (odata[1][k],)
		old_line += "|"
		if show_cpu: old_line += " %8i | %8i | %8i |" % (odata[2], odata[3], odata[4],)
		print prepend_timestamp(old_line, etime, 'fragtop')
		
	cache['history'].insert(0, (etime.as_timestamp(), pdiff, overall_cpus['system'], overall_cpus['irq'], overall_cpus['softirq'],) )
	if len(cache['history']) > history: cache['history'] = cache['history'][:history]
	cache['last']['frags'] = frags
	cache['last']['cpu'] = usage
	cache['last']['collected_on'] = etime.as_timestamp()

	sys.stdout.flush()
	return etime
コード例 #2
0
ファイル: pkttop.py プロジェクト: yangyang-1020/fortidebug
def do(sshc, cache, history, hz, raw, percentage):
    packets = ParserPacketDistribution(sshc).get()
    usage = ParserProcessCPU(sshc).get([])
    etime = ParserCurrentTime(sshc).get()

    if 'last' not in cache:
        cache['last'] = {
            'collected_on': etime.as_timestamp(),
            'packets': packets,
            'cpu': usage,
        }
        return

    time_difference = etime.as_timestamp() - cache['last']['collected_on']

    overall_cpus = {}
    for tmp in ['user', 'system', 'idle', 'iowait', 'irq', 'softirq']:
        overall_cpus[tmp] = int(
            round(
                ((usage['global'][tmp] - cache['last']['cpu']['global'][tmp]) *
                 100) / (time_difference * hz)))

    pdiff = {}
    for p in packets['packets']:
        if p not in packets['packets']:
            print >> sys.stderr, 'Error: packet distribution key %s missing in current statistics' % (
                p, )
            return
        elif p not in cache['last']['packets']['packets']:
            print >> sys.stderr, 'Error: packet distribution key %s missing in previous statistics' % (
                p, )
            return

        if raw:
            pdiff[p] = packets['packets'][p] - cache['last']['packets'][
                'packets'][p]
        else:
            pdiff[p] = int(
                round((((packets['packets'][p] -
                         cache['last']['packets']['packets'][p])) /
                       (time_difference))))

    total = sum(pdiff[x] for x in pdiff.keys())
    if percentage:
        for p in pdiff.keys():
            pdiff[p] = int(round((float(pdiff[p]) * 100) / total))

    if os.name == 'nt':
        os.system('cls')
        print "Packet size distribution   (written by Ondrej Holecek <*****@*****.**>)"
    else:
        print "\x1b[2J\x1b[H\033[1mPacket size distribution   (written by Ondrej Holecek <*****@*****.**>)\033[0m"

    filters_applied = "Applied filters: "
    if raw: filters_applied += "CNTS[raw] "
    else: filters_applied += "CNTS[diff] "
    if percentage: filters_applied += "PERC[yes] "
    else: filters_applied += "PERC[no] "
    filters_applied += "HIST[%i] " % (history, )

    print prepend_timestamp(
        "Overall CPU utilization: %3.1f %% user, %3.1f %% system, %3.1f %% idle"
        % (
            overall_cpus['user'],
            overall_cpus['system'],
            overall_cpus['idle'],
        ), etime, 'pkttop')
    print prepend_timestamp(
        "Overall CPU utilization: %3.1f %% iowait, %3.1f %% irq, %3.1f %% softirq"
        % (
            overall_cpus['iowait'],
            overall_cpus['irq'],
            overall_cpus['softirq'],
        ), etime, 'pkttop')
    print prepend_timestamp(filters_applied, etime, 'pkttop')

    # header
    skeys = sorted(pdiff.keys())
    hdr = " history "
    for k in skeys:
        left = k[0]
        right = k[1]
        c = ""
        if left == None:
            c = " <= %i" % (right, )
        elif right == None:
            c = " >= %i" % (left, )
        else:
            c = "<%i, %i>" % (
                left,
                right,
            )

        hdr += "| %12s " % (c, )
    hdr += "| %12s |" % ('total pkts', )
    print prepend_timestamp(hdr, etime, 'pkttop')

    # current line
    current_line = " %7i " % (0, )
    for k in skeys:
        current_line += "| %12i " % (pdiff[k], )
    current_line += "| %12i |" % (total, )
    print prepend_timestamp(current_line, etime, 'pkttop')

    # older lines
    for odata in cache['history']:
        old_line = " %7i " % (-int(round(etime.as_timestamp() - odata[0])), )
        for k in skeys:
            old_line += "| %12i " % (odata[1][k], )
        old_line += "| %12i |" % (odata[2], )
        print prepend_timestamp(old_line, etime, 'pkttop')

    cache['history'].insert(0, (etime.as_timestamp(), pdiff, total))
    if len(cache['history']) > history:
        cache['history'] = cache['history'][:history]
    cache['last']['packets'] = packets
    cache['last']['cpu'] = usage
    cache['last']['collected_on'] = etime.as_timestamp()

    sys.stdout.flush()
    return etime