0

Newer to GitLab, by using simple ping test using bash script file I try to fetch the value of the test, success or failure and the duration of the ping.

pingtest.sh file

#!/bin/bash
count=$1
target=$2
testname=$3

ping -c $count $target >/dev/null 2>&1

if [ $? -eq 0 ]

then

echo $testname,Success,$(ping -c $count $target | sed '$!d;s|.*/\([0-9.]*\)/.*|\1|')

else

echo $testname,Failure,$(ping -c $count $target | sed '$!d;s|.*/\([0-9.]*\)/.*|\1|')

fi

.gitlab-ci.yml file

image: centos

stages:
  - zero

job run_test_zero:
  stage: zero
  artifacts:
    when: always
    paths:
      - "report/*"
  script:
    - echo "TEST NAME|RESULT|DURATION" >> ./report/report.txt
    - chmod +x ./scripts/pingtest.sh
    - ./scripts/pingtest.sh 3 google.com pinggoolepass >> ./report/report.txt
    - ./scripts/pingtest.sh 3 google1.com pinggoolefail >> ./report/report.txt

report.txt file

TEST NAME|RESULT|DURATION
pinggoolepass,Success,6.066
pinggoolefail,Failure,

I'm unable to fetch the duration for failure ping test.

Please anyone suggest and guide me

  • `ping` on failure to resolve the host will just exit returning a non-zero code, it will not return the time taken for failure – Inian Jan 21 '19 at 09:27
  • Thanks @Inian. Is it possible to get the time taken in any other ways. Can any one suggest and guide me – Vijayan Karuppaiah Jan 21 '19 at 09:54
  • Depends on what time you mean. If you want the time it takes for `ping` to exit, use [`time -f "%e" ping`](https://stackoverflow.com/q/3795470/6770384). However, that value has no meaning and don't understand why you would want to print it. If you are talking about a timeout (not the case for google1.com) you might as well specify a timeout and use that value. By the way: Do you know that you run ping twice? First to check the exit code, then to extract the time. – Socowi Jan 21 '19 at 11:25
  • Thanks @Socowi Can you suggest in single execution time and the exit code. – User515 Jan 22 '19 at 13:08
  • If my answer resolved your question please accept it. Accepting an answer closes this questions and rewards the author of the accepted answer. If your question was not resolved consider explaining why and adding more information to your question. If you are no longer interested in a solution you may as well delete this question. – Socowi Feb 15 '19 at 12:11

1 Answers1

0

Using ping Only Once

First of all, you don't have to execute ping twice. There are multiple ways to extract the exit status and max rrt time in one go. Here is an alternate version of your script:

#!/bin/bash
count="$1"
target="$2"
testname="$3"

if time=$(set -o pipefail;
          ping -c "$count" "$target" 2>&- | awk -F/ 'END {print $6}')
then
    result=Success
else
    result=Failure
fi
echo "$testname,$result,$time"

or you could replace the if ... fi part with

output="$(ping -c "$count" "$target" 2>&-)"
[ $? = 0 ] && result=Success || result=Failure
time="$(awk -F/ 'END {print $6}' <<< "$output")"

Both of these script are equivalent to your script and do not print a time in case ping fails.

Measuring Time In Case of Failure

In order to »fetch the duration« you first have to specify what »the duration« is.

If you want the time it takes for ping to exit, use the time command or measure the time yourself with two calls to date or by using $EPOCHESECONDS in bash ≥ 5.0. However, that value has no meaning and I don't understand why you would want to print it.

If you are talking about a timeout (not the case for google1.com) you might as well specify a timeout yourself and use that value.

Socowi
  • 25,550
  • 3
  • 32
  • 54