Pulling out entries from file based on IP - awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pulling out entries from file based on IP - awk
# 1  
Old 03-24-2013
Pulling out entries from file based on IP - awk

i have several lines in a file that looks like this:


Code:
2013-03-23 19:02:33,122 DiscoverManager [Discover-15] WeblogicApplication-10.111.112.119-7400 FAILURE

i'm monitoring this file for strings that contain FAILURE. but i've been getting a lot of alerts that just aren't actionable. so i need another way around this.

each line has an IP and a port number, which is what i bolded above. i'm wondering, is there a way to do something like this:
  • if 10 or more entries for any specific IP are found in the log, AND each of the entry is for different port numbers, then alert?

my problem is, there isn't a list of known IPs or ports. any IP can be thrown in the file. so i'm curious if what i'm thinking is possible? i'm guessing awk can be used for this?

i was using a variation of this command to get a count:

Code:
awk '/FAILURE/ && /FAILURE/ {++c}c>=10 -1{o=$0 RS $0 RS $0; print o; c=0}' file

and i was using this to show me the actual offending lines from the file:

Code:
awk '/FAILURE/ && /FAILURE/' file


Last edited by SkySmart; 03-25-2013 at 07:32 AM..
# 2  
Old 03-24-2013
You could do something like:
Code:
awk ' /FAILURE/ {
        match( $0, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\-[0-9]+/ )
        IP = sprintf ("%s", substr($0, RSTART, RLENGTH))
        if (!(IP in A ))
        {
                A[IP]++
                ++c
        }
        if ( c == 10 )
        {
                print c " unique IPs found in FAILURE"
                for ( i in A )
                        print i
                exit 1
        }

} ' file

# 3  
Old 03-24-2013
Quote:
Code:
awk '/FAILURE/ && /FAILURE/' file

May I ask what do you expect from testing against the same condition twice?


Try:
Code:
awk -F- '/FAILURE/{if(!seen[$5,$6]){cnt[$5]++; seen[$5,$6]++; }  if(cnt[$5]>10) print $5,$6}' logfile

Note that this assumes your log file is consistent with respect to number of dashes.
If not, then you can match, as yoda showed, or you can pre-process, something along the lines:
Code:
awk '/FAILURE/{print $(NF-1), $NF) logfile | awk -F- '{if(!seen[$2,$3]){cnt[$2]++; seen[$2,$3]++; }  if(cnt[$2]>10) print $2,$3}' > IPs.list

And then post-process the captured IPs.list to grep for them in the orig file.

Last edited by mirni; 03-24-2013 at 11:50 PM..
This User Gave Thanks to mirni For This Post:
# 4  
Old 03-25-2013
Quote:
Originally Posted by mirni
May I ask what do you expect from testing against the same condition twice?


Try:
Code:
awk -F- '/FAILURE/{if(!seen[$5,$6]){cnt[$5]++; seen[$5,$6]++; }  if(cnt[$5]>10) print $5,$6}' logfile

Note that this assumes your log file is consistent with respect to number of dashes.
If not, then you can match, as yoda showed, or you can pre-process, something along the lines:
Code:
awk '/FAILURE/{print $(NF-1), $NF) logfile | awk -F- '{if(!seen[$2,$3]){cnt[$2]++; seen[$2,$3]++; }  if(cnt[$2]>10) print $2,$3}' > IPs.list

And then post-process the captured IPs.list to grep for them in the orig file.

I tried your second awk, and it appears that it is assuming the IPs in the log file will always be a specific column. that's accurate. and its my fault for not pointing that out.

the ips can be in any column. so i'm not sure how you'd tweak your awk statement to do that. btw, there was a missing "}" in it, so i changed it to this:

Code:
awk '/FAILURE/{print $(NF-1), $NF}' lfile | awk -F- '{if(seen[$2,$3]){cnt[$2]++; seen[$2,$3]++; }  if(cnt[$2]>1) print $2,$3}'

as you can see, i dropped it from 10 to 1, but still, nothing was spat out from the log even though there should have been.

