File: 1.00.5a/node/win32/node.txt (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.02</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: For Each objDisk in colDisks
123: DiskSpace = objDisk.FreeSpace / 1024 / 1024
124: If DiskSpace = "" Then
125:  DiskSpace = "0"
126: End If
127:     TestName= "disk.free." & Left(objDisk.DeviceID,1)
128:     AddTest TestName,"Space Free On " & objDisk.DeviceID & " (Mb)", DiskSpace, 0
129: 
130: Next 
131: 
132: 
133: For Each objItem in objProcessor
134: 
135:     If objItem.Name = "_Total" Then
136:     	AddTest "proc.load","Processor Load (%)",objItem.PercentProcessorTime,0
137: 	End If
138: Next
139: 
140: 
141:  
142: For Each objOS in colOperatingSystems
143:     dtmBootup = objOS.LastBootUpTime
144:     dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
145:     dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
146:     AddTest "uptime","Uptime (Hours)",dtmSystemUptime,0
147: Next
148:  
149: Function WMIDateStringToDate(dtmBootup)
150:     WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
151:         Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
152:             & " " & Mid (dtmBootup, 9, 2) & ":" & _
153:                 Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup,13, 2))
154: End Function
155: 
156: 
157: 
158: 
159: For Each objItem in memItems
160: 	TestName="mem."
161: 	If Len(objItem.Name) > 0 Then
162: 		TestName = TestName & objItem.Name & "."
163: 	End If
164: 	TestName = TestName & "free"
165: 	
166: 	AddTest TestName,"Memory Available (Kilobytes)",objItem.AvailableKBytes,0
167: 
168: Next
169: 
170: 
171: OB "</freenats-data>"
172: 
173: Function AddTest(testname,testdesc,testvalue,alertlevel)
174:  OB "<test NAME=""" & testname & """>"
175:  OB " <name>" & testname & "</name>"
176:  OB " <desc>" & testdesc & "</desc>"
177:  OB " <value>" & testvalue & "</value>"
178:  OB " <alertlevel>" & alertlevel & "</alertlevel>"
179:  OB "</test>"
180: End Function
181: 
182: If PullData=1 Then
183: 	DumpOB()
184: End If
185: 
186: If PushData=1 Then
187: 	' Push Da Data
188: 	WScript.Echo
189: 	WScript.Echo "Pushing Data..."
190: 	WScript.Echo "URL   : " & PushURL
191: 	WScript.Echo "NodeID: " & NodeID
192: 	WScript.Echo "Key   : " & NodeKey
193: 	
194: 	Set objHTTP = CreateObject("Microsoft.XMLHTTP")
195: 	objHTTP.open "POST", PushURL, False
196: 	 
197: 	objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
198: 	http_request = "nodeid=" & Escape(NodeID) & "&nodekey=" & Escape(NodeKey) & "&xml=" & Escape(OutputBuffer)
199: 	objHTTP.send http_request
200: 	 
201: 	response = objHTTP.responseText
202: 	 
203: 	Set objHTTP = Nothing
204: 	
205: 	If Right(response,1) = "1" Then
206: 		WScript.Echo "Post Successful"
207: 	Else
208: 		WScript.Echo "Encountered An Error:"
209: 		WScript.Echo response
210: 	End If
211: 	
212: End If