File: 1.00.9a/node/win32/node.vbs (View as HTML)

  1: ' REM FreeNATS Windows Node Bogeroo
  2: '
  3: ' This file is part of FreeNATS
  4: '
  5: ' FreeNATS is (C) Copyright 2008 PurplePixie Systems
  6: '
  7: ' FreeNATS is free software: you can redistribute it and/or modify
  8: ' it under the terms of the GNU General Public License as published by
  9: ' the Free Software Foundation, either version 3 of the License, or
 10: ' (at your option) any later version.
 11: '
 12: ' FreeNATS is distributed in the hope that it will be useful,
 13: ' but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: ' GNU General Public License for more details.
 16: '
 17: ' You should have received a copy of the GNU General Public License
 18: ' along with FreeNATS.  If not, see www.gnu.org/licenses
 19: '
 20: ' For more information see www.purplepixie.org/freenats
 21: 
 22: PushData=0
 23: PullData=1
 24: PushURL=""
 25: NodeID=""
 26: NodeKey="" 'for post only
 27: 
 28: Set colNamedArguments= WScript.Arguments.Named
 29: 
 30: If colNamedArguments.Exists("nooutput") Then
 31: 	PullData=0
 32: End If
 33: If colNamedArguments.Exists("pushurl") Then
 34: 	PushData=1
 35: 	PushURL=colNamedArguments.Item("pushurl")
 36: End If
 37: If colNamedArguments.Exists("nodeid") Then
 38: 	NodeID=colNamedArguments.Item("nodeid")
 39: End If
 40: If colNamedArguments.Exists("nodekey") Then
 41: 	NodeKey=colNamedArguments.Item("nodekey")
 42: End If
 43: 
 44: OutputBuffer = ""
 45: 
 46: OB "<?xml version=""1.0"" encoding=""UTF-8""?>"
 47: OB "<freenats-data>"
 48: OB " <header>"
 49: OB "  <name>FreeNATS VBS Node XML</name>"
 50: OB "  <version>0.03</version>"
 51: OB " </header>"
 52: 
 53: 
 54: 
 55: 
 56: 
 57: 
 58: Function OB(text)
 59: 	OutputBuffer = OutputBuffer & text & vbCrLf
 60: End Function
 61: 
 62: Function DumpOB()
 63: 	Wscript.Echo OutputBuffer
 64: End Function
 65: 
 66: alloweds = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890!$%^*()[]{}?/+=-_ "
 67: 
 68: Function SafeString(text)
 69: 	text = Replace(text,"&","+")
 70: 	output = ""
 71: 	For position = 1 to Len(text)
 72: 		character = Mid(text,position,1)
 73: 		pos = InStr(alloweds,character)
 74: 		If pos > 0 Then
 75: 			output = output & character
 76: 		Else
 77: 			output = output & "_"
 78: 		End If
 79: 	Next
 80: 	SafeString = output
 81: End Function
 82: 
 83: strComputer = "."
 84: Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 85: 
 86: 
 87: set objRefresher = CreateObject("WbemScripting.Swbemrefresher")
 88: Set objProcessor = objRefresher.AddEnum (objWMIService, "Win32_PerfFormattedData_PerfOS_Processor").objectSet
 89: 
 90: Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service")
 91: Set memItems = objRefresher.AddEnum (objWMIService, "Win32_PerfFormattedData_PerfOS_Memory").objectSet
 92: Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
 93: 
 94: Set WshNetwork = WScript.CreateObject("WScript.Network")
 95: AddTest "system.name","Computer Name",SafeString(WshNetwork.ComputerName),0
 96: 
 97: 
 98: objRefresher.Refresh ' No I don't really get it either
 99: WScript.Sleep 2000   ' but unless you do this you get even
100: objRefresher.Refresh ' weirder than usual values.
101: 
102: int ServiceRunning=0
103: int Alert=0
104: For Each objService in colRunningServices 
105: 	If objService.State = "Running" Then
106: 		ServiceRunning=1
107: 		Alert=0
108: 	Else
109: 		Alert=1
110: 		ServiceRunning=0
111: 	End If
112: 	TName = Replace(objService.Name," ","_")
113: 	TestName="srv." & SafeString(TName)
114:     AddTest TestName, SafeString(objService.DisplayName) & " Service", ServiceRunning, Alert
115: Next
116: 
117: 
118: intThresholdViolations = 0
119: 
120: 
121: Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
122: 
123: For Each objDisk in colDisks
124: 
125: DiskSpace = objDisk.FreeSpace / 1024 / 1024
126: DiskSize = objDisk.Size / 1024 / 1024
127: DiskUsed = DiskSize - DiskSpace
128: DiskUsedPerc = ( DiskUsed / DiskSize ) * 100
129: DiskInDrive = 1
130: If IsNull(DiskSpace)  Then
131:  DiskSpace = "0"
132:  DiskUsed = "0"
133:  DiskUsedPerc = "0.00"
134:  DiskInDrive = 0
135: End If
136: DiskSpace = Round(DiskSpace,2)
137: DiskUsed = Round(DiskUsed,2)
138: DiskUsedPerc = Round(DiskUsedPerc,2)
139: 
140: alertlevel = 0
141: If DiskUsedPerc > 80 Then
142:  alertlevel = 1
143: End If
144: If DiskUsedPerc > 90 Then
145:  alertlevel = 2
146: End If
147: 
148: 
149: If DiskInDrive = 1 Then
150: TestName= "disk.free." & Left(objDisk.DeviceID,1)
151: AddTest TestName,"Space Free On " & objDisk.DeviceID & " (Mb)", DiskSpace, alertlevel
152: TestName= "disk.used." & Left(objDisk.DeviceID,1)
153: AddTest TestName,"Space Used On " & objDisk.DeviceID & " (Mb)", DiskUsed, alertlevel
154: TestName= "disk.used.perc." & Left(objDisk.DeviceID,1)
155: AddTest TestName,"Percent Disk Usage On " & objDisk.DeviceID & " (%)", DiskUsedPerc, alertlevel
156: End If
157: 
158: Next
159: 
160: 
161: 
162: For Each objItem in objProcessor
163: 
164:     If objItem.Name = "_Total" Then
165:     	AddTest "proc.load","Processor Load (%)",objItem.PercentProcessorTime,0
166: 	End If
167: Next
168: 
169: 
170:  
171: For Each objOS in colOperatingSystems
172:     dtmBootup = objOS.LastBootUpTime
173:     dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
174:     dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
175:     AddTest "uptime","Uptime (Hours)",dtmSystemUptime,0
176: Next
177:  
178: Function WMIDateStringToDate(dtmBootup)
179:     WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
180:         Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
181:             & " " & Mid (dtmBootup, 9, 2) & ":" & _
182:                 Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup,13, 2))
183: End Function
184: 
185: 
186: 
187: 
188: For Each objItem in memItems
189: 	TestName="mem."
190: 	If Len(objItem.Name) > 0 Then
191: 		TestName = TestName & objItem.Name & "."
192: 	End If
193: 	TestName = TestName & "free"
194: 	
195: 	AddTest TestName,"Memory Available (Kilobytes)",objItem.AvailableKBytes,0
196: 
197: Next
198: 
199: 
200: OB "</freenats-data>"
201: 
202: Function AddTest(testname,testdesc,testvalue,alertlevel)
203:  OB "<test NAME=""" & testname & """>"
204:  OB " <name>" & testname & "</name>"
205:  OB " <desc>" & testdesc & "</desc>"
206:  OB " <value>" & testvalue & "</value>"
207:  OB " <alertlevel>" & alertlevel & "</alertlevel>"
208:  OB "</test>"
209: End Function
210: 
211: If PullData=1 Then
212: 	DumpOB()
213: End If
214: 
215: If PushData=1 Then
216: 	' Push Da Data
217: 	WScript.Echo
218: 	WScript.Echo "Pushing Data..."
219: 	WScript.Echo "URL   : " & PushURL
220: 	WScript.Echo "NodeID: " & NodeID
221: 	WScript.Echo "Key   : " & NodeKey
222: 	
223: 	Set objHTTP = CreateObject("Microsoft.XMLHTTP")
224: 	objHTTP.open "POST", PushURL, False
225: 	 
226: 	objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
227: 	http_request = "nodeid=" & Escape(NodeID) & "&nodekey=" & Escape(NodeKey) & "&xml=" & Escape(OutputBuffer)
228: 	objHTTP.send http_request
229: 	 
230: 	response = objHTTP.responseText
231: 	 
232: 	Set objHTTP = Nothing
233: 	
234: 	If Right(response,1) = "1" Then
235: 		WScript.Echo "Post Successful"
236: 	Else
237: 		WScript.Echo "Encountered An Error:"
238: 		WScript.Echo response
239: 	End If
240: 	
241: End If