|
Realtime Server Uptime for Windows - PHP
Written by Tony Bhimani
September 17, 2005
Requirements
Windows Operating System
Web Server with PHP support
Web Browser that supports JavaScript and DHTML
Download the source code: winuptime.zip
(shell_exec version only)
This script shows how to display your Windows server's uptime in a dynamic way.
I originally wrote the uptime script for Linux and now have decided to
post a Windows capable version. I have tested it on Windows XP and 2000
Server and I can only assume it will work on Windows Server 2003 (I don't
have a copy available to try it on). As long as "net statistics server"
is there and still reports the date & time the server booted up, I
don't foresee any problems. The code is identical to my Linux uptime version
and the only difference is how the uptime is gathered. This script uses
shell_exec so if you are on some type of shared hosting server or your
version of PHP has shell_exec disabled then this script will not work
for you.
XenoCafe runs on Linux, so I can't really so you an example of the Windows
script in action. However, it behaves exactly like my Linux
uptime script as shown below.
Server Uptime: 4 days, 20 hours, 57 minutes, 36 seconds
How it's done:
Using PHP, I read the date & time the Windows server was booted up
by calling shell_exec and giving it this command to run "net statistics
server" which returns back information about the server. Within that
information is when the server booted up as you can see from the example
output of running "net statistics server":
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\>net statistics server
Server Statistics for \\GRIFFINS
Statistics since 9/17/2005 12:31 PM
Sessions accepted 1
Sessions timed-out 0
Sessions errored-out 0
Kilobytes sent 0
Kilobytes received 0
Mean response time (msec) 0
System errors 0
Permission violations 0
Password violations 0
Files accessed 0
Communication devices accessed 0
Print jobs spooled 0
Times buffers exhausted
Big buffers 0
Request buffers 0
The command completed successfully.
C:\>
Using a regular expression the date & time is captured and stored
in a variable. That date & time is then converted into an Unix timestamp
using strtotime() and is then subtracted from the current Unix timestamp
that is pulled from the PHP time() function. This in effect gives the
uptime in seconds the server has been online. And much like in the Linux
version of the script, the uptime in seconds is written to a JavaScript
variable. Then I use some JavaScript to increment the number of uptime
seconds every second using the JavaScript setTimeout function. I parse
the uptime seconds and calculate the number of days, hours, minutes and
seconds the server has been up. Then use DHTML to write the uptime to
the screen dynamically.
Source Code:
<?php
// format the uptime in case the browser doesn't support dhtml/javascript
// static uptime string
function format_uptime($seconds) {
$secs = intval($seconds % 60);
$mins = intval($seconds / 60 % 60);
$hours = intval($seconds / 3600 % 24);
$days = intval($seconds / 86400);
if ($days > 0) {
$uptimeString .= $days;
$uptimeString .= (($days == 1) ? " day" : " days");
}
if ($hours > 0) {
$uptimeString .= (($days > 0) ? ", " : "") . $hours;
$uptimeString .= (($hours == 1) ? " hour" : " hours");
}
if ($mins > 0) {
$uptimeString .= (($days > 0 || $hours > 0) ? ", " : "") . $mins;
$uptimeString .= (($mins == 1) ? " minute" : " minutes");
}
if ($secs > 0) {
$uptimeString .= (($days > 0 || $hours > 0 || $mins > 0) ? ", " : "") . $secs;
$uptimeString .= (($secs == 1) ? " second" : " seconds");
}
return $uptimeString;
}
// get the server statistics with "net statistics server" by shell_exec
$winstats = shell_exec("net statistics server");
// grab the date & time the server started up
preg_match("(\d{1,2}/\d{1,2}/\d{4}\s+\d{1,2}\:\d{2}\s+\w{2})", $winstats, $matches);
// convert the readable date & time to a timestamp and deduct it from the current timestamp
// thus giving us the total uptime in seconds
$uptimeSecs = time() - strtotime($matches[0]);
// get the static uptime
$staticUptime = "Server Uptime: ".format_uptime($uptimeSecs);
?>
<html>
<head>
<script language="javascript">
<!--
var upSeconds=<?php echo $uptimeSecs; ?>;
function doUptime() {
var uptimeString = "Server Uptime: ";
var secs = parseInt(upSeconds % 60);
var mins = parseInt(upSeconds / 60 % 60);
var hours = parseInt(upSeconds / 3600 % 24);
var days = parseInt(upSeconds / 86400);
if (days > 0) {
uptimeString += days;
uptimeString += ((days == 1) ? " day" : " days");
}
if (hours > 0) {
uptimeString += ((days > 0) ? ", " : "") + hours;
uptimeString += ((hours == 1) ? " hour" : " hours");
}
if (mins > 0) {
uptimeString += ((days > 0 || hours > 0) ? ", " : "") + mins;
uptimeString += ((mins == 1) ? " minute" : " minutes");
}
if (secs > 0) {
uptimeString += ((days > 0 || hours > 0 || mins > 0) ? ", " : "") + secs;
uptimeString += ((secs == 1) ? " second" : " seconds");
}
var span_el = document.getElementById("uptime");
var replaceWith = document.createTextNode(uptimeString);
span_el.replaceChild(replaceWith, span_el.childNodes[0]);
upSeconds++;
setTimeout("doUptime()",1000);
}
// -->
</script>
</head>
<body onLoad="doUptime();">
<!-- Uses the DIV tag, but SPAN can be used as well -->
<div id="uptime" style="font-weight:bold;"><?php echo $staticUptime; ?></div>
</body>
</html>
|
This page has been viewed 10,732 times |
|