Tuesday, February 13, 2007

Server Uptime Script

The thing that you have to understand about J & E (former coworkers) is that they can script a sunny day. Script demons these guys were. Me? Yeah, not so much.

So as you might imagine, I was actually quite pleased with myself when I wrote my first vb script to pull server uptime. Of course it shelled out to uptime.exe and was very basic in it's functionality. My gleee was short-lived as J was quick to inform me that only people without penises shell out to stand alone exe's to accomplish tasks.

Alas, with J & E gone it has fallen on me to script things as needed. This is pretty scary when you consider the size company I work for and my relative skill set when it comes to scripting. The flip side is that it's given me a chance to improve.. which is coolio. So today I crafted a script that is pretty handy for pulling server uptime.

It's still pretty basic, but is flexable enough to use logic against the uptime value. i.e. if a server has been up too long, reboot it. Is it the most efficient code? Oh Hell no. E would probably crap a brick if he ever comes across it. Instantiating objects over and over again was one of his pet peaves.

Still it works, and my shabby development skills definitely allow for you to improve it when you steal it for your own use and claim to your manager that you came up with the idea. Hey it's cool; I know how it works. There's one guy in the world who actually knows how to write code, and he posts to the internet. Everyone else downloads, plagurizes, and re-posts it somewhere else. It's the great circle of life.

So here ya go.

'
'Try this in Production first. Just for grins.
'// USAGE: cscript.exe uptime.vbs (duh)

option explicit
'//Declarations
Dim strComputer, thefarm, aServer, intsystemuptime, objWMIService, colOperatingSystems, objOS

'// Get the farm 411
Set theFarm = CreateObject("MetaFrameCOM.MetaFrameFarm")
theFarm.Initialize 1

'// If this doesn't work we're screwed so exit
if Err.Number <> 0 Then
WScript.Echo "Can't create MetaFrameFarm object"
WScript.Echo "(" & Err.Number & ") " & Err.Description
WScript.Echo ""
WScript.Quit Err.Number
End if

'// Loop through the server name
For each aServer in thefarm.servers

'// set the hostname based on where we are in the loop
strComputer = aServer.servername

'// Compensate for small penis and crappy coding
on error resume next

IF not strComputer = "" then
wscript.echo "Checking " & strcomputer
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

'// Will error if the server is offline. You might want to know about that.
if Err.Number <> 0 Then
WScript.Echo "Something is jacked up. "
WScript.Echo "(" & Err.Number & ") " & Err.Description
WScript.Echo ""
End If

Set colOperatingSystems = objWMIService.ExecQuery _
("Select * From Win32_PerfFormattedData_PerfOS_System")

'//Nested For loop to enumerate the objects in the collection, yo.

For Each objOS in colOperatingSystems

'\\Get the number of seconds and divide by 3600 to get hours
'\\set as an integer because decimals are for nerds!

intSystemUptime = Int(objOS.SystemUpTime / 3600)
Wscript.Echo strComputer & " has been up for " & intSystemUptime & " hours."
wscript.echo ""
Next
Set objWMIService = nothing
Set colOperatingSystems = nothing
End If
Next

No comments: