#!/bin/bash
# BASH Shell script Common functions library, it includes lots of
# day-today-life subroutine
# Copyright (c) 2005 Vivek G Gite
# This library is licensed under GNU GPL version 2.0 or above
# For more info, please visit:
# http://cyberciti.biz/shell_scripting/bmsinstall.php
# --------------------------------------------------------------------
# Last updated : May 20, 2005.
# --------------------------------------------------------------------
# Please note this library is under development at the movement most of
# the work is done on FreeBSD and later I.m gone convert it on Linux.
# If you find any bug or looking for more features please write me an
# email at vivek AT cyberciti DOT biz or visit http://cyberciti.biz/fb
# Tested on
# >> FreeBSD REL 5.0 only
# >> Linux yet to start porting ;)
# --------------------------------------------------------------------
YES=0 # TRUE
NO=1 # FALSE
ERR=999 # DEFAULT VALUE OR ERROR CODE
# Determines if user is root or not, return true (zero) value if user is root
# else return nonzero value
isRootUser(){
[ "$($ID -u)" == "0" ] && return $YES || return $NO
}
# Returns true if user account exists in /etc/passwd
isUserExist(){
[ "$1" == "" ] && exit 999 || u="$1"
$GREP -E -w "^$u" $PASSWD_FILE >/dev/null
[ $? -eq 0 ] && return $YES || return $NO
}
# Displays OS name for example FreeBSD, Linux etc
getOs(){
echo "$($UNAME)"
}
# Display message and exit with exit code
die(){
message="$1"
exitCode=$2
echo "$message"
[ "$2" == "" ] && exit 1 || exit $exitCode
}
# Display hostname
# host (FQDN hostname), for example, vivek (vivek.text.com)
getHostName(){
[ "$OS" == "FreeBSD" ] && echo "$($HOSTNAME -s) ($($HOSTNAME))" || :
[ "$OS" == "Linux" ] && echo "$($HOSTNAME) ($($HOSTNAME -f))" || :
}
# Display CPU information such as Make, speed
getCpuInfo(){
if [ "$OS" == "FreeBSD" ]; then
if ( isRootUser ); then # this is more reliable
echo "$($GREP "CPU" /var/log/dmesg.today | $HEAD -1)"
else # this may fail
echo "$($DMESG | $GREP "CPU" | $HEAD -1)"
fi
elif [ "$OS" == "Linux" ]; then
:
fi
}
# Display avilable RAM in system
getRealRamInfo(){
if [ "$OS" == "FreeBSD" ]; then
if ( isRootUser ); then # this is more reliable
echo "$($GREP -E "^real memory" /var/log/dmesg.today)|$CUT -d'(' -f2 | cut -d')' -f1)"
else
echo "$($DMESG | $GREP -E '^real memory' | $CUT -d'(' -f2 | cut -d')' -f1)"
fi
elif [ "$OS" == "Linux" ]; then
:
fi
}
# Display system load for last 5,10,15 minutes
getSystemLoad(){
[ "$OS" == "FreeBSD" ] && echo "$($UPTIME | $AWK -F'averages:' '{ print $2 }')" || :
[ "$OS" == "Linux" ] && echo "$($UPTIME | $AWK -F'load average:' '{ print $2 }')" || :
}
# List total number of users logged in (both Linux and FreeBSD)
getNumberOfLoggedInUsers(){
[ "$OS" == "FreeBSD" -o "$OS" == "Linux" ] && echo "$($W -h | $WC -l)" || :
}
# List total number of ethernet interface
getNumberOfInterfaces(){
[ "$OS" == "FreeBSD" ] && echo "$($IFCONFIG | $GREP -Ew "\<UP" | $GREP -v lo0 | $WC -l)" || :
[ "$OS" == "Linux" ] && echo "$($NETSTAT -i | $GREP -Ev "^Iface|^Kernel|^lo" | $WC -l)" || :
}
# Display Dynamically loaded kernel module aka drivers (both linux and FreeBSD)
getNumberOfKernelModules(){
[ "$OS" == "FreeBSD" ] && echo "$($KLDSTAT | $GREP -vE "^Id Refs" | $WC -l)"
[ "$OS" == "Linux" ] && echo "$($LSMOD | $GREP -vE "^Module" | $WC -l)"
}
# List total number of running process
getNumberOfRunningProcess(){
[ "$OS" == "FreeBSD" ] && echo "$($PS -aux | $GREP -vE "^USER|ps -aux"|$WC -l)"
[ "$OS" == "Linux" ] && echo "$($LSMOD | $GREP -vE "^Module" | $WC -l)"
}
# List number of mounted file system partition
getNumberOfParittions() {
if [ "$OS" == "FreeBSD" ]; then
tmp="$($DF -aHt nonfs,nullfs,devfs| $GREP -vE "^Filesystem" | $AWK '{ print $1 " " }')"
echo "$($DF -aHt nonfs,nullfs,devfs| $GREP -vE "^Filesystem" |$WC -l) ($tmp)"
elif [ "$OS" == "Linux" ]; then
tmp="$($DF -aHt ext3 -t ext2|$GREP -vE "^Filesystem" |$AWK '{ print $1 " " }')"
echo "$($DF -aHt ext3 -t ext2|$GREP -vE "^Filesystem" |$WC -l) ($tmp)"
fi
}
# Display current OS runlevel with Description of it
getOsRunLevel(){
if [ "$OS" == "FreeBSD" ]; then
r="$($SYSCTL -a | $GREP -wE "^kern.securelevel" | $AWK '{ print $2}')"
case "$r" in
-1) d="Permanently insecure mode";;
0) d="Insecure mode";;
1) d="Secure mode";;
2) d="Highly secure mode";;
3) d="Network secure mode";;
*) d="Unknown runlevel";;
esac
elif [ "$OS" == "Linux" ]; then
r="$($RUNLEVEL | $AWK '{ print $2}')"
case "$r" in
1) d="Single user mode";;
2) d="Multi-user without NFS";;
3) d="Full multi-user";;
4) d="Unused/Experimental";;
5) d="Multi-user with X11 windows";;
*) d="Unknown runlevel";;
esac
fi
echo "$r ($d)"
}
# List total number of SCSI/IDE disks connected to FreeBSD/Linux box
# along with device name includes CDROM, IDE/SCSI hard disk drive
# Example 3 (ad0 ad1 acd0)
getDiskDrives(){
if [ "$OS" == "FreeBSD" ]; then
t="$($IOSTAT -d| $HEAD -1)"
c="$(echo $t | $WC -w)"
elif [ "$OS" == "Linux" ]; then
:
fi
echo "$c ($t)"
}
[ Back
| Home |
Download script (5.2K) |
Discuss/ask more @ shell scripting forum ]
This code licensed under
GNU GPL version 2.0
or above, every effort has been made to ensure all information and
examples (soruce code) are correct BUT no guarantee is implied or intended,
read our disclaimer and
privacy policy.
Copyright © 2004-2005
Vivek G Gite.
Html code was genrated by
genBASH2html shell script & GNU source-highlight