Read log file to create Performance index


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read log file to create Performance index
# 1  
Old 10-11-2015
Read log file to create Performance index

I am required to create a CSV file reading last 200000 lines form a log file. I have to grep 3 parameters from this log file and write these parameters in the .csv file, with time stamp. This script will be setup in a cron job which will run every 10 minutes. I have written the script but it is giving me an error. Please help. I am not understanding where am i committing a mistake.

Code:
#!/bin/bash

foldername=$(date +%Y%m%d)
LOG_NAME=""
mkdir -p  /root/kpi/"$foldername"


 a = tail -200000 $LOG_NAME | grep -c ACCESS_REQUEST

b = tail -200000 $LOG_NAME | grep -c ACCESS_ACCEPT

c = tail -200000 $LOG_NAME | grep -c ACCESS_REJECT

$a,$b,$c,$(date +%Y/%m/%d/%H/%M) >> /root/kpi/"$foldername"/kpi_$(date +%Y%m%d).csv

# 2  
Old 10-11-2015
Code:
#!/bin/bash

foldername=$(date +%Y%m%d)
LOG_NAME=""
mkdir -p  /root/kpi/"$foldername"
a=`tail -200000 $LOG_NAME | grep -c ACCESS_REQUEST`
b=`tail -200000 $LOG_NAME | grep -c ACCESS_ACCEPT`
c=`tail -200000 $LOG_NAME | grep -c ACCESS_REJECT`

echo "$a,$b,$c,$(date +%Y/%m/%d/%H/%M)" >> /root/kpi/"$foldername"/kpi_$(date +%Y%m%d).csv

You have to use the echo command or maybe the printf command to write to a file.
The tail operation has to run in a child process. The equal sign cannot have spaces on either side of it.
# 3  
Old 10-11-2015
Did you consider echoing or printfing the variables?

Or sth. like this might work:
Code:
tail -200000 $LOG_NAME | grep -Ec ACCESS_(REQUEST|ACCEPT|REJECT); date +%Y/%m/%d/%H/%M; } > /root/kpi/"$foldername"/kpi_$(date +%Y%m%d).csv

# 4  
Old 10-11-2015
I don't think RudiC's suggestion is going to work since you want separate counts of the number of lines containing each of the three strings, not a total of number of lines that contain one or more of the strings.

Since you have specified that the name of the log file to be processed is the empty string (not the name of a file), jim mcnamara's script won't work either. And, if you did provide the name of your log file, getting the last 200000 lines three times (while additions are being added to the log file by other processes) means that it will run slow and give you counts for the three strings from different sets of lines in your file. It is also dangerous to expand a variable without double quoting it if there is ANY possibility that it could contain whitespace characters, characters that are special in pathname expansions, or (as in this case) an empty string.

I would be tempted to try something more like:
Code:
#!/bin/bash

foldername=$(date +%Y%m%d)
LOG_NAME="/path/to/your/logfile"
mkdir -p  /root/kpi/"$foldername"

tail -200000 "$LOG_NAME" | awk -v ts=$(date +%Y/%m/%d/%H/%M) '
	/ACCESS_REQUEST/{ a++ }
	/ACCESS_ACCEPT/	{ b++ }
	/ACCESS_REJECT/	{ c++ }
	END 		{ printf("%d,%d,%d,%s\n", a, b, c, ts) }
' >> /root/kpi/"$foldername/kpi_$foldername.csv"

This only reads the log file once and gathers individual counts for each string, but you still have to fill in the real name of the log file this script is supposed to process.

And, as always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.

Last edited by Don Cragun; 10-14-2015 at 04:01 PM.. Reason: Add missing quotes.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 10-14-2015
Thanks a lot Don Cragun,

But i also need to use the Code to send an SMS using the variables. Can you help. I can tell you the Syntax for Wiritng the SMS.

Code:
/usr/bin/curl -O http://IP:80/Contacts.txt
contactfile=/root/script-maintainance/Contacts.txt

cat "$contactfile" | while read -r contactno;
do
/usr/bin/curl -s 'http://IP:80/cgi-bin/sendsms?username=elitecore&password=elitecore&to='$contactno'&text=Please+find+below+details+Total+Access+Requests:+$a+Total+AccessRequest+Accepted:+$b+Total+AccessRequest+Rejected:+$c >/dev/null
done

Please help
# 6  
Old 10-14-2015
Quote:
Originally Posted by Crazy_Nix
But i also need to use the Code to send an SMS using the variables.
Don's script printed out the found values in the END-part. Instead of writing them directly to the csv-file you can read them into variables. Change this line:
Code:
' >> /root/kpi/"$foldername/kpi_$foldername.csv"

To this:

Code:
' | tee -a /root/kpi/"$foldername/kpi_$foldername.csv" | IFS=',' read a b c ts

You now have 4 variables in the shell filled with the respective values, which you can use further on in you call to curl.

Btw:
Code:
/usr/bin/curl -O http://IP:80/Contacts.txt
contactfile=/root/script-maintainance/Contacts.txt

cat "$contactfile" | while read -r contactno;
do
/usr/bin/curl -s 'http://IP:80/cgi-bin/sendsms?username=elitecore&password=elitecore&to='$contactno'&text=Please+find+below+details+Total+Access+Requests:+$a+Total+AccessRequest+Accepted:+$b+Total+AccessRequest+Rejected:+$c >/dev/null
done

is perhaps not going to work. You should consider constructing the string differently, for clarity:

Code:
#! /bin/bash
# ...or whatever shell you want to use, but specify one explicitly

# /usr/bin/curl -O http://IP:80/Contacts.txt   # this will not do anything meaningful anyways

contactfile=/root/script-maintainance/Contacts.txt

SMS="Please+find+below+details+Total+Access+Requests:+${a}"
SMS="${SMS}+Total+AccessRequest+Accepted:+${b}"
SMS="${SMS}+Total+AccessRequest+Rejected:+${c}"
while read -r contactno ; do
     /usr/bin/curl -s 'http://IP:80/cgi-bin/sendsms?username=elitecore&password=elitecore&to='$contactno'&text='"${SMS}" >/dev/null
done < "$contactfile"

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read Lines from One file.. and create another.. and

Hello Members, I have one file which contains million of supplier code. I need to load these codes into database 1000 at a time. Database procedure reads from an external table which is based on the unix files. All I want to do is to read from the bigger file e.g. MAIN_FILE.txt and create... (1 Reply)
Discussion started by: chetanojha
1 Replies

2. UNIX for Dummies Questions & Answers

Filter logs for key words and create an index

well, im a total novice of unix commands and shell scripting, i never made anything. But yesterday in my work i was working with a lot of log files and i was wondering if there is a command for doing something like this: i have a bunch of files with text like this: blablabla errorcode1... (11 Replies)
Discussion started by: matius_88
11 Replies

3. UNIX for Dummies Questions & Answers

create table file from different files with index

Hi, I've several files with two collumns, where first collumn can be used as index. filename1 and filename2 how to create a file I should start with cat all files and extract first collumn to create an index? (4 Replies)
Discussion started by: sargotrons
4 Replies

4. Shell Programming and Scripting

Linux Script create index.html file

I need a script that can do this: A script that searches all directories and subdirectories for .html files When a .html file is found it creates a index.html file in that folder. It then edits the index.html file and inserts links to all of the .html files that are in that folder into the... (5 Replies)
Discussion started by: seashell11
5 Replies

5. Shell Programming and Scripting

Read a file and create egrep variable

I want to create a egrep variable from a file. For example: string=`cat query.txt` cat myfile.txt | egrep "$string" The string variable file has a list of one or multiple lines So the end result of: cat myfile.txt | egrep "$string" would be: cat myfile.txt | egrep... (2 Replies)
Discussion started by: numele
2 Replies

6. Shell Programming and Scripting

Read multiple log files and create output file and put the result

OS : Linux 2.6.9-67 - Red Hat Enterprise Linux ES release 4 Looking for a script that reads the following log files that gets generated everynight between 2 - 5am Master_App_20090717.log Master_App1_20090717.log Master_App2_20090717.log Master_App3_20090717.log... (2 Replies)
Discussion started by: aavam
2 Replies

7. Shell Programming and Scripting

Read a file and search a value in another file create third file using AWK

Hi, I have two files with the format shown below. I need to read first field(value before comma) from file 1 and search for a record in file 2 that has the same value in the field "KEY=" and write the complete record of file 2 with corresponding field 2 of the first file in to result file. ... (11 Replies)
Discussion started by: King Kalyan
11 Replies

8. Shell Programming and Scripting

read mp3 filename and create one XML for each file

Hi: I have a collection of mp3s and I need to create 1 xml file per mp3. I have: recording1.mp3 recording2.mp3 etc and I want to generate this kind of files. recording1.xml recording2.xml and inside each xml file I need to add a url prefix and then the filename at the end. ... (4 Replies)
Discussion started by: jason7
4 Replies

9. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies

10. Shell Programming and Scripting

Read words from file and create new file using K-shell.

Hi All, Please help me in creating files through K-shell scripts. I am having one file in this format. OWNER.TABLE_NAME OWNER.TABLE_NAME1 OWNER1.TABLE_NAME OWNER1.TABLE_NAME1 I want to read the above file and create new file through k shell script. The new file should looks like this.... (4 Replies)
Discussion started by: bsrajirs
4 Replies
Login or Register to Ask a Question