Shell script to get uptime, disk , cpu , RAM , system load, from multiple Linux servers – output the information on a single server in html format

in Categories Monitoring last updated April 10, 2008

Shell script to get uptime, disk usage, cpu usage, RAM usage, system load, etc; from multiple Linux servers and output the information on a single server in html format

#!/bin/bash
# Shell script to get uptime, disk usage, cpu usage, RAM usage,system load,etc. 
# from multiple Linux servers and output the information on a single server 
# in html format. Read below for usage/installation info
# *---------------------------------------------------------------------------*
# * dig_remote_linux_server_information.bash,v0.1, last updated on 25-Jul-2005*
# * Copyright (c) 2005 nixCraft project                                       *
# * Comment/bugs: http://cyberciti.biz/fb/                                    *
# * Ref url: http://cyberciti.biz/nixcraft/forum/viewtopic.php?t=97           *
# * This script is licensed under GNU GPL version 2.0 or above                *
# *---------------------------------------------------------------------------*
# *  Installation Info                                                        *
# ----------------------------------------------------------------------------*
# You need to setup ssh-keys to avoid password prompt, see url how-to setup 
# ssh-keys:
# cyberciti.biz/nixcraft/vivek/blogger/2004/05/ssh-public-key-based-authentication.html
# 
# [1] You need to setup correct VARIABLES script:
#
# (a) Change Q_HOST to query your host to get information
# Q_HOST="192.168.1.2 127.0.0.1 192.168.1.2"
#
# (b) Setup USR, who is used to connect via ssh and already setup to connect 
# via ssh-keys
# USR="nixcraft"
#
# (c)Show warning if server load average is below the limit for last 5 minute.
# setup LOAD_WARN as per your need, default is 5.0
#
# LOAD_WARN=5.0
#
# (d) Setup your network title using MYNETINFO
# MYNETINFO="My Network Info"
#
# (e) Save the file 
#
# Please refer to forum topic on this script: 
# Also download the .gif files and put them in your output dir
#
# ----------------------------------------------------------------------------
# Execute script as follows (and copy .gif file in this dir) :
# this.script.name > /var/www/html/info.html
# ============================================================================
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
# SSH SERVER HOST IPS, setup me 
# Change this to query your host
Q_HOST="192.168.1.2 127.0.0.1 192.168.1.2"
 
# SSH USER, change me
USR="nixcraft"
 
# Show warning if server load average is below the limit for last 5 minute
LOAD_WARN=5.0
 
# Your network info
MYNETINFO="My Network Info"
#
# if it  is run as cgi we can do reload stuff too :D
PBY='Powered by <a href="http://cyberciti.biz/download/">script</a>'
 
 
# font colours
GREEN='<font color="#00ff00">'
RED='<font color="#ff0000">'
NOC='</font>'
LSTART='<ul><li>'
LEND='</li></ul>'
# Local path to ssh and other bins
SSH="/usr/bin/ssh"
PING="/bin/ping"
NOW="$(date)"
 
## functions ##
writeHead(){
 echo '<HTML><HEAD><TITLE>Network Status</TITLE></HEAD>
 <BODY alink="#0066ff" bgcolor="#000000" link="#0000ff" text="#ccddee" vlink="#0033ff">'
 echo '<CENTER><H1>'
 echo "$MYNETINFO</H1>"
 echo "Generated on $NOW"
 echo '</CENTER>'
 
}
 
writeFoot(){
 echo "<HR><center>$PBY</center>"
  echo "</BODY></HTML>"
}
 
## main ##
 
