Extract part of FreeRadius’ log

Posted on

Today I needed to extract some records from FreeRadius” log — those with Called- or Calling-Station-Id like XXXYYZ[0-2]. Started to type #!/usr/bin/python in new file, but «suddenly» decided to do it with awk.

If you don’t know — FreeRadius’s log contains «sections» separated by empty lines. Every section contains records Called-Staion-Id = <number> (or Calling-Station-Id), and i need to extract sections regarding calls to/from specific numbers.

So, the five-minutes-working-code:

#
# cutlog.awk :
#
 
BEGIN {
    # flag to indicate "good" set of lines:
    good = 0
    # counters:
    selected = 0
    total = 0
}
{
    # add line to set:
    set = set $0 "\n"
 
    # if wanted condition occur, flag set as good:
    if ($0 ~ pattern) {
        good = 1
    }
 
    # if empty line...
    if (!length) {
        # print set if it is "good":
        if (good) {
            printf ("%s", set)
            selected += 1
        }
        total += 1
        # start to work again:
        good = 0
        set = ""
    }
}
END {
    printf("%i records (%i lines) processed\n", total, NR) &gt; "/dev/stderr"
    printf("%i records matched\n", selected) &gt; "/dev/stderr"
    printf("Pattern was: '%s'\n\n", pattern) &gt; "/dev/stderr"
}

Now we can extract needed records:

awk -f cutlog.awk pattern='Station-Id = \"XXXXYYZ[0-2]\"' detail-YYYYMMDD > detail-YYYYMMDD.part

UPDATE: Python is better ;-)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.