Search This Blog

Showing posts with label myproject. Show all posts
Showing posts with label myproject. Show all posts

Monday, August 16, 2010

CPU Monitoring(Doctortux)

Following command are usefull for monitoring cpu performance so they are included in the script.

#uptime

The above command helps us find the important aspect of the machine which is How long the machine is been running? in other world when did it start running?
The more the uptime the slower the performance.

#w

The above command helps to determine how many users are logged into the system.
The more the sessions the slower the performance.

#ps auxf | sort -nr -k 3 | head -10

The above command helps to determine the top 10 processes affecting cpu utilization.


#cat /proc/cpuinfo

The above command gives you information about cpu of machine.

Please Leave us with your comments and Queries/Suggestions.
I will try to reply asap.

Wednesday, August 11, 2010

Future plan

To enhance doctortux i have following plans.

Refer following link and try to incorporate most of functionality into the script.


http://www.cyberciti.biz/tips/top-linux-monitoring-tools.html

Monday, August 9, 2010

Actual script( Doctortux)

After a long discussion with various forums i have decided to name it as doctortux.

I m going to post modified content of Performance monitoring script.


#######################################################################
#!/bin/bash
#########################UPTIME########################################
fn_uptimeinfo()
{
echo "Machine Uptime Information:"
echo -e "\033[32m uptime"
tput sgr0 #restores the terminal settings to normal.
uptime
echo "Command Description:"
echo " "
echo "Number of users logged in to machine"
echo "load average ,,"
echo "________________________________________________________________"
}
fn_terminalinfo()
{
echo "Terminal Information:"
echo -e "\033[32m w"
tput sgr0
w
echo "________________________________________________________________"
}
#################################################################
#####################CONNECTION INFO############################
fn_numberofconnection()
{
echo "Connection Information"
echo -e "\033[32m netstat -nat | awk '{ print $5}' | cut -d: -f1 | sed -e '/^$/d' | uniq"
tput sgr0
netstat -nat | awk '{ print $5}' | cut -d: -f1 | sed -e '/^$/d' | uniq
}
#################################################################
######################CPU INFO########################################
fn_cpuinfo()
{
echo -e "\033[32m cat /proc/cpuinfo"
tput sgr0
cat /proc/cpuinfo
echo "________________________________________________________________"
}
fn_cpueaterprocess()
{
echo "Top 10 C.P.U Consumer Processes:"
echo -e "\033[32m ps -auxf | sort -nr -k 3 | head -10 "
tput sgr0
ps -auxf | sort -nr -k 3 | head -10
echo "________________________________________________________________"
}
######################################################################

#########################Memory Information###########################
fn_meminfo()
{
echo -e "\033[32m cat /proc/meminfo"
tput sgr0
cat /proc/meminfo
echo "________________________________________________________________"

echo -e "\033[32m free -m"
tput sgr0
free -m
echo "________________________________________________________________"

}

fn_memeaterprocess()
{
echo "Top 10 Memory Consumer Processes:"
echo -e "\033[32m ps -auxf | sort -nr -k 4 | head -10 "
tput sgr0
ps -auxf | sort -nr -k 4 | head -10
echo "________________________________________________________________"
}
######################################################################
#######################Disk Information###############################
fn_diskinfo()
{
echo "Disk Utilization"
echo -e "\033[32m df -h"
tput sgr0
df -h
echo "________________________________________________________________"
}
fn_partitioninfo()
{
echo "Disk Partition Information"
echo -e "\033[32m cat /proc/partitions"
tput sgr0
cat /proc/partitions
echo "________________________________________________________________"
}
######################################################################
fn_automatic()
{
echo "########CPU Information########"
fn_uptimeinfo
fn_terminalinfo
fn_cpuinfo
fn_cpueaterprocess
echo "########Memory Information########"
fn_meminfo
fn_memeaterprocess
echo "########Disk Information########"
fn_diskinfo
fn_partitioninfo
echo "#######Connection Information#######"
fn_numberofconnection
}
if [ $# -eq 0 ]
then
echo "Automatic Mode Selected"
fn_automatic
else
while getopts ai option 2>/dev/null
do
case "$option" in
a) echo "Automatic Mode Selected"
fn_automatic
echo "Thanks";;
i) echo "Interacive Mode Selected";;
?) echo "Please select proper option"
echo list of available options
echo "-a automatic -i interactive" ;;
esac
done
fi

Wednesday, July 28, 2010

Safest way of comparing values using Variable.

Method 1:
if [ "x$HAVE_CIFS" = "xyes" ]; then
...
fi

Method 2:
if [ "$HAVE_CIFS" = "yes" ]; then
...
fi

in above 2 methods method one is safest as it deals with versions of "test" that don't deal with empty strings correctly.

Now onwards i will try to use the safest way whenever is possible.

reference
http://www.linuxquestions.org/questions/showthread.php?p=4048568&posted=1#post4048568

More of Robust scripting article can be found in following blog entry.
http://learnlinuxwithrohan.blogspot.com/2010/08/robust-scripting.html

Thursday, July 22, 2010

Precaution to take before deleting file through script.

Here i m highlighting the best practice to delete a file when there is a variable involved as a file name.

#!/bin/bash
echo "Enter file or folder to delete from tmp"
read input
if [ ! -f "/tmp/$input" ]
then
echo "File Doesnt Exist"
else
rm -f "/tmp/$input"
fi


rm -f, --force
ignore nonexistent files, never prompt

Thursday, July 15, 2010

parsing command line option .

I have decided to run the script in two mode
1)Interactive.
2)Automatic.

Automatic mode ( -a ) (default):This mode shoudnt ask any input from user and should run automatically.

Interactive mode (-i) : This mode should ask user for input .


Initially i m trying to concentrate on automatic mode which is by default.



I have used getopts method to deal with command line arguments. However this might restricted to bash script because of getopts method.
Method used in program.
if [ $# -eq 0 ]
then
echo "Automatic Mode Selected"
else
while getopts ai option 2>/dev/null
do
case "$option" in
a) echo "Automatic Mode Selected";;
i) echo "Interacive Mode Selected";;
?) echo "Please select proper option"
echo list of available options
-a automatic -i interactive ;;
esac
done
fi

However I will also post universal method to establish the same task.

while [ "$#" -gt 0 ]; do
case "$1" in
-o|--long-option)
: do something
;;
-p|--option-with-arg)
: do something with $2
shift
;;
*)
# this could be an invalid option or just a file
# if no file is expected, do something like:
echo "invalid option or argument: $1"
;;
esac
shift
done

Please Leave us with your comments and Queries/Suggestions.
I will try to reply asap.

Trimming a variable in shell script.

The easiest way to trim a variable in shell script.
VARIABLE=$( echo $VARIABLE )
The shell automatically removes seemingly excessive whitespaces, which is why you have to double quote variables if you want to preserve them.

External Source:
http://www.unix.com/shell-programming-scripting/138857-how-trim-variable-linux.html#post302431753

Take a look at following example.
#!/bin/bash


VARIABLEA="Ajay "
VARIABLEB=" M Pawar"
echo "$VARIABLEA"
echo "$VARIABLEB"
VARIABLEC=$(echo "$VARIABLEA $VARIABLEB")
echo "$VARIABLEC"


VARIABLEM=$(echo $VARIABLEA)
VARIABLEN=$(echo $VARIABLEB)
echo "$VARIABLEM"
echo $VARIABLEN
VARIABLEO=$(echo "$VARIABLEM $VARIABLEN")
echo $VARIABLEO



Check the output yourself.

Wednesday, July 14, 2010

One of the critical element is to know How long the machine is running.
uptime is used for this purpose.
It provides following information.
1)How long the machine is running?
2)How many users are currently logged in the machine.
3)current workload,avg workload for last 5 min,avg workload for last 15 min.

Thursday, July 8, 2010

Color Combination used in script.

To make the script more user friendly I have decided to add some colors in output.

3 Basic colors are used in showing the output.

1)Green. (echo -e "\033[32m Hello World")
2)Red. (echo -e "\033[31m Hello World")
3)White. (The "tput sgr0" restores the terminal settings to normal.)

conventions
Green : Command used for execution
Red : User Input
White : Output./Notes


**************************************
More options

BGGREEN=`echo "\033[42m"` # background to green
BGCYAN=`echo "\033[45m"` # background to cyan
BGWHITE=`echo "\033[47m"` # background to white
FGBLACK=`echo "\033[30m"` # foreground to black
FGRED=`echo "\033[31m"` # foreground to red
BGBLACK=`echo "\033[40m"` # background to black
FGGREEN=`echo "\033[32m"` # foreground to green
BGWHITEFGBLUE=`echo "\033[91m"` # background to white & foreground to blue
#BGWHITEFGBLUE=`echo "\033[94m"` # background to white & foreground to blue
BGWHITEFGMAGENTA=`echo "\033[95m"` # background to white & fg to magenta
BGYELLOW=`echo "\033[43m"` # background to yellow
FGBLUE=`echo "\033[34m"` # foreground to blue
FGMAGENTA=`echo "\033[35m"` # foreground to magenta
FGCYAN=`echo "\033[36m"` # foreground to cyan
BGBLUE=`echo "\033[44m"` # background to blue
BGRED=`echo "\033[41m"` # background to red
FGWHITE=`echo "\033[37m"` # foreground to white
BGCYAN=`echo "\033[45m"` # background to cyan
BLINK=`echo "\033[5m"` #terminal to blink
NORMAL=`echo "\033[m"` #terminal to normal
UNDERLINE=`echo "\033[4m"` #terminal to underline
NOUNDERLINE=`echo "\033[24m"` #terminal to cancel underline
REVERSE=`echo "\033[7m"` #terminal to reverse video
BGMAGENTA=`echo "\033[45m"` # background to magenta
BOLD=`echo "\033[1m"` #bold

Click on image to see high resolution version of it.

For more detailed tput information please go through this informative article.
http://www.thegeekstuff.com/2011/01/tput-command-examples/?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+TheGeekStuff+%28The+Geek+Stuff%29

Wednesday, July 7, 2010

My project ( Performance Monitoring in Linux) How/When/Why about my project.

Today i have decided to write my own linux script for performance monitoring.

I know there are plenty of tool over there but i wanted to take this as a self learning to improve on my scripting and analyzing skills.

Performance can be monitored considering following aspects:
1)CPU
2)MEMORY (RAM)
3)HARDDISK IO
4)NETWORK