-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,55 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
# Log all commands for debugging | ||
exec 1> >(logger -s -t $(basename $0)) 2>&1 | ||
|
||
echo "Starting service validation..." | ||
|
||
# Function to check if a port is accepting connections | ||
check_port() { | ||
local port=$1 | ||
local timeout=$2 | ||
nc -z -w$timeout localhost $port | ||
return $? | ||
} | ||
|
||
# Function to check HTTP endpoint (adjust URL as needed) | ||
check_http() { | ||
local url=$1 | ||
local timeout=$2 | ||
curl -s -f -m $timeout "$url" > /dev/null | ||
return $? | ||
# Default values | ||
DEFAULT_HOST="localhost" | ||
DEFAULT_PORT="8080" | ||
DEFAULT_SERVICE="srv.service" | ||
|
||
# Function to check if service is running | ||
check_service() { | ||
local service_name=$1 | ||
|
||
echo "Checking if service $service_name is running..." | ||
if ! systemctl is-active --quiet "$service_name"; then | ||
echo "ERROR: Service $service_name is not running" | ||
systemctl status "$service_name" | ||
return 1 | ||
fi | ||
echo "Service $service_name is running" | ||
return 0 | ||
} | ||
|
||
# Function to check logs using journalctl | ||
check_logs() { | ||
local service=$1 | ||
local time_window=$2 | ||
|
||
# Check for errors in recent logs | ||
ERROR_COUNT=$(journalctl -u $service --since "$time_window ago" -p err | wc -l) | ||
|
||
if [ $ERROR_COUNT -gt 0 ]; then | ||
echo "WARNING: Found $ERROR_COUNT errors in the last $time_window" | ||
echo "Recent errors:" | ||
journalctl -u $service --since "$time_window ago" -p err --no-pager | ||
return 1 | ||
fi | ||
return 0 | ||
# Function to check web endpoint | ||
check_web() { | ||
local host=$1 | ||
local port=$2 | ||
local url="http://${host}:${port}/" | ||
|
||
echo "Checking web endpoint at $url..." | ||
if ! curl -s -f --connect-timeout 5 "$url" >/dev/null; then | ||
echo "ERROR: Web service is not responding at $url" | ||
return 1 | ||
fi | ||
echo "Web endpoint at $url is accessible" | ||
return 0 | ||
} | ||
|
||
# Main function | ||
main() { | ||
local host=${1:-$DEFAULT_HOST} | ||
local port=${2:-$DEFAULT_PORT} | ||
local service=${3:-$DEFAULT_SERVICE} | ||
|
||
# Check if service is running | ||
if ! systemctl is-active --quiet srv.service; then | ||
echo "ERROR: Service is not running" | ||
echo "Service status:" | ||
systemctl status srv.service | ||
exit 1 | ||
fi | ||
echo "Starting validation with host=$host, port=$port, service=$service" | ||
|
||
# Check process resources | ||
echo "Checking process resources..." | ||
SERVICE_PID=$(systemctl show --property MainPID --value srv.service) | ||
if [ -z "$SERVICE_PID" ] || [ "$SERVICE_PID" -eq "0" ]; then | ||
echo "ERROR: Cannot find service PID" | ||
exit 1 | ||
fi | ||
# Run checks | ||
check_service "$service" || exit 1 | ||
check_web "$host" "$port" || exit 1 | ||
|
||
# Check memory usage | ||
MEM_USAGE=$(ps -o pmem= -p $SERVICE_PID) | ||
if [ $(echo "$MEM_USAGE > 90" | bc -l) -eq 1 ]; then | ||
echo "WARNING: High memory usage detected: $MEM_USAGE%" | ||
fi | ||
|
||
# Check port availability (adjust port as needed) | ||
echo "Checking service port..." | ||
if ! check_port 8080 5; then | ||
echo "ERROR: Service port is not responding" | ||
exit 1 | ||
fi | ||
|
||
# Check HTTP endpoint (adjust URL as needed) | ||
echo "Checking service health endpoint..." | ||
if ! check_http "http://localhost:8080/" 5; then | ||
echo "ERROR: Health check failed" | ||
exit 1 | ||
fi | ||
echo "All validations passed successfully" | ||
exit 0 | ||
} | ||
|
||
# Check logs for the last 2 minutes | ||
echo "Checking service logs..." | ||
if ! check_logs srv.service "2 minutes"; then | ||
echo "WARNING: Found errors in recent logs" | ||
# You might want to exit 1 here depending on your requirements | ||
fi | ||
# Run main with command line arguments | ||
main "$@" |