Hi there,
Recently I asked about displaying info from /var/log/messages,
converting most recent max 100 events to a histogram plot.
Got around to playing with the suggestions, here's what I have:
Input data, one sample record from /var/log/messages:
Oct 20 00:57:01 deltree kernel: InpDrop: IN=ppp0 OUT= MAC= SRC=220.210.119.91 DST=220.240.117.195 LEN=48 TOS=0x00 PREC=0x20 TTL=111 ID=30103 DF PROTO=TCP SPT=4860 DPT=445 WINDOW=8496 RES=0x00 SYN URGP=0
135/tcp **
445/tcp *********************************************
1026/udp ***
1027/udp *
1028/udp *
real 0m0.558s
user 0m0.370s
sys 0m0.190s
#!/bin/bash
#
function fast_one
{
grep InpDrop: /var/log/messages | grep -v ICMP | tail -100 | \
awk -F"[ =]" '
BEGIN {
for (;n++<7;)
dwarfs=dwarfs dwarfs "*"
}
{
sub(/^.*PROTO=/,"")
key[NR]=$5"/"$1
}
END {
for (i in key)
cnt[key[i]]++
for (i in cnt)
if (cnt[i] > max)
max=cnt[i]
if (max > 50) {
for (i in cnt)
printf "%10s %s\n", tolower(i), substr(dwarfs,1,(cnt[i]+1)/2)
}
else {
for (i in cnt)
printf "%10s %s\n", tolower(i), substr(dwarfs,1,cnt[i])
}
}' | sort -n
fast_oneQuote:}
A hybrid solution that is much faster than the awk only solution,
possibly 'cos Ed suggested modulo 100 math / data collection over
entire file, instead of only last max 100 records matching the
'grep' filter above.
Loki's dwarfs over Chris' string-chopper. Thanks all.
Suggestions for cleanup to an awk: '#!/bin/awk -f'? I ran into syntax
brick wall trying to convert the thing. :(
Cheers,
Grant.