80 lines
2.2 KiB
Bash
Executable File
80 lines
2.2 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# "cryptovaltrading.com": {
|
|
# "cloud18": {
|
|
# "users": ["oleg"],
|
|
# "type": "cloud",
|
|
# "ssh_port": 7822,
|
|
# "to_check": "false"
|
|
# "timeout_sec": 5
|
|
# },
|
|
|
|
RootDir="${HOME}/prod"
|
|
|
|
AlertChannel=Alerts-CVTT
|
|
Sender=${RootDir}/ops/utils/send_mmost.sh
|
|
ConfigUrl=http://cloud16.cvtt.vpn:6789/admin/cvtt_hosts
|
|
|
|
HOSTS_CONFIG=$(curl -s ${ConfigUrl} | ${HOME}/bin/hjson -j)
|
|
|
|
get_domains() {
|
|
echo ${HOSTS_CONFIG} | jq -r '. | keys[]'
|
|
}
|
|
get_user_hosts() {
|
|
local User=${1}
|
|
local Domain=${2}
|
|
|
|
jdcmd="jq -r --arg domain \"$Domain\""
|
|
jdcmd="$jdcmd --arg usr \"$User\""
|
|
jdcmd="$jdcmd '.[\$domain]"
|
|
jdcmd="$jdcmd | to_entries[]"
|
|
jdcmd="$jdcmd | select(.value.users[]"
|
|
jdcmd="$jdcmd | contains(\$usr))"
|
|
jdcmd="$jdcmd | .key'"
|
|
echo ${HOSTS_CONFIG} | eval ${jdcmd} |sed "s/$/.$Domain/" # >&2
|
|
}
|
|
|
|
function host_alert() {
|
|
alert=${1}
|
|
if [ "${alert}" != "" ]
|
|
then
|
|
echo -e "### :fire: HOST ALERT \n${alert}" | ${Sender} ${AlertChannel}
|
|
fi
|
|
|
|
}
|
|
|
|
User=oleg
|
|
Hosts=()
|
|
DEFAULT_SSH_PORT=22
|
|
DEFAULT_TIMEOUT=5
|
|
DEFAULT_TO_CHECK="true"
|
|
Domains=("${Domains[@]}" "$(get_domains)")
|
|
|
|
for Domain in ${Domains[@]} ; do
|
|
Hosts=("${Hosts[@]}" "$(get_user_hosts ${User} ${Domain})")
|
|
done
|
|
|
|
for Host in ${Hosts[@]} ; do
|
|
host=$(echo $Host | cut -d'.' -f1)
|
|
Domain=$(echo $Host | cut -d'.' -f2-)
|
|
|
|
|
|
PortSSH=$(echo "$HOSTS_CONFIG" | jq -r --arg domain "$Domain" --arg host "$host" '.[$domain][$host].ssh_port // '"$DEFAULT_SSH_PORT"'')
|
|
|
|
Timeout=$(echo "$HOSTS_CONFIG" | jq -r --arg domain "$Domain" --arg host "$host" '.[$domain][$host].timeout_sec // '"$DEFAULT_TIMEOUT"'')
|
|
|
|
ToCheck=$(echo "$HOSTS_CONFIG" | jq -r --arg domain "$Domain" --arg host "$host" '.[$domain][$host].to_check // '"$DEFAULT_TO_CHECK"'')
|
|
to_check="${ToCheck^^}"
|
|
if [ "${to_check}" == "TRUE" -o "${to_check}" == "YES" -o "${to_check}" == "Y" -o "${to_check}" == "T" ] ; then
|
|
echo "Checking host: $Host on port $PortSSH"
|
|
else
|
|
continue
|
|
fi
|
|
|
|
# Use nc to check if the specified port is open
|
|
if ! nc -z -w ${Timeout} "$Host" "$PortSSH"; then
|
|
echo "Host $Host is not available on port $PortSSH"
|
|
host_alert "Host $Host is not available on port $PortSSH"
|
|
fi
|
|
done
|