#!/bin/bash
# sys_monitor2.sh
# Usage: sys_monitor2.sh [-u|-d|-f (D/M)| -c number | -m number]
# Sample system monitor script using options
# Tested under Fedora 9
Invalidoptions()
{
echo "Usage: `basename $0` [OPTIONS]"
echo "OPTIONS:"
echo -e "\t -d for display today's date"
echo -e "\t -u for Logged in users list"
echo -e "\t -f ARG for Disk and Memory Statistics"
echo -e "\t (ARG=D for disk statistics; ARG=M for memory statistics)"
echo -e "\t -c ARG for Top CPU consuming process"
echo -e "\t (ARG=10 means top 10 process)"
echo -e "\t -m ARG for Top Memory consuming process"
echo -e "\t (ARG=10 means top 10 process)"
echo -e "\t Note: Only one option at a time and -f,-c and -m require argument"
exit 1
}
Isnumber()
{
if [ $1 -eq $1 2> /dev/null ]
then
:
else
echo -e "You supplied bad argument, \"$1\" is not a number"
Invalidoptions
fi
}
if [ $# -lt 1 -o $# -gt 2 ]
then
Invalidoptions
fi
if [ $# -eq 1 -a "$1" != "-d" -a "$1" != "-u" -a "$1" != "-f" -a "$1" != "-c" ]
then
Invalidoptions
fi
if [ $# -eq 2 ] && [ "$1" != "-f" -a "$1" != "-c" -a "$1" != "-m" ]
then
Invalidoptions
fi
choice=
top="head -$2"
while getopts udf:c:m: choice
do
case $choice in
d) echo -e " Today's Date: \c"
date +" %d-%h-%Y Time: %T";;
u) echo -e "\tCurrently Logged In Users"
who;;
f)
if [ "$OPTARG" = "D" ]
then
echo -e "\t\tDisk Statistics"
df -h | grep "%"
elif [ "$OPTARG" = "M" ]
then
echo -e "\t Memory Statistics "
free -m | awk 'BEGIN{printf "\t\tTotal\tUsed\tFree\n"; OFS="\t" }\
/Mem/||/Swap/{printf "\t"; print $1,$2,$3,$4}'
else
Invalidoptions
fi;;
m) Isnumber $OPTARG
k3sort="sort -nr -k 3"
echo -e " PID PPID MEM CPU COMMAND "
ps -Ao pid= -o ppid= -o pmem= -o pcpu= -o comm=|$k3sort|$top;;
c) Isnumber $OPTARG
k4sort="sort -nr -k 4"
echo -e " PID PPID MEM CPU COMMAND "
ps -Ao pid= -o ppid= -o pmem= -o pcpu= -o comm=|$k4sort|$top;;
esac
done
Output:[root@localhost blog]# sh sys_monitor2.sh -u
Currently Logged In Users
root tty7 2009-09-23 13:48 (:0)
root pts/2 2009-09-23 14:36 (:0.0)
[root@localhost blog]# sh sys_monitor2.sh -d
Todays Date: 23-Sep-2009 Time: 16:50:38
[root@localhost blog]# sh sys_monitor2.sh -m 5
PID PPID MEM CPU COMMAND
3122 3102 9.6 3.0 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
[root@localhost blog]# sh sys_monitor2.sh -m
Usage: sys_monitor2.sh [OPTIONS]
OPTIONS:
-d for display today's date
-u for Logged in users list
-f ARG for Disk and Memory Statistics
(ARG=D for disk statistics; ARG=M for memory statistics)
-c ARG for Top CPU consuming process
(ARG=10 means top 10 process)
-m ARG for Top Memory consuming process
(ARG=10 means top 10 process)
Note: Only one option at a time and -f,-c and -m require argument
I read this script and this article tell me how to use this system in math and how to solve you math problem thanks for share it online paper proofreader .
ReplyDelete