itzforu
itzforu

Reputation: 115

generate report based on log file using shell script

I have to create a report based on the nagios log file. I am going to write a shell script for this. The log file is as follows :


[1420520400] CURRENT SERVICE STATE: abc.com;service;CRITICAL;HARD;3;OK : OK : Last on 10-01-2015, Users = 2, Employees = 0

[1420520400] CURRENT SERVICE STATE: def.com;service;CRITICAL;HARD;3;WARNING : Last on 10-01-2015, Users = 2, Employees = 0

[1420520400] CURRENT SERVICE STATE: ghi.com;service;CRITICAL;HARD;3;CRITICAL : Last on 2014-11-19, Users = 2, Employees = 0


From this file, I want to generate the report as follows :


Name : abc.om

Date : 10-01-2015

Users : 2

Employees : 0


Name : def.om

Date : 10-01-2015

Users : 2

Employees : 0


Name : ghi.om

Date : 2014-11-19

Users : 2

Employees : 0


It would be great if anyone help me to achieve this.

Upvotes: 0

Views: 3053

Answers (1)

Arnab Nandy
Arnab Nandy

Reputation: 6692

This command will give you the above output, from the log file just change the file name from input.log to the actual file name.

$ cat input.log |cut -d';' -f1,6|sed -e 's/\<CURRENT SERVICE STATE\>/NAME=/g'|sed -e 's/\<OK\>//g'|sed -e 's/\<Last on\>/Date =/g'|tr -d ':'|sed 's/WARNING//g'|sed 's/CRITICAL//g'|cut -c 14-|tr -s ' '|tr ',;' '\n'                                                                         

Output:

enter image description here

Here, I used '=' but you can change the output exactly same as above if you use, following command,

$ cut -d';' -f1,6 input.log|sed -e 's/\<CURRENT SERVICE STATE\>/NAME=/g'|sed -e 's/\<OK\>//g'|sed -e 's/\<Last on\>/Date =/g'|tr -d ':'|sed 's/WARNING//g'|sed 's/CRITICAL//g'|cut -c 14-|tr -s ' '|tr ',;' '\n' |tr '=' ':'

Upvotes: 1

Related Questions