bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/scollector/collectors/metadata_windows.go (about) 1 package collectors 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net" 7 "sort" 8 "strings" 9 "time" 10 11 "bosun.org/metadata" 12 "bosun.org/opentsdb" 13 "bosun.org/slog" 14 "github.com/StackExchange/wmi" 15 ) 16 17 func init() { 18 collectors = append(collectors, &IntervalCollector{F: c_meta_windows_version, Interval: time.Minute * 30}) 19 collectors = append(collectors, &IntervalCollector{F: c_meta_windows_ifaces, Interval: time.Minute * 30}) 20 } 21 22 func c_meta_windows_version() (opentsdb.MultiDataPoint, error) { 23 var md opentsdb.MultiDataPoint 24 var dst []Win32_OperatingSystem 25 q := wmi.CreateQuery(&dst, "") 26 err := wmi.Query(q, &dst) 27 if err != nil { 28 slog.Error(err) 29 return md, err 30 } 31 32 var dstComputer []Win32_ComputerSystem 33 q = wmi.CreateQuery(&dstComputer, "") 34 err = wmi.Query(q, &dstComputer) 35 if err != nil { 36 slog.Error(err) 37 return md, err 38 } 39 40 var dstBIOS []Win32_BIOS 41 q = wmi.CreateQuery(&dstBIOS, "") 42 err = wmi.Query(q, &dstBIOS) 43 if err != nil { 44 slog.Error(err) 45 return md, err 46 } 47 48 for _, v := range dst { 49 metadata.AddMeta("", nil, "version", v.Version, true) 50 metadata.AddMeta("", nil, "versionCaption", v.Caption, true) 51 } 52 53 for _, v := range dstComputer { 54 metadata.AddMeta("", nil, "manufacturer", v.Manufacturer, true) 55 metadata.AddMeta("", nil, "model", v.Model, true) 56 metadata.AddMeta("", nil, "memoryTotal", v.TotalPhysicalMemory, true) 57 } 58 59 for _, v := range dstBIOS { 60 metadata.AddMeta("", nil, "serialNumber", v.SerialNumber, true) 61 } 62 return md, nil 63 } 64 65 type Win32_OperatingSystem struct { 66 FreePhysicalMemory uint64 67 FreeVirtualMemory uint64 68 TotalVirtualMemorySize uint64 69 TotalVisibleMemorySize uint64 70 Caption string 71 Version string 72 } 73 74 type Win32_ComputerSystem struct { 75 Manufacturer string 76 Model string 77 TotalPhysicalMemory uint64 78 NumberOfLogicalProcessors uint32 79 } 80 81 type Win32_BIOS struct { 82 SerialNumber string 83 } 84 85 func c_meta_windows_ifaces() (opentsdb.MultiDataPoint, error) { 86 var md opentsdb.MultiDataPoint 87 var dstConfigs []Win32_NetworkAdapterConfiguration 88 q := wmi.CreateQuery(&dstConfigs, "WHERE MACAddress != null") 89 err := wmi.Query(q, &dstConfigs) 90 if err != nil { 91 slog.Error(err) 92 return md, err 93 } 94 95 mNicConfigs := make(map[uint32]*Win32_NetworkAdapterConfiguration) 96 for i, nic := range dstConfigs { 97 mNicConfigs[nic.InterfaceIndex] = &dstConfigs[i] 98 } 99 100 mNicTeamIDtoSpeed := make(map[string]uint64) 101 mNicTeamIDtoMaster := make(map[string]string) 102 var dstTeamMembers []MSFT_NetLbfoTeamMember 103 q = wmi.CreateQuery(&dstTeamMembers, "") 104 err = wmi.QueryNamespace(q, &dstTeamMembers, "root\\StandardCimv2") 105 if err == nil { 106 for _, teamMember := range dstTeamMembers { 107 mNicTeamIDtoSpeed[teamMember.InstanceID] = teamMember.ReceiveLinkSpeed 108 mNicTeamIDtoMaster[teamMember.InstanceID] = teamMember.Team 109 } 110 } 111 112 var dstAdapters []Win32_NetworkAdapter 113 q = wmi.CreateQuery(&dstAdapters, "WHERE PhysicalAdapter=True and MACAddress <> null and NetConnectionStatus = 2") //Only adapters with MAC addresses and status="Connected" 114 err = wmi.Query(q, &dstAdapters) 115 if err != nil { 116 slog.Error(err) 117 return md, err 118 } 119 120 mNicIndextoIPs := make(map[int]string) 121 ifaces, _ := net.Interfaces() 122 for _, iface := range ifaces { 123 if iface.Flags&(net.FlagLoopback|net.FlagPointToPoint) != 0 { 124 continue 125 } 126 rawAds, _ := iface.Addrs() 127 addrs := make([]string, len(rawAds)) 128 for i, rAd := range rawAds { 129 addrs[i] = rAd.String() 130 } 131 sort.Strings(addrs) 132 j, _ := json.Marshal(addrs) 133 mNicIndextoIPs[iface.Index] = string(j) 134 } 135 136 for _, v := range dstAdapters { 137 tag := opentsdb.TagSet{"iface": fmt.Sprint("Interface", v.InterfaceIndex)} 138 metadata.AddMeta("", tag, "description", v.Description, true) 139 metadata.AddMeta("", tag, "name", v.NetConnectionID, true) 140 metadata.AddMeta("", tag, "mac", strings.Replace(v.MACAddress, ":", "", -1), true) 141 if v.Speed != nil && *v.Speed != 0 { 142 metadata.AddMeta("", tag, "speed", v.Speed, true) 143 } else { 144 nicSpeed := mNicTeamIDtoSpeed[v.GUID] 145 metadata.AddMeta("", tag, "speed", nicSpeed, true) 146 } 147 148 nicMaster := mNicTeamIDtoMaster[v.GUID] 149 if nicMaster != "" { 150 metadata.AddMeta("", tag, "master", nicMaster, true) 151 } 152 153 nicIPs := mNicIndextoIPs[int(v.InterfaceIndex)] 154 if nicIPs == "" { 155 nicIPs = "[]" 156 } 157 metadata.AddMeta("", tag, "addresses", nicIPs, true) 158 } 159 return md, nil 160 } 161 162 type Win32_NetworkAdapterConfiguration struct { 163 IPAddress *[]string //Both IPv4 and IPv6 164 InterfaceIndex uint32 165 } 166 type MSFT_NetLbfoTeamMember struct { 167 Name string 168 ReceiveLinkSpeed uint64 169 Team string 170 InstanceID string 171 }