Issue
I have written a simple script to find the latency acorss the network (script at end of post)
When I run this script from the CLI (as root) using
./latencytest it runs fine and I get an output like
Latency To LondonDB4|1|20180627112833|Latency with in Limits||
Latency To LondonDB4|D|maxlatency|LondonDB4|16.8|Max Latency|ms|
Latency To LondonDB4|D|minlatency|LondonDB4|4.59|Min Latency|ms|
Latency To LondonDB4|D|avglatency|LondonDB4|6.02|Average Latency|ms|
Latency To LondonDB4|D|packetloss|LondonDB4|0|Packetloss||
however I added the following line to cron (as root and tried a few varations)
* * * * * cd /opt/mutiny/bin;./latencytest > /dev/null
I get the follwoing out put apearing
Latency To LondonDB4|1|20180627112801|Latency with in Limits||
Latency To LondonDB4|D|maxlatency|LondonDB4||Max Latency|ms|
Latency To LondonDB4|D|minlatency|LondonDB4||Min Latency|ms|
Latency To LondonDB4|D|avglatency|LondonDB4||Average Latency|ms|
Latency To LondonDB4|D|packetloss|LondonDB4||Packetloss||
Note the lack of values for the various metrix in the output.
This is running on Centos and i get mail in the var/spool/mail/root that says
From [email protected] Wed Jun 27 11:17:01 2018
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected]
Received: by mutiny-remote.localdomain (Postfix, from userid 0)
id 22CB9872769; Wed, 27 Jun 2018 11:17:01 +0100 (BST)
From: "(Cron Daemon)" <[email protected]>
To: [email protected]
Subject: Cron <root@mutiny-remote> cd /opt/mutiny/bin;./latencytest > /dev/null
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=818>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_GB.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <[email protected]>
Date: Wed, 27 Jun 2018 11:17:01 +0100 (BST)
(standard_in) 2: syntax error
it seems to be flagging a syntax error that it does not flag when running it manual.
So finaly the script, (I have removed some of the the logic and commented out the locking for testing purposes)
I am asuming that it might be the fping / AWK command as the but I am not sure how to work out what is causing the syntax error.
#!/bin/bash
## create lock directory so only one instance of script can run
#if ! mkdir /tmp/myscript.lock 2>/dev/null; then
# echo "Lockfile found exiting" >&2
# exit 1
#fi
#get current date and time
date=$(date +%Y%m%d%H%M%S)
### this area of the script we set up the remote host to test , number of ping to send for each test and size of packet.
size=500
number=50
IP=8.8.8.8
hostname=database 4
folder="/opt/mutiny/agentResults"
### here we can set up the warning and critical values for each of the 3 values. average and max latency and packet lost per script cycle
### we are not testing the minimum value just reporting it.
### Due to how ping works the first packet can have a high spike in latency of possible 10+ ms above the average. so it is
### sugested that focus should be on average and packet loss.
maxW=5
maxC=15
avgW=5
avgC=15
lossW=1
lossC=5
avgS=OK
maxS=OK
lossS=OK
## we also set the status agent begin status state to 1 (this is OK 0, is critical and 2 is warning)
status=1
statusline="Latency with in Limits"
### First step is to get the latency vaules we can report on, we use fping for this and then various cut and awk steps to extract the required data
variable=$(fping -c $number -p 50 -b $size $IP 2>&1 | awk '/min/ {print $5,$8;}' OFS='/')
sent=$(echo $variable | cut -d '/' -f 1)
recived=$(echo $variable | cut -d '/' -f 2 )
min=$(echo $variable | cut -d '/' -f 4)
avg=$(echo $variable | cut -d '/' -f 5)
max=$(echo $variable | cut -d '/' -f 6)
loss=$(echo $sent-$recived | bc)
### Next we need to determ if any of the vaules are in a warning or critical state and update the return string to report this clearly.
### we test max, avg and then packet loss. in this way the agent will report the highest critical status over all.
### one we have updated the status to the required value we are ready to out put it all in to mutiny agent format
## This block of code creates the table that mutiny will display with in the node agent.
### line one sets up the headers
##last we close the table
echo "Latency To $hostname|$status|$date|$statusline||" > $folder/latencyout.out
echo "Latency To $hostname|D|maxlatency|$hostname|$max|Max Latency|ms|" >> $folder/latencyout.out
echo "Latency To $hostname|D|minlatency|$hostname|$min|Min Latency|ms|" >> $folder/latencyout.out
echo "Latency To $hostname|D|avglatency|$hostname|$avg|Average Latency|ms|" >> $folder/latencyout.out
echo "Latency To $hostname|D|packetloss|$hostname|$loss|Packetloss||" >> $folder/latencyout.out
rm -rf "/tmp/myscript.lock"
Solution
Please delete if you wish but as I was posting this some one showed me the issue.
the problem was different enviromental variables used when running manualy and running from Cron. Mainly the paths
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home//.local/bin:/home//bin
vs
PATH=/usr/bin:/bin
The simple solution was to use the full path to fping in the script, which found by typing
whereis fping
Script now runs correctly. Could also have changed the path env for Cron but I think using full paths in script is the better method in this case.
If this question is duplicated please delete, but I thought it might help some others.
Answered By - DevilWAH