writeHead
echo '<TABLE WIDTH=100% BORDER=2 BORDERCOLOR="#000080" CELLPADDING=4 CELLSPACING=4 FRAME=HSIDES RULES=NONE" >'
echo '<TR VALIGN=TOP>'
for host in $Q_HOST
do
  #echo '<TD WIDTH=33% BGCOLOR="#0099ff">'
  echo '<TD BGCOLOR="#0099ff">'
  _CMD="$SSH $USR@$host"
  rhostname="$($_CMD hostname)"
 
  ruptime="$($_CMD uptime)"
  if $(echo $ruptime | grep -E "min|days" >/dev/null); then
    x=$(echo $ruptime | awk '{ print $3 $4}')
  else
    x=$(echo $ruptime | sed s/,//g| awk '{ print $3 " (hh:mm)"}')
  fi
  ruptime="$x"
 
  rload="$($_CMD uptime |awk -F'average:' '{ print $2}')"
  x="$(echo $rload | sed s/,//g | awk '{ print $2}')"
  y="$(echo "$x >= $LOAD_WARN" | bc)"
  [ "$y" == "1" ] && rload="$RED $rload (High) $NOC" || rload="$GREEN $rload (Ok) $NOC"
 
  rclock="$($_CMD date +"%r")"
  rtotalprocess="$($_CMD ps axue | grep -vE "^USER|grep|ps" | wc -l)"
  rfs="$($_CMD df -hT | grep -vE "^Filesystem|shm" \
  | awk 'BEGIN{print "<ul>"}{w=sprintf("%d",$6);print "<li>" $7 \
  "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" $6 \
  "(" $2 ")<BR> <img src=\"indicator.gif\" height=\"4\" width=\"" w "\"> \
  <BR><img src=\"graph.gif\"> \
  <BR>" $4"/"$3 "</li>"}END{ print "</ul>"}')"
 
  rusedram="$($_CMD free -mto | grep Mem: | awk '{ print $3 " MB" }')"
  rfreeram="$($_CMD free -mto | grep Mem: | awk '{ print $4 " MB" }')"
  rtotalram="$($_CMD free -mto | grep Mem: | awk '{ print $2 " MB" }')"
 
  $PING -c1  $host>/dev/null
  if [ "$?" != "0" ] ; then
    rping="$RED Failed $NOC"   
  else
    rping="$GREEN Ok $NOC"
    echo "<b><u>$rhostname</u></b><BR>" 
    echo "Ping status: $rping<BR>"
    echo "Time: $rclock<BR>"
    echo "Uptime: $ruptime <BR>"
    echo "Load avarage: $LSTART $rload $LEND"
    echo "Total running process: $LSTART $rtotalprocess $LEND"
    echo "Disk status:"
    echo "$rfs"
    echo "Ram/swap status:<ul>" 
    echo "<li>Used RAM: $rusedram</li>" 
    echo "<li>Free RAM: $rfreeram</li>"
    echo "<li>Total RAM: $rtotalram </li></ul>"
  fi   
 
  echo "</td>"
done  
  echo "</tr></table>"
writeFoot

Shell script to General FreeBSD and Linux System information

in Categories Monitoring last updated February 26, 2009

Shell script to display general FreeBSD and Linux System information such as, Hostname, OS version, Kernel version, Processor/CPU, Total RAM, System load, network interface, total logged in users, Hard disks, Runlevel etc in neat format.

How do I use the script?

You need to download following two files for FreeBSD operating systems:

  1. File # 1 Save this file as /usr/local/etc/bashmonscripts
  2. File # 2 : Save this file as /usr/local/etc/defaults.conf

For Linux system, you need to update /usr/local/etc/defaults.conf to point out exact binary path location. /usr/local/etc/bashmonscripts is nothing but functions library used by this script. bashmonscripts works under both Linux and FreeBSD.

#!/bin/bash
# Shell script to General FreeBSD and Linux System information such as:
# Hostname, OS version, Kernel version, Processor/CPU, Total RAM,
# System load, network interface, total logged in users, Hard disks,
# Runlevel etc. I will write separate scripts for detailed system 
# information :D
# Copyright (c) 2005 nixCraft project
# This script is licensed under GNU GPL version 2.0 or above
# For more info, please visit: 
#            http://cyberciti.biz/shell_scripting/bmsinstall.php
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
SPATH="/usr/local/etc/bashmonscripts"
INITBMS="$SPATH/defaults.conf" 
[ ! -f $INITBMS ] && exit 1 || . $INITBMS
 
