-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingpie.sh
executable file
·184 lines (161 loc) · 4.29 KB
/
pingpie.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
JSON_CONFIG='./config.json' # TODO: set this up to have an override mechanism when custom path provided
declare -A fail_counter
declare -A last_fired
main() {
ensure_deps
load_vars
parse_config
perform_checks
}
load_vars() {
if [[ -f ".env" ]]; then
echo 'Loading vars...'
export $(grep -v '^#' .env | xargs)
else
echo "Error: .env not found."
exit 1
fi
REQUIRED_VARS=("ACCOUNT_SID" "AUTH_TOKEN" "FROM_PHONE" "TO_PHONE")
for var in "${REQUIRED_VARS[@]}"; do
if [[ -z "${!var}" ]]; then
echo "Error: Required variable $var is not set in $ENV_FILE."
exit 1
fi
done
}
ensure_deps() {
echo 'Verifying dependencies'
local dependencies=("curl" "jq" "date")
determine_package_manager
for dep in "${dependencies[@]}"; do
if ! command -v "$dep" &>/dev/null; then
if [[ "$PACKAGE_MANAGER" == "UNKNOWN" ]]; then
echo "$dep is not installed on your system and your package manager is unknown. Cannot auto-install. Exiting."
return 1
else
prompt_install
fi
else
echo "$dep is already installed."
fi
done
}
prompt_install() {
read -rp "Would you like to install $dep with $PACKAGE_MANAGER? [y/n] " response
if [[ "$response" == "y" || "$response" == "Y" ]]; then
echo "Installing $dep with $PACKAGE_MANAGER..."
$INSTALL_CMD "$dep"
if [[ $? -ne 0 ]]; then
echo "Failed to install $dep. Please install it manually."
else
echo "$dep installed successfully."
fi
else
echo "$dep is required but was not installed. Exiting."
exit 1
fi
}
determine_package_manager() {
if [[ -f /etc/os-release ]]; then
. /etc/os-release
case "$ID" in
ubuntu | debian)
PACKAGE_MANAGER="apt"
INSTALL_CMD="sudo apt update && sudo apt install -y"
;;
fedora)
PACKAGE_MANAGER="dnf"
INSTALL_CMD="sudo dnf install -y"
;;
centos | rhel)
PACKAGE_MANAGER="yum"
INSTALL_CMD="sudo yum install -y"
;;
arch)
PACKAGE_MANAGER="pacman"
INSTALL_CMD="sudo pacman -Sy --noconfirm"
;;
*)
PACKAGE_MANAGER="UNKNOWN"
INSTALL_CMD="UNKNOWN"
echo "Unsupported distribution. Please install dependencies manually."
;;
esac
else
PACKAGE_MANAGER="UNKNOWN"
INSTALL_CMD="UNKNOWN"
echo "/etc/os-release not found. Unable to determine package manager."
fi
}
parse_config() {
if [[ ! -f "$JSON_CONFIG" ]]; then
echo "Error: JSON file $JSON_CONFIG not found."
exit 1
fi
if jq -e '.alerts' "$JSON_CONFIG" &>/dev/null; then
for alert in $(jq -c '.alerts[]' "$JSON_CONFIG"); do
url=$(echo "$alert" | jq -r '.url')
name=$(echo "$alert" | jq -r '.name')
allowed_fails=$(echo "$alert" | jq -r '.allowedFails')
if [[ -z "$url" || -z "$name" || ! "$allowed_fails" =~ ^[0-9]+$ ]]; then
echo "Error: Invalid alert structure detected."
exit 1
fi
fail_counter["$name"]=0
last_fired["$name"]=0
echo "Alert:"
echo " URL: $url"
echo " Name: $name"
echo " Allowed Fails: $allowed_fails"
done
else
echo "Error: 'alerts' key is missing in the JSON data."
exit 1
fi
}
perform_checks() {
first_alert=$(jq -c '.alerts[0].name' "$JSON_CONFIG")
while true; do
for alert in $(jq -c '.alerts[]' "$JSON_CONFIG"); do
url=$(echo "$alert" | jq -r '.url')
name=$(echo "$alert" | jq -r '.name')
allowed_fails=$(echo "$alert" | jq -r '.allowedFails')
if [[ "$name" == "${first_alert//\"/}" ]]; then
sleep 30
fi
check
done
done
}
check() {
response=$(curl -L --max-time 5 --write-out "%{http_code} %{url_effective}" --silent --output /dev/null "$url")
http_code=$(echo "$response" | awk '{print $1}')
final_url=$(echo "$response" | awk '{print $2}')
if [[ "$http_code" =~ ^2 ]]; then
echo "Check for $url returned a 2xx"
fail_counter[$name]=0
else
echo "Error: Request to $url failed with HTTP status code $http_code."
((fail_counter["$name"]++))
fi
if [[ ${fail_counter[$name]} -gt $allowed_fails ]]; then
handle_alert
fi
}
handle_alert() {
current_time=$(date +%s)
time_diff=$((current_time - last_fired[$name]))
if [[ "$time_diff" -gt 900 ]]; then
alert
fi
}
alert() {
MESSAGE="Warning! Your alert for $name has failed! Please take action if necessary!"
curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$ACCOUNT_SID/Messages.json" \
--data-urlencode "Body=$MESSAGE" \
--data-urlencode "From=$FROM_PHONE" \
--data-urlencode "To=$TO_PHONE" \
-u "$ACCOUNT_SID:$AUTH_TOKEN"
}
main