Tuesday, February 1, 2022

[SOLVED] Filter out exceptions and routing to a file based on the instance

Issue

i have a scenario where i need to prepare/write exceptions to a file from the log file by grepping Instance name and writing all its related exceptions to Instance.txt

example test-dom-01.txt,test-dom-01.txt,test-dom-04.txt ...so on with its associated errors

out.log

Performing Health Check for Instance : test-dom-01
test-dom-01 Instance is----->  Running
****Existing Exceptions***** on instance: test-dom-01
catalina.0.log:INFO: Error parsing HTTP request header
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in the HTTP protocol
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in 
catalina.0.log:SEVERE: Exception when trying to read white list from  probably because file list is empty

--> Health Check for Instance :test-dom-01 is completed

Performing Health Check for Instance : tests-rs-dom2-1
tests-rs-dom2-1 Instance is----->  Running
****Existing Exceptions***** on instance: tests-rs-dom2-1 
catalina.0.log:INFO: Error parsing HTTP request header
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
catalina.out:   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)2019-09-19 15:23:03.266 [xxxx BACK END TCPLink Reader (Server-555)]
catalina.out:   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)2019-09-19 15:28:53 INFO
catalina.0.log:Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
catalina.0.log:org.apache.catalina.LifecycleException: Failed to initialize component [Connector[AJP/1.3-7002]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardService[Catalina]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize connector [Connector[AJP/1.3-7002]]
catalina.out:java.lang.Error: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]


--> Health Check for Instance :tests-rs-dom2-1 is completed

Performing Health Check for Instance : test-dom-04
test-dom-04 Instance is----->  Running
****Existing Exceptions***** on instance: test-dom-04
catalina.0.log:Caused by: java.net.BindException: Address already in use
catalina.0.log:Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
catalina.0.log:org.apache.catalina.LifecycleException: Failed to initialize component [Connector[AJP/1.3-7002]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardService[Catalina]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize connector [Connector[AJP/1.3-7002]]
catalina.out:java.lang.Error: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]


--> Health Check for Instance :test-dom-04 is completed

The Ask is i have to move the existing exceptions to each file ignoring the rest of the lines just exceptions have to move to separate file

Expected output for test-dom-01 file is test-dom-01.txt and content is

catalina.0.log:INFO: Error parsing HTTP request header
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in the HTTP protocol
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in 
catalina.0.log:SEVERE: Exception when trying to read white list from  probably because file list is empty

Expected out put for tests-rs-dom2-1 is tests-rs-dom2-1.txt

catalina.0.log:INFO: Error parsing HTTP request header
catalina.0.log:java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
catalina.out:   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)2019-09-19 15:23:03.266 [xxxx BACK END TCPLink Reader (Server-555)]
catalina.out:   at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)2019-09-19 15:28:53 INFO
catalina.0.log:Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
catalina.0.log:org.apache.catalina.LifecycleException: Failed to initialize component [Connector[AJP/1.3-7002]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [StandardService[Catalina]]
catalina.out:Caused by: org.apache.catalina.LifecycleException: Failed to initialize connector [Connector[AJP/1.3-7002]]
catalina.out:java.lang.Error: org.apache.catalina.LifecycleException: Failed to initialize component [StandardServer[-1]]

Tried:

awk '/on instance: test-dom-01/,/ /' out.log > test-dom-01.txt

but no luck


Solution

You may use this awk:

awk '/ on instance: /{fn=$NF ".txt"; next} !NF{close(fn); fn=""} fn{print > fn}' out.log


Answered By - anubhava
Answer Checked By - Robin (WPSolving Admin)