Advertisement
Guest User

shhtml

a guest
Jul 23rd, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.09 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #####################################################
  4. # AUTHOR: xxxxxx #
  5. #####################################################
  6.  
  7. case $1 in
  8. mem)
  9.  
  10. memory=`free -m`
  11.  
  12. mem_used=`echo "$memory" | awk '/^Mem/ {print $3}'`
  13. mem_free=`echo "$memory" | awk '/^Mem/ {print $4}'`
  14. mem_total=`echo "$memory" | awk '/^Mem/ {print $2}'`
  15. mem_used_p=`awk 'BEGIN{printf "%.0f\n",('"$mem_used"'/'"$mem_total"'*100)}'`
  16.  
  17. sw_used=`echo "$memory" | awk '/^Swap/ {print $3}'`
  18. sw_free=`echo "$memory" | awk '/^Swap/ {print $4}'`
  19. sw_total=`echo "$memory" | awk '/^Swap/ {print $2}'`
  20. sw_used_p=`awk 'BEGIN{printf "%.0f\n",('"$sw_used"'/'"$sw_total"'*100)}'`
  21.  
  22. echo -e "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
  23. echo "<tr>"
  24.     echo "<td width=\"20%\" class=\"styleh\" align=\"right\">"
  25.     echo "<a id=\"text_hron\"><b>RAM:</b> $mem_used"M"/$mem_total"M" $mem_used_p% &nbsp;</a>"
  26.     echo "</td>"
  27.  
  28. [[ $mem_used_p -ge 75 ]] && wmem=1 || wmem=0;
  29.  
  30.  
  31. echo "<td width=\"80%\" class=\"styleh\">"
  32.     echo "<hr id=\"hron\" style=\"vertical-align: middle; display: inline-block; background-color: #fff; height: $wmem"px"; border: 0; width: $mem_used_p%;\"/>"
  33.     echo "<hr id=\"hroff\" style=\"vertical-align: middle; display: inline-block; background-color: #fff; height: 0px; border: 0; width: $((100-$mem_used_p))%;\"/>"
  34. echo "</td>"
  35. echo "</tr>"
  36.  
  37. echo "<tr>"
  38.     echo "<td width=\"20%\" class=\"styleh\" align=\"right\">"
  39.     echo "<a href=\"?service=swap\" id=\"text_hron\" title=\"Clear swap space\"><b>SWAP:</b> $sw_used"M"/$sw_total"M" $sw_used_p% &nbsp;</a>"
  40.     echo "</td>"
  41.  
  42. [[ $sw_used_p -ge 75 ]] && wsw=1 || wsw=0;
  43.  
  44.  
  45. echo "<td width=\"80%\" class=\"styleh\">"
  46.     echo "<hr id=\"hron\" style=\"vertical-align: middle; display: inline-block; background-color: #fff; height: $wsw"px"; border: 0; width: $sw_used_p%;\"/>"
  47.     echo "<hr id=\"hroff\" style=\"vertical-align: middle; display: inline-block; background-color: #fff; height: 0px; border: 0; width: $((100-$sw_used_p))%;\"/>"
  48. echo "</td>"
  49. echo "</tr>"
  50.  
  51. echo -e "</table>"
  52.  
  53. ;;
  54.  
  55.  
  56. cpu)
  57. cpu=`/var/www/bin/cpu.sh`
  58. echo -e "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
  59. echo "<tr>"
  60.     echo "<td width=\"20%\" class=\"styleh\" align=\"right\">"
  61.     echo "<a id=\"text_hron\"><b>`grep "^cpu[0-9]\{1,\} " /proc/stat |wc -l` x CPU:</b> $cpu% &nbsp;</a>"
  62.     echo "</td>"
  63.  
  64. [[ $cpu -ge 75 ]] && wcpu=1 || wcpu=0;
  65.  
  66.  
  67. echo "<td width=\"15%\" class=\"styleh\">"
  68.     echo "<a id=\"text_hron\">`uptime | awk '{print toupper($0)}'` &nbsp;</a>"
  69. echo "</td>"
  70. echo "<td class=\"styleh\">"
  71.     echo "<hr id=\"hron\" style=\"vertical-align: middle; display: inline-block; background-color: #fff; height: $wcpu"px"; border: 0; width: $cpu%;\"/>"
  72.     echo "<hr id=\"hroff\" style=\"vertical-align: middle; display: inline-block; background-color: #fff; height: 0px; border: 0; width: $((100-$cpu))% ;\"/>"
  73. echo "</td>"
  74. echo "</tr>"
  75. echo -e "</table>"
  76. ;;
  77.  
  78.  
  79. disk)
  80.  
  81. disk="`df -h`"
  82. disksize=`echo "$disk" | awk '/\/$/ {print $2}' | head -n1`;
  83. diskuse=`echo "$disk" | awk '/\/$/ {print $3}' | head -n1`;
  84. diskuse_p=`echo "$disk" | awk '/\/$/ {print $5}' | head -n1`;
  85. dstats=`iostat -dkx 1 2 | awk '/^[a-z]*da[0-9]? /' | tail -n1`
  86.  
  87. rps=`echo "$dstats" |awk '/^[a-z]*da[0-9]? / {printf $4}' | tail -n1`
  88. wps=`echo "$dstats" |awk '/^[a-z]*da[0-9]? / {printf $5}' | tail -n1`
  89. usage=`echo "$dstats" | awk '/^[a-z]*da[0-9]? / {printf "%.0f\n",((($4+$5)*($11/1000))*100)}' | tail -n1`
  90.  
  91. echo -e "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
  92. echo "<tr>"
  93.     echo "<td width=\"20%\" class=\"styleh\" align=\"right\">"
  94.     echo "<a id=\"text_hron\"><b>DISK:</b> Used $diskuse/$disksize $diskuse_p &nbsp;</a>"
  95.     echo "</td>"
  96.  
  97. [[ `echo $diskuse_p | cut -d "%" -f1` -ge 75 ]] && wdisk=1 || wdisk=0;
  98.  
  99. echo "<td width=\"80%\" class=\"styleh\">"
  100.     echo "<hr id=\"hron\" style=\"vertical-align: middle; display: inline-block; background-color: #fff; height: $wdisk"px"; border: 0; width: $diskuse_p;\"/>"
  101.     echo "<hr id=\"hroff\" style=\"vertical-align: middle; display: inline-block; background-color: #fff; height: 0px; border: 0; width: $((100-`echo $diskuse_p | cut -d "%" -f1`))%;\"/>"
  102. echo "</td>"
  103. echo "</tr>"
  104.  
  105. echo "<tr>"
  106.     echo "<td width=\"20%\" class=\"styleh\" align=\"right\">"
  107.     echo "<a id=\"text_hron\"><b></b> R:$rps/s W:$wps/s Util: $usage"%" &nbsp;</a>"
  108.     echo "</td>"
  109.  
  110. [[ $usage -ge 75 && $usage -le 100 ]] && wusage=1 || wusage=0;
  111. [[ $usage -ge 101 ]] && wusage=2;
  112.  
  113. echo "<td width=\"80%\" class=\"styleh\">"
  114.     echo "<hr id=\"hron\" style=\"vertical-align: middle; display: inline-block; background-color: #fff; height: $wusage"px"; border: 0; width: $usage"%";\"/>"
  115.     echo "<hr id=\"hroff\" style=\"vertical-align: middle; display: inline-block; background-color: #fff; height: 0px; border: 0; width: $((100-$usage))%;\"/>"
  116. echo "</td>"
  117. echo "</tr>"
  118.  
  119.  
  120. echo -e "</table>"
  121. ;;
  122.  
  123. services)
  124. services=`netstat -natp`
  125. procs=`ps -A`
  126. (echo "$services" | grep -E "master|smtpd" > /dev/null 2>&1) && postfix=text_off || postfix=text_on;
  127. (echo "$services" | grep "amavis" > /dev/null 2>&1) && amavis=text_off || amavis=text_on;
  128. (echo "$procs" | grep "clamd" > /dev/null 2>&1) && clam=text_off || clam=text_on;
  129. (echo "$procs"  | grep "incron" > /dev/null 2>&1) && incron=text_off || incron=text_on;
  130. (echo "$procs"  | grep "cron$" > /dev/null 2>&1) && cron=text_off || cron=text_on;
  131. (echo "$procs"  | grep "dovecot$" > /dev/null 2>&1) && dovecot=text_off || dovecot=text_on;
  132. (echo "$services" | grep ":8080.*apache" > /dev/null 2>&1) && cache=text_off || cache=text_on;
  133. (iptables -L | grep -m1 "^DROP" > /dev/null 2>&1) && firewall=text_off || firewall=text_on;
  134.  
  135. if [ $postfix  = "text_off" -a $amavis = "text_off" -a $clam = "text_off" -a $incron = "text_off" -a $cron = "text_off" -a $cache = "text_off" -a $firewall = "text_off" -a $dovecot = "text_off" ];
  136. then
  137.     system="HEALTHY";
  138.     sysid="text_off";
  139.     else
  140.     system="ALERT"
  141.     sysid="text_on";
  142. fi
  143.  
  144.  
  145. echo -e "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
  146. echo "<tr>"
  147.     echo "<td width=\"20%\" class=\"styleh\" align=\"right\">"
  148.     echo "<a id=\"$sysid\"><b>$system</b> &nbsp;</a>"
  149.     echo "</td>"
  150.    
  151.     echo "<td width=\"80%\" class=\"styleh\">"
  152.  
  153.         echo "<a href="?service=postfix" id=\"$postfix\" title=\"Start or Stop Postfix\">POSTFIX </a>"
  154.         echo "<a href="?service=amavis" id=\"$amavis\" title=\"Start or Stop Amavis. Update Spamassassin.\"> AMAVIS </a>"
  155.         echo "<a href="?service=cache" id=\"$cache\" title=\"Clear cache\"> CACHE </a>"
  156.         echo "<a href="?service=clamav" id=\"$clam\" title=\"Start or Stop Clamav. Last update: `freshclam -V`\"> CLAMAV </a>"    
  157.         echo "<a href="?service=dovecot" id=\"$dovecot\" title=\"Start or Stop Dovecot\"> DOVECOT </a>"
  158.         echo "<a href="?service=incron" id=\"$incron\" title=\"Start or Stop Incron\"> INCRON </a>"
  159.         echo "<a href="?service=cron" id=\"$cron\" title=\"Start or Stop Cron scheduler\"> CRON </a>"
  160.         echo "<a href="?service=firewall" id=\"$firewall\" title=\"Start or Stop Firewall\"> FIREWALL </a>"
  161.         echo "<a href="?service=reboot" id=\"text_off\" title=\"Reboot the machine\" onclick=\"return confirm('Are you sure you want to RESTART?')\"> REBOOT </a>"
  162.  
  163.     echo "</td>"
  164. echo "</tr>"
  165. echo -e "</table>"
  166. ;;
  167.  
  168. network)
  169.  
  170. netstat=`netstat -natu`
  171.  
  172. conns=`echo "$netstat" | grep "ESTABLISHED" | wc -l`
  173. syn_in=`echo "$netstat" | grep "SYN_RECV" | wc -l`
  174. syn_out=`echo "$netstat" | grep "SYN_SENT" | wc -l`
  175. con_wait=`echo "$netstat" | grep "TIME_WAIT" | wc -l`
  176.  
  177. rx1=`cat /sys/class/net/*e*[0-9]*/statistics/rx_bytes | head -1`
  178. tx1=`cat /sys/class/net/*e*[0-9]*/statistics/tx_bytes | head -1`
  179. kbr1=$(($rx1/1024));
  180. kbt1=$(($tx1/1024));
  181.  
  182. sleep 1;
  183.  
  184. rx2=`cat /sys/class/net/*e*[0-9]*/statistics/rx_bytes | head -1`
  185. tx2=`cat /sys/class/net/*e*[0-9]*/statistics/tx_bytes | head -1`
  186. kbr2=$(($rx2/1024));
  187. kbt2=$(($tx2/1024));
  188.  
  189. if [ $(($kbr2-$kbr1)) -le 1024 ]
  190. then
  191.     kbr="$(($kbr2-$kbr1))KB/s";
  192.     kbpsr="$((($kbr2-$kbr1)*8))kbps";
  193. else
  194.     kbr="$((($kbr2-$kbr1)/1204))MB/s"
  195.     kbpsr="$(((($kbr2-$kbr1)/1204)*8))mbps"
  196. fi
  197.  
  198.  
  199. if [ $(($kbt2-$kbt1)) -le 1024 ]
  200. then
  201.     kbt="$(($kbt2-$kbt1))KB/s";
  202.     kbpst="$((($kbt2-$kbt1)*8))kbps";
  203. else
  204.     kbt="$((($kbt2-$kbt1)/1204))MB/s"
  205.     kbpst="$(((($kbt2-$kbt1)/1204)*8))mbps"
  206. fi
  207.  
  208.  
  209. mbr="$(($kbr2/1024))MB";
  210. mbt="$(($kbt2/1024))MB";
  211.  
  212.  
  213. echo -e "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"
  214. echo "<tr>"
  215.     echo "<td width=\"20%\" class=\"styleh\" align=\"right\">"
  216.     echo "<a id=\"text_hron\"><b>NETWORK</b> &nbsp;</a>"
  217.     echo "</td>"
  218.    
  219.     echo "<td width=\"80%\" class=\"styleh\">"
  220.  
  221.         echo "<a id=\"text_hron\">Traffic in/out: $mbr/$mbt&nbsp;</a>"
  222.  
  223.         echo "<a id=\"text_hron\">Downlink: $kbr($kbpsr)&nbsp;</a>"
  224.         echo "<a id=\"text_hron\">Uplink: $kbt($kbpst)&nbsp;</a>"
  225.  
  226.         echo "<a id=\"text_hron\">Connections: $conns&nbsp;</a>"
  227.         echo "<a id=\"text_hron\">Wait: $con_wait&nbsp;</a>"
  228.         echo "<a id=\"text_hron\">Syn in/out: $syn_in/$syn_out&nbsp;</a>"
  229.  
  230.     echo "</td>"
  231. echo "</tr>"
  232. echo -e "</table>"
  233. ;;
  234.  
  235. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement