Skip to content

Instantly share code, notes, and snippets.

@nyxcalamity
Created April 22, 2015 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyxcalamity/e2e5cdeb8ea5e70a26f7 to your computer and use it in GitHub Desktop.
Save nyxcalamity/e2e5cdeb8ea5e70a26f7 to your computer and use it in GitHub Desktop.
Socket monitoring script
##!/bin/bash
#Performs socket monitoring for specific port and outputs the result to a file.
#
#Parameters:
#1st: log file (default:socket-monitoring.sh.csv)
#2nd: number of checks per 1 sec (default:2)
#3rd: port (default:61622)
#
#Example:
# socket-monitoring.sh /media/ws/rhel-socket-monitoring.log 2 61622
#-----------------------------------------------------------------------------------------------------------------------
#Processing params
#-----------------------------------------------------------------------------------------------------------------------
if [[ ! -z $1 ]]; then
CSV_FILE=$1
else
CSV_FILE=$0.csv
fi
if [[ ! -z $2 ]]; then
FREQUENCY=$2
else
FREQUENCY=2
fi
if [[ ! -z $3 ]]; then
PORT=$3
else
PORT=61622
fi
#-----------------------------------------------------------------------------------------------------------------------
#Performing monitoring
#-----------------------------------------------------------------------------------------------------------------------
rm -f $CSV_FILE
DATE=$(date +%Y-%m-%d)
while true; do
TIME="$DATE $(date +%k:%M:%S)"
SOCKETS=$(ss -nat state time-wait dst :$PORT | wc -l)
echo "$TIME,$SOCKETS" >> $CSV_FILE
sleep $(bc <<< "scale=2; 1/$FREQUENCY")
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment