coll_process.vbs
5.64 KB
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
Option Explicit
Dim strArguments
Dim strKBP_ID
Dim strComputer
Dim strUser
Dim strPassword
Dim strFunction
Dim ipAddr
Dim objLocator
Dim objWMIService
Dim objWMISQLService
Dim colItems
Dim processItems
Dim objItem
Dim proItem
Dim cntproperty
Dim FS
Dim OutStream
Dim strFunctionNum
Dim intFunctionNum
Dim i
Dim strSubKBP_ID
Dim strKPI_ID
Dim strClassName
Dim strPropertyName
Dim strKPI_VALUE
Dim strCalType
Dim strTime
Dim Pos
Dim strProcessName
Dim strDatabaseServer
Dim ScriptName
Dim Path
Dim machine_name
Dim logicdisknum
Dim logicdiskname
Dim kpi_machine_name
Dim total
Dim cpu
Dim memory
Dim disk
Dim log
On Error Resume Next
For i = 0 To WScript.Arguments.Count-1
strArguments = WScript.Arguments(i)
Pos = InStr(1,strArguments,"=",1)
If (Mid(strArguments,1,Pos-1)="IP_ADDR") Then
strComputer = Mid(strArguments,Pos+1,Len(strArguments))
End If
If (Mid(strArguments,1,Pos-1)="USER_NAME") Then
strUser= Mid(strArguments,Pos+1,Len(strArguments))
End If
If (Mid(strArguments,1,Pos-1)="PASSWORD") Then
strPassword = Mid(strArguments,Pos+1,Len(strArguments))
End If
If (Mid(strArguments,1,Pos-1)="PROCESS_NAME") Then
strProcessName = Mid(strArguments,Pos+1,Len(strArguments))
End If
If (Mid(strArguments,1,Pos-1)="UNIT_ID") Then
UNIT_ID = Mid(strArguments,Pos+1,Len(strArguments))
End If
If (Mid(strArguments,1,Pos-1)="KBP_ID") Then
strKBP_ID = Mid(strArguments,Pos+1,Len(strArguments))
End If
If (Mid(strArguments,1,Pos-1)="IP_ADDR") Then
ipAddr = Mid(strArguments,Pos+1,Len(strArguments))
End If
Next
if strKBP_ID ="" Then
strKBP_ID ="10-10-23"
End If
If strComputer="." Then
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Else
Set objLocator = CreateObject( "WbemScripting.SWbemLocator" )
Set objWMIService = objLocator.ConnectServer ()
objWMIService.Security_.impersonationlevel = 3
End If
If(strComputer=".") Then
strComputer = "local"
End If
Set FS = CreateObject("Scripting.FileSystemObject")
strTime = CStr(Now())
strTime = Replace(strTime, " ", "")
strTime = Replace(strTime, "-", "")
strTime = Replace(strTime, ":", "")
'MsgBox strTime
ScriptName = WScript.ScriptFullName
Path = Left(ScriptName, InstrRev(ScriptName, "\bin"))
Set OutStream = FS.OpenTextFile(Path&"D:\jx_nms\data\process.txt", 2, True)
'For i = 0 To WScript.Arguments.Count-1
WScript.echo "--------------"&WScript.Arguments(i)
strArguments = WScript.Arguments(i)
Pos = InStr(1,strArguments,"=",1)
strProcessName = "javaw.exe"
'进程名称
OutStream.Write strKBP_ID & "#" & "PM-00-06-001-90" & "#" & Now() & "#" & strProcessName & vbcrlf
'进程运行路径
strKPI_VALUE = getProcessPath(strProcessName)
OutStream.Write strKBP_ID & "#" & "PM-00-06-001-91" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'进程运行状态
strKPI_VALUE = getProcessStatus(strProcessName)
OutStream.Write strKBP_ID & "#" & "PM-00-06-001-92" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'进程占用CPU百分比
strKPI_VALUE = getProcessCPUTime(strProcessName)
OutStream.Write strKBP_ID & "#" & "PM-00-06-001-01" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'进程占用内存
strKPI_VALUE = getProcessUsedMemory(strProcessName)
OutStream.Write strKBP_ID & "#" & "PM-00-06-001-02" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'Next
'进程名称
Function getProcessName(ByVal ProcessName)
Dim Name
Set colItems = objWMIService.ExecQuery( "Select * from Win32_Process where Name = '" & ProcessName & "'")
For each objItem in colItems
Name = objItem.Name
Next
getProcessName = Name
End Function
'进程运行路径
Function getProcessPath(ByVal ProcessName)
Dim Path
Set colItems = objWMIService.ExecQuery( "Select * from Win32_Process where Name = '" & ProcessName & "'")
For each objItem in colItems
Path = objItem.ExecutablePath
Next
getProcessPath = Path
End Function
'进程数量
Function getProcessNum()
Dim Num
Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem " )
For each objItem in colItems
Num = objItem.NumberOfProcesses
Next
getProcessNum = Num
End Function
'进程运行状态
Function getProcessStatus(ByVal ProcessName)
Dim Status
Status = "DOWN"
Set colItems = objWMIService.ExecQuery( "Select * from Win32_Process where Name = '" & ProcessName & "'")
If colItems.Count > 0 Then
Status = "UP"
End If
getProcessStatus = Status
End Function
'进程占用CPU利用率
Function getProcessCPUTime(ByVal ProcessName)
Dim TotalKernel
Dim TotalUser
Dim Total
Dim Idle
TotalKernel = 0
TotalUser = 0
Total = 0
Idle = 0
Set colItems = objWMIService.ExecQuery( "Select * from Win32_Process" )
For each objItem in colItems
TotalKernel = TotalKernel + objItem.KernelModeTime
TotalUser = TotalUser + objItem.UserModeTime
Next
Total = TotalKernel + TotalUser
Dim KernelTime
Dim UserTime
KernelTime = 0
UserTime = 0
Set processItems = objWMIService.ExecQuery( "Select * from Win32_Process where Name = '" & ProcessName & "'")
For each proItem in processItems
KernelTime = KernelTime + proItem.KernelModeTime
UserTime = UserTime + proItem.UserModeTime
Next
getProcessCPUTime = ROUND((CDBL(KernelTime) + CDBL(UserTime)) / CDBL(Total) * CDBL(100),2)
End Function
'进程占用内存百分比
Function getProcessUsedMemory(ByVal ProcessName)
Dim MemorySize
Set colItems = objWMIService.ExecQuery( "Select * from Win32_Process where Name = '" & ProcessName & "'")
For each objItem in colItems
MemorySize = objItem.PeakWorkingSetSize
Next
Dim Capability
Set colItems = objWMIService.ExecQuery( "Select * from Win32_LogicalMemoryConfiguration")
For each objItem in colItems
Capability = objItem.TotalPhysicalMemory
Next
getProcessUsedMemory = ROUND(CDBL(MemorySize) / (CDBL(Capability) * CDBL(1000)) * CDBL(100),2)
End Function