Bash: Create Simple Server System Information

by admin on March 26, 2011

in Http Maid Note

A few days ago, at the time when i’m free and i have completed all of my jobs, i was walking around a few of web hosting provider’s websites to get reading. I found that most of them have the uptime statuses of their servers, and it is kind of exclaimed if we have something like that too, we can display our box status and we will always know our box system information without typing command one by one in ssh. So the idea is creating a simple bash script that will show some information like uptime, memory usage, disk usage, port status, etc., and export the statuses to txt or html file so we can we can see our box statuses with browser. It is good when we are not in home so we can check the statuses from phone, laptop, or computer somewhere. Okay, let’s get it on!

First we will check the box uptime status by using uptime command, and you will get the following result:

05:35:16 up 21:40,  1 user,  load average: 0.00, 0.00, 0.00

The informations shown above are server time, uptime, current user, and load average, but we only need uptime to be displayed right? So we’re going cut off another statuses and grab uptime only.

uptime | awk {'print $3'}

and we got:


You can use that for displaying uptime status which means 21 hours and 40 minutes, but it’s not so good because there’s a comma character in the end of status right? If you wanna throw that comma character then we should display 5 characters before comma.

uptime | awk {'print $3'} | awk 'match($0,","){print substr($0,RSTART-1,5)}'

and finally we got:


I think it is still monoton right? How about ’21 Hours’ and ’40 Minutes’ display? :D Now we’re not going to use comma but ‘colon’, grab 2 characters before and after ‘colon’.

echo Uptime: `uptime | awk {'print $3'} | awk 'match($0,":"){print substr($0,RSTAR-1,2)}'` Hours and `uptime | awk {'print $3'} | awk 'match($0,":"){print substr($0,RSTART+1,2)}'` Minutes

So we got the beautiful one :D

Uptime: 21 Hours and 40 Minutes

We have finished with uptime and now continue to average cpu load which the result is still from the same command with uptime, we only have to print the last three fields.

echo Load Average: `uptime | awk {'print $8, $9, $10'}`

here’s the result:

Load Average: 0.00, 0.00, 0.00

Next is memory usage with ‘free’ command to show the status in Kilobytes or we can use ‘free -m’ in Megabytes and ‘free -g’ in Gigabytes. When we type ‘free -m’ command it will be:

             total       used       free     shared    buffers     cached
Mem:           512         94        417          0          0          0
-/+ buffers/cache:         94        417
Swap:            0          0          0

For the note, memory usage displayed in OpenVZ and XEN is a little bit different, OpenVZ will display the same usage of memory in ‘Mem’ and ‘buffers/cache’ field while on the other side XEN real memory usage is in ‘buffers/cache’ field so we will grab the ‘buffers/cache’ field.

echo `free -m | grep '+' | awk {'print $3'}`MB

we got:


Is that enough for memory status? Let’s add a few more statistics for memory total and percentage of usage.

for memory total we grab the second field from the first row

echo `free -m | grep 'Mem:' | awk {'print $2'}`MB



for the percentage we divide memory usage times a hundred with total memory available

echo $((`free -m | grep '+' | awk {'print $3'}`*100/`free -m | grep 'Mem:' | awk {'print $2'}`))%

and the percentage of memory usage is


Now we combine those three memory statuses

echo "Memory Usage: $((`free -m | grep '+' | awk {'print $3'}`*100/`free -m | grep 'Mem:' | awk {'print $2'}`))% (`free -m | grep '+' | awk {'print $3'}`MB) from `free -m | grep 'Mem:' | awk {'print $2'}`MB Available"

to get:

Memory Usage: 18% (94MB) from 512MB Available

What do we got so far? Uptime, load average, and memory usage, how about disk usage capacity?

df -h

will be displaying:

Filesystem            Size  Used Avail Use% Mounted on
/dev/simfs             10G  1.4G  8.7G  14% /
none                  256M  4.0K  256M   1% /dev

If you wanna minimize the results to be location, percentage of usage, memory usage, and memory total, then we grab

echo "`df -h | grep '/dev/simfs' | awk {'print $6'}`    `df -h | grep '/dev/simfs' | awk {'print $5, $4, $2'}`"
echo `df -h | grep 'none' | awk {'print $6, $5, $4, $2'}`

so we got:

/    14% 8.7G 10G
/dev 1% 256M 256M

I guess those statuses are enough for simple server information, if you need to monitor your port, you have to install nc

yum install nc

and for example if you want to display http port, you can use the following command

nc -zv -w30 `hostname` 80

and you will get the result like this

Connection to yourhostname 80 port [tcp/http] succeeded!

if your port is down or not running, you will get

nc: connect to yourhostname port 80 (tcp) failed: Connection refused

Now, to display the status(es) of your port(s), you can simplify it by grabbing the last word (succeeded / refused) and use if else statement

HTTP=`nc -zv -w30 $hostname 80`
if [[ $HTTP == *succeeded* ]]
	echo HTTP: Up
	echo HTTP: Down

That’s it. Pretty well for monitoring right? You only need to export the result of those statuses to html or text file and move it to your public directory, maybe a little touch with html code can make it look better. Anyway here’s the example of simple server system information i made: here. I added a few system information and modified the appearance with table. :D

{ 1 comment… read it below or add one }

Leave a Comment

Previous post:

Next post: