Я написал простой скрипт для поиска задержки в сети (скрипт в конце поста)
Когда я запускаю этот скрипт из CLI (как root), используя
. / Latencytest работает нормально, и я получаю вывод типа
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||
однако я добавил следующую строку в cron (как root и попробовал несколько изменений)
* * * * * cd /opt/mutiny/bin;./latencytest > /dev/null
Я получаю следующие данные
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||
Обратите внимание на отсутствие значений для различных метрик в выходных данных.
Это работает на Centos, и я получаю почту в var / spool / mail / root, которая говорит
From root@mutiny-remote.localdomain Wed Jun 27 11:17:01 2018
Return-Path: <root@mutiny-remote.localdomain>
X-Original-To: root
Delivered-To: root@mutiny-remote.localdomain
Received: by mutiny-remote.localdomain (Postfix, from userid 0)
id 22CB9872769; Wed, 27 Jun 2018 11:17:01 +0100 (BST)
From: "(Cron Daemon)" <root@mutiny-remote.localdomain>
To: root@mutiny-remote.localdomain
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: <20180627101701.22CB9872769@mutiny-remote.localdomain>
Date: Wed, 27 Jun 2018 11:17:01 +0100 (BST)
(standard_in) 2: syntax error
похоже, что это означает синтаксическую ошибку, которая не помечается при запуске вручную.
Итак, наконец, сценарий (я удалил часть логики и закомментировал блокировку для целей тестирования)
Я предполагаю, что это может быть команда fping / AWK, но я не уверен, как определить причину синтаксической ошибки.
#!/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"