Bash – Building your own “top” command

Top

“top” is versatile monitoring tool, providing run time summary view of many stats (CPU/Memory/Swap/Process/Load average etc), so admins prefer it a lot while troubleshooting several issues. It is provided by procps-ng-3.3.10-23.el7.x86_64 package, so you can install it as that well, if you feel missing, usually it is shipped by default in Fedora variant *nix servers (Red Hat / CentOS).

What if you want to build your own top command, for some further customization or in case it is not available. Please read further for steps for that. Feel free to update your customization as per your choice in your environment.

Understanding top output

In order to create our top, we need to first understand its functionality, so that we are as  closer as possible.

It’s divided in two parts. The first stanza displaces following information:

  • System uptime, load average, logged in users
  • Process summary
  • CPU statistics
  • Memory statistics
  • Swap statistics

pic1

Next stanza deals with process details and their consumption

pic2.PNG

Building your top step By step:

For writing 1st line, we can use below CLI excerpt:

pic3

Further 2nd line need some work, some knowledge of “ps”,”awk” & “grep” command:

pic4

Now proceeding towards our CPU%, this can be easily achieved through sar command.

pic5

Now coming towards memory/swap stats, try your free command as below

pic6

pic7

After 1st stanza is completed, let’s move to second stanza for process information with this small excerpt

pic8

Since we have completed almost all steps, let’s proceed to compile all the steps and print top replica.

So running below code in all on shell will show some great “top like” output.

[root@tower ~]# clear ;echo "top - `uptime`"; echo "Tasks: `ps -ef | egrep -v 'UID|-ef'| wc -l` total, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| grep ^R | wc -l` running, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| grep ^S* | wc -l` sleeping, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| egrep '^T|^t' | wc -l` stopped, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| egrep ^Z | wc -l` zombie"; echo "%Cpu(s): `sar 1 1 | egrep -v 'user|Average|Linux|^$' | awk '{print $4 " us, " $6 " sy, " $5 " ni, " $9 " id, " $7 " wa, " $8 " st" }'`"; echo "Kib Mem : `free | grep -v total | head -1 | awk '{ print $2 " total, " $4 " free, " $3 " used, " $6 " buff/cache"}'`";echo "Kib Swap: `free | grep -v total | tail -1 | awk '{ print $2 " total, " $4 " free, " $3 " used. "}'` `free | grep -v total | head -1 | awk '{ print $7 " avail Mem"}'`"; echo ; ps -eo pid,user,pri,ni,vsz,rss,state,%cpu,%mem,cputime,comm | sort -rk8 | head -20

pic9.PNG

Icing the cake

pic10.PNG

Similar to a cake, no code is good unless you can present it good. Let’s write a small bash script and then try to execute the code from there.

[root@tower ~]# cat mytop.sh
#!/bin/bash
clear
echo "********************** Welcome to my custom top iteration $1 **************************"
echo
echo "top - `uptime`"; echo "Tasks: `ps -ef | egrep -v 'UID|-ef'| wc -l` total, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| grep ^R | wc -l` running, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| grep ^S* | wc -l` sleeping, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| egrep '^T|^t' | wc -l` stopped, `ps auxx | egrep -v 'grep|USER|auxx' | awk '{print $8}'| egrep ^Z | wc -l` zombie"
echo "%Cpu(s): `sar 1 1 | egrep -v 'user|Average|Linux|^$' | awk '{print $4 " us, " $6 " sy, " $5 " ni, " $9 " id, " $7 " wa, " $8 " st" }'`"
echo "Kib Mem : `free | grep -v total | head -1 | awk '{ print $2 " total, " $4 " free, " $3 " used, " $6 " buff/cache"}'`"
echo "Kib Swap: `free | grep -v total | tail -1 | awk '{ print $2 " total, " $4 " free, " $3 " used. "}'` `free | grep -v total | head -1 | awk '{ print $7 " avail Mem"}'`"
echo
ps -eo pid,user,pri,ni,vsz,rss,state,%cpu,%mem,cputime,comm | sort -rk8 | head -20
echo
echo "****************************************************************************************"
sleep $2
[root@tower ~]#

Provide execute permission to script using chmod command as below

[root@tower ~]# chmod +x  mytop.sh 

Now using this script inside simple loop with 1st positional parameter as loop iteration and 2nd as sleep number

[root@tower ~]# for i in {1..100} ; do ./mytop.sh $i 4 ; done

pic11

So this top command will run 100 times (iterations) repeating after every 4 seconds.

So, finally all set, you have successfully created your custom “top” command using bash shell. It is easy to understand and test in your environment. Do read, share with your friends and let us know about your feedback. Happy Learning, stay tuned 🙂 

– Admin

References:

man pages:

  • top
  • sar
  • ps
  • free
  • awk

20 thoughts on “Bash – Building your own “top” command

  1. hello!,I like your writing very much! share we communicate more about your post on AOL? I need a specialist on this area to solve my problem. Maybe that’s you! Looking forward to see you.

    Like

  2. Hello there, I discovered your web site via Google whilst searching for a similar matter, your site got here up, it appears to be like great. I have bookmarked it in my google bookmarks.

    Like

Leave a comment