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