david/scripts-archive
david
/
scripts-archive
Archived
1
0
Fork 0
This repository has been archived on 2022-04-16. You can view files and clone it, but cannot push or open issues or pull requests.
scripts-archive/heartbeat.sh

148 lines
4.2 KiB
Bash

#!/bin/bash
#\
# \
# \_ _ _ _ _ _ _ _ _ _ _ _ _
# #\
# # \
# heartbeat monitoring # \
# script # /
# # /
# _ _ _ _ _ _ _ _ _ _ _ _ _#/
# /
# /
#/
# **** config section ****
#do not touch as long as you're not me
author="david@socialnerds.org"
version="0.1"
giturl="git://git.aec.at/mbots.git"
configfile="/etc/mbots/mbots.conf"
log2stdout="1"
logwhat="mbots_heartbeat"
file="/etc/mbots/heartbeat.lst"
# **** preflight ****
#read configfile
if [ -r $configfile ]; then
source $configfile
else
echo "ERROR: configfile not found. terminating."
exit 1
fi
#load bashlib
if [ -d $bashlibpath ]; then
source $bashlibpath/main
source $bashlibpath/logengine
log debug "preflight - logengine loaded"
else
echo "ERROR: bashlib not found. terminating."
exit 1
fi
#check environment
log info "**** starting heartbeat bot ****"
#set linecount to 1
linecount="1"
#read one line at a time
while read line; do
#find first letter to determine if line is empty or a comment
fletter=${line:0:1}
#check for first letter
if [ -z $fletter ]; then
#skip line it's empty
log debug "config - skipping line $linecount in $file: it's empty"
elif [ $fletter = "#" ]; then
#skip line it's a comment
log debug "config - skipping line $linecount in $file: it's a comment"
else
# getting hostname from heartbeat.lst
hostname=$(echo $line | awk '{print $1}')
log debug "checking $hostname"
#check ipv6
ipv6=$(dig $hostname AAAA +short | grep ":")
if [ -z "$ipv6" ]; then
log warning "no ipv6 record for $hostname"
log debug "checking /etc/hosts"
hosts=$(cat /etc/hosts | grep -iw $hostname | awk '{print $1}' | grep ":")
if [ -z $hosts ]; then
log warning "no ipv6 hosts entry found"
else
for var in $hosts; do
log info "pinging $hostname($var)"
ping6 -c 1 $var &> /dev/null
err=$?
if [ $err = "0" ]; then
log info "$hostname($var) is reachable"
else
log error "$hostname($var) is not reachable"
fi
done
fi
else
for var in $ipv6; do
log info "pinging $hostname($var)"
ping6 -c 1 $var &> /dev/null
err=$?
if [ $err = "0" ]; then
log info "$hostname($var) is reachable"
else
log error "$hostname($var) is not reachable"
fi
done
fi
#check ipv4
ipv4=$(dig $hostname A +noall +answer | grep -v ";" | grep -v "CNAME" | awk '{print $5}' | grep -v "^$")
if [ -z "$ipv4" ]; then
log warning "no ipv4 record for $hostname"
log debug "checking /etc/hosts"
hosts=$(cat /etc/hosts | grep -iw $hostname | awk '{print $1}' | grep ".")
if [ -z $hosts ]; then
log warning "no ipv4 hosts entry found"
else
for var in $hosts; do
log info "pinging $hostname($var)"
ping -c 1 $var &> /dev/null
err=$?
if [ $err = "0" ]; then
log info "$hostname($var) is reachable"
else
log error "$hostname($var) is not reachable"
fi
done
fi
else
for var in $ipv4; do
log info "pinging $hostname($var)"
ping -c 1 $var &> /dev/null
err=$?
if [ $err = "0" ]; then
log info "$hostname($var) is reachable"
else
log error "$hostname($var) is not reachable"
fi
done
fi
fi
let linecount++
done < $file
log debug "**** all checks done ****"
# **** end of script ****