{"id":287,"date":"2018-05-22T07:47:51","date_gmt":"2018-05-22T07:47:51","guid":{"rendered":"https:\/\/secureethics.com\/?p=287"},"modified":"2018-08-04T08:10:31","modified_gmt":"2018-08-04T08:10:31","slug":"server-monitoring-script","status":"publish","type":"post","link":"https:\/\/secureethics.com\/?p=287","title":{"rendered":"Server Monitoring script"},"content":{"rendered":"<p>This script can be used for monitoring the server like RAM, Server Load, OS Version, Server IP, Swap space while login in ssh terminal and display the server status.<\/p>\n<p><strong>Step: 1<\/strong><\/p>\n<p>Copy the below codes into the script file<\/p>\n<p>Example : vim \/opt\/monitor.sh(copy and paste below code into monitor.sh)<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"theme:undefined font-size:16 toolbar:1 toolbar-hide:false toolbar-delay:false lang:php decode:true Assist\">#!bin\/bash\r\n#Author Balakrishnan S from secueethics.com\r\n\r\nHostname=`hostname`\r\nUsers=`users`\r\nRam_Size=`free -h |grep Mem |awk '{print substr($2,0,length($2)-1)}'`\r\nSwap_Size=`free -h |grep Swap |awk '{print substr($2,0,length($2)-1)}'`\r\nRam_used=`free -h |grep Mem |awk '{print substr($3,0,length($3)-1)}'`\r\nSwap_used=`free -h |grep Swap |awk '{print substr($3,0,length($3)-1)}'`\r\nServer_Load=`uptime |awk '{print substr($8,0,length($8)-1),substr($9,0,length($9)-1),$10}'`\r\nServer_IP=`ip a |grep 172.27.221* |awk '{print substr($2,0,length($2)-3)}'`\r\nCPU_load=`uptime |awk '{print substr($10,0,length($10)-3)}'`\r\nOS_version=`lsb_release -d |awk '{print $2,$3,$4}'`\r\nSupport=\"admin@secureethics.com\"\r\n\r\n#Color Details\r\n# Text Color Variables\r\nBLK=\"\\033[00;30m\" # BLACK\r\nR=\"\\033[00;31m\" # RED\r\nGRN=\"\\033[00;32m\" # GREEN\r\nBR=\"\\033[00;33m\" # BROWN\r\nBL=\"\\033[00;34m\" # BLUW\r\nP=\"\\033[00;35m\" # PURPLE\r\nC=\"\\033[00;36m\" # CYAN\r\nLtG=\"\\033[00;37m\" # LIGHT GRAY\r\n\r\nDkG=\"\\033[01;30m\" # DARK GRAY\r\nLtR=\"\\033[01;31m\" # LIGHT RED\r\nLtGRN=\"\\033[01;32m\" # LIGHT GREEN\r\nY=\"\\033[01;33m\" # YELLOW\r\nLtBL=\"\\033[01;34m\" # LIGHT BLUE\r\nLtP=\"\\033[01;35m\" # LIGHT PURPLE\r\nLtC=\"\\033[01;36m\" # LIGHT CYAN\r\nW=\"\\033[01;37m\" # WHITE\r\n\r\nRESET=\"\\033[0m\"\r\n\r\n#set -x\r\n\r\n## Ram Usage\r\n\r\nif [ $Ram_used -eq $Ram_Size ];\r\nthen\r\nRam_status=\"$R\"Critical\"\"\r\nRam_used=`free -h |grep Mem |awk '{print $3}'`\r\nelse\r\nRam_status=\"$Y\"Normal\"\"\r\nRam_used=`free -h |grep Mem |awk '{print $3}'`\r\nfi\r\n\r\n###Swap space Usage\r\n\r\nif [ $Swap_used -eq $Swap_Size ];\r\nthen\r\nSwap_status=\"$R\"Critical\"\"\r\nSwap_used=`free -h |grep Swap |awk '{print $3}'`\r\nelse\r\nSwap_status=\"$Y\"Normal\"\"\r\nSwap_used=`free -h |grep Swap |awk '{print $3}'`\r\nfi\r\n\r\n#set -x\r\n\r\n###Cpu Load\r\n\r\nif [ 5 -le $CPU_load ];\r\n#if [ 5 -le 5 ];\r\nthen\r\nCPU_status=\"$R\"CPU_over_loaded\"\"\r\nelse\r\nCPU_status=\"$Y\"Normal\"\"\r\nfi\r\n\r\n#echo \"$CPU_status\"\r\n\r\n#Load_15_Mins=`uptime |awk '{print $10}'`\r\necho -e $W \"############## Server Status #############################################################\"\r\n#echo \"########################################################### Server details ###################\"\r\necho -e $BL\" Build Server = `echo $GRN Please change here usage server` \"\r\necho -e $BL\" Server name = `echo $GRN $Hostname` \"\r\necho -e $BL\" Server IP Address = `echo $GRN $Server_IP` \"\r\necho -e $BL\" Server OS Version = `echo $GRN $OS_version` \"\r\necho -e $BL\" Server Load Average = `echo $GRN $Server_Load` Status = $CPU_status \"\r\necho -e $BL\" Server RAM Usage = `echo $GRN $Ram_used out of $Ram_Size GB` Status = $Ram_status \"\r\necho -e $BL\" Server Swap usage = `echo $GRN $Swap_used out of $Swap_Size GB ` Status = $Swap_status\"\r\necho -e $BL\" Currently login users = `echo $GRN $Users` \"\r\necho -e $BL\" Technical Support = `echo $GRN $Support` \"\r\n##echo \" Server Load Average 15 Min= $Load_15_Mins \"\r\necho -e $W \"############################################################# Server Status ##############\"\r\necho -e $RESET\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step: 2<\/strong><\/p>\n<p>schedule the cron job for\u00a0 server performance update status\u00a0 and output redirect to \/etc\/motd<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Note :<\/strong><\/p>\n<p>cron job update the\u00a0 \/etc\/motd file every <strong>1 min<\/strong> once<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-304\" src=\"https:\/\/secureethics.com\/wp-content\/uploads\/2018\/05\/crontab.png\" alt=\"\" width=\"365\" height=\"41\" srcset=\"https:\/\/secureethics.com\/wp-content\/uploads\/2018\/05\/crontab.png 365w, https:\/\/secureethics.com\/wp-content\/uploads\/2018\/05\/crontab-300x34.png 300w\" sizes=\"auto, (max-width: 365px) 100vw, 365px\" \/><\/p>\n<p>Step: 3<\/p>\n<p>Once login through ssh the server and you will get the server status like below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-425\" src=\"https:\/\/secureethics.com\/wp-content\/uploads\/2018\/05\/script_out.png\" alt=\"\" width=\"627\" height=\"179\" srcset=\"https:\/\/secureethics.com\/wp-content\/uploads\/2018\/05\/script_out.png 627w, https:\/\/secureethics.com\/wp-content\/uploads\/2018\/05\/script_out-300x86.png 300w, https:\/\/secureethics.com\/wp-content\/uploads\/2018\/05\/script_out-520x148.png 520w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This script can be used for monitoring the server like RAM, Server Load, OS Version, Server IP, Swap space while login in ssh terminal and display the server status. Step: 1 Copy the below codes into the script file Example&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":425,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/secureethics.com\/index.php?rest_route=\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/secureethics.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/secureethics.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/secureethics.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/secureethics.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=287"}],"version-history":[{"count":0,"href":"https:\/\/secureethics.com\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/secureethics.com\/index.php?rest_route=\/wp\/v2\/media\/425"}],"wp:attachment":[{"href":"https:\/\/secureethics.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/secureethics.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/secureethics.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}