---------- Post updated at 09:07 AM ---------- Previous update was at 09:03 AM ----------

Quote:
Originally Posted by Yoda
You could do something like:
Code:
awk ' /FAILURE/ {
        match( $0, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\-[0-9]+/ )
        IP = sprintf ("%s", substr($0, RSTART, RLENGTH))
        if (!(IP in A ))
        {
                A[IP]++
                ++c
        }
        if ( c == 10 )
        {
                print c " unique IPs found in FAILURE"
                for ( i in A )
                        print i
                exit 1
        }

} ' file

looks like this could work. when i ran your command i got something like this:

Code:
10 unique IPs found in FAILURE
10.51.65.16-1521

10.50.44.188-1521
10.51.111.22-7443
10.51.101.29-1443
10.58.60.21-1521
10.51.19.12-7443
10.11.34.23-1443
10.51.19.15-80
10.51.65.17-1521

the script is suppose to only count IPs that occur at least 10 times in the log file. looks like this is counting all IPs. and i'm not sure where it is applying the threshold of 10. but it looks like this could very well work.

also, notice the blank line. not sure what that means.
# 5  
Old 03-25-2013
I misread your requirement! To find an IP that has 10 or more occurrence:
Code:
awk ' /FAILURE/ {
        match( $0, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\-[0-9]+/ )
        IP = sprintf ("%s", substr($0, RSTART, RLENGTH))
        A[IP]++
} END {
        for ( ip in A )
        {
                if ( A[ip] >= 10 )
                        print "IP: " ip " found " A[ip] " times in the log"
        }
} ' file

BTW that blank line might be due to a record with pattern: FAILURE but no IP address in it.
This User Gave Thanks to Yoda For This Post:
# 6  
Old 03-25-2013
Quote:
Originally Posted by Yoda
I misread your requirement! To find an IP that has 10 or more occurrence:
Code:
awk ' /FAILURE/ {
        match( $0, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\-[0-9]+/ )
        IP = sprintf ("%s", substr($0, RSTART, RLENGTH))
        A[IP]++
} END {
        for ( ip in A )
        {
                if ( A[ip] >= 10 )
                        print "IP: " ip " found " A[ip] " times in the log"
        }
} ' file

BTW that blank line might be due to a record with pattern: FAILURE but no IP address in it.
perfect. this works as expected. i have one more need. the above tells you how many times an ip is found in the file if the port number of the IP is the same.

but i'd also like to alert if an IP is found in the log with different port numbers.

for instance, your code currently alerts on this type of scenario:

Code:
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.20.111-1207 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.20.111-1207 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.20.111-1207 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.20.111-1207 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.20.111-1207 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.20.111-1207 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.20.111-1207 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.20.111-1207 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.20.111-1207 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.20.111-1207 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax

notice the port numbers are all the same.

can it be tweaked to also alert when the ports are different for any of the IPs and the number all these occurrences is 15 or more?:

Code:
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.24.164-1920 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.24.164-1321 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.24.164-1822 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.24.164-1960 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.24.164-1023 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.24.164-1420 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.24.164-1930 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.24.164-1920 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.24.164-1290 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax
2013-03-23 13:05:55,987  [-46] OracleSensor-10.19.24.164-1207 ERROR util.URIUtils - [PLATFORM.UTIL.E.15] URI syntax

# 7  
Old 03-25-2013
Code:
awk ' /FAILURE/ {
        match( $0, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ )
        IP = sprintf ("%s", substr($0, RSTART, RLENGTH))
        match( $0, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\-[0-9]+/ )
        IP_PO = sprintf ("%s", substr($0, RSTART, RLENGTH))
        A_IP[IP]++
        A_IP_PO[IP_PO]++
} END {
        for ( ip in A_IP )
        {
                if ( A_IP[ip] >= 10 )
                {
                        print "IP: " ip " found " A_IP[ip] " times in the log"
                        for ( ip_po in A_IP_PO )
                        {
                                split(ip_po, V, "-")
                                if ( ip == V[1] )
                                        print "IP & PORT: " ip_po " Number of occurrences: " A_IP_PO[ip_po]
                        }
                }
        }
} ' file

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Partial file pulling

I am connecting to another server through sftp. I am running one batch script to pull file from another server. sometimes i am receiving partial files. I am using below commands in batch script. ls -ltr new.txt mget new.txt bye The file is of 1 MB only.In most of the cases , i received... (6 Replies)
Discussion started by: srinath01
6 Replies

2. Shell Programming and Scripting

Pulling Data, Then Moving to the Next File

I'm scanning a list of emails- I need to pull 2 pieces of data, then move to the next file: Sender's Email Address Email Date I need these to be outputted into a single column- separated by a ",". Like this: Email1's Address, Email1's Date Stamp Email2's Address, Email2's Date Stamp... (4 Replies)
Discussion started by: sudo
4 Replies

3. UNIX for Dummies Questions & Answers

Pulling Parms from Config File

Hello all, I'm working on a general script for something at work. I'm an up-and-comer backup for a Shell Scripter this company has had for 35 years lol. Anyway, I have a config file I'm trying to pull Variables from as the Config File is used for multiple scripts. Does the below make sense and... (7 Replies)
Discussion started by: phunk
7 Replies

4. Shell Programming and Scripting

pulling different fields from a csv file

Hi, I have a requirment where I need to pull different columns from a .csv file. Here is the sample of the csv file. account,item,flag1,flag2,flag3,flag4,flag5,......feed,tran I will be have a config.txt file which will have the following information. item,flag5,flag10,feed,tran... (2 Replies)
Discussion started by: akdevula
2 Replies

5. Shell Programming and Scripting

Pulling Ip's from log and redirecting to a file

Hi all, I am fairly new to scripting, but I do try and script as much as possible but the more advanced stuff does tend to boggle my mind a bit. I am at a bit of a loss with this one. I get entries in my DNS logs, like the below: I want to extract only the IP address, without the hashes... (5 Replies)
Discussion started by: codenjanod
5 Replies

6. UNIX for Dummies Questions & Answers

Pulling a file off a backup tape

I have AIX 5.1 This may sound like a really dumb question but I have never done this before. I would like to pull a file off a backup tape and put back on the AIX is this as simple as as doing a mount /dev/rmt1 then the file name that is on the tape /dump/rpt/xxxxxx Do I just copy it... (14 Replies)
Discussion started by: rocker40
14 Replies

7. Shell Programming and Scripting

Pulling data and following lines from file

I saw a few posts close to what i want to do, but they didn't look like they would work exactly.. or I need to think out of the box on this. I have a file that I keep server stats in for my own performance analysis. this file has the output from many commands in it (uptime, vmstats, ps, swap... (2 Replies)
Discussion started by: MizzGail
2 Replies

8. Shell Programming and Scripting

pulling a column from a file in ksh

I would like to pull a column from a file and place it in a variable: The file would look like this: N.Korea gibberish garbage S.Korea gibberish garbage USA gibberish garbage Iraq gibberish garbage Canada gibberish garbage and items in the first... (8 Replies)
Discussion started by: dangral
8 Replies

9. UNIX for Dummies Questions & Answers

pulling the following line from a file

I have return files from a process that has then original input record followed on the next line by a response record..either AA,........... for accepted or EE,.......... for errored. i.e 11,new,123 AA,accepted 12,exist,443 EE,rejected 13,old,223 AA,accepted I want to write a small... (4 Replies)
Discussion started by: peter.herlihy
4 Replies

10. UNIX for Advanced & Expert Users

Pulling out fields from a file

Hi, I have a file that contains 1400 lines similar to the one shown below: NAME=sara, TOWN=southampton, POSTCODE=SO18777, EMAIL=sara@hotmail.com, PASSWORD=asjdflkjds etc etc (note: this is one line). Each line has the same fields, but on each line they are in a different order. Eg. the line... (2 Replies)
Discussion started by: Saz
2 Replies
Login or Register to Ask a Question