check program ntpq
with path /bin/bash -c '/usr/bin/ntpq -4pn |tail -n +3 |awk '"'"'{ print $3 }'"'"' |grep -q 16'
if status = 0 then alert
I'm using a shell quote escaping extensively to have ' and " in parallel.
Short explanation of multiple single and double quotes:
with path /bin/bash -c '/usr/bin/ntpq -4pn |tail -n +3 |awk '"'"'{ print $3 }'"'"' |grep -q 16'
^^^^^ ^^^^^
01234 56789
0. ' - end first quotation which uses single quotes
1. " - start second quotation using double quotes
2. ' - quoted character itself
3. " - end second quotation using double quotes
4. ' - start third quotation using single quotes
5. ' - end third quotation using single quotes
6. " - start fourth quotation using double quotes
7. ' - quoted character itself
8. " - end fourth quotation using double quotes
9. ' - start fifth quotation using single quotes
In fact, shell interprets all above as long concatenated string.
When I run this command in shell all is fine and works as expected - no output and status code is 1.
When the same check is run by monit, I have the following output:
# monit status ntpq
Monit 5.25.1 uptime: 39d 23h 12m
Program 'ntpq'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
last exit value 1
last output Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options: (standard)
-f progfile --file=progfile
-F fs --field-separator=fs
-v var=val --assign=var=val
Short options: GNU long options: (extensions)
-b --characters-as-bytes
-c --traditional
-C --copyright
-d[file] --dump-variables[=file]
-D[file] --debug[=file]
-e 'program-text' --source='program-text'
-E file --exec=file
-g --gen-pot
-h --help
-i includefile --include=includefile
-l library --load=library
-L[fatal|invalid] --lint[=fatal|invalid]
-M --bignum
-N --use-lc-numeric
-n --non-decimal-data
-o[file] --pretty-print[=file]
-O --optimize
-p[file]
data collected Mon, 05 Feb 2018 17:05:56
I've also tried to quote all _expression_ with double quotes which makes it even less readable :) with the same result.
Any suggestions on what went wrong would be highly appreciated.
Thanks in advance.