awk script on a list of hosts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script on a list of hosts
# 1  
Old 10-13-2012
awk script on a list of hosts

Code:
[Sun Oct 14 00:05:43 2012] SERVICE NOTIFICATION: SOC;invoice-skysmart-01.net;monthly_process_check;CRITICAL;notify-by-email;CRITICAL: (0) instance(s) of (monthly-processor6) running on host (less than 1)
[Sun Oct 14 00:05:43 2012] SERVICE NOTIFICATION: SOC;invoice-02.skysmart.net;JAVA_PROCESS_CHECK;CRITICAL;notify-by-email;CRITICAL: (0) instance(s) of (/opt/bmc/am) running on host (less than 1)
[Sun Oct 14 00:05:44 2012] SERVICE NOTIFICATION: SOC;invoice-03.skysmart.net;dns_inbounding.skysmart.net;WARNING;notify-by-email;Warning: 69.59.252.183 did not return 16.15.20.33 on query for inbound2.skysmart.net.
[Sun Oct 14 00:05:44 2012] SERVICE NOTIFICATION: SOC;invoice-04.skysmart.net;check.atlas.skysmart.net_dns_integrity;WARNING;notify-by-email;Warning: 10.50.10.10 did not return 78.13.23.13 on query for atlaa.atlas.skysmart.net.
[Sun Oct 14 00:06:19 2012] SERVICE NOTIFICATION: SOC;invoice-05.skysmart.net;LOAD_CHECK;WARNING;notify-by-email;WARNING: 1 minute average (6.81) above warning threshold (6).

I have a log file that contains hundreds of thousands of lines similar to the above. I also have a file that contains a list of host names (one host per line).

Im in need of a awk script that would read in the list of host names and wherever any host from the list is found in the log file, the awk script should output the line(s) containing the host name.

What is the best way to do this? i'm concerned about efficiency. and this awk script i used for another similar project worked well. but i'm drawing a blank on how to change it around to do what i want:

Code:
HSLIST=$1
LOGFILE=$2
STRING=$3

awk 'BEGIN {
  while((getline < "'${HSLIST}'")>0)
     S[$0]

  FS="\n"; RS="}\n"
}

/servicestatus {/{

  for(X in D) delete D[X];

  for(N=2; N<=NF; N++)
  {
       gsub(/^[ \t]+/, "", $N);
       split($N, A, "=");
       D[A[1]] = A[2]
       i = 3;
       while (i in A)
          D[A[1]] = D[A[1]] "=" A[i++];
  }

  if (D["host_name"] in S)
       printf("%s -------- %s\n", D["host_name"])

}' $LOGFILE

i'm hoping this would give some of you awk experts some ideas.

OS: Linux/SunOS
Language: Bash

Last edited by SkySmart; 10-14-2012 at 10:19 AM..
# 2  
Old 10-13-2012
Quote:
Originally Posted by SkySmart
I have a log file that contains hundreds of thousands of lines similar to the above. I also have a file that contains a list of host names (one host per line
Could you please provide content of second file.Smilie
# 3  
Old 10-14-2012
Quote:
Originally Posted by pamu
Could you please provide content of second file.Smilie
i did. every single line in the file looks like the very first thing i quoted in my first post.

so the host list file would contain the host names:

invoice-02.skysmart.net
invoice-03.skysmart.net
.......

and the contents of the other file has already been posted.
# 4  
Old 10-14-2012
If you don't mind changing your above script.Smilie
Assuming your input file is as what you have shown above..

try this..

Code:
awk -F ";" 'FNR==NR{a[$0]=$0;next}{if(a[$2]){print a[$2]}}' Host_file Log_file

This User Gave Thanks to pamu For This Post:
# 5  
Old 10-14-2012
Quote:
Originally Posted by pamu
If you don't mind changing your above script.Smilie
Assuming your input file is as what you have shown above..

try this..

Code:
awk -F ";" 'FNR==NR{a[$0]=$0;next}{if(a[$2]){print a[$2]}}' Host_file Log_file

this seems to work. but it seems to only show a particular field from the matching lines. the field it outputs to the screen is the field containing the host names. i want it to show the entire line that was found in the log that contained the host name. can you please modify it to do that??

thank you Smilie
# 6  
Old 10-14-2012
Quote:
Originally Posted by SkySmart
i want it to show the entire line that was found in the log that contained the host name. can you please modify it to do that??
Code:
 awk -F ";" 'FNR==NR{a[$0]=$0;next}{if(a[$2]){print $0}}' Host_file Log_file

This User Gave Thanks to pamu For This Post:
# 7  
Old 10-14-2012
Adapting pamu's suggestion:
Code:
 awk -F ";" 'FNR==NR{a[$0]=$0;next} ($2 in a)' host file

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

How to copy a tar file on a series of remote hosts and untar it on those hosts?

Am trying to copy a tar file onto a series of remote hosts and untar it at the destination. Need to do this without having to do multiple ssh. Actions to perform within a single ssh session via shell script - copy a file - untar at destination (remote host) OS : Linux RHEL6 (3 Replies)
Discussion started by: sankasu
3 Replies

2. UNIX for Dummies Questions & Answers

Awk/script to list the owners of files

I have to list the files in a directory and along with that, list the owner of each of those files. Can someone please help me with a way to get this info please? Gayathri (2 Replies)
Discussion started by: ggayathri
2 Replies

3. Shell Programming and Scripting

netstat command to be executed for list of hosts

Hi All, Need to run the netstat -i command on the list of hosts and check if "Ierrs" and "Oerrs" has value greaterthan 0. for Ex: below output, driver bge1 and bge3 has Oerrs and Ierrs value > 0, So, script should report saying "Netstat status for $host, driver bge1 has Oerrs = 20, Failed"... (5 Replies)
Discussion started by: Optimus81
5 Replies

4. UNIX for Dummies Questions & Answers

List processes that are running on other hosts

Hi: How to list processes from all hosts, as opposed to the one you are working at? "ps ux" appears to list processes of the user on a single host only. Thanks. N.B Phil (5 Replies)
Discussion started by: phil518
5 Replies

5. Shell Programming and Scripting

Copy a file from local host to a list of remote hosts --- perl script

Hi friends, i need to prepare a script ( in perl) i have a file called "demo.exe" in my local unix host. i have a list of remote hosts in a file "hosts.txt" now i need to push "demo.exe" file to all the hosts in "hosts.txt" file. for this i need to prepare a script(in perl, but shell... (5 Replies)
Discussion started by: siva kumar
5 Replies

6. AIX

aix tcp wrappers hosts.allow hosts.deny?

hi all just installed the netsec.options.tcpwrapper from expansion pack, which used to be a rpm, for my aix 6.1 test box. it is so unpredictable. i set up the hosts.deny as suggested for all and allow the sshd for specific ip addresses/hostnames. the tcpdchk says the hosts allowed and... (0 Replies)
Discussion started by: wf201626
0 Replies

7. Shell Programming and Scripting

Script: nslookup few hosts at once

Hi Expert, Is there anyway for me to nslookup more than one host at one time, instead of doing it one by one. Thank you. (2 Replies)
Discussion started by: regmaster
2 Replies

8. Shell Programming and Scripting

Change .bashrc on a list of hosts

Hi, My scripting skills are somewhat basic... I need a way to log into a list of hostname/IPs as a user, su to root and then create/append root's .bashrc Thanks (0 Replies)
Discussion started by: jag7720
0 Replies

9. Shell Programming and Scripting

problem feeding netcat a list of hosts

Hi, I'm having difficulty in making a bash script to get netcat to scan a list of hosts and their ports from another file and could use some help. Here's an example host list, "nc.host": 192.168.2.110 22 And here's the first script I tried to feed "nc.host" into netcat: "nc1.sh" ... (3 Replies)
Discussion started by: seanovision
3 Replies

10. UNIX for Dummies Questions & Answers

hosts.allow & hosts.deny

Hi! Im trying to use host.allow & host.deny to resrtic access to my sun machine, but it doesnt seem to work... I want to allow full access from certain IPīs (ssh,http,ftp,etc...) but deny all kind of conections from outsideworld, the way that im doing that is: hosts.allow ALL:127.0.0.1... (2 Replies)
Discussion started by: Sorrento
2 Replies
Login or Register to Ask a Question