Xymon Mailing List Archive search

client-local.cfg backticks expression help

2 messages in this thread

list Sebastian Auriol · Tue, 16 Apr 2013 16:29:29 +0100 ·
Using Jeremy's amazing example here
http://lists.xymon.com/pipermail/xymon/2013-January/036615.html I'm trying
to do something a little simpler...
 
Basically, if the line contains "debug:" replace it and the rest of the line
with "DEBUG LINE DETECTED AND REPLACED".  Otherwise just output the log
normally.
 
Here is what I have tried and the part within the backticks works fine (if I
exclude the exec part that hangs my bash session) from a command line to
produce the name of the file:

log:`exec 2>/dev/null; F=/path/to/file/log.log; T=/tmp/substitutedlog.log;
C=$(sed 's/debug:.*/DEBUG LINE DETECTED AND REPLACED/' $F > $T); echo
$T`:1024
log:`exec 2>/dev/null; F=/path/to/file/log.log; T=/tmp/substitutedlog.log;
sed 's/debug:.*/DEBUG LINE DETECTED AND REPLACED/' $F > $T; echo $T`:1024
log:`exec 2>/dev/null; F=/path/to/file/log.log; T=/tmp/substitutedlog.log;
sed "s/debug:.*/DEBUG LINE DETECTED AND REPLACED/" $F > $T; echo $T`:1024
log:`F=/path/to/file/log.log; T=/tmp/substitutedlog.log; sed
"s/debug:.*/DEBUG LINE DETECTED AND REPLACED/" $F > $T; echo $T`:1024

But /usr/share/xymon-client/logs/xymonclient.log gets this:

sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file

Or this:

sh: -c: line 0: unexpected EOF while looking for matching `"'
sh: -c: line 1: syntax error: unexpected end of file

...depending on whether I use single or double quotes in the sed expression.

Kind regards, 

SebA
list Jeremy Laidman · Wed, 17 Apr 2013 11:02:00 +1000 ·
quoted from Sebastian Auriol
On 17 April 2013 01:29, SebA <user-4631430d620a@xymon.invalid> wrote:
Using Jeremy's amazing example here
http://lists.xymon.com/pipermail/xymon/2013-January/036615.html I'm trying
to do something a little simpler...
Yeah, simpler is good.  I wouldn't call my example "amazing", instead it's
"complicated", or even "hackgly".  Actually, I'm amazed it works!
quoted from Sebastian Auriol

log:`exec 2>/dev/null; F=/path/to/file/log.log; T=/tmp/substitutedlog.log;
C=$(sed 's/debug:.*/DEBUG LINE DETECTED AND REPLACED/' $F > $T); echo
$T`:1024
Yup.
quoted from Sebastian Auriol

But /usr/share/xymon-client/logs/xymonclient.log gets this:
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
I'm fairly sure the problem is the colon in "debug:".  The Xymon
client-side binary logfetch parses the log line from
/tmp/logfetch.$HOSTNAME.cfg.  The first thing it does is to split the line
on the colon delimiter, into three parts, the last one being the max log
message size.  The logfetch program doesn't process any escapes or quoting,
and only cares about the colons.

In my post to which you linked, I mentioned this problem.  I handled it by
putting creating a quote using printf from a hex representation, put it in
a variable ($Z), and used that in my commands.

So this might stop the errors, and let the full command parse correctly:

log:`exec 2>/dev/null; COLON=$(printf "\x3a"); F=/path/to/file/log.log;
T=/tmp/substitutedlog.log;C=$(sed "s/debug${COLON}.*/DEBUG LINE DETECTED
AND REPLACED/" $F > $T); echo $T`:1024

This could be considered an obscure way of quoting, so I could get the
colon into the grep parameter.  Unlike grep, GNU sed can use quoted hex to
represent arbitrary characters, so you might be able to get away with this:

log:`exec 2>/dev/null; F=/path/to/file/log.log;
T=/tmp/substitutedlog.log;C=$(sed "s/debug\x3a.*/DEBUG LINE DETECTED AND
REPLACED/" $F > $T); echo $T`:1024

In bash, the same \xnn format is expanded in strings of the form $'...', so
this can be used if you weren't using sed to do pattern matching.  For
example: grep $'debug\x3a.*' > /tmp/tmpfile.

I'm not sure why you're using C=$() rather than just the commands -
probably just a hang-over from my example.  There's unlikely to be any
output to put into $C, and you don't use it anyway.  I had it in my post
because I wanted to do some calculations with it.  So you can simplify this
to be:

log:`exec 2>/dev/null; F=/path/to/file/log.log;
T=/tmp/substitutedlog.log;sed "s/debug\x2a.*/DEBUG LINE DETECTED AND
REPLACED/" $F > $T; echo $T`:1024

In fact, this is almost simple enough that variables don't aid
maintainability, so I'd probably go with this, which is easier for a
sysadmin to grok IMHO:

log:`sed "s/debug\x2a.*/DEBUG LINE DETECTED AND REPLACED/"
/path/to/file/log.log > /tmp/substitutedlog.log 2>/dev/null; echo
/tmp/substitutedlog.log`:1024

J