echo "Hostname:" $(getHostName)
echo "OS Name:" $($UNAME)
echo "OS Version:" $($UNAME -mr)
echo "Processor:" $(getCpuInfo)
echo "Total Physical Memory (RAM):" $(getRealRamInfo)
echo "System Load:" $(getSystemLoad)
echo "Date & Time:" $($DATE)
echo "Network interface:" $(getNumberOfInterfaces)
echo "Total logged in users:" $(getNumberOfLoggedInUsers)
echo "Total running process:" $(getNumberOfRunningProcess)
echo "Total SCSI/IDE disks:" $(getDiskDrives)
echo "Mounted partitions:" $(getNumberOfParittions)
echo "Loaded dynamic kernel modules/drivers:" $(getNumberOfKernelModules)
echo "OS Rulevel:" $(getOsRunLevel)

Shell script to find all world-writable files and directories on Linux / UNIX system

in Categories Monitoring last updated April 10, 2008
#!/bin/bash
# Shell script to find all world-writable files and directories on Linux or 
# FreeBSD system
#
# TIP:
# Set 'umask 002' so that new files created will not be world-writable
# And use command 'chmod o-w filename' to disable world-wriable file bit
#
# Copyright (c) 2005 nixCraft project
# This script is licensed under GNU GPL version 2.0 or above
# For more info, please visit: 
#            http://cyberciti.biz/shell_scripting/bmsinstall.php
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
#SPATH="/usr/local/etc/bashmonscripts"
#INITBMS="$SPATH/defaults.conf" 
#[ ! -f $INITBMS ] && exit 1 || . $INITBMS
 
[ $# -eq 1 ] && : || die "Usage: $($BASENAME $0) directory" 1
 
DIRNAME="$1"
$FIND $DIRNAME -xdev -perm +o=w ! \( -type d -perm +o=t \) ! -type l -print

Shell script to find all programs and scripts with setuid bit set on

in Categories Monitoring last updated April 10, 2008
#!/bin/bash
# Shell script to find all programs and scripts with setuid bit set on.
# If your system ever cracked (aka hacked) then system has this kind of binary 
# installed; besides the normal setuuid scripts/programs
#
# *TIP*
# User directory /home and webroots such as /www canbe mounted with
# nosuid option.
#
# Copyright (c) 2005 nixCraft project.
# This script is licensed under GNU GPL version 2.0 or above
# For more info, please visit: 
#            http://cyberciti.biz/shell_scripting/bmsinstall.php
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
#SPATH="/usr/local/etc/bashmonscripts"
#INITBMS="$SPATH/defaults.conf" 
#[ ! -f $INITBMS ] && exit 1 || . $INITBMS
 
[ $# -eq 1 ] && : || die "Usage: $($BASENAME $0) directory" 1
 
DIRNAME="$1"
$FIND $DIRNAME -xdev -type f -perm +u=s -print

Shell script to find all programs and scripts with setgid bit set on

in Categories Monitoring last updated April 10, 2008

setuid and setgid (short for set user ID upon execution and set group ID upon execution, respectively) are Unix access rights flags that allow users to run an executable with the permissions of the executable’s owner or group. They are often used to allow users on a computer system to run programs with temporarily elevated privileges in order to perform a specific task. While the assumed user id or group id privileges provided are not always elevated, at a minimum they are specific.

setuid and setgid are needed for tasks that require higher privileges than those which a common user has, such as changing his or her login password. Some of the tasks that require elevated privileges may not immediately be obvious, though — such as the ping command, which must send and listen for control packets on a network interface.

Sample Shell Script

#!/bin/bash
# Shell script to find all programs and scripts with setgid bit set on.
# If your system ever cracked (aka hacked) then system has this kind of binary 
# installed; besides the normal setuuid scripts/programs
# Copyright (c) 2005 nixCraft project
# This script is licensed under GNU GPL version 2.0 or above
# For more info, please visit: 
#            http://cyberciti.biz/shell_scripting/bmsinstall.php
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
#SPATH="/usr/local/etc/bashmonscripts"
#INITBMS="$SPATH/defaults.conf" 
#[ ! -f $INITBMS ] && exit 1 || . $INITBMS
 
[ $# -eq 1 ] && : || die "Usage: $($BASENAME $0) directory" 1
 
DIRNAME="$1"
$FIND $DIRNAME -xdev -type f -perm +g=s -print