-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentrypoint.sh
82 lines (72 loc) · 4.62 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/sh
buildDate=$(cat /usr/local/pinger.build)
echo "Image Build Date: $buildDate"
echo "Instance Endpoint Name: ${ENDPOINT_NAME:=Pinger}"
echo "Checking required Environment Variables..."
fatal=0
if [ "${PING_URL:-missing}" = "missing" ]; then fatal=1; echo "Env var PING_URL is required."; fi;
if [ "${EXPECTED_RESPONSE:-missing}" = "missing" ]; then fatal=1; echo "Env var EXPECTED_RESPONSE is required."; fi;
if [ "${RELAY_HOST:-missing}" = "missing" ]; then fatal=1; echo "Env var RELAY_HOST is required."; fi;
if [ "${RELAY_USERNAME:-missing}" = "missing" ]; then fatal=1; echo "Env var RELAY_USERNAME is required."; fi;
if [ "${RELAY_PASSWORD:-missing}" = "missing" ]; then fatal=1; echo "Env var RELAY_PASSWORD is required."; fi;
if [ "${RELAY_SENDER_EMAIL_ADDRESS:-missing}" = "missing" ]; then fatal=1; echo "Env var RELAY_SENDER_EMAIL_ADDRESS is required."; fi;
if [ "${TO_EMAIL_ADDR:-missing}" = "missing" ]; then fatal=1; echo "Env var TO_EMAIL_ADDR is required."; fi;
if [ "${RELIABLE_REFERENCE_PING_HOST:-missing}" = "missing" ]; then fatal=1; echo "Env var RELIABLE_REFERENCE_PING_HOST is required (eg. google.com)."; fi;
if [ -z "${RELIABLE_REFERENCE_PING_HOST##*/*}" ]; then fatal=1; echo "Env var RELIABLE_REFERENCE_PING_HOST is a host, not a URL."; fi;
if [ $fatal -eq 1 ]; then echo "FATAL ERROR: missing/bad environment variables need to be corrected."; exit; fi;
echo "Checking logs..."
pingLogFile=$( echo "/var/log/pinger/${ENDPOINT_NAME}.ping.curr.log" | tr "[:blank:]" _ )
firstStatusTS=$([ -f "$pingLogFile" ] && head -n1 "$pingLogFile" | sed 's/,.*//' || echo 0);
if [ "$firstStatusTS" -eq 0 ];
then
logfiletext="Ping log file does not exist. A new file will be initialized."
initText="INIT"
else
logfiletext="Ping log file already exists, starting on $(date -d "@$firstStatusTS" +%D), with $(wc -l < "$pingLogFile") pings so far.";
initText="RE-INIT (reboot)"
fi;
echo "Configuring Crontab job..."
set -f
cronjob="ENDPOINT_NAME=\"${ENDPOINT_NAME:=Pinger}\"
INTERVAL_MIN=\"${INTERVAL_MIN:=5}\"
THRESHOLD_FAILS_FOR_EMAIL=\"${THRESHOLD_FAILS_FOR_EMAIL:=1}\"
PING_URL=\"${PING_URL}\"
IPv6=\"${IPv6:=0}\"
ALLOW_INSECURE=\"${ALLOW_INSECURE:=0}\"
RELIABLE_REFERENCE_PING_HOST=\"${RELIABLE_REFERENCE_PING_HOST}\"
EXPECTED_RESPONSE=\"${EXPECTED_RESPONSE}\"
RELAY_SENDER_EMAIL_ADDRESS=\"${RELAY_SENDER_EMAIL_ADDRESS}\"
RELAY_SENDER_INFORMAL_NAME=\"${RELAY_SENDER_INFORMAL_NAME:=Pinger}\"
TO_EMAIL_ADDR=\"${TO_EMAIL_ADDR}\"
STATUS_EMAIL_DAYS=\"${STATUS_EMAIL_DAYS:=30}\"
# m h dom mon dow command
*/${INTERVAL_MIN:=5} * * * * /usr/local/pinger.sh
"
if crontab -u root -r > /dev/null 2>&1
then
echo "Prior Crontab has been removed."
fi
echo "$cronjob" | awk '{$1=$1;print}' | crontab -u root -;
echo "Crontab has been set."
set +f
echo "Sending init/test email..."
emailText="To: $TO_EMAIL_ADDR
Subject: ${initText} - $ENDPOINT_NAME
From: $RELAY_SENDER_INFORMAL_NAME <$RELAY_SENDER_EMAIL_ADDRESS>
Ping has been successfully initialized
(Pinger version built on: $buildDate)
Name: $ENDPOINT_NAME
URL: $PING_URL $(if [ $ALLOW_INSECURE -eq 0 ]; then echo ''; else echo '(invalid certs allowed)'; fi;)
IPv6: $(if [ $IPv6 -eq 1 ]; then echo 'Yes'; else echo 'No'; fi;)
Expected Response: \"$EXPECTED_RESPONSE\"
Ping every: $INTERVAL_MIN minunte(s)
Threshold: $THRESHOLD_FAILS_FOR_EMAIL failure(s) will be required to send an email.
Reference Ping Host: $RELIABLE_REFERENCE_PING_HOST will be pinged to verify source server's connection when no response from URL.
Status Report: Emailed every ${STATUS_EMAIL_DAYS} day(s).
History: $logfiletext
- Note: mount /var/log/pinger/ as a docker volume to preserve history between reboots.
"
echo -e "$emailText" | awk '{$1=$1;print}' | curl -s -T "-" --ssl-reqd --url "$RELAY_HOST" --mail-from "$RELAY_SENDER_EMAIL_ADDRESS" --mail-rcpt "$TO_EMAIL_ADDR" --user "${RELAY_USERNAME}:${RELAY_PASSWORD}"
echo " sent."
echo "Starting cron, awaiting ping jobs..."
crond -f # keep process in -(f)oreground