Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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