≡ Menu

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

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 script'
# font colours
GREEN=''
RED=''
NOC=''
LSTART='
  • ' LEND='
' # Local path to ssh and other bins SSH="/usr/bin/ssh" PING="/bin/ping" NOW="$(date)" ## functions ## writeHead(){ echo 'Network Status ' echo '

' echo "$MYNETINFO

" echo "Generated on $NOW" echo '
' } writeFoot(){ echo "
$PBY
" echo "" } ## main ## writeHead echo '' echo '' for host in $Q_HOST do #echo '" done echo "
' echo '' _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 "
    "}{w=sprintf("%d",$6);print "
  • " $7 \ "            " $6 \ "(" $2 ")
    \
    \
    " $4"/"$3 "
  • "}END{ print "
"}')" 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 "$rhostname
" echo "Ping status: $rping
" echo "Time: $rclock
" echo "Uptime: $ruptime
" echo "Load avarage: $LSTART $rload $LEND" echo "Total running process: $LSTART $rtotalprocess $LEND" echo "Disk status:" echo "$rfs" echo "Ram/swap status:
    " echo "
  • Used RAM: $rusedram
  • " echo "
  • Free RAM: $rfreeram
  • " echo "
  • Total RAM: $rtotalram
" fi echo "
" writeFoot

Comments on this entry are closed.

  • Pingback: AirV’s Blog » Archive du blog » Monitoring()

  • echo “Load avarage: $LSTART $rload $LEND”

    should really read

    echo “Load average: $LSTART $rload $LEND”

  • Dipak

    I have installed this script in to /var/www/html/ directory and changed name to info.html.
    I ran this script from command line
    #./info.html
    and I got an error line 110
    and 126

    What is next how do I run this script?

  • Hello Dipak.

    We cannot read what you put in lines 110 and 126.
    Try to be more explicit.. (next time if you want gurus to help you try to give them the informatons they need to debug ).

    this script is running under the linux box and display the results on a page, so try to visit http://the_server_where_the_script_is_running/info.html.

    Try it it post your results …

  • idiot hunter

    Moussa NDIAYE: If you were such a “guru” you would probably guess that he just copied the script as is,
    cat -n somefile gets you linenumbers…guru my ass ;)

  • Panya

    This link
    cyberciti.biz/nixcraft/vivek/blogger/2004/05/ssh-public-key-based-authentication.html

    is no longer valid…..

  • Palani

    hi,
    i am very new to shell script program, i tried u r script but i don’t know how to proceed
    and if run the script i am getting this error
    kindly guide me how to run the script ,

    script output !

    My Network Info
    <span style=color:
    Generated on Fri Oct 3 16:41:02 GMT 2008

    ./109.sh: line 110: syntax error near unexpected token `newline’
    ./109.sh: line 110: ` echo ”

    thanks & regards

    Palani.K

  • where are those gif files?

  • amran

    hi,
    i am new in scripting. i try run this script, but it always promt me password

    [root@amranlinux src]# ./check-linux
    Network Status

    My Network Info
    Generated on Tue Jun 23 17:16:07 MYT 2009

    root@172.18.1.2’s password:

  • Yoyo

    Dipak,

    You cannot simply rename a Bash script to *.html.

    from the comments within the script itself:
    # Execute script as follows (and copy .gif file in this dir) :
    # this.script.name > /var/www/html/info.html

    Yoyo

  • Scrummie02

    I’m getting this when running the script:
    awk: BEGIN{print ”
    awk: ^ unterminated string

    has anyone seen this before?

  • Ankur

    u can make them in one line.. its just bcoz of the spacing problem in the script..

  • Thanks for this code.

    I am new to bash script and am making an application to gather system info. Your example of how too get CPU and RAM info is great.

  • Thanks, great tool.
    We’ve got several servers at job, and they have different username/passwords. So I’ve modified the script to get servers with different usernames/passwords.

    I put the script to Github, here you can use if you want that version:
    https://github.com/samet/Monitor.sh

  • tyrobert

    hey did u ever figure out the issue here? I am running into the same problem

  • No lo eiedtnno, me lo podrias explicar mjor paso a poso lo que tengo que hacer para conseguirlo?. Gracias

  • soulaf

    cane i have a help on my project please?
    i’m a student in univercity in morocco and i have a project to do before the end of this week
    create a shell script with the name infoprocess wich ,when i tappe name proc1 name proc2…to name procn shows me informations about them like that:
    proc1
    |
    |
    proc2
    |
    |
    i’m waiting for your ansewer and thank you all