Wednesday, November 19, 2014

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

0 comments: