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

by on April 10, 2008 · 18 comments

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

  1. #!/bin/bash
  2. # Shell script to get uptime, disk usage, cpu usage, RAM usage,system load,etc.
  3. # from multiple Linux servers and output the information on a single server
  4. # in html format. Read below for usage/installation info
  5. # *---------------------------------------------------------------------------*
  6. # * dig_remote_linux_server_information.bash,v0.1, last updated on 25-Jul-2005*
  7. # * Copyright (c) 2005 nixCraft project *
  8. # * Comment/bugs: http://cyberciti.biz/fb/ *
  9. # * Ref url: http://cyberciti.biz/nixcraft/forum/viewtopic.php?t=97 *
  10. # * This script is licensed under GNU GPL version 2.0 or above *
  11. # *---------------------------------------------------------------------------*
  12. # * Installation Info *
  13. # ----------------------------------------------------------------------------*
  14. # You need to setup ssh-keys to avoid password prompt, see url how-to setup
  15. # ssh-keys:
  16. # cyberciti.biz/nixcraft/vivek/blogger/2004/05/ssh-public-key-based-authentication.html
  17. #
  18. # [1] You need to setup correct VARIABLES script:
  19. #
  20. # (a) Change Q_HOST to query your host to get information
  21. # Q_HOST="192.168.1.2 127.0.0.1 192.168.1.2"
  22. #
  23. # (b) Setup USR, who is used to connect via ssh and already setup to connect
  24. # via ssh-keys
  25. # USR="nixcraft"
  26. #
  27. # (c)Show warning if server load average is below the limit for last 5 minute.
  28. # setup LOAD_WARN as per your need, default is 5.0
  29. #
  30. # LOAD_WARN=5.0
  31. #
  32. # (d) Setup your network title using MYNETINFO
  33. # MYNETINFO="My Network Info"
  34. #
  35. # (e) Save the file
  36. #
  37. # Please refer to forum topic on this script:
  38. # Also download the .gif files and put them in your output dir
  39. #
  40. # ----------------------------------------------------------------------------
  41. # Execute script as follows (and copy .gif file in this dir) :
  42. # this.script.name > /var/www/html/info.html
  43. # ============================================================================
  44. # This script is part of nixCraft shell script collection (NSSC)
  45. # Visit http://bash.cyberciti.biz/ for more information.
  46. # -------------------------------------------------------------------------
  47.  
  48. # SSH SERVER HOST IPS, setup me
  49. # Change this to query your host
  50. Q_HOST="192.168.1.2 127.0.0.1 192.168.1.2"
  51.  
  52. # SSH USER, change me
  53. USR="nixcraft"
  54.  
  55. # Show warning if server load average is below the limit for last 5 minute
  56. LOAD_WARN=5.0
  57.  
  58. # Your network info
  59. MYNETINFO="My Network Info"
  60. #
  61. # if it is run as cgi we can do reload stuff too :D
  62. PBY='Powered by <a href="http://cyberciti.biz/download/">script</a>'
  63.  
  64.  
  65. # font colours
  66. GREEN='<font color="#00ff00">'
  67. RED='<font color="#ff0000">'
  68. NOC='</font>'
  69. LSTART='<ul><li>'
  70. LEND='</li></ul>'
  71. # Local path to ssh and other bins
  72. SSH="/usr/bin/ssh"
  73. PING="/bin/ping"
  74. NOW="$(date)"
  75.  
  76. ## functions ##
  77. writeHead(){
  78. echo '<HTML><HEAD><TITLE>Network Status</TITLE></HEAD>
  79. <BODY alink="#0066ff" bgcolor="#000000" link="#0000ff" text="#ccddee" vlink="#0033ff">'
  80. echo '<CENTER><H1>'
  81. echo "$MYNETINFO</H1>"
  82. echo "Generated on $NOW"
  83. echo '</CENTER>'
  84.  
  85. }
  86.  
  87. writeFoot(){
  88. echo "<HR><center>$PBY</center>"
  89. echo "</BODY></HTML>"
  90. }
  91.  
  92. ## main ##
  93.  
  94. writeHead
  95. echo '<TABLE WIDTH=100% BORDER=2 BORDERCOLOR="#000080" CELLPADDING=4 CELLSPACING=4 FRAME=HSIDES RULES=NONE" >'
  96. echo '<TR VALIGN=TOP>'
  97. for host in $Q_HOST
  98. do
  99. #echo '<TD WIDTH=33% BGCOLOR="#0099ff">'
  100. echo '<TD BGCOLOR="#0099ff">'
  101. _CMD="$SSH $USR@$host"
  102. rhostname="$($_CMD hostname)"
  103.  
  104. ruptime="$($_CMD uptime)"
  105. if $(echo $ruptime | grep -E "min|days" >/dev/null); then
  106. x=$(echo $ruptime | awk '{ print $3 $4}')
  107. else
  108. x=$(echo $ruptime | sed s/,//g| awk '{ print $3 " (hh:mm)"}')
  109. fi
  110. ruptime="$x"
  111.  
  112. rload="$($_CMD uptime |awk -F'average:' '{ print $2}')"
  113. x="$(echo $rload | sed s/,//g | awk '{ print $2}')"
  114. y="$(echo "$x >= $LOAD_WARN" | bc)"
  115. [ "$y" == "1" ] && rload="$RED $rload (High) $NOC" || rload="$GREEN $rload (Ok) $NOC"
  116.  
  117. rclock="$($_CMD date +"%r")"
  118. rtotalprocess="$($_CMD ps axue | grep -vE "^USER|grep|ps" | wc -l)"
  119. rfs="$($_CMD df -hT | grep -vE "^Filesystem|shm" \
  120. | awk 'BEGIN{print "<ul>"}{w=sprintf("%d",$6);print "<li>" $7 \
  121. "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" $6 \
  122. "(" $2 ")<BR> <img src=\"indicator.gif\" height=\"4\" width=\"" w "\"> \
  123. <BR><img src=\"graph.gif\"> \
  124. <BR>" $4"/"$3 "</li>"}END{ print "</ul>"}')"
  125.  
  126. rusedram="$($_CMD free -mto | grep Mem: | awk '{ print $3 " MB" }')"
  127. rfreeram="$($_CMD free -mto | grep Mem: | awk '{ print $4 " MB" }')"
  128. rtotalram="$($_CMD free -mto | grep Mem: | awk '{ print $2 " MB" }')"
  129.  
  130. $PING -c1 $host>/dev/null
  131. if [ "$?" != "0" ] ; then
  132. rping="$RED Failed $NOC"
  133. else
  134. rping="$GREEN Ok $NOC"
  135. echo "<b><u>$rhostname</u></b><BR>"
  136. echo "Ping status: $rping<BR>"
  137. echo "Time: $rclock<BR>"
  138. echo "Uptime: $ruptime <BR>"
  139. echo "Load avarage: $LSTART $rload $LEND"
  140. echo "Total running process: $LSTART $rtotalprocess $LEND"
  141. echo "Disk status:"
  142. echo "$rfs"
  143. echo "Ram/swap status:<ul>"
  144. echo "<li>Used RAM: $rusedram</li>"
  145. echo "<li>Free RAM: $rfreeram</li>"
  146. echo "<li>Total RAM: $rtotalram </li></ul>"
  147. fi
  148.  
  149. echo "</td>"
  150. done
  151. echo "</tr></table>"
  152. writeFoot


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

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

  • n2j3

    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?

  • Moussa NDIAYE

    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

  • krish

    where are those gif files?

  • http://www.cyberciti.biz/ Vivek Gite
  • 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..

  • Mark

    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.

  • Samet Atdag

    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

  • Lara

    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

Previous Script:

Next Script: