treii28
Posts: 88
Joined: Fri May 10, 2013 4:52 pm

simple heartbeat

Tue May 07, 2019 4:19 pm

I've been playing around with various host monitoring tools primarily looking for something very lightweight for checking up-status of my growing collection of Pis. I stumbled on some examples of using ping return status from the command line so I whipped together a very short script to do a simple ping check from a list of hostnames and/or ips and show the results in a json string. I thought someone might find it useful.

Just create a file called ping.lst with one entry per line of the fqdn, local alias and/or ip address of any hosts you want to check. Success gets set to 1, failure to 0 in the resulting json.

SW

ping.sh

Code: Select all

#!/bin/bash

declare -A MYMAP
for a in `cat ping.lst`
  do
    ping -c 1 -t 1 -q "$a" 2>&1 > /dev/null && MYMAP["$a"]=1 || MYMAP["$a"]=0
  done

function assoc2json {
    typeset -n v=$1
    printf '%q\n' "${!v[@]}" "${v[@]}" | jq -R -c -n '[inputs] | . as $v | (length / 2) as $n | reduce range($n) as $idx ({}; .[$v[$idx]]=$v[$idx+$n])'
}

assoc2json MYMAP

treii28
Posts: 88
Joined: Fri May 10, 2013 4:52 pm

Re: simple heartbeat

Tue May 07, 2019 4:21 pm

you could also convert it to use values submitted on the command line by changing the `for a in` line to:

Code: Select all

for a in $*

Return to “Other programming languages”