forked from iamacarpet/go-win64api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysinfo.go
778 lines (697 loc) · 26.7 KB
/
sysinfo.go
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
//go:build windows
// +build windows
package winapi
import (
"fmt"
"net"
"reflect"
"strconv"
"strings"
"time"
ole "github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
"github.com/scjalliance/comshim"
so "github.com/jetrmm/go-win64api/shared"
)
func GetSystemProfile() (so.Hardware, so.OperatingSystem, so.Memory, []so.Disk, []so.Network, error) {
comshim.Add(1)
defer comshim.Done()
retHW := so.Hardware{}
retOS := so.OperatingSystem{}
retMEM := so.Memory{}
retDISK := make([]so.Disk, 0)
retNET := make([]so.Network, 0)
// Pre-WMI Queries
var err error
retHW.IsUsingUEFI, err = sysinfo_uefi_check()
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, fmt.Errorf("Failed to get UEFI status, %s", err.Error())
}
retHW.SecureBootEnabled, err = sysinfo_secureboot_check()
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, fmt.Errorf("Failed to get SecureBoot status, %s", err.Error())
}
unknown, err := oleutil.CreateObject("WbemScripting.SWbemLocator")
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, fmt.Errorf("Unable to create initial object, %s", err.Error())
}
defer unknown.Release()
wmi, err := unknown.QueryInterface(ole.IID_IDispatch)
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, fmt.Errorf("Unable to create query interface, %s", err.Error())
}
defer wmi.Release()
serviceRaw, err := oleutil.CallMethod(wmi, "ConnectServer")
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, fmt.Errorf("Error Connecting to WMI Service, %s", err.Error())
}
service := serviceRaw.ToIDispatch()
defer service.Release()
// Query 1 - BIOS information.
err = func() error {
resultRaw, err := oleutil.CallMethod(service, "ExecQuery", "SELECT SerialNumber, Manufacturer, SMBIOSBIOSVersion, ReleaseDate FROM Win32_BIOS")
if err != nil {
return fmt.Errorf("Unable to execute query while getting BIOS info. %s", err.Error())
}
result := resultRaw.ToIDispatch()
defer result.Release()
countVar, err := oleutil.GetProperty(result, "Count")
if err != nil {
return fmt.Errorf("Unable to get property Count while processing BIOS info. %s", err.Error())
}
count := int(countVar.Val)
if count > 0 {
itemRaw, err := oleutil.CallMethod(result, "ItemIndex", 0)
if err != nil {
return fmt.Errorf("Failed to fetch result row while processing BIOS info. %s", err.Error())
}
item := itemRaw.ToIDispatch()
defer item.Release()
resSerialNumber, err := oleutil.GetProperty(item, "SerialNumber")
if err != nil {
return fmt.Errorf("Error while getting property SerialNumber in BIOS info. %s", err.Error())
}
retHW.ServiceTag = resSerialNumber.ToString()
resVersion, err := oleutil.GetProperty(item, "SMBIOSBIOSVersion")
if err != nil {
return fmt.Errorf("Error while getting property Version in BIOS info. %s", err.Error())
}
retHW.BIOSVersion = resVersion.ToString()
resManufacturer, err := oleutil.GetProperty(item, "Manufacturer")
if err != nil {
return fmt.Errorf("Error while getting property Manufacturer in BIOS info. %s", err.Error())
}
retHW.BIOSManufacturer = resManufacturer.ToString()
resReleaseDate, err := oleutil.GetProperty(item, "ReleaseDate")
if err != nil {
return fmt.Errorf("Error while getting property ReleaseDate in BIOS info. %s", err.Error())
}
if resReleaseDate.Value() != nil {
if resVReleaseDate, ok := resReleaseDate.Value().(string); ok {
resVVRD := strings.Split(resVReleaseDate, "+")[0]
retHW.BIOSReleaseDate, err = time.Parse("20060102150405.999999", resVVRD)
if err != nil {
return fmt.Errorf("Unable to parse BIOS release date into valid time.Time. %s", err.Error())
}
} else {
return fmt.Errorf("Unable to assert BIOS release date as string. Got type %s", reflect.TypeOf(resReleaseDate.Value()).Name())
}
}
} else {
return fmt.Errorf("Error while getting BIOS info, no BIOS record found.")
}
return nil
}()
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, err
}
// Query 1.5 - TPM information.
retHW.TPMSpecVersion, err = sysinfo_tpm_specversion()
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, err
}
// Query 2 - Computer System information.
err = func() error {
resultRaw, err := oleutil.CallMethod(service, "ExecQuery", "SELECT AutomaticManagedPagefile, Manufacturer, Model, TotalPhysicalMemory, SystemFamily FROM Win32_ComputerSystem")
if err != nil {
return fmt.Errorf("Unable to execute query while getting Computer System info. %s", err.Error())
}
result := resultRaw.ToIDispatch()
defer result.Release()
countVar, err := oleutil.GetProperty(result, "Count")
if err != nil {
return fmt.Errorf("Unable to get property Count while processing Computer System info. %s", err.Error())
}
count := int(countVar.Val)
if count > 0 {
itemRaw, err := oleutil.CallMethod(result, "ItemIndex", 0)
if err != nil {
return fmt.Errorf("Failed to fetch result row while processing Computer System info. %s", err.Error())
}
item := itemRaw.ToIDispatch()
defer item.Release()
resMPF, err := oleutil.GetProperty(item, "AutomaticManagedPagefile")
if err != nil {
return fmt.Errorf("Error while getting property AutomaticManagedPagefile in Computer System info. %s", err.Error())
}
if resMPF.Value() != nil {
if resVMPF, ok := resMPF.Value().(bool); ok {
retMEM.SystemManagedPageFile = resVMPF
} else {
return fmt.Errorf("Error asserting AutomaticManagedPagefile to bool. Got type %s", reflect.TypeOf(resMPF.Value()).Name())
}
}
resManufacturer, err := oleutil.GetProperty(item, "Manufacturer")
if err != nil {
return fmt.Errorf("Error while getting property Manufacturer in Computer System info. %s", err.Error())
}
retHW.Manufacturer = resManufacturer.ToString()
resModel, err := oleutil.GetProperty(item, "Model")
if err != nil {
return fmt.Errorf("Error while getting property Model in Computer System info. %s", err.Error())
}
retHW.Model = resModel.ToString()
resSystemFamily, err := oleutil.GetProperty(item, "SystemFamily")
if err != nil {
return fmt.Errorf("Error while getting property SystemFamily in Computer System info. %s", err.Error())
}
retHW.SystemFamily = resSystemFamily.ToString()
resTM, err := oleutil.GetProperty(item, "TotalPhysicalMemory")
if err != nil {
return fmt.Errorf("Error while getting property TotalPhysicalMemory in Computer System info. %s", err.Error())
}
if resTM.Value() != nil {
if resVTM, ok := resTM.Value().(string); ok {
if retMEM.TotalRAM, err = strconv.ParseUint(resVTM, 10, 64); err != nil {
return fmt.Errorf("Error while converting TotalPhysicalMemory to integer. %s", err.Error())
}
} else {
return fmt.Errorf("Error asserting TotalPhysicalMemory to string. Got type %s", reflect.TypeOf(resTM.Value()).Name())
}
}
}
return nil
}()
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, err
}
// Query 3 - Hardware UUID information.
err = func() error {
resultRaw, err := oleutil.CallMethod(service, "ExecQuery", "SELECT UUID FROM Win32_ComputerSystemProduct")
if err != nil {
return fmt.Errorf("Unable to execute query while getting UUID info. %s", err.Error())
}
result := resultRaw.ToIDispatch()
defer result.Release()
countVar, err := oleutil.GetProperty(result, "Count")
if err != nil {
return fmt.Errorf("Unable to get property Count while processing UUID info. %s", err.Error())
}
count := int(countVar.Val)
if count > 0 {
itemRaw, err := oleutil.CallMethod(result, "ItemIndex", 0)
if err != nil {
return fmt.Errorf("Failed to fetch result row while processing UUID info. %s", err.Error())
}
item := itemRaw.ToIDispatch()
defer item.Release()
resUUID, err := oleutil.GetProperty(item, "UUID")
if err != nil {
return fmt.Errorf("Error while getting property Hardware UUID. %s", err.Error())
}
retHW.HardwareUUID = resUUID.ToString()
}
return nil
}()
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, err
}
// Query 4 - Operating System information.
err = func() error {
resultRaw, err := oleutil.CallMethod(service, "ExecQuery", "SELECT Caption, Version, OSArchitecture, OSLanguage, TotalVisibleMemorySize, FreePhysicalMemory, TotalVirtualMemorySize, FreeVirtualMemory, LastBootUpTime, SystemDrive FROM Win32_OperatingSystem")
if err != nil {
return fmt.Errorf("Unable to execute query while getting Operating System info. %s", err.Error())
}
result := resultRaw.ToIDispatch()
defer result.Release()
countVar, err := oleutil.GetProperty(result, "Count")
if err != nil {
return fmt.Errorf("Unable to get property Count while processing Operating System info. %s", err.Error())
}
count := int(countVar.Val)
if count > 0 {
itemRaw, err := oleutil.CallMethod(result, "ItemIndex", 0)
if err != nil {
return fmt.Errorf("Failed to fetch result row while processing Operating System info. %s", err.Error())
}
item := itemRaw.ToIDispatch()
defer item.Release()
resFriendlyName, err := oleutil.GetProperty(item, "Caption")
if err != nil {
return fmt.Errorf("Error while getting property Friendly Name from Operating System info. %s", err.Error())
}
retOS.FriendlyName = resFriendlyName.ToString()
resVersion, err := oleutil.GetProperty(item, "Version")
if err != nil {
return fmt.Errorf("Error while getting property Version from Operating System info. %s", err.Error())
}
retOS.Version = resVersion.ToString()
resArch, err := oleutil.GetProperty(item, "OSArchitecture")
if err != nil {
return fmt.Errorf("Error while getting property Architecture from Operating System info. %s", err.Error())
}
retOS.Architecture = resArch.ToString()
resLanguageCode, err := oleutil.GetProperty(item, "OSLanguage")
if err != nil {
return fmt.Errorf("Error while getting property Language from Language Code info. %s", err.Error())
}
if resLanguageCode.Value() != nil {
if resVLC, ok := resLanguageCode.Value().(int32); ok {
retOS.LanguageCode = uint16(resVLC)
} else {
return fmt.Errorf("Error while setting Lanauge Code property to int32. Got type %s", reflect.TypeOf(resLanguageCode.Value()).Name())
}
}
resAvailRAM, err := oleutil.GetProperty(item, "TotalVisibleMemorySize")
if err != nil {
return fmt.Errorf("Error while getting property TotalVisibleMemorySize from Operating System info. %s", err.Error())
}
if resAvailRAM.Value() != nil {
if resVAvailRAM, ok := resAvailRAM.Value().(string); ok {
if retMEM.UsableRAM, err = strconv.ParseUint(resVAvailRAM, 10, 64); err != nil {
return fmt.Errorf("Error parsing Available RAM integer. %s", err.Error())
}
} else {
return fmt.Errorf("Error asserting Available RAM as string from Operating System Info. Got type %s", reflect.TypeOf(resAvailRAM.Value()).Name())
}
}
resFreeRAM, err := oleutil.GetProperty(item, "FreePhysicalMemory")
if err != nil {
return fmt.Errorf("Error while getting property FreePhysicalMemory from Operating System info. %s", err.Error())
}
if resFreeRAM.Value() != nil {
if resVFreeRAM, ok := resFreeRAM.Value().(string); ok {
if retMEM.FreeRAM, err = strconv.ParseUint(resVFreeRAM, 10, 64); err != nil {
return fmt.Errorf("Error parsing Free RAM integer. %s", err.Error())
}
} else {
return fmt.Errorf("Error asserting Free RAM as string from Operating System Info. Got type %s", reflect.TypeOf(resFreeRAM.Value()).Name())
}
}
resPageFileSize, err := oleutil.GetProperty(item, "TotalVirtualMemorySize")
if err != nil {
return fmt.Errorf("Error while getting property TotalVirtualMemorySize from Operating System info. %s", err.Error())
}
if resPageFileSize.Value() != nil {
if resVPFS, ok := resPageFileSize.Value().(string); ok {
if vms, err := strconv.ParseUint(resVPFS, 10, 64); err != nil {
return fmt.Errorf("Error parsing total virtual memory integer. %s", err.Error())
} else {
retMEM.TotalPageFile = (vms - retMEM.UsableRAM)
}
} else {
return fmt.Errorf("Error asserting TotalVirtualMemorySize as string from Operating System Info. Got type %s", reflect.TypeOf(resPageFileSize.Value()).Name())
}
}
resFreePageFile, err := oleutil.GetProperty(item, "FreeVirtualMemory")
if err != nil {
return fmt.Errorf("Error while getting property FreeVirtualMemory from Operating System info. %s", err.Error())
}
if resFreePageFile.Value() != nil {
if resVFPF, ok := resFreePageFile.Value().(string); ok {
if fvms, err := strconv.ParseUint(resVFPF, 10, 64); err != nil {
return fmt.Errorf("Error parsing free virtual memory integer. %s", err.Error())
} else {
retMEM.FreePageFile = (fvms - retMEM.FreeRAM)
}
} else {
return fmt.Errorf("Error asserting FreeVirtualMemory as string from Operating System Info. Got type %s", reflect.TypeOf(resFreePageFile.Value()).Name())
}
}
resLastBootUpTime, err := oleutil.GetProperty(item, "LastBootUpTime")
if err != nil {
return fmt.Errorf("Error while getting property LastBootUpTime from Operating System info. %s", err.Error())
}
if resLastBootUpTime.Value() != nil {
if resLBUT, ok := resLastBootUpTime.Value().(string); ok {
retOS.LastBootUpTime, err = ConvertWMITime(resLBUT)
if err != nil {
return fmt.Errorf("Error parsing LastBootUpTime: %s", err)
}
} else {
return fmt.Errorf("Error asserting LastBootUpTime as string from Operating System Info. Got type %s", reflect.TypeOf(resFreePageFile.Value()).Name())
}
}
resSystemDrive, err := oleutil.GetProperty(item, "SystemDrive")
if err != nil {
return fmt.Errorf("Error while getting property SystemDrive from Operating System info. %s", err.Error())
}
retOS.SystemDrive = resSystemDrive.ToString()
}
return nil
}()
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, err
}
// Query 5 - Processor information.
err = func() error {
resultRaw, err := oleutil.CallMethod(service, "ExecQuery", "SELECT Name, NumberOfCores, NumberOfLogicalProcessors FROM Win32_Processor")
if err != nil {
return fmt.Errorf("Unable to execute query while getting Operating System info. %s", err.Error())
}
result := resultRaw.ToIDispatch()
defer result.Release()
countVar, err := oleutil.GetProperty(result, "Count")
if err != nil {
return fmt.Errorf("Unable to get property Count while processing Operating System info. %s", err.Error())
}
count := int(countVar.Val)
for i := 0; i < count; i++ {
err = func() error {
itemRaw, err := oleutil.CallMethod(result, "ItemIndex", i)
if err != nil {
return fmt.Errorf("Failed to fetch result row while processing Processor info. %s", err.Error())
}
item := itemRaw.ToIDispatch()
defer item.Release()
retCPU := so.CPU{}
resFriendlyName, err := oleutil.GetProperty(item, "Name")
if err != nil {
return fmt.Errorf("Error while getting property Friendly Name from Processor info. %s", err.Error())
}
retCPU.FriendlyName = resFriendlyName.ToString()
resNC, err := oleutil.GetProperty(item, "NumberOfCores")
if err != nil {
return fmt.Errorf("Error while getting property Number of Cores from Processor info. %s", err.Error())
}
if resNC.Value() != nil {
if resVNC, ok := resNC.Value().(int32); ok {
retCPU.NumberOfCores = uint8(resVNC)
} else {
return fmt.Errorf("Error asserting NumberOfCores as int32 from Processor Info. Got type %s", reflect.TypeOf(resNC.Value()).Name())
}
}
resNLP, err := oleutil.GetProperty(item, "NumberOfLogicalProcessors")
if err != nil {
return fmt.Errorf("Error while getting property Number of Logical Processors from Processor info. %s", err.Error())
}
if resNLP.Value() != nil {
if resVNLP, ok := resNLP.Value().(int32); ok {
retCPU.NumberOfLogical = uint8(resVNLP)
} else {
return fmt.Errorf("Error asserting NumberOfCores as int32 from Processor Info. Got type %s", reflect.TypeOf(resNLP.Value()).Name())
}
}
retHW.CPU = append(retHW.CPU, retCPU)
return nil
}()
if err != nil {
return err
}
}
return nil
}()
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, err
}
// Query 6 - Memory DIMM information.
err = func() error {
resultRaw, err := oleutil.CallMethod(service, "ExecQuery", "SELECT Capacity, MemoryType, Speed FROM Win32_PhysicalMemory")
if err != nil {
return fmt.Errorf("Unable to execute query while getting Operating System info. %s", err.Error())
}
result := resultRaw.ToIDispatch()
defer result.Release()
countVar, err := oleutil.GetProperty(result, "Count")
if err != nil {
return fmt.Errorf("Unable to get property Count while processing Operating System info. %s", err.Error())
}
count := int(countVar.Val)
for i := 0; i < count; i++ {
err = func() error {
itemRaw, err := oleutil.CallMethod(result, "ItemIndex", i)
if err != nil {
return fmt.Errorf("Failed to fetch result row while processing Processor info. %s", err.Error())
}
item := itemRaw.ToIDispatch()
defer item.Release()
retMD := so.MemoryDIMM{}
resCapacity, err := oleutil.GetProperty(item, "Capacity")
if err != nil {
return fmt.Errorf("Error while getting property Capacity from Memory DIMM info. %s", err.Error())
}
if resCapacity.Value() != nil {
if resVCapacity, ok := resCapacity.Value().(string); ok {
if retMD.Size, err = strconv.ParseUint(resVCapacity, 10, 64); err != nil {
return fmt.Errorf("Error parsing Capacity integer. %s", err.Error())
}
} else {
return fmt.Errorf("Error asserting Capacity as string from Memory DIMM Info. Got type %s", reflect.TypeOf(resCapacity.Value()).Name())
}
}
resMT, err := oleutil.GetProperty(item, "MemoryType")
if err != nil {
return fmt.Errorf("Error while getting property Memory Type from Memory DIMM info. %s", err.Error())
}
if resMT.Value() != nil {
if resVMT, ok := resMT.Value().(int32); ok {
retMD.MType = memoryType[int(resVMT)]
} else {
return fmt.Errorf("Error asserting Memory Type as int32 from Memory DIMM Info. Got type %s", reflect.TypeOf(resMT.Value()).Name())
}
}
resSpeed, err := oleutil.GetProperty(item, "Speed")
if err != nil {
return fmt.Errorf("Error while getting property Speed from Memory DIMM info. %s", err.Error())
}
if resSpeed.Value() != nil {
if resVSpeed, ok := resSpeed.Value().(int32); ok {
retMD.Speed = uint16(resVSpeed)
} else {
return fmt.Errorf("Error asserting Speed as int32 from Memory DIMM Info. Got type %s", reflect.TypeOf(resSpeed.Value()).Name())
}
}
retHW.Memory = append(retHW.Memory, retMD)
return nil
}()
if err != nil {
return err
}
}
return nil
}()
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, err
}
// Query 7 - Logical Disk information.
err = func() error {
resultRaw, err := oleutil.CallMethod(service, "ExecQuery", "SELECT Name, DriveType, FreeSpace, Size, FileSystem FROM Win32_LogicalDisk WHERE DriveType=3")
if err != nil {
return fmt.Errorf("Unable to execute query while getting Logical Disk info. %s", err.Error())
}
result := resultRaw.ToIDispatch()
defer result.Release()
countVar, err := oleutil.GetProperty(result, "Count")
if err != nil {
return fmt.Errorf("Unable to get property Count while processing Logical Disk info. %s", err.Error())
}
count := int(countVar.Val)
for i := 0; i < count; i++ {
err = func() error {
itemRaw, err := oleutil.CallMethod(result, "ItemIndex", i)
if err != nil {
return fmt.Errorf("Failed to fetch result row while processing Logical Disk info. %s", err.Error())
}
item := itemRaw.ToIDispatch()
defer item.Release()
retDR := so.Disk{}
resName, err := oleutil.GetProperty(item, "Name")
if err != nil {
return fmt.Errorf("Error while getting property Name from Logical Disk info. %s", err.Error())
}
retDR.DriveName = resName.ToString() + `\`
resFileSystem, err := oleutil.GetProperty(item, "FileSystem")
if err != nil {
return fmt.Errorf("Error while getting property File System from Logical Disk info. %s", err.Error())
}
retDR.FileSystem = resFileSystem.ToString()
resTotalSize, err := oleutil.GetProperty(item, "Size")
if err != nil {
return fmt.Errorf("Error while getting property Size from Logical Disk info. %s", err.Error())
}
if resTotalSize.Value() != nil {
if resVTSize, ok := resTotalSize.Value().(string); ok {
if retDR.TotalSize, err = strconv.ParseUint(resVTSize, 10, 64); err != nil {
return fmt.Errorf("Error parsing Total Size (DISK) integer. %s", err.Error())
}
} else {
return fmt.Errorf("Error asserting Total Size (DISK) as string from Logical Disk Info. Got type %s", reflect.TypeOf(resTotalSize.Value()).Name())
}
}
resFreeSP, err := oleutil.GetProperty(item, "FreeSpace")
if err != nil {
return fmt.Errorf("Error while getting property FreeSpace from Logical Disk info. %s", err.Error())
}
if resFreeSP.Value() != nil {
if resVAvail, ok := resFreeSP.Value().(string); ok {
if retDR.Available, err = strconv.ParseUint(resVAvail, 10, 64); err != nil {
return fmt.Errorf("Error parsing Available (DISK) integer. %s", err.Error())
}
} else {
return fmt.Errorf("Error asserting Available (DISK) as string from Logical Disk Info. Got type %s", reflect.TypeOf(resFreeSP.Value()).Name())
}
}
retDR.BitLockerEnabled, retDR.BitLockerEncrypted, _ = sysinfo_bitlocker_check(resName.ToString())
retDR.BitLockerRecoveryInfo, _ = GetBitLockerRecoveryInfoForDrive(resName.ToString())
retDISK = append(retDISK, retDR)
return nil
}()
if err != nil {
return err
}
}
return nil
}()
if err != nil {
return retHW, retOS, retMEM, retDISK, retNET, err
}
// Query 8 - Network Adapter information.
err = func() error {
resultRaw, err := oleutil.CallMethod(service, "ExecQuery", "SELECT Description, IPAddress, MACAddress, IPSubnet, DHCPEnabled FROM Win32_NetworkAdapterConfiguration WHERE MACAddress <> NULL")
if err != nil {
return fmt.Errorf("Unable to execute query while getting Network Adapter info. %s", err.Error())
}
result := resultRaw.ToIDispatch()
defer result.Release()
countVar, err := oleutil.GetProperty(result, "Count")
if err != nil {
return fmt.Errorf("Unable to get property Count while processing Network Adapter info. %s", err.Error())
}
count := int(countVar.Val)
for i := 0; i < count; i++ {
err = func() error {
itemRaw, err := oleutil.CallMethod(result, "ItemIndex", i)
if err != nil {
return fmt.Errorf("Failed to fetch result row while processing Network Adapter info. %s", err.Error())
}
item := itemRaw.ToIDispatch()
defer item.Release()
retNW := so.Network{}
resName, err := oleutil.GetProperty(item, "Description")
if err != nil {
return fmt.Errorf("Error while getting property Description from Network Adapter info. %s", err.Error())
}
retNW.Name = resName.ToString()
resDHCP, err := oleutil.GetProperty(item, "DHCPEnabled")
if err != nil {
return fmt.Errorf("Error while getting property DHCPEnabled from Network Adapter info. %s", err.Error())
}
var ok bool
if resDHCP.Value() != nil {
if retNW.DHCPEnabled, ok = resDHCP.Value().(bool); !ok {
return fmt.Errorf("Error while asserting type bool for DHCPEnabled at Network Adapter info. Got type %s", reflect.TypeOf(resDHCP.Value()).Name())
}
}
resMAC, err := oleutil.GetProperty(item, "MACAddress")
if err != nil {
return fmt.Errorf("Error while getting property MAC Address from Network Adapter info. %s", err.Error())
}
retNW.MACAddress = resMAC.ToString()
var ips []interface{}
var subnets []interface{}
resIP, err := oleutil.GetProperty(item, "IPAddress")
if err != nil {
return fmt.Errorf("Error while getting property IP Address from Network Adapter info. %s", err.Error())
}
if uintptr(resIP.Val) != 0 && resIP.VT != ole.VT_NULL {
ips = resIP.ToArray().ToValueArray()
}
resSubnet, err := oleutil.GetProperty(item, "IPSubnet")
if err != nil {
return fmt.Errorf("Error while getting property IP Subnet from Network Adapter info. %s", err.Error())
}
if uintptr(resSubnet.Val) != 0 && resSubnet.VT != ole.VT_NULL {
subnets = resSubnet.ToArray().ToValueArray()
}
for i, v := range ips {
var ip, subnet string
if ip, ok = v.(string); !ok {
return fmt.Errorf("Unable to assert IP as string. Got type %s", reflect.TypeOf(v).Name())
}
if subnet, ok = subnets[i].(string); !ok {
return fmt.Errorf("Unable to assert Subnet as string. Got type %s", reflect.TypeOf(subnets[i]).Name())
}
if strings.Contains(subnet, ".") {
tsubnet := ParseIPv4Mask(subnet)
tIP := net.ParseIP(ip)
tIPNet := net.IPNet{IP: tIP, Mask: tsubnet}
ip = tIPNet.String()
} else {
tsbits, err := strconv.Atoi(subnet)
if err != nil {
return fmt.Errorf("Unable to convert IPv6 Subnet to integer.")
}
tsubnet := net.CIDRMask(tsbits, 128)
tIP := net.ParseIP(ip)
tIPNet := net.IPNet{IP: tIP, Mask: tsubnet}
ip = tIPNet.String()
}
retNW.IPAddressCIDR = append(retNW.IPAddressCIDR, ip)
}
retNET = append(retNET, retNW)
return nil
}()
if err != nil {
return err
}
}
return nil
}()
return retHW, retOS, retMEM, retDISK, retNET, err
}
var memoryType = map[int]string{
0: "Unknown",
1: "Other",
2: "DRAM",
3: "Synch DRAM",
4: "Cache DRAM",
5: "EDO",
6: "EDRAM",
7: "VRAM",
8: "SRAM",
9: "RAM",
10: "ROM",
11: "Flash",
12: "EEPROM",
13: "FEPROM",
14: "EPROM",
15: "CDRAM",
16: "3DRAM",
17: "SDRAM",
18: "SGRAM",
19: "RDRAM",
20: "DDR",
21: "DDR2",
22: "DDR2 FB-DIMM",
24: "DDR3",
25: "FBD2",
26: "DDR4",
27: "LPDDR",
28: "LPDDR2",
29: "LPDDR3",
30: "LPDDR4",
}
func ParseIPv4Mask(s string) net.IPMask {
mask := net.ParseIP(s)
if mask == nil {
return nil
}
return net.IPv4Mask(mask[12], mask[13], mask[14], mask[15])
}
func ConvertWMITime(s string) (time.Time, error) {
if len(s) == 26 {
return time.Time{}, fmt.Errorf("Invalid Length of DATETIME string")
}
offset := s[22:]
iOffset, err := strconv.Atoi(offset)
if err != nil {
return time.Time{}, fmt.Errorf("Error parsing offset: %s", err)
}
var h, m int = (iOffset / 60), (iOffset % 60)
var hr, mn string
if h < 10 {
hr = "0" + strconv.Itoa(h)
} else {
hr = strconv.Itoa(h)
}
if m < 10 {
mn = "0" + strconv.Itoa(m)
} else {
mn = strconv.Itoa(m)
}
res, err := time.Parse("20060102150405.999999-07:00", s[:22]+hr+":"+mn)
if err != nil {
return time.Time{}, fmt.Errorf("Error parsing time: %s", err)
}
return res, nil
}