collwindows.vbs
28.3 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
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 objItem
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&"data\local.txt", 2, True)
'-----------------------扩展指标-----------------
'1.系统描述 CM-00-01-999-01
'2.主机位置 CM-00-01-999-02
'3.主机运行时间 CM-00-01-999-03
'4.主机系统时间 CM-00-01-999-04
'5.硬盘数量 CM-00-01-999-05
'6.硬盘分区大小 CM-00-01-999-06
'7.硬盘各分区英文描述 CM-00-01-999-07
'8.硬盘各分区中文描述 CM-00-01-999-08
'9.硬盘总空间 CM-00-01-999-09
'10.硬盘总使用空间 CM-00-01-999-10
'11.硬盘未使用空间 CM-00-01-999-11
'12.剩余内存空间 PM-00-01-999-12
'13.已使用内容 PM-00-01-999-13
'
'If strFunction ="getHostConfig" Then
'主机名称
kpi_machine_name=getComputerName()
machine_name = replace(kpi_machine_name,"-","_")
total = strKBP_ID + "-10:" + machine_name + "-total"
cpu = strKBP_ID + "-11:" + machine_name
memory = strKBP_ID + "-12:" + machine_name
disk = strKBP_ID + "-13:" + machine_name
log = strKBP_ID + "-20:" + machine_name
'主机IP,用于告警
OutStream.Write total & "#" & "CM-00-01-001-50" & "#" & "主机IP:" & "#" & Now() & "#" & ipAddr & vbcrlf
'主机名称
OutStream.Write total & "#" & "CM-00-01-001-01" & "#" & "主机名称:" & "#" & Now() & "#" & kpi_machine_name & vbcrlf
'主机类型
OutStream.Write total & "#" & "CM-00-01-001-03" & "#" & "主机类型:" & "#" & Now() & "#" & "Windows" & vbcrlf
'IP地址
OutStream.Write total & "#" & "CM-00-01-001-50" & "#" & "主机IP:" & "#" & Now() & "#" & strComputer & vbcrlf
'系统描述
strKPI_VALUE = getComputerSystemDescription()
OutStream.Write total & "#" & "CM-00-01-999-01" & "#" & "系统描述:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'操作系统
strKPI_VALUE = getOperatingSystem()
OutStream.Write total & "#" & "CM-00-01-001-08" & "#" & "操作系统:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'主机位置
strKPI_VALUE = getComputerDomain()
OutStream.Write total & "#" & "CM-00-01-999-02" & "#" & "主机位置: " & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'主机状态
strKPI_VALUE = getComputerStatus()
'OutStream.Write strKBP_ID & "#" & "主机状态:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'主机运行时间
strKPI_VALUE = getRunningTime()
OutStream.Write total & "#" & "CM-00-01-999-03" & "#" & "主机运行时间:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'主机系统时间
strKPI_VALUE = getComputerTime()
OutStream.Write total & "#" & "CM-00-01-999-04" & "#" & "主机系统时间:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'系统日志采集
'getEvent
'End If
'If strFunction ="getDisk" Then
'硬盘数量
strKPI_VALUE = getDiskDriverNumber()
OutStream.Write total & "#" & "CM-00-01-999-05" & "#" & "硬盘数量:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'硬盘容量
strKPI_VALUE = getDiskDriverCapability()
OutStream.Write total & "#" & "CM-00-01-001-09" & "#" & "硬盘容量:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'硬盘分区数量
strKPI_VALUE = getDiskDriverPartitions()
logicdisknum = strKPI_VALUE
OutStream.Write total & "#" & "CM-00-01-999-06" & "#" & "硬盘分区数量:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'硬盘分区描述1
logicdiskname = getDeviceID()
strKPI_VALUE = getDeviceID()
OutStream.Write total & "#" & "CM-00-01-999-07" & "#" & "硬盘分区描述1:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'硬盘分区描述
strKPI_VALUE = getLogicalDiskDescription()
'OutStream.Write total & "#" & "CM-00-01-999-08" & "#" & "硬盘分区描述:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'硬盘分区大小
strKPI_VALUE = getLogicalDiskCapability()
OutStream.Write total & "#" & "CM-00-01-999-09" & "#" & "硬盘分区大小:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'硬盘分区已使用空间
strKPI_VALUE = getLogicalDiskUsedSpace()
OutStream.Write total & "#" & "CM-00-01-999-10" & "#" & "硬盘分区已使用空间:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'硬盘未使用空间
strKPI_VALUE = getLogicalDiskFreeSpace()
OutStream.Write total & "#" & "CM-00-01-999-11" & "#" & "硬盘未使用空间:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'硬盘各个分区利用率
getDiskUsedRate()
'strKPI_VALUE = getCDiskUsedRate()
'OutStream.Write strKBP_ID & "#" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'End If
'If strFunction ="getCPU" Then
'CPU数量
strKPI_VALUE = getProcessorNumber()
OutStream.Write total & "#" & "CM-00-01-001-04" & "#" & "CPU数量:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'WScript.echo strKBP_ID & "#" & "CM-10-50-10-10" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'CPU描述
strKPI_VALUE = getProcessorDescription()
OutStream.Write total & "#" & "CM-00-01-001-05" & "#" &"CPU描述:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'CPU使用率
strKPI_VALUE = getProcessorUsage()
OutStream.Write cpu + "-cpu" & "#" & "PM-00-01-001-05" & "#" & "CPU使用率:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'End If
'If strFunction ="getMem" Then
'内存容量
strKPI_VALUE = getTotalPhysicalMemory()
OutStream.Write total & "#" & "CM-00-01-001-07" & "#" & "内存容量:" & "#" & Now() & "#" & round(strKPI_VALUE/1024,3) & vbcrlf
'交换分区大小
strKPI_VALUE = getTotalVirtualMemory()
OutStream.Write total & "#" & "CM-00-01-001-13" & "#" & "交换分区大小:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'剩余内存容量
strKPI_VALUE = getFreePhysicalMemory()
OutStream.Write memory & "-memory" & "#" & "PM-00-01-999-01" & "#" & "剩余内存容量:" & "#" & Now() & "#" & round(strKPI_VALUE/1024/1024,3) & vbcrlf
'已使用内存
strKPI_VALUE = getUsedPhysicalMemory()
OutStream.Write memory & "-memory" & "#" & "PM-00-01-999-02" & "#" & "已使用内存:" & "#" & Now() & "#" & round(strKPI_VALUE/1024/1024,3) & vbcrlf
'内存使用率
strKPI_VALUE = getMemoryUsage()
OutStream.Write memory & "-memory" & "#" & "PM-00-01-002-01" & "#" & "内存使用率:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'交换分区已使用空间
strKPI_VALUE = getUsedVirtualMemory()
OutStream.Write memory & "-memory" & "#" & "PM-00-01-999-03" & "#" & "交换分区已使用空间:" & "#" & Now() & "#" & round(strKPI_VALUE/1024/1024,3) & vbcrlf
'交换分区剩余空间
strKPI_VALUE = getAvailableVirtualMemory()
OutStream.Write memory & "-memory" & "#" & "PM-00-01-999-04" & "#" & "交换分区剩余空间:" & "#" & Now() & "#" & round(strKPI_VALUE/1024/1024,3) & vbcrlf
'交换分区利用率
strKPI_VALUE = getVirtualMemoryUsage()
OutStream.Write memory & "-memory" & "#" & "FM-00-01-002-04" & "#" & "交换分区利用率:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'End If
'If strFunction ="getProcess" Then
'进程数量
strKPI_VALUE = getProcessNum()
OutStream.Write total & "#" & "PM-00-01-005-05" & "#" & "进程总数量" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'End If
For i = 0 To WScript.Arguments.Count-1
'WScript.echo "--------------"&WScript.Arguments(i)
strArguments = WScript.Arguments(i)
Pos = InStr(1,strArguments,"=",1)
If (Mid(strArguments,1,Pos-1)="PROCESS_NAME") Then
strProcessName = Mid(strArguments,Pos+1,Len(strArguments))
OutStream.Write "-------------------------------------------------------------------------------" & vbcrlf
'If strFunction ="getProcessDetail" Then
'进程名称
OutStream.Write strKBP_ID & "#" & "进程名称:" & "#" & Now() & "#" & strProcessName & vbcrlf
'进程运行路径
strKPI_VALUE = getProcessPath(strProcessName)
OutStream.Write strKBP_ID & "#" & "进程运行路径:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'进程参数
'进程运行级别
'进程运行状态
strKPI_VALUE = getProcessStatus(strProcessName)
OutStream.Write strKBP_ID & "#" & "进程运行状态:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'进程占用CPU时间
strKPI_VALUE = getProcessCPUTime(strProcessName)
OutStream.Write strKBP_ID & "#" & "进程占用CPU时间:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'进程占用内存
strKPI_VALUE = getProcessUsedMemory(strProcessName)
OutStream.Write strKBP_ID & "#" & "进程占用内存:" & "#" & Now() & "#" & strKPI_VALUE & vbcrlf
'End If
End If
Next
'系统描述
Function getComputerSystemDescription()
Dim ProcessorDescription
Dim ComputerDescription
Dim OperatingDescription
Set colItems = objWMIService.ExecQuery( "Select Description from Win32_Processor" )
For each objItem in colItems
ProcessorDescription = objItem.Description
Next
Set colItems = objWMIService.ExecQuery( "Select Description from Win32_ComputerSystem" )
For each objItem in colItems
ComputerDescription = objItem.Description
Next
Set colItems = objWMIService.ExecQuery( "Select Description from Win32_OperatingSystem" )
For each objItem in colItems
OperatingDescription = objItem.Description
Next
getComputerSystemDescription = "Hardware"& ProcessorDescription&",ComputerDescription-Software" & OperatingDescription
End Function
'主机名称
Function getComputerName()
Dim ComputerName
Set colItems = objWMIService.ExecQuery( "Select Name from Win32_ComputerSystem" )
For each objItem in colItems
ComputerName = objItem.Name
Next
getComputerName = ComputerName
End Function
'操作系统
Function getOperatingSystem()
Dim OperatingSystem
Set colItems = objWMIService.ExecQuery( "Select Caption from Win32_OperatingSystem" )
For each objItem in colItems
OperatingSystem = objItem.Caption
Next
getOperatingSystem = OperatingSystem
End Function
'主机位置
Function getComputerDomain()
Dim Domain
Set colItems = objWMIService.ExecQuery( "Select Domain from Win32_ComputerSystem" )
For each objItem in colItems
Domain = objItem.Domain
Next
getComputerDomain = Domain
End Function
'主机状态
Function getComputerStatus()
Dim Status
Set colItems = objWMIService.ExecQuery( "Select Status from Win32_ComputerSystem" )
For each objItem in colItems
Status = objItem.Status
Next
getComputerStatus = Status
End Function
'主机运行时间
Function getRunningTime()
Dim KernelModeTime
Set colItems = objWMIService.ExecQuery( "Select KernelModeTime from Win32_Process where ProcessId=0" )
For each objItem in colItems
KernelModeTime = objItem.KernelModeTime
Next
getRunningTime = ROUND(KernelModeTime/10000000,0)
End Function
'主机系统时间
Function getComputerTime()
Dim LocalDateTime
Dim strTime
strTime = ""
Set colItems = objWMIService.ExecQuery( "Select LocalDateTime from Win32_OperatingSystem " )
For each objItem in colItems
LocalDateTime = objItem.LocalDateTime
Next
getComputerTime = Mid(CStr(LocalDateTime),1,14)
End Function
'CPU数量
Function getProcessorNumber()
Dim Number
Set colItems = objWMIService.ExecQuery( "Select NumberOfProcessors from Win32_ComputerSystem" )
For each objItem in colItems
Number = objItem.NumberOfProcessors
Next
getProcessorNumber = Number
End Function
'CPU描述
Function getProcessorDescription()
Dim Description
Set colItems = objWMIService.ExecQuery( "Select * from Win32_Processor" )
For each objItem in colItems
Description = Description & objItem.Name & " " & objItem.Description & ","
Next
getProcessorDescription = Description
End Function
'CPU使用率
Function getProcessorUsage()
Dim UsedRate
Dim TotalKernel
Dim TotalUser
Dim Total
Dim Idle
TotalKernel = 0
TotalUser = 0
Idle = 0
Total = 0
Set colItems = objWMIService.ExecQuery( "Select * from Win32_Process" )
For each objItem in colItems
TotalKernel = TotalKernel + objItem.KernelModeTime
TotalUser = TotalUser + objItem.UserModeTime
If objItem.ProcessId = 0 Then Idle = objItem.KernelModeTime
Next
Total = TotalKernel + TotalUser
getProcessorUsage = ROUND((Total - Idle) / CDBL(Total) * CDBL(100),2)
End Function
'进程名称
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 KernelTime
Dim UserTime
Set colItems = objWMIService.ExecQuery( "Select * from Win32_Process where Name = '" & ProcessName & "'")
For each objItem in colItems
KernelTime = objItem.KernelModeTime
UserTime = objItem.UserModeTime
Next
getProcessCPUTime = ROUND( (KernelTime)/10000000,0) + ROUND( (UserTime)/10000000,0)
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
getProcessUsedMemory = MemorySize
End Function
'物理内存容量
Function getTotalPhysicalMemory()
Dim Capability
Set colItems = objWMIService.ExecQuery( "Select * from Win32_LogicalMemoryConfiguration")
For each objItem in colItems
Capability = objItem.TotalPhysicalMemory
Next
getTotalPhysicalMemory = Capability
End Function
'虚拟内存容量
Function getTotalVirtualMemory()
Dim TotalVirtualMemory
Set colItems = objWMIService.ExecQuery( "Select TotalVirtualMemory from Win32_LogicalMemoryConfiguration")
For each objItem in colItems
TotalVirtualMemory = objItem.TotalVirtualMemory
Next
getTotalVirtualMemory = TotalVirtualMemory
End Function
'已使用虚拟内存容量
Function getUsedVirtualMemory()
Dim UsedVirtualMemory
UsedVirtualMemory = getTotalVirtualMemory() - getAvailableVirtualMemory()
getUsedVirtualMemory = UsedVirtualMemory
End Function
'剩余虚拟内存容量
Function getAvailableVirtualMemory()
Dim AvailableVirtualMemory
Set colItems = objWMIService.ExecQuery( "Select AvailableVirtualMemory from Win32_LogicalMemoryConfiguration")
For each objItem in colItems
AvailableVirtualMemory = objItem.AvailableVirtualMemory
Next
getAvailableVirtualMemory = AvailableVirtualMemory
End Function
'虚拟内存使用率
Function getVirtualMemoryUsage()
Dim TotalVirtualMemory
Dim AvailableVirtualMemory
Dim UsedVirtualMemory
TotalVirtualMemory = getTotalVirtualMemory()
AvailableVirtualMemory = getAvailableVirtualMemory()
UsedVirtualMemory = TotalVirtualMemory - AvailableVirtualMemory
getVirtualMemoryUsage = ROUND(CDBL(UsedVirtualMemory) / CDBL(TotalVirtualMemory) * CDBL(100),0)
End Function
'剩余内存容量
Function getFreePhysicalMemory()
Dim FreePhysicalMemory
Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem")
For each objItem in colItems
FreePhysicalMemory = objItem.FreePhysicalMemory
Next
getFreePhysicalMemory = FreePhysicalMemory
End Function
'已使用内存
Function getUsedPhysicalMemory()
Dim TotalPhysicalMemory
Dim FreePhysicalMemory
TotalPhysicalMemory = getTotalPhysicalMemory()
FreePhysicalMemory = getFreePhysicalMemory()
getUsedPhysicalMemory = TotalPhysicalMemory - FreePhysicalMemory
End Function
'内存使用率
Function getMemoryUsage()
Dim TotalPhysicalMemory
Dim FreePhysicalMemory
Dim UsedPhysicalMemory
TotalPhysicalMemory = getTotalPhysicalMemory()
FreePhysicalMemory = getFreePhysicalMemory()
UsedPhysicalMemory = TotalPhysicalMemory - FreePhysicalMemory
getMemoryUsage = ROUND(CDBL(UsedPhysicalMemory) / CDBL(TotalPhysicalMemory) * CDBL(100),2)
End Function
'硬盘数量
Function getDiskDriverNumber()
Dim Number
Dim Index
Number = 0
Set colItems = objWMIService.ExecQuery( "Select Index from Win32_DiskDrive" )
For each objItem in colItems
Index = objItem.Index
If (Index + 1) > Number Then Number = Index + 1
Next
getDiskDriverNumber = Number
End Function
'硬盘容量
Function getDiskDriverCapability()
Dim Capability
Capability = 0
Set colItems = objWMIService.ExecQuery( "Select Size from Win32_DiskDrive " )
For each objItem in colItems
Capability = Capability + objItem.Size
Next
getDiskDriverCapability = Round(Capability/1024/1024/1024,3)
End Function
'硬盘分区数量
Function getDiskDriverPartitions()
Dim Partitions
Partitions = 0
Set colItems = objWMIService.ExecQuery( "Select Partitions from Win32_DiskDrive" )
For each objItem in colItems
if IsNull(objItem.Partitions) or IsEmpty(objItem.Partitions) or len(objItem.Partitions)=0 then
Partitions = Partitions + 0
Else
Partitions = Partitions + objItem.Partitions
End If
Next
getDiskDriverPartitions = Partitions
'MsgBox getDiskDriverPartitions
End Function
'硬盘分区大小
Function getLogicalDiskCapability()
Dim Capability
Capability = 0
Set colItems = objWMIService.ExecQuery( "Select Size from Win32_LogicalDisk where MediaType='12'" )
For each objItem in colItems
If objItem.Size <> "" Then Capability = Capability + objItem.Size
Next
getLogicalDiskCapability = Round(Capability/1024/1024/1024,3)
End Function
'硬盘未使用空间
Function getLogicalDiskFreeSpace()
Dim FreeSpace
FreeSpace = 0
Set colItems = objWMIService.ExecQuery( "Select FreeSpace from Win32_LogicalDisk where MediaType='12'" )
For each objItem in colItems
If objItem.FreeSpace <> "" Then FreeSpace = FreeSpace + objItem.FreeSpace
Next
getLogicalDiskFreeSpace = Round(FreeSpace/1024/1024/1024,3)
End Function
'硬盘分区已使用空间
Function getLogicalDiskUsedSpace()
Dim UsedSpace
UsedSpace = 0
UsedSpace = getLogicalDiskCapability() - getLogicalDiskFreeSpace
getLogicalDiskUsedSpace = UsedSpace
End Function
'硬盘C分区利用率
Function getCDiskUsedRate()
Dim UsedSpace
Dim Capability
Dim FreeSpace
Capability = 0
Set colItems = objWMIService.ExecQuery( "Select Size from Win32_LogicalDisk where DeviceID='C:'" )
For each objItem in colItems
If objItem.Size <> "" Then Capability = Capability + objItem.Size
Next
FreeSpace = 0
Set colItems = objWMIService.ExecQuery( "Select FreeSpace from Win32_LogicalDisk where DeviceID='C:'" )
For each objItem in colItems
If objItem.FreeSpace <> "" Then FreeSpace = FreeSpace + objItem.FreeSpace
Next
UsedSpace = 0
UsedSpace = Capability - FreeSpace
getCDiskUsedRate = ROUND(CDBL(UsedSpace) / CDBL(Capability) * CDBL(100),0)
End Function
Function getDiskUsedRate()
Dim UsedSpace
Dim Capability
Dim FreeSpace
Dim temp
Dim diskrate
For i = 0 To logicdisknum-1
Capability = 0
temp = Mid(logicdiskname,i*3+1,2)
'OutStream.Write temp & vbcrlf
Set colItems = objWMIService.ExecQuery( "Select Size from Win32_LogicalDisk where DeviceID='" & temp & "'" )
For each objItem in colItems
If objItem.Size <> "" Then Capability = Capability + objItem.Size
Next
'OutStream.Write Capability & vbcrlf
FreeSpace = 0
Set colItems = objWMIService.ExecQuery( "Select FreeSpace from Win32_LogicalDisk where DeviceID='" & temp & "'")
For each objItem in colItems
If objItem.FreeSpace <> "" Then FreeSpace = FreeSpace + objItem.FreeSpace
Next
UsedSpace = 0
UsedSpace = Capability - FreeSpace
'OutStream.Write UsedSpace & vbcrlf
diskrate = ROUND(CDBL(UsedSpace) / CDBL(Capability) * CDBL(100),2)
OutStream.Write disk & "-" & temp & "-disk" & "#" & "CM-00-01-001-16" & "#" & "盘符号:" & "#" & Now() & "#" & temp & vbcrlf
OutStream.Write disk & "-" & temp & "-disk" & "#" & "PM-00-01-004-04" & "#" & "剩余空间:" & "#" & Now() & "#" & Round(FreeSpace/1024/1024,3) & vbcrlf
OutStream.Write disk & "-" & temp & "-disk" & "#" & "PM-00-01-004-03" & "#" & "利用率:" & "#" & Now() & "#" & diskrate & vbcrlf
Next
End Function
'硬盘分区描述
Function getLogicalDiskDescription()
Dim Description
Dim FreeSpace
Set colItems = objWMIService.ExecQuery( "Select * from Win32_LogicalDisk where MediaType='12'" )
For each objItem in colItems
Description = Description & objItem.Description & " " & objItem.Name & ","
Next
getLogicalDiskDescription = Description
End Function
'Separates one field between sStart And sEnd
Function SeparateField(ByVal sFrom, ByVal sStart, ByVal sEnd)
Dim PosB: PosB = InStr(1, sFrom, sStart, 1)
If PosB > 0 Then
PosB = PosB + Len(sStart)
Dim PosE: PosE = InStr(PosB, sFrom, sEnd, 1)
If PosE = 0 Then PosE = InStr(PosB, sFrom, vbCrLf, 1)
If PosE = 0 Then PosE = Len(sFrom) + 1
SeparateField = Mid(sFrom, PosB, PosE - PosB)
End If
End Function
Function GetKeyValue(ByVal Arguments,ByVal KeyName)
Dim Value, Found
If InStr(1, Arguments, "," & KeyName & "=")>0 Then
Found = True
'Separate value of a key.
Value = SeparateField(strArguments, "," & KeyName & "=", ",")
End If
If isempty(Found) Then Value = ""
GetKeyValue = Value
End Function
'File functions
Function GetFile(ByVal FileName)
Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
'Go To windows folder If full path Not specified.
If InStr(FileName, ":\") = 0 And Left (FileName,2)<>"\\" Then
FileName = FS.GetSpecialFolder(0) & "\" & FileName
End If
On Error Resume Next
GetFile = FS.OpenTextFile(FileName).ReadAll
End Function
Function GetINIKeyValue(Section, KeyName, Default, FileName)
Dim INIContents, PosSection, PosEndSection, sContents, Value, Found
'Get contents of the INI file As a string
INIContents = GetFile(FileName)
'Find section
PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
If PosSection>0 Then
'Section exists. Find end of section
PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
'?Is this last section?
If PosEndSection = 0 Then PosEndSection = Len(INIContents)+1
'Separate section contents
sContents = Mid(INIContents, PosSection, PosEndSection - PosSection)
If InStr(1, sContents, vbCrLf & KeyName & "=", vbTextCompare)>0 Then
Found = True
'Separate value of a key.
Value = SeparateField(sContents, vbCrLf & KeyName & "=", vbCrLf)
End If
End If
If isempty(Found) Then Value = Default
GetINIKeyValue = Value
End Function
'硬盘分区数量
Function getDeviceID()
Dim Descriptionx
' Dim FreeSpace
Set colItems = objWMIService.ExecQuery( "Select * from Win32_LogicalDisk where MediaType='12'" )
For each objItem in colItems
Descriptionx = Descriptionx & objItem.Name & ","
Next
getDeviceID = Descriptionx
End Function
'系统日志
Function getEvent()
Dim p
Dim v
Dim temp
Dim max_event_id
Dim colLoggedEvents
Dim objEvent
Dim my_day
Dim my_month
Dim my_year
Dim my_day7
Dim my_date
Dim my_month7
Dim my_year7
Dim my_date7
Dim tmp_con
temp=0
max_event_id=readFile
'get date 当前的wmi格式的时间
'get date 当前的wmi格式的时间
my_day = day(now)
if my_day<10 then
my_day="0" & my_day
end if
my_month = Month(Now)
If my_month < 10 then
my_month="0" & my_month
end if
my_year = year(now)
my_date=my_year & my_month & my_day & "235959.000000+480"
'get date before today N day 定义取多少天前的wmi格式的时间,单位天,也就是检查的时间间隔范围,一般为7
Const before_days = 1
my_day7 = day(now - before_days)
if my_day7<10 then
my_day7="0" & my_day7
end if
my_month7 = Month(Now - before_days)
If my_month7 < 10 then
my_month7="0" & my_month7
end if
my_year7 = year(now - before_days)
my_date7=my_year7 & my_month7 & my_day7 & "000000.000000+480"
'MsgBox my_date7 & "---" & my_date
'假如第一次部署,取前20分钟的日志
If max_event_id=0 Then
tmp_con = "and TimeWritten >= '" & my_date7 & "' and " & "TimeWritten <= '" & my_date & "'"
End If
'MsgBox max_event_id & "aa---" & tmp_con
Set colLoggedEvents = objWMIService.ExecQuery("Select * from Win32_NTLogEvent Where Logfile = 'System' and RecordNumber > " & max_event_id & tmp_con)
'where后可带的字段名为:EventType ,Logfile,RecordNumber,SourceName ,TimeWritten,objEvent.ComputerName
For Each objEvent in colLoggedEvents
If objEvent.RecordNumber>temp Then
temp=objEvent.RecordNumber
End If
OutStream.Write log & "-log-" & objEvent.RecordNumber & "#" & "CM-00-01-900-10" & "#" & "事件等级号:" & "#" & Now() & "#" & objEvent.Category & vbcrlf
OutStream.Write log & "-log-" & objEvent.RecordNumber & "#" & "CM-00-01-900-11" & "#" & "事件号:" & "#" & Now() & "#" & objEvent.EventCode & vbcrlf
OutStream.Write log & "-log-" & objEvent.RecordNumber & "#" & "CM-00-01-900-12" & "#" & "日志记录编号:" & "#" & Now() & "#" & objEvent.RecordNumber & vbcrlf
OutStream.Write log & "-log-" & objEvent.RecordNumber & "#" & "CM-00-01-900-13" & "#" & "事件来源:" & "#" & Now() & "#" & objEvent.SourceName & vbcrlf
OutStream.Write log & "-log-" & objEvent.RecordNumber & "#" & "CM-00-01-900-14" & "#" & "事件类型:" & "#" & Now() & "#" & objEvent.Type & vbcrlf
OutStream.Write log & "-log-" & objEvent.RecordNumber & "#" & "CM-00-01-900-15" & "#" & "事件时间:" & "#" & Now() & "#" & WMIDateStringToDate(objEvent.TimeWritten) & vbcrlf
OutStream.Write log & "-log-" & objEvent.RecordNumber & "#" & "CM-00-01-900-16" & "#" & "用户:" & "#" & Now() & "#" & formatStr(objEvent.User) & vbcrlf
Next
If temp<>0 Then
writeFile(temp)
End If
End Function
Function WMIDateStringToDate(dtmEventDate)
WMIDateStringToDate = CDate(Mid(dtmEventDate, 5, 2) & "/" & _
Mid(dtmEventDate, 7, 2) & "/" & Left(dtmEventDate, 4) _
& " " & Mid (dtmEventDate, 9, 2) & ":" & _
Mid(dtmEventDate, 11, 2) & ":" & Mid(dtmEventDate, _
13, 2))
End Function
Function readFile()
Dim maxline
Dim fs
Dim ts
Set fs =createobject("scripting.filesystemobject")
If fs.fileexists("d:\jx_nms\data\sys_event_count.txt") Then
Set ts=fs.opentextfile("d:\jx_nms\data\sys_event_count.txt",1,True)
maxline=ts.readline
readFile = maxline
Else
readFile=0
End If
End Function
Function writeFile(maxevent)
Dim f
Dim fs
Dim ts
set fs =createobject("scripting.filesystemobject")
set f =fs.opentextfile("d:\jx_nms\data\sys_event_count.txt",2,True)
f.writeline maxevent
f.close
End Function
Function formatStr(StrValue)
If IsNull(StrValue) or IsEmpty(StrValue) or len(StrValue)=0 Then
formatStr="N/A"
Else
formatStr=StrValue
End If
End Function