Created
September 4, 2014 12:49
-
-
Save mayconbordin/01179d5d45e1658669d1 to your computer and use it in GitHub Desktop.
Shell script for logging CPU, memory, network and HDD usage
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 | |
usage() { | |
echo "Usage: $0 COMMAND" | |
echo "" | |
echo "Commands:" | |
echo " start <output-dir> <interval> [<cpu-mem-net-log> <hdd-log>] Start the monitor" | |
echo " stop Stop the monitor" | |
exit 1 | |
} | |
[ "$#" -ge 1 ] || usage | |
PARSE_SAR='BEGIN { date="" } { if (NR == 1) date=$4; if (NF == 11 && $3 != "kbmemfree") printf("%s\t%s\t", $3, $4); if ($3 == "all") printf("%s\t%.2f\t", $9, (100.0-$9)); if ($3 == "eth0") printf("%s\t%s", $6, $7); if ($3 == "CPU") { if (NR > 3) printf("\n"); printf("%s %s %s\t", date, $1, $2); } }' | |
PARSE_IOSTAT='{ if (NF == 1) printf("%s\t", $1); if ($1 == "sda") printf("%s\t%s", $3, $4); if (NF == 0 && NR > 2) printf("\n"); }' | |
BASE_DIR=$(dirname $0) | |
COMMAND=$1 | |
PID_FILE=$BASE_DIR/monitor.pid | |
if [ "$COMMAND" == "start" ]; then | |
echo -ne "Starting monitor... " | |
echo -ne "" > $PID_FILE | |
OUTPUT_DIR=$2 | |
if [ -z "$3" ]; then | |
INTERVAL=1 | |
else | |
INTERVAL=$3 | |
fi | |
if [ -z "$4" ]; then | |
CPU_MEM_NET_LOG=$OUTPUT_DIR/cpu_mem_net.log | |
else | |
CPU_MEM_NET_LOG=$OUTPUT_DIR/$4 | |
fi | |
if [ -z "$5" ]; then | |
HDD_LOG=$OUTPUT_DIR/hdd.log | |
else | |
HDD_LOG=$OUTPUT_DIR/$5 | |
fi | |
export S_TIME_FORMAT=ISO | |
nohup sar -ur -n DEV $INTERVAL | awk -W interactive "$PARSE_SAR" > $CPU_MEM_NET_LOG 2>&1 & | |
echo $! >> $PID_FILE | |
nohup iostat -dmt $INTERVAL | awk -W interactive "$PARSE_IOSTAT" > $HDD_LOG 2>&1 & | |
echo $! >> $PID_FILE | |
echo -ne "DONE\n" | |
exit 0 | |
elif [ "$COMMAND" == "stop" ]; then | |
echo -ne "Stopping monitor... " | |
while read pid; do | |
kill -9 $pid | |
done <$PID_FILE | |
echo -ne "" > $PID_FILE | |
echo -ne "DONE\n" | |
elif [ "$COMMAND" == "info" ]; then | |
echo "Log file: cpu_mem_net" | |
echo "Columns:" | |
echo " timestamp Reading date in format YYYY-MM-DD HH:mm:ss AM|PM" | |
echo " cpu_free Percentage of free CPU" | |
echo " cpu_used Percentage of used CPU" | |
echo " mem_free Amount of free memory in KBytes" | |
echo " mem_used Amount of used memory in KBytes" | |
echo " net_recv Network download rate in KBytes/s" | |
echo " net_sent Network upload rate in KBytes/s" | |
echo "" | |
echo "Log file: hdd" | |
echo "Columns:" | |
echo " timestamp Reading date in format YYYY-MM-DDTHH:mm:ssZZZZ" | |
echo " hdd_read HDD read rate in MBytes/s" | |
echo " hdd_write HDD write rate in MBytes/s" | |
else | |
usage | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
I tried the script above and the result was the following:
./monitor.sh
Usage: ./monitor.sh COMMAND
Commands:
start [ ] Start the monitor
stop Stop the monitor
My question is what modification I need to do in order to get the desired result?
Thanks