Tuesday, September 22, 2009

Sample System Monitor Shell Script -Menu Based


#!/bin/bash
# sys_monitor.sh
# Sample system monitor script using menu
# Tested under Fedora 9
#
# Create the following menu and clear the screen each time it appears
#

clear
echo -e "\033[1m `uname -or` Monitor script\033[0m"
items=" 1.Date
2.Current Users
3.Disk Statistics
4.Memory Statistics
5.Top 10 Memory consuming process
6.Top 10 CPU consuming process
7.Exit"

exit_function()
{
clear
exit
}

#function enter is used to go back to menu and clear screen

enter()
{
ans=
echo ""
echo -e "Do you want to continue(y/n):\c"
stty -icanon min 0 time 0
# When -icanon is set then one character has been received.
# min 0 means that read should read 0 characters.
# time 0 ensures that read is terminated the moment one character is hit.

while [ -z "$ans" ]
do
read ans
done
#The while loop ensures that so long as at least one character is not received
# "read" continue to get executed

if [ "$ans" = "y" -o "$ans" = "Y" ]
then
stty sane # Restoring terminal settings
clear
else
stty sane
exit_function
fi
}

choice=
h10="head -10"
while true
do
echo -e "\n\t PROGRAM MENU \n"
echo -e "$items \n"
echo -n "Enter your choice :"
read choice
case $choice in
1) clear; echo -e "\n\n\t\t Today's Date \n"
date +" %d-%h-%Y Time %T"; enter;;
2) clear; echo -e "\n\n\t\t Currently Logged In Users\n"
who; enter;;
3) clear; echo -e "\n\n\t\t Disk Statistics\n"
df -h | grep "%"
enter;;
4) clear; echo -e "\n\n\t\t Memory Statistics\n "
free -m | awk 'BEGIN{printf "\t\tTotal\tUsed\tFree\n\n"; OFS="\t" }\
/Mem/||/Swap/{printf "\t"; print $1,$2,$3,$4}'
enter;;
5) clear
k3sort="sort -nr -k 3"
echo -e "\033[1m PID PPID MEM CPU COMMAND \033[0m "
ps -Ao pid= -o ppid= -o pmem= -o pcpu= -o comm=|$k3sort|$h10
enter;;
6) clear
k4sort="sort -nr -k 4"
echo -e "\033[1m PID PPID MEM CPU COMMAND \033[0m"
ps -Ao pid= -o ppid= -o pmem= -o pcpu= -o comm=|$k4sort|$h10
enter;;
7)exit_function ;;
*)echo -e "You entered wrong option \n Please enter 1,2,3,4,5,6 or 7\n"
echo " Press enter to continue"
read
clear
esac
done

Output:
2.6.25-14.fc9.i686 GNU/Linux Monitor script
PROGRAM MENU

1.Date
2.Current Users
3.Disk Statistics
4.Memory Statistics
5.Top 10 memory cosuming process
6.Top 10 CPU consuming process
7.Exit

Enter your choice :5
PID PPID MEM CPU COMMAND
3122 3102 9.2 2.6 firefox
2765 2540 1.9 0.0 nautilus
3849 1 1.7 1.0 ktorrent
2882 1 1.6 0.0 tomboy
2810 1 1.6 0.0 /usr/bin/sealer
4041 1 1.4 0.1 gnome-terminal
2394 2393 1.4 1.8 Xorg
2759 2540 1.2 0.0 gnome-panel
2876 1 0.9 0.0 clock-applet
2870 1 0.9 0.0 mixer_applet2

Do you want to continue(y/n):

12 comments:

  1. Hi Shridhar,

    Thank you for your suggestion. with "read -n 1 choice" you can't get result of choices 10th onwards. In the script I given 7 choices only, but for flexibility I used "read choice".
    I think you know that. But I cleared it for others.

    ReplyDelete
  2. yeah really nice script...easy to understand for any beginner..

    ReplyDelete
  3. thanks sir for making this blog and sharing info !
    it is very helpful.

    ReplyDelete
  4. Thanks a lot. It will be very helpful.

    ReplyDelete
  5. I read your sample system script and its really help them and tell me how to solve math question online thanks for sharing non plagiarized term papers .

    ReplyDelete
  6. http://sysacd.in/portfolio-view/red-hat-linux-training-in-chennai/
    http://sysacad.in/portfolio-view/linux-training-in-chennai/
    http://sysacad.in/portfolio-view/red-hat-linux-training-in-chennai/
    http://sysacad.in/portfolio-view/red-hat-virtualization-rhcva/
    http://sysacad.in/portfolio-view/shell-scripting-training-in-chennai/
    http://sysacad.in/portfolio-view/ibm-aix-system-administration/
    http://sysacad.in/portfolio-view/ibm-hacmp-powerha/

    ReplyDelete