From 67892819486fed81d42a456672bfc6f6c9a0067b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Choch=C3=B3=C5=82?= Date: Thu, 14 Sep 2017 15:14:04 +0200 Subject: [PATCH] Initial commit --- README.md | 8 +++++++ statesnap.sh | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 README.md create mode 100755 statesnap.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..00c8f09 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +* Servcheck +Ever had problem with running services after server reboot? +This script snaps state of things such as: +- List of unique processes names +- List of opened TCP/UDP ports along with proces name +- Firewall rules dump +- Routing table +After restart, you can use the script to create diff's with latest snaps made, making it easy to find out what isn't working. diff --git a/statesnap.sh b/statesnap.sh new file mode 100755 index 0000000..9288a26 --- /dev/null +++ b/statesnap.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +# Definicje kolorków +GREEN='\033[00;92m' +BLUE='\033[00;94m' +RST='\033[0m' + +function msg() { + echo -e "$BLUE * $GREEN$1$RST" +} + +function makeSnap { + FOLDER=~/server_states/state_$(date '+%Y%m%d_%H%M%S') + mkdir -p $FOLDER + ps aux | awk '{ print $11 }' | sort | uniq | egrep -v '^\[' > $FOLDER/ps.out + netstat -lpn | egrep "(tcp.*LISTEN|udp)" | sort | uniq | awk '{split($NF,arr,"/"); print $1 " " $4 " " arr[2]}' > $FOLDER/netstat.out + (/sbin/iptables -L -n; echo -e "\n#NAT\n"; /sbin/iptables -L -nt nat) > $FOLDER/iptables.out + /sbin/ip route > $FOLDER/routing.out + msg "Written state snapshot to $FOLDER" +} + +function compare { + FOLDER=~/server_states/$(ls ~/server_states/ | tail -n 1) + msg "Comparing with snap $FOLDER" + echo -e "$GREEN\nProcesses diff$RST" + echo -e "$BLUE============================================================================$RST" + ps aux | awk '{ print $11 }' | sort | uniq | egrep -v '^\[' | diff - $FOLDER/ps.out + echo -e "$GREEN\nOpened ports diff$RST" + echo -e "$BLUE============================================================================$RST" + netstat -lpn | egrep "(tcp.*LISTEN|udp)" | sort | uniq | awk '{split($NF,arr,"/"); print $1 " " $4 " " arr[2]}' | diff - $FOLDER/netstat.out + echo -e "$GREEN\nFirewall rules diff$RST" + echo -e "$BLUE============================================================================$RST" + (/sbin/iptables -L -n; echo -e "\n#NAT\n"; /sbin/iptables -L -nt nat) | diff - $FOLDER/iptables.out + echo -e "$GREEN\nRouting table diff$RST" + echo -e "$BLUE============================================================================$RST" + /sbin/ip route | diff - $FOLDER/routing.out +} + +function printHelp { + echo -e "Server State 0.2b" + echo -e "$BLUE============================================================================$RST" + echo -e "Użycie: $0 [PARAMETRY]...\n" + printf " $GREEN%-14s$RST %s\n" "snap" "- Make server status snapshot" + printf " $GREEN%-14s$RST %s\n\n" "compare" "- Make a diff comparison against latest snapshot" + exit 0; +} + +# przy braku argumentów wyświetl pomoc +if [ $# -lt 1 ]; then + printHelp +fi + +# główny kod +case $1 in + compare) + compare + ;; + snap) + makeSnap + ;; + *) + msg "Nieprawidłowa komenda. Uruchom \"$0 help\" celem wyświetlenia pomocy" + ;; +esac + +