Skip to content

Instantly share code, notes, and snippets.

@mg6
Last active April 7, 2023 02:32
Show Gist options
  • Save mg6/474dc992917d77d6af537167eaf47791 to your computer and use it in GitHub Desktop.
Save mg6/474dc992917d77d6af537167eaf47791 to your computer and use it in GitHub Desktop.
influxping — report ping results to InfluxDB
#!/bin/bash
# MIT License
#
# Copyright (c) 2016 Maciej Gamrat
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
usage() {
cat <<EOF 1>&2
NAME
$(basename $0) - report ping results to InfluxDB
SYNOPSIS
$(basename $0) [OPTION]... HOST...
DESCRIPTION
Ping specified HOST(s) and report times to InfluxDB instance.
--db <string>
InfluxDB database name to write to. Defaults to "netstats".
--host <string>
Host or IP address. Defaults to "localhost".
--port <number>
Host port number to use. Defaults to 8086.
--proto <string>
Protocol to use. Defaults to "http".
--series <string>
Measurement series name. Defaults to "ping".
-h, --help
Displays usage inforation.
AUTHOR
Written by Maciej Gamrat <influxping@gamrat.it>.
EOF
exit 2
}
if [ $# -eq 0 ]; then
usage
fi
proto="${INFLUXPING_PROTO:-http}"
host="${INFLUXPING_HOST:-localhost}"
port="${INFLUXPING_PORT:-8086}"
db="${INFLUXPING_DB:-netstats}"
series="${INFLUXPING_SERIES:-ping}"
while true; do
case "$1" in
-h|--help)
usage
;;
--proto)
proto="$2"
shift 2
;;
--host)
host="$2"
shift 2
;;
--port)
port="$2"
shift 2
;;
--db)
db="$2"
shift 2
;;
--series)
series="$2"
shift 2
;;
--)
shift
break
;;
*)
break
;;
esac
done
if [ $# -eq 0 ]; then
usage
fi
while true; do
for HOST in "$@"; do (
output=$(ping -c1 $HOST)
status=$?
if [ $status -eq 0 ]; then
ms=$(grep -Eo 'time=[0-9.]+' <<< "$output" | cut -d'=' -f2)
value_pair=",value=$ms"
fi
line="$series,host=$HOST status=${status}i${value_pair}"
curl -XPOST "$proto://$host:$port/write?db=$db" --data-binary "$line" &&
echo "$line"
) done
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment