0

I'm planning to develop a script to check the uptime percentage of my server. By uptime percentage I mean something like 99.7% or something like that. As every webhost says they provide 99.9% uptime, I want to write a script which will actually check that what is the uptime percentage actually.

I do know how to get the uptime in terms of seconds or hr-min-sec. I use this below script for that purpose.

$uptime = shell_exec("cut -d. -f1 /proc/uptime");

But, what I'm not getting is how can I calculate the uptime percentage like 99% uptime or something like that. I'm looking for some ideas and help on this.

I will be really happy if you guys share some opinion and help about thing instead giving negative vote to this question.

P.S.: I've already checked this question: Measuring server uptime but this does not answer my problem.

Community
  • 1
  • 1
iSaumya
  • 1,503
  • 5
  • 21
  • 50
  • Mmmm. Calculate the seconds from when you start check. Then do a comparison like tot_seconds_calculated : 100 = uptime_seconds : x . Then do the mathematic calculation – Marco Mura Dec 11 '14 at 16:44
  • 2
    The command uptime returns the amount of time since the machine booted, not the amount of time the site is available. – robbmj Dec 11 '14 at 16:45
  • Yah! I know so what command should I run? Thats I need your help guys. Can anyone write a simple snippet to show how you want me to implement. – iSaumya Dec 11 '14 at 16:48
  • I don't know of a reliable way to do this. Even if your web server is running 100% of the time, the site may still be periodically unavailable, due to a multitude of issues. – robbmj Dec 11 '14 at 16:50
  • Is there no way to do this? Really. If I can show the uptime percentage my site is running. That is ok for me. – iSaumya Dec 11 '14 at 16:53
  • 2
    The only thing I can think of is to write a script that requests a page from your site and simply records if the server responded or not. You will have to run the script quite frequently and from multiple servers in different locations to reliable determine the uptime. – robbmj Dec 11 '14 at 16:53

2 Answers2

1

The common way is to monitor the server externally, as the server will have a hard time judging its own visibility. Its like trying to watch yourself in the third person without a mirror.

If you are not concerned with the downtime of the server's internet connection, you can monitor your open ports locally, and get a percentage of the time a port was open and closed. Thats about as close as you will get for self performance rating.

Flosculus
  • 6,880
  • 3
  • 18
  • 42
  • I understand. So it's not possible. – iSaumya Dec 11 '14 at 16:57
  • You could pull it off, if you can true proxy a request out and back into your own server. I'm not hot on networking so I don't know how a network card responds when receiving an external request from itself. – Flosculus Dec 11 '14 at 17:00
  • I dont know how to do this what you are saying. – iSaumya Dec 11 '14 at 17:03
  • Well its providing difficult to know what you want. These are abstract solutions we are giving you, hoping you will derive your own solution from them. – Flosculus Dec 11 '14 at 17:08
  • I understand. I also have one of my own idea but that is neither fisible nor smart solution. I thought to set a corn job (every minute) on a php file which will try to access a page and if the header is 200 then insert some flag into database. Then somehow calculate from db. Bad idea I know! :( – iSaumya Dec 11 '14 at 17:10
  • Well you said it :P, thats kinda what I was thinking. – Flosculus Dec 11 '14 at 17:13
  • Just point a Pingdom account at your site and they'll track uptime for you. – ceejayoz Dec 11 '14 at 17:17
  • Thanks man. But it is not free I guess. It is a paid service. – iSaumya Dec 11 '14 at 18:00
  • @iSaumya They have a free level that should be plenty for your needs here. – ceejayoz Dec 11 '14 at 18:48
-2

Memcache::getStats — Get statistics of the server

Here is a page that includes a ready made script to return all available stats as well as code snippets for grabbing certain values. Everything youo need is here:

http://php.net/manual/en/memcache.getstats.php

The particular value you are seeking is returned in:

uptime   (Number of seconds this server has been running)

As ceejayoz pointed out, Memcache has to be installed. It is not native PHP. You can Google install php memcache with your server O/S name.

Len_D
  • 1,422
  • 1
  • 12
  • 21
  • 2
    Where are you getting that memcache is installed? – ceejayoz Dec 11 '14 at 16:59
  • 2
    Re: your edit, even if Memcache is installed, the Memcache server might be a totally different machine, making its stats useless for this purpose. It's also insane to advise installing Memcache just to get server uptime stats, **especially** since it has the same drawbacks as the server's built-in `uptime` command - it just tells you how long since the last reboot of the service. – ceejayoz Dec 11 '14 at 17:09
  • It works under the right circumstances. I see your point. We use it here as part of our daily I.T. stat reporting and there are no issues. But, yes, other people's mileage may vary. – Len_D Dec 11 '14 at 18:29