I'm working on a set of scripts to monitor some external services that log to a TCP port.
function handleMessage {
while read message
do
# Handle $message
done
}
nc -d $ipadd $port | handleMessage
This code works fine when log messages are delimited by new lines like the following
Service started
ERROR: Something terrible happened
Service stopped
But one of the services delimits messages using LOGXXXX and allows new lines within messages so it's output looks more like this:
LOG0001 Service startedLOG0002 ERROR: Something terrible happened
NullPointerException at SomeMethod (File1.java)
SomeOtherMethod (File2.java)LOG0003 Service stopped
Using my code above handleMessage is called for each of the lines instead of for each set of LOGXXXX segments and if time passes with no messages containing newlines my script gets very far behind in handling messages.
Is there an option on nc or another program I can use to read from this TCP port and break on a custom delimiter, or some option to have a bash function handle each write to stdout instead of only being called for each line?
nc's output tosedto do the line-splitting, and give the result of that mangling to your script.