File: 1.00.10a/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: strComputer="."
 28: wmiUser=""
 29: wmiPass=""
 30: wmiDomain=""
 31: wmiSimple=1
 32: 
 33: Set colNamedArguments= WScript.Arguments.Named
 34: 
 35: If colNamedArguments.Exists("nooutput") Then
 36: 	PullData=0
 37: End If
 38: If colNamedArguments.Exists("pushurl") Then
 39: 	PushData=1
 40: 	PushURL=colNamedArguments.Item("pushurl")
 41: End If
 42: If colNamedArguments.Exists("nodeid") Then
 43: 	NodeID=colNamedArguments.Item("nodeid")
 44: End If
 45: If colNamedArguments.Exists("nodekey") Then
 46: 	NodeKey=colNamedArguments.Item("nodekey")
 47: End If
 48: If colNamedArguments.Exists("host") Then
 49: 	strComputer=colNamedArguments.Item("host")
 50: End If
 51: If colNamedArguments.Exists("user") Then
 52: 	wmiUser=colNamedArguments.Item("user")
 53: 	wmiSimple=0
 54: End If
 55: If colNamedArguments.Exists("pass") Then
 56: 	wmiPass=colNamedArguments.Item("pass")
 57: End If
 58: If colNamedArguments.Exists("domain") Then
 59: 	wmiDomain=colNamedArguments.Item("domain")
 60: 	wmiSimple=0
 61: End If
 62: 
 63: OutputBuffer = ""
 64: 
 65: OB "<?xml version=""1.0"" encoding=""UTF-8""?>"
 66: OB "<freenats-data>"
 67: OB " <header>"
 68: OB "  <name>FreeNATS VBS Node XML</name>"
 69: OB "  <version>0.03</version>"
 70: OB " </header>"
 71: 
 72: 
 73: 
 74: 
 75: 
 76: 
 77: Function OB(text)
 78: 	OutputBuffer = OutputBuffer & text & vbCrLf
 79: End Function
 80: 
 81: Function DumpOB()
 82: 	Wscript.Echo OutputBuffer
 83: End Function
 84: 
 85: alloweds = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890!$%^*()[]{}?/+=-_ "
 86: 
 87: Function SafeString(text)
 88: 	text = Replace(text,"&","+")
 89: 	output = ""
 90: 	For position = 1 to Len(text)
 91: 		character = Mid(text,position,1)
 92: 		pos = InStr(alloweds,character)
 93: 		If pos > 0 Then
 94: 			output = output & character
 95: 		Else
 96: 			output = output & "_"
 97: 		End If
 98: 	Next
 99: 	SafeString = output
100: End Function
101: 
102: If wmiSimple = 1 Then
103:  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
104: Else
105:  set objLocator = CreateObject("WbemScripting.SWbemLocator")
106:  set objWMIService = objLocator.ConnectServer(strComputer,"root/cimv2",wmiUser,wmiPass)
107:  objWMIService.Security_.impersonationlevel=3 ' at level 3 you are impersonating the pope
108: End If
109: 
110: 
111: set objRefresher = CreateObject("WbemScripting.Swbemrefresher")
112: Set objProcessor = objRefresher.AddEnum (objWMIService, "Win32_PerfFormattedData_PerfOS_Processor").objectSet
113: 
114: Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service")
115: Set memItems = objRefresher.AddEnum (objWMIService, "Win32_PerfFormattedData_PerfOS_Memory").objectSet
116: Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
117: 
118: Set WshNetwork = WScript.CreateObject("WScript.Network")
119: AddTest "system.name","Computer Name",SafeString(WshNetwork.ComputerName),0
120: 
121: 
122: objRefresher.Refresh ' No I don't really get it either
123: WScript.Sleep 2000   ' but unless you do this you get even
124: objRefresher.Refresh ' weirder than usual values.
125: 
126: int ServiceRunning=0
127: int Alert=0
128: For Each objService in colRunningServices 
129: 	If objService.State = "Running" Then
130: 		ServiceRunning=1
131: 		Alert=0
132: 	Else
133: 		Alert=1
134: 		ServiceRunning=0
135: 	End If
136: 	TName = Replace(objService.Name," ","_")
137: 	TestName="srv." & SafeString(TName)
138:     AddTest TestName, SafeString(objService.DisplayName) & " Service", ServiceRunning, Alert
139: Next
140: 
141: 
142: intThresholdViolations = 0
143: 
144: 
145: Set colDisks = objWMIService.ExecQuery ("Select * from Win32_LogicalDisk")
146: 
147: For Each objDisk in colDisks
148: 
149: DiskSpace = objDisk.FreeSpace / 1024 / 1024
150: DiskSize = objDisk.Size / 1024 / 1024
151: DiskUsed = DiskSize - DiskSpace
152: DiskUsedPerc = ( DiskUsed / DiskSize ) * 100
153: DiskInDrive = 1
154: If IsNull(DiskSpace)  Then
155:  DiskSpace = "0"
156:  DiskUsed = "0"
157:  DiskUsedPerc = "0.00"
158:  DiskInDrive = 0
159: End If
160: DiskSpace = Round(DiskSpace,2)
161: DiskUsed = Round(DiskUsed,2)
162: DiskUsedPerc = Round(DiskUsedPerc,2)
163: 
164: alertlevel = 0
165: If DiskUsedPerc > 80 Then
166:  alertlevel = 1
167: End If
168: If DiskUsedPerc > 90 Then
169:  alertlevel = 2
170: End If
171: 
172: 
173: If DiskInDrive = 1 Then
174: TestName= "disk.free." & Left(objDisk.DeviceID,1)
175: AddTest TestName,"Space Free On " & objDisk.DeviceID & " (Mb)", DiskSpace, alertlevel
176: TestName= "disk.used." & Left(objDisk.DeviceID,1)
177: AddTest TestName,"Space Used On " & objDisk.DeviceID & " (Mb)", DiskUsed, alertlevel
178: TestName= "disk.used.perc." & Left(objDisk.DeviceID,1)
179: AddTest TestName,"Percent Disk Usage On " & objDisk.DeviceID & " (%)", DiskUsedPerc, alertlevel
180: End If
181: 
182: Next
183: 
184: 
185: 
186: For Each objItem in objProcessor
187: 
188:     If objItem.Name = "_Total" Then
189:     	AddTest "proc.load","Processor Load (%)",objItem.PercentProcessorTime,0
190: 	End If
191: Next
192: 
193: 
194:  
195: For Each objOS in colOperatingSystems
196:     dtmBootup = objOS.LastBootUpTime
197:     dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
198:     dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
199:     AddTest "uptime","Uptime (Hours)",dtmSystemUptime,0
200: Next
201:  
202: Function WMIDateStringToDate(dtmBootup)
203:     WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
204:         Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
205:             & " " & Mid (dtmBootup, 9, 2) & ":" & _
206:                 Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup,13, 2))
207: End Function
208: 
209: 
210: 
211: 
212: For Each objItem in memItems
213: 	TestName="mem."
214: 	If Len(objItem.Name) > 0 Then
215: 		TestName = TestName & objItem.Name & "."
216: 	End If
217: 	TestName = TestName & "free"
218: 	
219: 	AddTest TestName,"Memory Available (Kilobytes)",objItem.AvailableKBytes,0
220: 
221: Next
222: 
223: 
224: OB "</freenats-data>"
225: 
226: Function AddTest(testname,testdesc,testvalue,alertlevel)
227:  OB "<test NAME=""" & testname & """>"
228:  OB " <name>" & testname & "</name>"
229:  OB " <desc>" & testdesc & "</desc>"
230:  OB " <value>" & testvalue & "</value>"
231:  OB " <alertlevel>" & alertlevel & "</alertlevel>"
232:  OB "</test>"
233: End Function
234: 
235: If PullData=1 Then
236: 	DumpOB()
237: End If
238: 
239: If PushData=1 Then
240: 	' Push Da Data
241: 	WScript.Echo
242: 	WScript.Echo "Pushing Data..."
243: 	WScript.Echo "URL   : " & PushURL
244: 	WScript.Echo "NodeID: " & NodeID
245: 	WScript.Echo "Key   : " & NodeKey
246: 	
247: 	Set objHTTP = CreateObject("Microsoft.XMLHTTP")
248: 	objHTTP.open "POST", PushURL, False
249: 	 
250: 	objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
251: 	http_request = "nodeid=" & Escape(NodeID) & "&nodekey=" & Escape(NodeKey) & "&xml=" & Escape(OutputBuffer)
252: 	objHTTP.send http_request
253: 	 
254: 	response = objHTTP.responseText
255: 	 
256: 	Set objHTTP = Nothing
257: 	
258: 	If Right(response,1) = "1" Then
259: 		WScript.Echo "Post Successful"
260: 	Else
261: 		WScript.Echo "Encountered An Error:"
262: 		WScript.Echo response
263: 	End If
264: 	
265: End If