1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
| ' **************************************************
' VBScript using Windows Management Instrumentation
' (WMI) and the High Performance (HiPerf) Performance
' Counters (RAW) Provider.
' **************************************************
' Collects Statistics from WMI enabled Windows 2000
' (NOT WinNT 4) systems, and formats the output for
' use in an MRTG environment
' **************************************************
' Copyright(c) 2001, 2002 - Garth K. Williams
' http://www.wtcs.org http://snmp4tpc.com
' Please respect my work, and don't claim this as yours!
' **************************************************
'Option Explicit
Dim oArgs, Zippo, FileName, VerNo, RelDate, CopyRight, ScrDesc, ScrDesc2, ScrDesc3, LogoSwitch, DriveLetter1, DriveLetter2, DriveSpace1, DriveSpace2, HostName, Value1, Value2, UpTime
Set sh = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Zippo = "0"
' ----------------------------------
' Script Constants
' ----------------------------------
FileName = "wmi_freedisk.vbs"
VerNo = "3.0"
RelDate = "25Mar2003"
CopyRight = "Copyright (c) 2003 - Garth K. Williams (WWW.WTCS.ORG)"
ScrDesc = "Collects Logical Disk Free Space (in MBytes) using"
ScrDesc2 = "Windows Management Instrumentation (WMI) calls."
ScrDesc3 = "This script will not run on Windows NT 4.0!"
' ----------------------------------
' ----------------------------------
' Debugging and Logging Options
' ----------------------------------
LogEvents = "0" ' Log to Application Event log (1=Yes, 0=No)
Debug2Console = "0" ' Write debug information to console (1=Yes, 0=No)
' ----------------------------------
' ----------------------------------
' User should be running CSCRIPT.EXE
' If not, FAIL SCRIPT, display usage
' ----------------------------------
If ScrEngine="wscript.exe" Then
msgbox "This program is designed to run using CSCRIPT.EXE. You are trying to run it using WSCRIPT.EXE." & vbCrlf & vbCrlf & "Usage: cscript //nologo " & FileName & " [HostName] [Drive1:] [Drive2:]" & vbCrlf & vbCrlf & "Version: " & VerNo & " " & "Date: " & RelDate & " " & CopyRight, 0, ""& FileName & " (MRTG Helper by WTCS.ORG) - Incorrect Script Engine"
wscript.quit
End If
' ----------------------------------
' User should pass valid HostName on command line
' Check for it. If no argument, FAIL SCRIPT, display usage
' ----------------------------------
Set oArgs=wscript.arguments
If wscript.arguments.Count < 3 Then ' User didn't specify arguments, so quit and display usage
wscript.echo "-------------------------------------------------------------------------"
wscript.echo FileName & " - " & "MRTG [Target]: Plug-in script"
wscript.echo "Version: " & VerNo & " - " & "Date: " & RelDate
wscript.echo CopyRight
wscript.echo "-------------------------------------------------------------------------"
wscript.echo ScrDesc
wscript.echo ScrDesc2
wscript.echo ScrDesc3
wscript.echo "-------------------------------------------------------------------------"
wscript.echo "Error: Incorrect syntax!"
wscript.echo "Usage: cscript //nologo " & FileName & " [HostName] [Drive1:] [Drive2:]"
wscript.echo "Ex. Target[xyz]: `cscript //nologo " & FileName & " 192.168.200.10 C: D:`"
wscript.echo "HostName: IP Address or NetBIOS host name of target system"
wscript.echo "Note 1: To monitor one disk, you MUST enter the SAME drive letter TWICE!"
wscript.echo "Note 2: You MUST have admin privileges on the target system"
wscript.echo "Note 3: You MUST use BACKTICKS (`) to surround the Target[xyz]: line"
wscript.echo "-------------------------------------------------------------------------"
wscript.echo "Return Value1 Return Value2 Return Value3 Return Value 4"
wscript.echo "Free MB (Disk1) Free MB (Disk2) Uptime Host Name"
wscript.echo "-------------------------------------------------------------------------"
wscript.quit (5) ' Terminate with "Invalid procedure call or argument" error
Else
HostName = oArgs.item(0)
DriveLetter1 = oArgs.item(1)
DriveLetter2 = oArgs.item(2)
' debug
If Debug2Console = 1 Then
wscript.echo "Debug: Detected HostName: " & HostName & " and Drive Letters: " & DriveLetter1 & ", " & DriveLetter2
End If
' wscript.quit 0
End If
strComputer = HostName
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' -------------------------------------
' Set the instantiation for LogicalDisk
' -------------------------------------
Set Win32Dsk1 = GetObject("winmgmts:{impersonationLevel=impersonate}!//"& HostName & "/root/cimv2:Win32_LogicalDisk.DeviceID=""" & DriveLetter1 & """")
Set Win32Dsk2 = GetObject("winmgmts:{impersonationLevel=impersonate}!//"& HostName & "/root/cimv2:Win32_LogicalDisk.DeviceID=""" & DriveLetter2 & """")
If Debug2Console = 1 Then
wscript.echo "Debug: Calling GetStat subroutine, passing Drive Letter: " & DriveLetter
End If
Call GetStat(DriveLetter)
If Debug2Console = 1 Then
wscript.echo "Debug: Returned successfully from GetStat subroutine."
End If
'
Uptime = "unknown"
' **************************************
' Output in MRTG format
' **************************************
If Debug2Console = 1 Then
wscript.echo "Debug: Output Data."
End If
DriveSpace1 = DriveSpace1/1024
DriveSpace2 = DriveSpace2/1024
DriveSpace1 = CLng (DriveSpace1)
DriveSpace2 = Clng (DriveSpace2)
wscript.echo DriveSpace1
wscript.echo DriveSpace2
wscript.echo UpTime
wscript.echo HostName
If LogEvents = 1 Then
sh.LogEvent 4, "wmi_dskstats.vbs processing complete. Host: " & HostName & ", StatID: " & StatID ' Log INFORMATION to App Event Log
End If
wscript.quit
' **************************************
' SubRoutine to collect the stats
' **************************************
Private Sub GetStat(DriveLetter)
If Debug2Console = 1 Then
wscript.echo "Debug: In GetStat subroutine, getting Disk stats for: " & DriveLetter
End If
DriveSpace1 = Win32Dsk1.FreeSpace
DriveSpace2 = Win32Dsk2.FreeSpace
End Sub
' **************************************
Sub OnError()
' **************************************
' Error processing Subroutine
If Debug2Console = 1 Then
wscript.echo "Debug: In Error Processing Subroutine."
End If
Wscript.echo "Error Number: " & Cstr(Err.Number) & vbCrlf & "Description: " & Err.Description
If LogEvents = 1 Then
sh.LogEvent 1, "wmi_dskstats.vbs processing failed!. Host: " & HostName & ", Drive Letter: " & DriveLetter & vbCrlf & "Error Number: " & Cstr(Err.Number) & ", Description: " & Err.Description & vbCrlf & "Error Code: " & Cstr(Err.Number) & ", Description: " & Err.Description ' Log INFORMATION to App Event Log
End If
wscript.quit
End Sub
' **************************************
Function ScrEngine()
' **************************************
' Check Script Engine Type Function
If Debug2Console = 1 Then
wscript.echo "Debug: In Script Engine Check Routine."
End If
On Error Resume Next
EngineName=Wscript.FullName
If Err.Number <>0 Then
wscript.echo "Error!"
wscript.echo "Error (" & Err.Number & ") Description: " & Err.Description
wscript.quit
End If
PosX=InStrRev(EngineName,"\",-1,vbTextCompare)
ScrEngine=LCase(Mid(EngineName,PosX+1))
End Function
' **************************************
Function MakeNiceTime (LastBootUpTime)
' **************************************
' Now, call function to parse WMILastBootUpTime
' to recreate a "friendly" Date variable
' (i.e. 5/3/2001 10:36:15 PM) - returns MakeNiceTime
' **************************************
If Debug2Console = 1 Then
wscript.echo "Debug: In 'MakeNiceTime' function."
End If
Strip1 = Left(LastBoot,14)
YearIs = Left(Strip1,4)
Strip2 = Right(Strip1,10)
MonthIs = Left(Strip2,2)
Strip3 = Right(Strip2,8)
DayIs = Left(Strip3,2)
Strip4 = Right(Strip3,6)
HourIs = Left(Strip4,2)
Strip5 = Right(Strip4,4)
MinIs = Left(Strip5,2)
Strip6 = Right(Strip5,2)
SecIs = Left(Strip6,2)
' Accomodate for LastBootUpTime (GMT) showing 10:52:XX AM when
' REAL time (PST) was was 17:52:XX PM, a 7 hour difference
' Depending on the Time Zone, this number will need to be changed
HourIs = HourIs + 7
If Houris > 24 Then
HourIs = HourIs - 24
DayIs = DayIs + 1
End If
' Determine AM or PM
If HourIs < 12 Then
AmPm = "AM"
End If
If HourIs > 12 Then
AmPm = "PM"
End If
' Create format like now()
MakeNiceTime = MonthIs & "/" & DayIs & "/" & YearIs & " " & HourIs & ":" & MinIs & ":" & SecIs & " " & AmPm
End Function
' **************************************
Function GetTime (Seconds)
' **************************************
' Based on a script from Mergatroid
' from www.cool.co.za
' Modified to calculate number of days
' and hours from total hours, and
' format different message return types
' Returns GetTime
' **************************************
If Debug2Console = 1 Then
wscript.echo "Debug: In 'GetTime' Function."
End If
' Need to create a temporary variable for calcs
intSec = Seconds
' Extract the different time parts from the seconds
HOURS = intSec\3600
MINUTES = (intSec Mod 3600)\60
SECONDS = (intSec Mod 3600) Mod 60
' Calc number of days and hours from total hours
DAYS = HOURS\24
HOURCALC = DAYS * 24
HOURDIFF = HOURS - HOURCALC
' Prefix a "0" to each if less than 10 (i.e. 05)
If DAYS < 10 Then
DAYS = "0" & DAYS
End If
If HOURDIFF < 10 Then
HOURDIFF = "0" & HOURDIFF
End If
If MINUTES < 10 Then
MINUTES = "0" & MINUTES
End If
If SECONDS < 10 Then
SECONDS = "0" & SECONDS
End If
' Finally, select your return format below
' GetTime = DAYS & ":" & HOURDIFF & ":" & MINUTES & ":" & SECONDS
GetTime = DAYS & " Day(s), " & HOURDIFF & " Hour(s), " & MINUTES & " Minute(s) and " & SECONDS & " Second(s)"
End Function |