|
| 1 | +#!/usr/bin/env sh |
| 2 | + |
| 3 | +# Default values |
| 4 | +DEFAULT_URL="http://127.0.0.1:15000/config_dump" |
| 5 | +DEFAULT_REQ_TMOUT=1 # Single request timeout in seconds |
| 6 | +DEFAULT_INTERVAL=1 # Interval between requests in seconds |
| 7 | +DEFAULT_TIMEOUT=60 # Total timeout in seconds |
| 8 | + |
| 9 | +# Display usage |
| 10 | +usage() { |
| 11 | + echo "Usage: $0 [options]" |
| 12 | + echo "Options:" |
| 13 | + echo " -u, --url URL URL to check (default: $DEFAULT_URL)" |
| 14 | + echo " -r, --req-tmout SEC Single request timeout in seconds (default: $DEFAULT_REQ_TMOUT)" |
| 15 | + echo " -i, --interval SEC Interval between requests in seconds (default: $DEFAULT_INTERVAL)" |
| 16 | + echo " -t, --timeout SEC Total timeout in seconds (default: $DEFAULT_TIMEOUT)" |
| 17 | + echo " -h, --help Show this help message" |
| 18 | +} |
| 19 | + |
| 20 | +# Initialize variables with default values |
| 21 | +URL=$DEFAULT_URL |
| 22 | +REQ_TMOUT=$DEFAULT_REQ_TMOUT |
| 23 | +INTERVAL=$DEFAULT_INTERVAL |
| 24 | +TIMEOUT=$DEFAULT_TIMEOUT |
| 25 | + |
| 26 | +# Parse command line arguments |
| 27 | +while [[ $# -gt 0 ]]; do |
| 28 | + case $1 in |
| 29 | + -u|--url) |
| 30 | + URL="$2" |
| 31 | + shift 2 |
| 32 | + ;; |
| 33 | + -r|--req-tmout) |
| 34 | + REQ_TMOUT="$2" |
| 35 | + shift 2 |
| 36 | + ;; |
| 37 | + -i|--interval) |
| 38 | + INTERVAL="$2" |
| 39 | + shift 2 |
| 40 | + ;; |
| 41 | + -t|--timeout) |
| 42 | + TIMEOUT="$2" |
| 43 | + shift 2 |
| 44 | + ;; |
| 45 | + -h|--help) |
| 46 | + usage |
| 47 | + exit 0 |
| 48 | + ;; |
| 49 | + *) |
| 50 | + echo "Unknown parameter: $1" |
| 51 | + usage |
| 52 | + exit 1 |
| 53 | + ;; |
| 54 | + esac |
| 55 | +done |
| 56 | + |
| 57 | +# Calculate start time |
| 58 | +start_time=$(date +%s) |
| 59 | + |
| 60 | +echo "Starting HTTP service check..." |
| 61 | +echo "URL: $URL" |
| 62 | +echo "Single request timeout (REQ_TMOUT): $REQ_TMOUT seconds" |
| 63 | +echo "Request interval (INTERVAL): $INTERVAL seconds" |
| 64 | +echo "Total timeout (TIMEOUT): $TIMEOUT seconds" |
| 65 | + |
| 66 | +while true; do |
| 67 | + # Check if total timeout is exceeded |
| 68 | + current_time=$(date +%s) |
| 69 | + elapsed=$((current_time - start_time)) |
| 70 | + |
| 71 | + if [ $elapsed -ge $TIMEOUT ]; then |
| 72 | + echo "Total timeout of $TIMEOUT seconds reached. Service check failed!" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + |
| 76 | + # Send HTTP request |
| 77 | + response=$(curl -s -o /dev/null -w "%{http_code}" \ |
| 78 | + --connect-timeout $REQ_TMOUT \ |
| 79 | + --max-time $REQ_TMOUT \ |
| 80 | + "$URL" 2>/dev/null) |
| 81 | + |
| 82 | + if [ "$response" = "200" ]; then |
| 83 | + echo "HTTP service is ready!" |
| 84 | + echo "Total time elapsed: $elapsed seconds" |
| 85 | + exit 0 |
| 86 | + else |
| 87 | + echo "Service not ready, HTTP status code: $response" |
| 88 | + echo "Time elapsed: $elapsed seconds, timeout: $TIMEOUT seconds" |
| 89 | + echo "Waiting $INTERVAL seconds before next attempt..." |
| 90 | + sleep $INTERVAL |
| 91 | + fi |
| 92 | +done |
| 93 | + |
0 commit comments