Last active
October 17, 2023 14:09
-
-
Save fegue/d5fddff9fe53016423f5b971365e62ed to your computer and use it in GitHub Desktop.
Use `sar`to monitor system usage and log it to a file
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 | |
##################################################### | |
# Script Name: System Usage Monitor for Raspberry Pi | |
# Description: This script uses the 'sar' command to monitor system usage on a Raspberry Pi over a specified duration. | |
# Usage: ./start_sar.sh <custom_name> <interval_in_seconds> <count> | |
# | |
# Installation: sudo apt install sysstat | |
# Source: https://github.com/sysstat/sysstat | |
# | |
# Arguments: | |
# <custom_name> : A custom name prefix for the output file. | |
# <interval_in_seconds> : Interval between each sample in seconds. | |
# <count> : Total number of samples to be taken. | |
# | |
# Output: | |
# The output will be a file named `<custom_name>_<start_time>_to_<end_time>.dat` | |
# stored in a directory called `sar` within the user's home directory. | |
# | |
# Example: | |
# ./start_sar.sh my_monitor 5 60 | |
# This would monitor the system every 5 seconds, for a total of 60 times. | |
##################################################### | |
# Check if the correct number of arguments are provided | |
if [ "$#" -ne 3 ]; then | |
echo "Usage: $0 <custom_name> <interval_in_seconds> <count>" | |
exit 1 | |
fi | |
# Extract arguments | |
CUSTOM_NAME=$1 | |
INTERVAL=$2 | |
COUNT=$3 | |
# Calculate total duration in seconds | |
DURATION=$((INTERVAL * COUNT)) | |
# Define the start time in the format YYYYMMDD_HHMMSS | |
START_TIME=$(date +"%Y%m%d_%H%M%S") | |
# Calculate the end time based on the given duration | |
END_TIME=$(date -d "+$DURATION seconds" +"%Y%m%d_%H%M%S") | |
# Define the directory path and ensure it exists | |
DIR_PATH="${HOME}/sar" | |
mkdir -p $DIR_PATH | |
# Generate the filename | |
OUTPUT_FILE="${DIR_PATH}/${CUSTOM_NAME}_${START_TIME}_to_${END_TIME}.dat" | |
# Execute sar | |
sar -A -o $OUTPUT_FILE $INTERVAL $COUNT > /dev/null 2>&1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment