Last active
February 1, 2026 14:01
-
-
Save levisre/da046edfa8d3e9d778e90d602d51157f to your computer and use it in GitHub Desktop.
Check smb service by running nc and alert to telegram
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 | |
| # Completely created by vibe coding | |
| # Telegram Bot API Token | |
| TELEGRAM_BOT_TOKEN="YOUR_TELEGRAM_BOT_TOKEN" | |
| # Telegram Chat ID to send messages to | |
| TELEGRAM_CHAT_ID="YOUR_TELEGRAM_CHAT_ID" | |
| # Samba host to check | |
| SAMBA_HOST="YOUR_SAMBA_HOST" | |
| # Samba port | |
| SAMBA_PORT=445 | |
| # Time to wait between checks (in seconds) | |
| CHECK_INTERVAL=10 | |
| # Log file | |
| LOG_FILE="/var/log/smb_healthcheck.log" | |
| PID_FILE="/var/run/smb_healthcheck.pid" | |
| # Function to log messages | |
| log_message() { | |
| echo "$(date): $1" >> "$LOG_FILE" | |
| } | |
| # Function to send Telegram message | |
| send_telegram_message() { | |
| MESSAGE="CRITICAL: The Samba service on $SAMBA_HOST is down at $(date)!" | |
| log_message "Samba service on $SAMBA_HOST is not responding. Sending Telegram alert." | |
| curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \ | |
| -d "chat_id=$TELEGRAM_CHAT_ID" \ | |
| -d "text=$MESSAGE" > /dev/null | |
| } | |
| start() { | |
| log_message "Starting Samba health check for $SAMBA_HOST..." | |
| # shellcheck disable=SC2028 | |
| echo $$ > "$PID_FILE" | |
| while true; do | |
| # Check if the port is open | |
| if ! nc -z -w 5 "$SAMBA_HOST" "$SAMBA_PORT"; then | |
| send_telegram_message | |
| else | |
| log_message "Samba service on $SAMBA_HOST is running." | |
| fi | |
| # Wait for the next check | |
| sleep "$CHECK_INTERVAL" | |
| done | |
| } | |
| stop() { | |
| log_message "Stopping Samba health check." | |
| kill "$(cat "$PID_FILE")" | |
| rm "$PID_FILE" | |
| } | |
| case "$1" in | |
| start) | |
| start & | |
| ;; | |
| stop) | |
| stop | |
| ;; | |
| *) | |
| echo "Usage: $0 {start|stop}" | |
| exit 1 | |
| ;; | |
| esac |
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
| /var/log/smb_healthcheck.log { | |
| daily | |
| rotate 7 | |
| compress | |
| delaycompress | |
| missingok | |
| notifempty | |
| create 0640 root adm | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment