Link to home
Start Free TrialLog in
Avatar of mcburn13
mcburn13Flag for United States of America

asked on

WMI Uptime Script Reporting Incorrect Number on W2k Server

Trying to use VBS from our monitoring server to generate an alarm for a W2k server that has uptime 25 hours or more.  Its the best way I can think of to get an alarm if its daily reboot function hangs for whatever reason.  After much testing I finally got the script below working but I realized it is reporting the incorrect amount of hours.  I used uptime.exe it shows it as 1 day 11 hours (35 hrs total) and this script is reporting 2433 minutes (40.55 hours).  I really don't understand (yet) the whole process of converting UTC to a real number and I'm not sure I want to is there anyone familiar with this script that could provide a fix or at least reason the reporting is off?

---script---
strComputer = "servername.domain.com"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
 
For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
    dtmSystemUptime = DateDiff("n", dtmLastBootUpTime, Now) 
    Wscript.Echo "Message: System Uptime Alarm"
    Wscript.Echo "Data:"
    Wscript.Echo "Uptime"&vbTab& dtmSystemUptime
    WScript.Quit(0)
Next
 
Function WMIDateStringToDate(dtmBootup)
    WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
        Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
            & " " & Mid (dtmBootup, 9, 2) & ":" & _
                Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup,13, 2))
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of TheNautican
TheNautican

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcburn13

ASKER

I found that script too but I need the output to be only the total number of hours in a whole #. This script outputs days THEN hours.  If I can figure out how to change this script to add up only the total hours I think I'd be golden..
Avatar of TheNautican
TheNautican

how would you like an uptime of say, 2 days, 5 hours, and 35 minutes to be displayed?
53hrs or 54hrs?

-Naut
53 hours would be fine...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for trying it works fine on my Win7 machine but on this Windows 2000 server it is off by five hours. See the screenshot comparing this script to the uptime.exe utility output.   Really scratching my head on this one. I guess W2k reads these UTC #s differently?
User generated image
sounds totally hack but maybe if we can add a "subtract 5" operator in there it would fix this.  But then I'd have to monitor the monitor to make sure the difference is always 5.  Thanks so much for giving this your attention so far.
Ok, we dont have uptime.exe on our 2008 servers but i ran two other things and got the same time the VBS reports. First, I went into Task manager and went to the performance tab. Under system you'll see "Up time" A quick conversion of the days plus the hours matched the vbs script. 2nd I ran C:\systeminfo | find "System Boot Time" and then manually added that time up as well and it matched the VBScript.

I found this blub on microsofts website about uptime.exe. This might
explain why you are getting different times.


It is important to note that the uptime and availability measurements reflect the local view of the operating system only. The "Total Uptime" measurement refers to the sum of all the time during the measured period where the operating system reported itself to be in normal operation. It does not reflect times when the server might be unavailable for other reasons, such at network outages.

-Naut
The thing is I'm using a monitoring utility that needs a single whole number to report on. I was able to get it in that format with your updated script its just that it is reporting 5 hours off.  I can't use uptime.exe as it only outputs in a certain format.

This has to be something with W2k as this script reports correctly on later OS's. I am trying to figure out how to fudge that time converter to subtract 5 so maybe that would work for me. If anyone knows how let me know...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think my last obstacle is being able to specify the servername to run against.  In my previous script I used strComputer but not sure how to insert that with the current.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Might I ask what you are using as the base to compare the script to? Also, how does that base compare to the other methods such as What Task Manager shows for the uptime?

-Naut
I just know when the machine was rebooted- the uptime.exe utility will match the CPU idle time and coincide with the time it was restarted. For some reason this script works perfectly (as you wrote it) on Operating Systems later than W2k but for some reason W2k reports it with an additional 5 hours added to it.   With my - 5 operator in there, it is now reporting it spot on but couldn't have done it without your help thanks!
After further research there does appear to be something wrong with w2k servers as pointed out by the Scripting Guy.
http://blogs.technet.com/b/heyscriptingguy/archive/2005/08/02/how-can-i-determine-the-uptime-for-a-server.aspx

Try this code to see if you get the same 5 hour error.

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
 
For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
    dtmSystemUptime = DateDiff("n", dtmLastBootUpTime, Now)
    dtmSystemUptime = Int(dtmSystemUptime / 60)
    Wscript.Echo dtmSystemUptime & " Hrs"
Next
 
Function WMIDateStringToDate(dtmBootup)
    WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
        Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
            & " " & Mid (dtmBootup, 9, 2) & ":" & _
                Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup,13, 2))
End Function

Open in new window

i came up with the "Subtract 5" operator to add to the script