~scripters/scripting/trunk

« back to all changes in this revision

Viewing changes to network/pingcheck

  • Committer: Michael Lustfield
  • Date: 2010-09-29 14:56:49 UTC
  • Revision ID: michael@profarius.com-20100929145649-8kdawtezhiksi2z3
Minor touchups and enhancements to pingcheck. Functionality is almost identical but usability improved.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
32
32
#
33
33
# USAGE:
34
 
#   pingcheck
 
34
#   pingcheck [configuration_file]
35
35
#
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.
38
38
#
39
39
#  REQUIRES:
40
 
#    sendemail curl
41
 
 
 
40
#    curl
 
41
#    sendemail [ if email_errors=1 ]
42
42
 
43
43
##                                         ##
44
44
# Session Validation / Basic Error Checking #
45
45
##                                         ##
46
46
 
47
 
# Check if the session is interactive
 
47
# Allows user specified config
 
48
 
 
49
if [[ "$1" ]]; then
 
50
  config_file="$1"
 
51
else
 
52
  config_file="/etc/default/pingcheck"
 
53
fi
 
54
 
 
55
# Prevent verbose output if non-interactive
 
56
 
48
57
test -t 1 && interactive=1
49
58
 
50
 
# Set colors
 
59
# Pretty simple functions
 
60
 
51
61
if ((interactive)); then
52
62
  red=$(tput setaf 1; tput bold)
53
63
  yellow=$(tput setaf 3; tput bold)
55
65
  reset=$(tput sgr0)
56
66
fi
57
67
 
58
 
# Echo if we have tty
59
 
function ttyecho() {
 
68
function echo_tty() {
60
69
  ((interactive)) && echo -e "$@"
61
70
}
62
71
 
63
 
# Pretty output for each response
64
 
function response() {
 
72
function write_response() {
65
73
  url="$1"; code="$2"; delay="$3"
66
74
  ((interactive)) && printf "%-60s%-10s%-10s\n" "$url" "$delay" "$code"
67
75
}
68
76
 
69
 
# Echo on die
70
77
function die() {
71
 
  echo "$@";
 
78
  echo_tty "$@";
72
79
  exit 1
73
80
}
74
81
 
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
 
83
 
 
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
80
88
 
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
91
99
email_errors=1
92
100
 
93
 
# Send email if error code is 000
94
 
# This indicates either a timeout or DNS issue and is sometimes temporary
95
 
email_on_000=1
96
 
 
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"
115
119
# Testing
116
120
##
117
121
 
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
120
124
timeout=5
121
125
 
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
124
 
validate_timeout=1
125
 
 
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"
129
131
 
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
132
 
use_regex=1
133
 
 
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"
137
136
 
138
137
# File with list of URL's to 'pingcheck'
139
138
# One per line
143
142
#configured=1
144
143
PINGCHECK_DEFAULTS
145
144
 
146
 
  die "Please edit /etc/default/pingcheck"
 
145
  die "Please edit "$config_file""
147
146
else
148
147
 
149
 
  # Read configuration file
150
 
  source /etc/default/pingcheck
151
 
fi
152
 
 
153
 
((configured)) || die "Please edit /etc/default/pingcheck"
154
 
 
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"
165
 
 
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."
168
 
 
169
 
# Check installed apps
 
148
  source "$config_file"
 
149
fi
 
150
 
 
151
# Basic configuration checking
 
152
 
 
153
((configured)) || die "Please configure "$config_file""
 
154
 
 
155
if ((email_errors)); then
 
156
  for var in recipient sender subject smtp_server; do
 
157
    [[ "${!var}" ]] || die "Please set $var in $config_file."
 
158
  done
 
159
fi
 
160
 
 
161
for var in timeout url_file; do
 
162
  [[ "${!var}" ]] || die "Please set $var in $config_file."
 
163
done
 
164
 
 
165
[[ -f "$url_file" ]] || die "Not Found: $url_file\nFile must contain LIST of URL's to check."
 
166
 
 
167
# Checking for required apps
 
168
 
170
169
if ((email_errors)); then
171
170
  type -P sendemail >/dev/null || die "This script requires that sendemail be installed."
172
171
fi
177
176
# Check For Valid Network #
178
177
##                       ##
179
178
 
180
 
if ((validate_timeout)); then
 
179
if [[ "$validate_url" ]]; then
181
180
  status=$(curl -fksI -o /dev/null -w "%{http_code}" "$validate_url")
182
181
 
183
182
  if [[ "$status" != "200" ]]; then
184
 
    die $(ttyecho "Network Validation: ${red}Failed${reset}\n")
 
183
    die "Network Validation: ${red}Failed${reset}\n"
185
184
  else
186
 
    ttyecho "Network Validation: ${green}Passed${reset}\n"
 
185
    echo_tty "Network Validation: ${green}Passed${reset}\n"
187
186
  fi
188
187
fi
189
188
 
192
191
# Read URL List & Check (Ping) For Life #
193
192
##                                     ##
194
193
 
195
 
# Print headings if interactive
196
 
response "URL" "STATUS" "DELAY"
 
194
write_response "URL" "STATUS" "DELAY"
197
195
 
198
 
# Start reading list of URL's
199
196
while read url; do
200
197
 
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#*|}"
205
201
 
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"
212
 
      ((count++))
213
 
    fi
 
204
    write_response "$url" "${yellow}$status${reset}" "$delay"
 
205
    error_1="$error_1 $status - $url\n"
 
206
    ((count++))
214
207
 
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"
219
212
    ((count++))
220
213
 
221
214
  # If status code was 200, then we check the body against the regular expression
222
215
  else
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"
228
221
        ((count++))
229
222
      else
230
 
        response "$url" "${green}$status${reset}" "$delay"
 
223
        write_response "$url" "${green}$status${reset}" "$delay"
231
224
      fi
232
225
    fi
233
226
  fi
238
231
# Email Reporting #
239
232
##               ##
240
233
 
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
244
 
 
245
 
    # Translate subject
246
 
    now=$(date)
247
 
    host=$(hostname)
248
 
    subject="${subject/\%date/$now}"
249
 
    subject="${subject/\%host/$host}"
250
 
    subject="${subject/\%count/$count}"
251
 
 
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"
257
 
      fi
258
 
    fi
259
 
 
260
 
    # Send Message
261
 
    sendemail -f "$sender" -t "$recipient" -u "$subject" -m "$message_body" -s "$smtp_server"
262
 
  fi
263
 
fi
 
234
[[ -n "$error_1$error_2" ]] || exit
 
235
((email_errors)) || exit
 
236
 
 
237
now=$(date)
 
238
host=$(hostname)
 
239
subject="${subject/\%date/$now}"
 
240
subject="${subject/\%host/$host}"
 
241
subject="${subject/\%count/$count}"
 
242
 
 
243
message_body="Code Errors:\n$error_1"
 
244
[[ -n "$error_2" ]] && message_body="$message_body\n\nExpression Errors:\n$error_2"
 
245
 
 
246
sendemail -f "$sender" -t "$recipient" -u "$subject" -m "$message_body" -s "$smtp_server"
 
247