-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcheck_process_mem.vbs
180 lines (130 loc) · 6.72 KB
/
check_process_mem.vbs
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
'// Adapted for Nagio Check Memory by Felipe Ferreira
Option Explicit
Const retvalUnknown = 1
Dim SYSDATA, SYSEXPLANATION
Dim b, strService, intMaxMem
Dim x, intSleep,qry, objWMIService, colListOfServices, obj, objService, strComputer ' for restarting function
if not ( WScript.Arguments.Count = 2 ) then
WScript.Echo "Missing parameters: <process name> <Max Memory MB usage>"
Wscript.quit 0
end if
strComputer = "."
strService = Wscript.Arguments(0)
intMaxMem = Wscript.Arguments(1)
b = CheckProcessMemory( "localhost", "", strService, intMaxMem )
if ( b = True ) then
WScript.Echo "OK - Process" & StrService & " " & SYSEXPLANATION & " |mem=" & SYSDATA
Wscript.quit 0
else
WScript.Echo "CRITICAL - Service has been restarted " & SYSEXPLANATION & " |mem=" & SYSDATA
'RESTART SERVICE HERE
RestartService( StrService )
Wscript.quit 2
end if
' //////////////////////////////////////////////////////////////////////////////
Function RestartService(strService)
intSleep = 7
'cleanup service name if has .exe
if instr(strService, ".exe") then
strService = Left(strService, Len(strService) - 4)
end if
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
qry = "SELECT * FROM Win32_Service WHERE Name='" & strService & "'"
Set colListOfServices = objWMIService.ExecQuery (qry)
For Each objService in colListOfServices
'WScript.Echo "Your "& strService & " service has been stoped"
objService.StopService()
WSCript.Sleep intSleep
'Set obj = CreateObject("Scripting.FileSystemObject") 'Calls the File System Object
'obj.DeleteFile("D:\MosaicoWeb\at5mosaicoweb\webapps\FileSupply\temp\*.* ") 'Deletes the file throught the DeleteFile function
objService.StartService()
'WScript.Echo "Your "& strService & " service has been started"
Next
'WScript.Echo "Your "& strService & " service has been restarted"
end Function
Function CheckProcessMemory( strComputer, strCredentials, strProcessName, nMaxMB )
Dim objWMIService
CheckProcessMemory = retvalUnknown ' Default return value
SYSDATA = "" ' Will store the number of MBs used by the process
SYSEXPLANATION = "" ' Set initial value
If( Not getWMIObject( strComputer, strCredentials, objWMIService, SYSEXPLANATION ) ) Then
Exit Function
End If
CheckProcessMemory = checkProcessMemoryWMI( objWMIService, strComputer, strProcessName, nMaxMB, SYSDATA, SYSEXPLANATION )
End Function ' //////////////////////////////////////////////////////////////////////////////
Function checkProcessMemoryWMI( objWMIService, strComputer, strProcessName, nMaxMB, BYREF strSysData, BYREF strSysExplanation )
Dim objProcess, colProcesses, nDiff
checkProcessMemoryWMI = retvalUnknown ' Default return value
On Error Resume Next
Set colProcesses = objWMIService.ExecQuery( "Select * from Win32_Process" )
If( Err.Number <> 0 ) Then
strSysData = ""
strSysExplanation = "Unable to query WMI on computer [" & strComputer & "]"
Exit Function
End If
If( colProcesses.Count <= 0 ) Then
strSysData = ""
strSysExplanation = "Win32_Process class does not exist on computer [" & strComputer & "]"
Exit Function
End If
On Error Goto 0
For Each objProcess in colProcesses
If( Err.Number <> 0 ) Then
checkProcessMemoryWMI = retvalUnknown
strSysExplanation = "Unable to list processes on computer [" & strComputer & "]"
Exit Function
End If
If UCase( objProcess.Name )= UCase( strProcessName ) Then
nDiff = nMaxMB - CInt( objProcess.WorkingSetSize / ( 1024 * 1024 ) )
strSysData = CInt( objProcess.WorkingSetSize / ( 1024 * 1024 ) )
strSysExplanation = "Memory usage=[" & CInt( objProcess.WorkingSetSize / ( 1024 * 1024 ) ) & "MB], maximum allowed=[" & nMaxMB & " MB]"
If nDiff >= 0 then
checkProcessMemoryWMI = True
Else
checkProcessMemoryWMI = False
End if
Exit Function
End If
Next
checkProcessMemoryWMI = retvalUnknown
strSysExplanation = "Process [" & strProcessName & "] is not running on computer [" & strComputer & "]"
End Function
' //////////////////////////////////////////////////////////////////////////////
' //////////////////////////////////////////////////////////////////////////////
Function getWMIObject( strComputer, strCredentials, BYREF objWMIService, BYREF strSysExplanation )
On Error Resume Next
Dim objNMServerCredentials, objSWbemLocator, colItems
Dim strUsername, strPassword
getWMIObject = False
Set objWMIService = Nothing
If( strCredentials = "" ) Then
' Connect to remote host on same domain using same security context
Set objWMIService = GetObject( "winmgmts:{impersonationLevel=Impersonate}!\\" & strComputer &"\root\cimv2" )
Else
Set objNMServerCredentials = CreateObject( "ActiveXperts.NMServerCredentials" )
strUsername = objNMServerCredentials.GetLogin( strCredentials )
strPassword = objNMServerCredentials.GetPassword( strCredentials )
If( strUsername = "" ) Then
getWMIObject = False
strSysExplanation = "No alternate credentials defined for [" & strCredentials & "]. In the Manager application, select 'Options' from the 'Tools' menu and select the 'Server Credentials' tab to enter alternate credentials"
Exit Function
End If
' Connect to remote host using different security context and/or different domain
Set objSWbemLocator = CreateObject( "WbemScripting.SWbemLocator" )
Set objWMIService = objSWbemLocator.ConnectServer( strComputer, "root\cimv2", strUsername, strPassword )
If( Err.Number <> 0 ) Then
objWMIService = Nothing
getWMIObject = False
strSysExplanation = "Unable to access [" & strComputer & "]. Possible reasons: WMI not running on the remote server, Windows firewall is blocking WMI calls, insufficient rights, or remote server down"
Exit Function
End If
objWMIService.Security_.ImpersonationLevel = 3
End If
If( Err.Number <> 0 ) Then
objWMIService = Nothing
getWMIObject = False
strSysExplanation = "Unable to access '" & strComputer & "'. Possible reasons: no WMI installed on the remote server, no rights to access remote WMI service, or remote server down"
Exit Function
End If
getWMIObject = True
End Function