31
31
# along with this program. If not, see <http://www.gnu.org/licenses/>.
34
# pingcheck [configuration_file]
36
36
# At first run, this script offers to create a default settings file. Let
37
37
# it do this, then edit the file that it asks you to.
41
# sendemail [ if email_errors=1 ]
44
44
# Session Validation / Basic Error Checking #
47
# Check if the session is interactive
47
# Allows user specified config
52
config_file="/etc/default/pingcheck"
55
# Prevent verbose output if non-interactive
48
57
test -t 1 && interactive=1
59
# Pretty simple functions
51
61
if ((interactive)); then
52
62
red=$(tput setaf 1; tput bold)
53
63
yellow=$(tput setaf 3; tput bold)
60
69
((interactive)) && echo -e "$@"
63
# Pretty output for each response
72
function write_response() {
65
73
url="$1"; code="$2"; delay="$3"
66
74
((interactive)) && printf "%-60s%-10s%-10s\n" "$url" "$delay" "$code"
75
# Check that the configuration file exists or else offer to create it.
76
if [[ ! -e /etc/default/pingcheck ]]; then
82
# Check for config; offer to create template if it doesn't exist
84
if [[ ! -f "$config_file" ]]; then
77
85
((interactive)) || exit 1
78
read -p "File '/etc/default/pingcheck' does not exist, Create defaults? (y/n) " genfile
86
read -p "File '$config_file' does not exist, Create defaults? (y/n) " genfile
79
87
[[ "$genfile" != "y" && "$genfile" != "Y" ]] && exit 1
81
cat > /etc/default/pingcheck <<PINGCHECK_DEFAULTS
89
cat >"$config_file" <<PINGCHECK_DEFAULTS
82
90
# When a configuration option is presented as a number the value must be 0 or 1
83
91
# 0 - Disable feature
84
92
# 1 - Enable feature
90
98
# Send email if error occurs
93
# Send email if error code is 000
94
# This indicates either a timeout or DNS issue and is sometimes temporary
97
101
# List of addresses to receive alerts
98
102
# For SMS check out http://bit.ly/cfy6nf
99
103
recipient="user1@domain.com,user2@domain.com"
118
# Maximum time to wait for a response and download before it is considered dead
119
# This timout will generate a 000 error code
122
# Maximum time to wait for a write_response and download before it is considered dead
123
# This timout will generate a unreachable (000) error code
122
126
# Validate that the system can connect to a real website before testing others
123
127
# This is useful if the testing system loses network connectivity
126
# Known good URL to validate system has connectivity
128
# If no URL is given, this check will not be performed
127
129
# Must generate a 200 status; 301 (redirect) will fail
128
130
validate_url="www.google.com"
130
# After website returns 200, the body of the page can be checked
131
# This is useful if known messages will exist on the page if an error exists
134
# This is the regex passed to grep to be used in regex error validation
135
# The default "messages error" is what will show in a Drupal website.
136
regex_pattern="messages error"
132
# After website returns 200, the body of the page can be checked against a regular
133
# expression. This is the regex passed to grep
134
# The example "messages error" is what will show on Drupal if errors are present
135
#regex_pattern="messages error"
138
137
# File with list of URL's to 'pingcheck'
144
143
PINGCHECK_DEFAULTS
146
die "Please edit /etc/default/pingcheck"
145
die "Please edit "$config_file""
149
# Read configuration file
150
source /etc/default/pingcheck
153
((configured)) || die "Please edit /etc/default/pingcheck"
155
# Make sure all configuarion options were set
156
[[ "$email_errors" ]] || die "'email_errors' not set"
157
[[ "$recipient" ]] || die "'recipient' not set"
158
[[ "$sender" ]] || die "'sender' not set"
159
[[ "$subject" ]] || die "'subject' not set"
160
[[ "$smtp_server" ]] || die "'smtp_server' not set"
161
[[ "$timeout" ]] || die "'timeout' not set"
162
[[ "$use_regex" ]] || die "'use_regex' not set"
163
[[ "$regex_pattern" ]] || die "'regex_pattern' not set"
164
[[ "$url_file" ]] || die "'url_file' not set"
166
# Check the url list is available
167
[[ -e "$url_file" ]] || die "Not Found: $url_file\nFile must contain LIST of URL's to check."
169
# Check installed apps
148
source "$config_file"
151
# Basic configuration checking
153
((configured)) || die "Please configure "$config_file""
155
if ((email_errors)); then
156
for var in recipient sender subject smtp_server; do
157
[[ "${!var}" ]] || die "Please set $var in $config_file."
161
for var in timeout url_file; do
162
[[ "${!var}" ]] || die "Please set $var in $config_file."
165
[[ -f "$url_file" ]] || die "Not Found: $url_file\nFile must contain LIST of URL's to check."
167
# Checking for required apps
170
169
if ((email_errors)); then
171
170
type -P sendemail >/dev/null || die "This script requires that sendemail be installed."
177
176
# Check For Valid Network #
180
if ((validate_timeout)); then
179
if [[ "$validate_url" ]]; then
181
180
status=$(curl -fksI -o /dev/null -w "%{http_code}" "$validate_url")
183
182
if [[ "$status" != "200" ]]; then
184
die $(ttyecho "Network Validation: ${red}Failed${reset}\n")
183
die "Network Validation: ${red}Failed${reset}\n"
186
ttyecho "Network Validation: ${green}Passed${reset}\n"
185
echo_tty "Network Validation: ${green}Passed${reset}\n"
192
191
# Read URL List & Check (Ping) For Life #
195
# Print headings if interactive
196
response "URL" "STATUS" "DELAY"
194
write_response "URL" "STATUS" "DELAY"
198
# Start reading list of URL's
199
196
while read url; do
201
# Nice list of options to get our status code
202
198
response=$(curl -fksI --max-time $timeout -o /dev/null -w "%{http_code}|%{time_total}" "$url")
203
199
status="${response%|*}"
204
200
delay="${response#*|}"
206
202
# If status_code is 000, then we have a timeout or DNS failure
207
203
if [[ "$status" == "000" ]]; then
208
response "$url" "${yellow}$status${reset}" "$delay"
209
# User may or may not care about this
210
if ((email_on_000)); then
211
error_1="$error_1 $status - $url\n"
204
write_response "$url" "${yellow}$status${reset}" "$delay"
205
error_1="$error_1 $status - $url\n"
215
208
# If status_code is not 200, then there's an issue
216
209
elif [[ "$status" != "200" ]]; then
217
response "$url" "${red}$status${reset}" "$delay"
210
write_response "$url" "${red}$status${reset}" "$delay"
218
211
error_1="$error_1 $status - $url\n"
221
214
# If status code was 200, then we check the body against the regular expression
223
if ((use_regex)); then
216
if [[ "$regex_pattern" ]]; then
224
217
# If the regex matches, there is an error on the page
225
218
if curl -fks --max-time $timeout "$url" | grep -q "$regex_pattern"; then
226
response "$url" "${red}RegErr${reset}" "$delay"
219
write_response "$url" "${red}RegErr${reset}" "$delay"
227
220
error_2="$error_2 RegErr - $url\n"
230
response "$url" "${green}$status${reset}" "$delay"
223
write_response "$url" "${green}$status${reset}" "$delay"
238
231
# Email Reporting #
241
# If there were errors to report and email reporting is configured, send an alert
242
if [[ "$error_1" != "" || "$error_2" != "" ]]; then
243
if ((email_errors)); then
248
subject="${subject/\%date/$now}"
249
subject="${subject/\%host/$host}"
250
subject="${subject/\%count/$count}"
252
# Compose message body
253
message_body="Code Errors:\n$error_1"
254
if ((use_regex)); then
255
if [[ "$error_2" != "" ]]; then
256
message_body="$message_body\n\nExpression Errors:\n$error_2"
261
sendemail -f "$sender" -t "$recipient" -u "$subject" -m "$message_body" -s "$smtp_server"
234
[[ -n "$error_1$error_2" ]] || exit
235
((email_errors)) || exit
239
subject="${subject/\%date/$now}"
240
subject="${subject/\%host/$host}"
241
subject="${subject/\%count/$count}"
243
message_body="Code Errors:\n$error_1"
244
[[ -n "$error_2" ]] && message_body="$message_body\n\nExpression Errors:\n$error_2"
246
sendemail -f "$sender" -t "$recipient" -u "$subject" -m "$message_body" -s "$smtp_server"