Last active
October 5, 2020 11:32
-
-
Save ashishb888/729c9eab0207cfc9f473deb98e5b0050 to your computer and use it in GitHub Desktop.
Analyze the top command (batch mode) output. Extract the used and idle CPU usage and sort by used CPU in descending order
This file contains 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
#Single file. Works well with double digits e.g. %Cpu(s): 16.5 us, 81.7 id. You may have to change -f2,10 to -f3,11 or something related | |
#!/bin/bash | |
inFile=$1 | |
grep 'Cpu(s)' $inFile | cut -d ' ' -f2,10 | sed '/[a-z]/d' | sort -t',' -k1nr,1 | |
# Calling the script | |
# bash top-cpu-usage-analysis.sh /var/tmp/cpu-stats-2020-10-01T18-16-58.log | |
#Sample output | |
# us idle | |
# 41.7 57.6 | |
# 28.6 69.9 | |
# 24.8 73.5 | |
# 24.7 74.4 | |
# 20.4 78.5 | |
# 19.0 80.1 | |
# 18.9 80.2 | |
# 17.2 82.0 | |
# 16.7 81.7 | |
# 16.5 82.7 | |
# 15.6 83.4 | |
# 14.9 83.7 | |
# 12.1 86.5 | |
# 12.1 86.7 | |
# 11.7 87.0 | |
# 10.6 88.1 | |
# 10.5 88.9 | |
# 10.4 89.0 | |
#Multiple files | |
for i in {1..8}; do echo server$i ; sleep 5; grep 'Cpu(s)' server$i/cpu-stats-2020-10* | cut -d ' ' -f2,10 | sed '/[a-z]/d' | sort -t',' -k1nr,1 > ../top-analysis/$RANDOM.log; done | |
#Sample output | |
# [myuser@host top-analysis]$ head -n 5 * | |
# ==> server1.log <== | |
# us idle | |
# 41.7 57.6 | |
# 28.6 69.9 | |
# 24.8 73.5 | |
# 24.7 74.4 | |
# 20.4 78.5 | |
# | |
# ==> server2.log <== | |
# 22.1 76.5 | |
# 21.9 76.7 | |
# 19.3 79.3 | |
# 15.5 83.2 | |
# 14.8 84.6 | |
# | |
# ==> server3.log <== | |
# 32.4 67.0 | |
# 31.0 66.9 | |
# 23.8 74.3 | |
# 23.5 74.6 | |
# 22.3 76.1 | |
# | |
# ==> server4.log <== | |
# 55.8 34.3 | |
# 28.2 69.4 | |
# 25.2 73.3 | |
# 24.7 73.2 | |
# 24.0 73.3 | |
# | |
# ==> server5.log <== | |
# 64.8 29.9 | |
# 50.2 46.5 | |
# 42.3 54.9 | |
# 38.8 56.9 | |
# 36.4 60.8 | |
# | |
# ==> server6.log <== | |
# 53.6 45.5 | |
# 39.8 57.5 | |
# 35.9 59.8 | |
# 33.7 63.6 | |
# 29.8 67.9 | |
# | |
# ==> server7.log <== | |
# 84.1 13.7 | |
# 82.9 14.8 | |
# 80.7 16.4 | |
# 78.1 19.5 | |
# 77.7 17.8 | |
# | |
# ==> server8.log <== | |
# 11.6 86.2 | |
# 11.0 88.1 | |
# 10.8 88.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment