Script to monitor Machine Health (Project Doctortux) Input needed.

I have written little script to check the CPU performance of the machine.

Request you to contribute your comments on the same.

Feel free to add your own scriptlet to make it better.

I call it as doctortux

I have decided to run the script in two mode

1)Interactive.(Not yet included in script)

2)Automatic.(currently giving CPU information.)

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 .

Code:
##############################Option Check###############################

#!/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 "<current time> <Uptime since> <hour:min>"

echo "Number of users logged in to machine"

echo "load average <for last one minute>,<for last 5 minutes>,<for last 15 minutes>"

echo "________________________________________________________________"

}

fn_terminalinfo()

{

echo "Terminal Information:"

echo -e "\033[32m w"

tput sgr0

w

echo "________________________________________________________________"

}

#################################################################

######################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_partioninfo

}

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

         -a automatic -i interactive ;;

    esac

  done

fi
 
What do want to accept from the user in the interactive mode.
From the given title, I assume that the input should be for other devices like hdd, ram etc.
 
viridian said:
Looks good! You should add a who's logged in option too. Could be useful. Also last few errors in syslog.
I have used w in the script which displays how many terminal connections are established in the machine.

It also gives you who has logged in .

E.g

Code:
# w

 21:25:51 up  3:38,  1 user,  load average: 1.05, 1.02, 0.94

USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT

root     pts/1    TomcatServer 18:56    0.00s  0.59s  0.00s w

from above output its clear that somebody has taken a terminal from machine (TomcatServer) with root access.
 
viridian said:
Well there are many ways to include network information. You could parse the information from vnstat or something similar (Need to install, isn't shipped by default). vnStat - a network traffic monitor for Linux and BSD
Modified the script.
#################################################################
Code:
#######################################################################
#!/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 "<current time> <Uptime since> <hour:min>"
echo "Number of users logged in to machine"
echo "load average <for last one minute>,<for last 5 minutes>,<for last 15 minutes>"
echo "________________________________________________________________"
}
fn_terminalinfo()
{
echo "Terminal Information:"
echo -e "\033[32m w"
tput sgr0
w
echo "________________________________________________________________"
}
#################################################################
######################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
}
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
         -a automatic -i interactive ;;
    esac
  done
fi


--- Updated Post - Automerged ---

I m planning to include some content from error file .What is the most common location for checking the error log in system?
 
DoctorTux version 1.0 added.

More details are put in following blog.
Linux Learning
Code:
#######################################################################
#!/bin/bash
#########################UPTIME########################################
#V1:included os info by uname -a,lsb_release -a
#Seperator added.
#Bug removed : ps -auxf is replaced with ps auxf
fn_uptimeinfo()
{
echo "Machine Uptime Information:"
echo -e "\033[32m uptime"
tput sgr0                    #restores the terminal settings to normal.
uptime
echo "Command Description:"
echo "<current time> <Uptime since> <hour:min>"
echo "Number of users logged in to machine"
echo "load average <for last one minute>,<for last 5 minutes>,<for last 15 minutes>"
echo "________________________________________________________________"
}
fn_terminalinfo()
{
echo "Terminal Information:"
echo -e "\033[32m w"
tput sgr0
w
echo "________________________________________________________________"
}
#################################################################
######################KERNAL INFO###################################
fn_kernal()
{
echo "Kernal Version:"
echo -e "\033[32m uname -r"
tput sgr0
uname -r
}
fn_os()
{
echo "OS Info"
echo -e "\033[32m uname -a"
tput sgr0
uname -a
echo "Detailed"
echo -e "\033[32m lsb_release -a"
tput sgr0
lsb_release -a

}
####################################################################
######################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_seperator()
{
echo -e "\033[33m ====================================================================="
tput sgr0
}
fn_automatic()
{
echo  "########OS Information#####"
fn_os
fn_kernal
fn_seperator
echo "########CPU Information########"
fn_uptimeinfo
fn_terminalinfo
fn_cpuinfo
fn_cpueaterprocess
fn_seperator
echo "########Memory Information########"
fn_meminfo
fn_memeaterprocess
fn_seperator
echo "########Disk Information########"
fn_diskinfo
fn_partitioninfo
fn_seperator
}
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
         -a automatic -i interactive ;;
    esac
  done
fi
 
This is nice! Also other suggestions

- Minor: kernel is spelled kernal in the script

- Security: At the beginning of the script you probably want to define the absolute paths of the commands being used e.g. TPUT = /bin/tput (or wherever the path is). This is to ensure that similarly named malicious files in the current path cannot be run. It's generally a good practice [citation needed] ;-)

- Security: Dissuade the user from running it as root, have a small check that quits if it is running as root.

These are minor quibbles which would be good to have since (I assume) that this script will be continuously developed and more cooler features will be added in future.
 
viridian said:
This is nice! Also other suggestions

- Minor: kernel is spelled kernal in the script

- Security: At the beginning of the script you probably want to define the absolute paths of the commands being used e.g. TPUT = /bin/tput (or wherever the path is). This is to ensure that similarly named malicious files in the current path cannot be run. It's generally a good practice [citation needed] ;-)

- Security: Dissuade the user from running it as root, have a small check that quits if it is running as root.

These are minor quibbles which would be good to have since (I assume) that this script will be continuously developed and more cooler features will be added in future.

Thanks for the useful suggestion.

But i cant implement security feature as you dont get command in same position in different flavors of linux.

By not running the script with root privilege you might end up with some command not running at all.
 
Back
Top