Last active
December 11, 2024 21:16
-
-
Save filipnet/4780bfd4cc8170f5ef9ca298044709c6 to your computer and use it in GitHub Desktop.
This script checks the configuration syntax of various services on a Linux system. It executes predefined check commands for each service, verifies the output, and reports whether the service configuration is valid or not. The script supports common services like Nginx, Apache, SSH, and others. The script is easily expandable—services can be add…
This file contains hidden or 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
| #!/bin/bash | |
| # =========================== | |
| # Service Configuration | |
| # =========================== | |
| # Format: "<Service Name>;<Check Command>;<Expected String>" | |
| SERVICES=( | |
| "apache;apachectl configtest;Syntax OK" | |
| #"haproxy;haproxy -c -f /etc/haproxy/haproxy.cfg;Configuration file is valid" | |
| "nginx;nginx -t;syntax is ok" | |
| "rspamd;rspamadm configtest;syntax OK" | |
| "ssh;sshd -t;syntax OK" | |
| "syslog-ng;syslog-ng -s;Syntax OK" | |
| "php-fpm;php-fpm -t;test is successful" | |
| ) | |
| # =========================== | |
| # Configuration | |
| # =========================== | |
| # Set SHOW_OUTPUT to true to show command outputs under the status line, false to hide them | |
| SHOW_OUTPUT=true | |
| # =========================== | |
| # Color Codes for Output | |
| # =========================== | |
| BOLD="\e[1m" | |
| GREEN="\e[32m" | |
| RED="\e[31m" | |
| RESET="\e[0m" | |
| # =========================== | |
| # Service Check Function | |
| # =========================== | |
| check_service() { | |
| local service_name="$1" | |
| local check_command="$2" | |
| local expected_string="$3" | |
| # Execute the check command and capture the output | |
| output=$($check_command 2>&1) | |
| # If the output is empty, consider the service as valid | |
| if [[ -z "$output" ]]; then | |
| echo -e "[${BOLD}${GREEN}Valid${RESET}] ${BOLD}$service_name${RESET}" | |
| if $SHOW_OUTPUT; then | |
| echo -e " (No output)" | |
| fi | |
| else | |
| # Check if expected string is present in the output | |
| if [[ -n "$expected_string" ]] && echo "$output" | grep -q "$expected_string"; then | |
| echo -e "[${BOLD}${GREEN}Valid${RESET}] ${BOLD}$service_name${RESET}" | |
| if $SHOW_OUTPUT; then | |
| echo -e " $output" | |
| fi | |
| else | |
| echo -e "[${BOLD}${RED}Invalid${RESET}] ${BOLD}$service_name${RESET}" | |
| if $SHOW_OUTPUT; then | |
| echo -e " $output" | |
| fi | |
| fi | |
| fi | |
| } | |
| # =========================== | |
| # Main Program | |
| # =========================== | |
| for entry in "${SERVICES[@]}"; do | |
| IFS=";" read -r service_name check_command expected_string <<< "$entry" | |
| check_service "$service_name" "$check_command" "$expected_string" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment