'==========================================================================
' NAME: Uptime.vbs
' COMMENT: Check Uptime of server and report if greater than 30 days
' AUTHOR: Craig Boyce, Rodney District Council
' EMAIL: craig.boyce@rodney.govt.nz
' DATE  : 30/10/2006
' VERSION : 2.00
'==========================================================================
 
On Error Resume Next
 
Dim objShell, objWMIService, WSHNetwork
Dim ServerName, strTestName, strAlarmState, strUpTime, strOutput
Dim logmessage, logfile, fso, f
 
' Set Variables
 
Set objShell = WScript.CreateObject("WScript.Shell")
Set WSHNetwork = WScript.CreateObject("WScript.Network")
 
ServerName = objShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
 
strTestName = "uptime" ' Change this to what you want the test column to be.
strUpTime = 30 ' Change this to the length of time required before alerting a reboot is required.
 
strAlarmState = "green"
strOutput   = ""
 
' Check to see If used with BBWin or Big Brother client and set extPath.
extPath = objShell.RegRead("HKLM\SOFTWARE\BBWin\tmpPath")
If extPath = "" Then
	extPath = objShell.RegRead("HKLM\SOFTWARE\Quest Software\BigBrother\bbnt\ExternalPath\")
End If
 
' ========================================
' Main Code Starts Here
 
strComputer = "."   ' "." for local computer 
 
On Error Resume Next 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
 
If Err.Number = 0 Then 
     On Error Goto 0 
     strQuery = "select * from Win32_PerfRawData_PerfOS_System" 
     Set colObjects = objWMIService.ExecQuery(strQuery) 
 
 
     For Each objWmiObject In colObjects 
       intPerfTimeStamp = objWmiObject.Timestamp_Object 
       intPerfTimeFreq = objWmiObject.Frequency_Object 
       intCounter = objWmiObject.SystemUpTime 
     Next 
 
     ' Calculation in seconds: 
     'Calculations for Raw Counter Data:PERF_ELAPSED_TIME 
     'http://msdn.microsoft.com/library/en-us/perfmon/base/perf_elapsed_tim... 
     iUptimeInSec = (intPerfTimeStamp - intCounter)/intPerfTimeFreq 
 
     ' convert the seconds 
     sUptime = ConvertTime(iUptimeInSec)
 
	If CInt(Left(sUptime, 2)) > CInt(strUpTime) Then
		strOutput = strOutput & "Uptime: " & sUptime & "exceeds " & strUpTime & " Days , This Server needs to be scheduled for a monthly restart"
		strAlarmState = "red"
	Else
		strOutput = strOutput & "Uptime: " & sUptime
		strAlarmState = "green"
	End If
 
End If 
 
' Write the file for BB
WriteFile extPath, strTestName, strAlarmState, strOutput
 
'===========================================================
' FUNCTIONS and SUBS start here
 
' This SUB is used for outputting the file to the external's directory in bb
 
SUB WriteFile(strExtPath, strTestName, strAlarmState, strOutput)
    Set fso = CreateObject("Scripting.FileSystemObject")	
    strOutput = strAlarmState & " " & Date & " " & Time & vbcrlf & vbcrlf & strOutput & vbcrlf
    Set f = fso.OpenTextFile(strExtPath & "\" & strTestName , 8 , TRUE)
    f.Write strOutput
    f.Close
    Set fso = Nothing
END Sub
 
Function ConvertTime(seconds) 
     ConvSec = seconds Mod 60 
     ConvMin = (seconds Mod 3600) \ 60 
     ConvHour =  (seconds Mod (3600 * 24)) \ 3600 
     ConvDays =  seconds \ (3600 * 24) 
     ConvertTime = ConvDays & " days " & ConvHour & " hours " & ConvMin & " minutes " 
End Function