#!/usr/bin/perl

#developed from original script by Andrew Stringer 09/10/2002 onwards.
#written 31/10/2003 to report ntp status for network


sub ntpget
{
$result1= `/usr/sbin/ntpq -p tick.mediahub.co.uk`;
$result2= `/usr/sbin/ntpq -p tock.mediahub.co.uk`;

$result= "tick.mediahub.co.uk \n".$result1."\n tock.mediahub.co.uk \n".$result2;

#return result
return $result;
#end sub with curly brace
}

sub Uptime
{

#generate uptime
#on Linux
$result= `/usr/bin/uptime`;
#on SGI
#$result= `/usr/bsd/uptime`;

#attempt to extract uptime from $result,
#first split $result by , char
@array= split (/,/, $result);
$days1=@array[0];
$hours=@array[1];

#split 1st field by two spaces
@array1= split (/up/,$days1);
$days= @array1[1];


#return result
return $days, $hours;
#end sub with curly brace
}

sub ntptrace
{
$trace = `/usr/sbin/ntptrace` ;

$trace =~ s/0x0a/0x0a\n/g ;
$trace =~ s/:/\t/g ;

return $trace ;
}


MAIN:
#start of body of program
{

$green = "#00bb00" ;
$amber = "#ffff00" ;
$red = "#cc0000" ;
$white = "#ffffff" ;
$grey = "#999999" ;


$remotehost=$ENV{'REMOTE_ADDR'} ;
$useragent=$ENV{'HTTP_USER_AGENT'} ;


#call uptime sub
&Uptime;

#call ntpget & ntptrace sub
&ntpget;
&ntptrace;


$date = localtime;


print "Content-type: text/html\n\n";

print <<ENDOFTEXT1 ;

<html>
<head>
<title>tick.mediahub.co.uk status</title>
<META HTTP-EQUIV="refresh" CONTENT="60">
</head>

<body text="#000000" bgcolor="#ffffff" link="0000EE" vlink="#551A8B">

<center>
<table BORDER="0" Width="80%">
<tr>
<td>
<a href="http://www.mediahub.co.uk/index.html">
<img SRC="/media_logo.gif" BORDER=0  title="Up a level" alt="Click here to return to the Media Hub's web page"></a></td>
<td align="center">
<font size=+3>tick.mediahub.co.uk</font> 
<br> Uptime: $days $hours (hours:mins)
<br>Your local computer's time is
<form name="Tick">
<input type="text" size="12" name="Clock">
</form>

<script>
function show()
{
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var seconds=Digital.getSeconds()
var dn="AM"
if (hours>12)
{
dn="PM"
hours=hours-12
//this is so the hours written out is
//in 12-hour format, instead of the default //24-hour format.
}
if (hours==0)
hours=12
//this is so the hours written out
//when hours=0 (meaning 12a.m) is 12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
document.Tick.Clock.value=
hours+":"+minutes+":"+seconds+" "+dn
setTimeout("show()",1000)
}
show()
</script>

</td></tr></table>
</center>

<br>

ENDOFTEXT1

print <<ENDOFTEXT2 ;
<center>
<table border="0" bgcolor="#ffffcc">
<th>Current Time:-</th>
<tr>
<td>It is now  <b>$date</b></td>
</tr>
</table>

<table border="0" bgcolor="#ffffcc">
<th>Network Time Status</th>
<tr><td>
<pre>
$result
</pre>
</td></tr>

<th>Key - 1st Column</th>
<tr><td>
<b>space</b> -unreachable / sync to this server / excessive sync distance, 
<b>x</b> -False Ticker, <b>+</b> -Peering Candidate,<br /> 
<b>-</b> -Outlyer (Discarded), <b>#</b> -Selected Candidate,
<b>*</b> -Selected System Peer, <b>o</b> -PPS Peer.<br />
</td></tr>

<th>Key - Other Columns</th>
<tr><td>
<b>remote</b> -Upstream server, <b>refid</b> -Upstream Server Source, 
<b>st</b> -Stratum, <b>offset</b> -Time offset in Milliseconds.<br>
<br>
</td></tr>

<tr><td>
<br>
<b>NTP Hierachy:-</b>
</td><tr>

<tr><td>
<pre>
$trace
</pre>
</td></tr>

</table>
</center>
<br>


</body>
</html>

ENDOFTEXT2
}
exit (0);
