|
1 | 1 | #!/bin/bash
|
2 | 2 | set -e
|
3 | 3 |
|
4 |
| -# Log all commands for debugging |
5 |
| -exec 1> >(logger -s -t $(basename $0)) 2>&1 |
6 |
| - |
7 |
| -echo "Starting service validation..." |
8 |
| - |
9 |
| -# Function to check if a port is accepting connections |
10 |
| -check_port() { |
11 |
| - local port=$1 |
12 |
| - local timeout=$2 |
13 |
| - nc -z -w$timeout localhost $port |
14 |
| - return $? |
15 |
| -} |
16 |
| - |
17 |
| -# Function to check HTTP endpoint (adjust URL as needed) |
18 |
| -check_http() { |
19 |
| - local url=$1 |
20 |
| - local timeout=$2 |
21 |
| - curl -s -f -m $timeout "$url" > /dev/null |
22 |
| - return $? |
| 4 | +# Default values |
| 5 | +DEFAULT_HOST="localhost" |
| 6 | +DEFAULT_PORT="8080" |
| 7 | +DEFAULT_SERVICE="srv.service" |
| 8 | + |
| 9 | +# Function to check if service is running |
| 10 | +check_service() { |
| 11 | + local service_name=$1 |
| 12 | + |
| 13 | + echo "Checking if service $service_name is running..." |
| 14 | + if ! systemctl is-active --quiet "$service_name"; then |
| 15 | + echo "ERROR: Service $service_name is not running" |
| 16 | + systemctl status "$service_name" |
| 17 | + return 1 |
| 18 | + fi |
| 19 | + echo "Service $service_name is running" |
| 20 | + return 0 |
23 | 21 | }
|
24 | 22 |
|
25 |
| -# Function to check logs using journalctl |
26 |
| -check_logs() { |
27 |
| - local service=$1 |
28 |
| - local time_window=$2 |
29 |
| - |
30 |
| - # Check for errors in recent logs |
31 |
| - ERROR_COUNT=$(journalctl -u $service --since "$time_window ago" -p err | wc -l) |
32 |
| - |
33 |
| - if [ $ERROR_COUNT -gt 0 ]; then |
34 |
| - echo "WARNING: Found $ERROR_COUNT errors in the last $time_window" |
35 |
| - echo "Recent errors:" |
36 |
| - journalctl -u $service --since "$time_window ago" -p err --no-pager |
37 |
| - return 1 |
38 |
| - fi |
39 |
| - return 0 |
| 23 | +# Function to check web endpoint |
| 24 | +check_web() { |
| 25 | + local host=$1 |
| 26 | + local port=$2 |
| 27 | + local url="http://${host}:${port}/" |
| 28 | + |
| 29 | + echo "Checking web endpoint at $url..." |
| 30 | + if ! curl -s -f --connect-timeout 5 "$url" >/dev/null; then |
| 31 | + echo "ERROR: Web service is not responding at $url" |
| 32 | + return 1 |
| 33 | + fi |
| 34 | + echo "Web endpoint at $url is accessible" |
| 35 | + return 0 |
40 | 36 | }
|
41 | 37 |
|
| 38 | +# Main function |
| 39 | +main() { |
| 40 | + local host=${1:-$DEFAULT_HOST} |
| 41 | + local port=${2:-$DEFAULT_PORT} |
| 42 | + local service=${3:-$DEFAULT_SERVICE} |
42 | 43 |
|
43 |
| -# Check if service is running |
44 |
| -if ! systemctl is-active --quiet srv.service; then |
45 |
| - echo "ERROR: Service is not running" |
46 |
| - echo "Service status:" |
47 |
| - systemctl status srv.service |
48 |
| - exit 1 |
49 |
| -fi |
| 44 | + echo "Starting validation with host=$host, port=$port, service=$service" |
50 | 45 |
|
51 |
| -# Check process resources |
52 |
| -echo "Checking process resources..." |
53 |
| -SERVICE_PID=$(systemctl show --property MainPID --value srv.service) |
54 |
| -if [ -z "$SERVICE_PID" ] || [ "$SERVICE_PID" -eq "0" ]; then |
55 |
| - echo "ERROR: Cannot find service PID" |
56 |
| - exit 1 |
57 |
| -fi |
| 46 | + # Run checks |
| 47 | + check_service "$service" || exit 1 |
| 48 | + check_web "$host" "$port" || exit 1 |
58 | 49 |
|
59 |
| -# Check memory usage |
60 |
| -MEM_USAGE=$(ps -o pmem= -p $SERVICE_PID) |
61 |
| -if [ $(echo "$MEM_USAGE > 90" | bc -l) -eq 1 ]; then |
62 |
| - echo "WARNING: High memory usage detected: $MEM_USAGE%" |
63 |
| -fi |
64 |
| - |
65 |
| -# Check port availability (adjust port as needed) |
66 |
| -echo "Checking service port..." |
67 |
| -if ! check_port 8080 5; then |
68 |
| - echo "ERROR: Service port is not responding" |
69 |
| - exit 1 |
70 |
| -fi |
71 |
| - |
72 |
| -# Check HTTP endpoint (adjust URL as needed) |
73 |
| -echo "Checking service health endpoint..." |
74 |
| -if ! check_http "http://localhost:8080/" 5; then |
75 |
| - echo "ERROR: Health check failed" |
76 |
| - exit 1 |
77 |
| -fi |
| 50 | + echo "All validations passed successfully" |
| 51 | + exit 0 |
| 52 | +} |
78 | 53 |
|
79 |
| -# Check logs for the last 2 minutes |
80 |
| -echo "Checking service logs..." |
81 |
| -if ! check_logs srv.service "2 minutes"; then |
82 |
| - echo "WARNING: Found errors in recent logs" |
83 |
| - # You might want to exit 1 here depending on your requirements |
84 |
| -fi |
| 54 | +# Run main with command line arguments |
| 55 | +main "$@" |
0 commit comments