PageRenderTime 172ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/github.com/google/cadvisor/utils/sysinfo/sysinfo.go

https://gitlab.com/unofficial-mirrors/kubernetes
Go | 203 lines | 167 code | 13 blank | 23 comment | 61 complexity | 67cac9126c5bca1b2b70b2812619f9c8 MD5 | raw file
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sysinfo
  15. import (
  16. "fmt"
  17. "regexp"
  18. "strconv"
  19. "strings"
  20. info "github.com/google/cadvisor/info/v1"
  21. "github.com/google/cadvisor/utils/sysfs"
  22. )
  23. var schedulerRegExp = regexp.MustCompile(`.*\[(.*)\].*`)
  24. // Get information about block devices present on the system.
  25. // Uses the passed in system interface to retrieve the low level OS information.
  26. func GetBlockDeviceInfo(sysfs sysfs.SysFs) (map[string]info.DiskInfo, error) {
  27. disks, err := sysfs.GetBlockDevices()
  28. if err != nil {
  29. return nil, err
  30. }
  31. diskMap := make(map[string]info.DiskInfo)
  32. for _, disk := range disks {
  33. name := disk.Name()
  34. // Ignore non-disk devices.
  35. // TODO(rjnagal): Maybe just match hd, sd, and dm prefixes.
  36. if strings.HasPrefix(name, "loop") || strings.HasPrefix(name, "ram") || strings.HasPrefix(name, "sr") {
  37. continue
  38. }
  39. disk_info := info.DiskInfo{
  40. Name: name,
  41. }
  42. dev, err := sysfs.GetBlockDeviceNumbers(name)
  43. if err != nil {
  44. return nil, err
  45. }
  46. n, err := fmt.Sscanf(dev, "%d:%d", &disk_info.Major, &disk_info.Minor)
  47. if err != nil || n != 2 {
  48. return nil, fmt.Errorf("could not parse device numbers from %s for device %s", dev, name)
  49. }
  50. out, err := sysfs.GetBlockDeviceSize(name)
  51. if err != nil {
  52. return nil, err
  53. }
  54. // Remove trailing newline before conversion.
  55. size, err := strconv.ParseUint(strings.TrimSpace(out), 10, 64)
  56. if err != nil {
  57. return nil, err
  58. }
  59. // size is in 512 bytes blocks.
  60. disk_info.Size = size * 512
  61. disk_info.Scheduler = "none"
  62. blkSched, err := sysfs.GetBlockDeviceScheduler(name)
  63. if err == nil {
  64. matches := schedulerRegExp.FindSubmatch([]byte(blkSched))
  65. if len(matches) >= 2 {
  66. disk_info.Scheduler = string(matches[1])
  67. }
  68. }
  69. device := fmt.Sprintf("%d:%d", disk_info.Major, disk_info.Minor)
  70. diskMap[device] = disk_info
  71. }
  72. return diskMap, nil
  73. }
  74. // Get information about network devices present on the system.
  75. func GetNetworkDevices(sysfs sysfs.SysFs) ([]info.NetInfo, error) {
  76. devs, err := sysfs.GetNetworkDevices()
  77. if err != nil {
  78. return nil, err
  79. }
  80. netDevices := []info.NetInfo{}
  81. for _, dev := range devs {
  82. name := dev.Name()
  83. // Ignore docker, loopback, and veth devices.
  84. ignoredDevices := []string{"lo", "veth", "docker"}
  85. ignored := false
  86. for _, prefix := range ignoredDevices {
  87. if strings.HasPrefix(name, prefix) {
  88. ignored = true
  89. break
  90. }
  91. }
  92. if ignored {
  93. continue
  94. }
  95. address, err := sysfs.GetNetworkAddress(name)
  96. if err != nil {
  97. return nil, err
  98. }
  99. mtuStr, err := sysfs.GetNetworkMtu(name)
  100. if err != nil {
  101. return nil, err
  102. }
  103. var mtu int64
  104. n, err := fmt.Sscanf(mtuStr, "%d", &mtu)
  105. if err != nil || n != 1 {
  106. return nil, fmt.Errorf("could not parse mtu from %s for device %s", mtuStr, name)
  107. }
  108. netInfo := info.NetInfo{
  109. Name: name,
  110. MacAddress: strings.TrimSpace(address),
  111. Mtu: mtu,
  112. }
  113. speed, err := sysfs.GetNetworkSpeed(name)
  114. // Some devices don't set speed.
  115. if err == nil {
  116. var s int64
  117. n, err := fmt.Sscanf(speed, "%d", &s)
  118. if err != nil || n != 1 {
  119. return nil, fmt.Errorf("could not parse speed from %s for device %s", speed, name)
  120. }
  121. netInfo.Speed = s
  122. }
  123. netDevices = append(netDevices, netInfo)
  124. }
  125. return netDevices, nil
  126. }
  127. func GetCacheInfo(sysFs sysfs.SysFs, id int) ([]sysfs.CacheInfo, error) {
  128. caches, err := sysFs.GetCaches(id)
  129. if err != nil {
  130. return nil, err
  131. }
  132. info := []sysfs.CacheInfo{}
  133. for _, cache := range caches {
  134. if !strings.HasPrefix(cache.Name(), "index") {
  135. continue
  136. }
  137. cacheInfo, err := sysFs.GetCacheInfo(id, cache.Name())
  138. if err != nil {
  139. return nil, err
  140. }
  141. info = append(info, cacheInfo)
  142. }
  143. return info, nil
  144. }
  145. func GetNetworkStats(name string) (info.InterfaceStats, error) {
  146. // TODO(rjnagal): Take syfs as an argument.
  147. sysFs := sysfs.NewRealSysFs()
  148. return getNetworkStats(name, sysFs)
  149. }
  150. func getNetworkStats(name string, sysFs sysfs.SysFs) (info.InterfaceStats, error) {
  151. var stats info.InterfaceStats
  152. var err error
  153. stats.Name = name
  154. stats.RxBytes, err = sysFs.GetNetworkStatValue(name, "rx_bytes")
  155. if err != nil {
  156. return stats, err
  157. }
  158. stats.RxPackets, err = sysFs.GetNetworkStatValue(name, "rx_packets")
  159. if err != nil {
  160. return stats, err
  161. }
  162. stats.RxErrors, err = sysFs.GetNetworkStatValue(name, "rx_errors")
  163. if err != nil {
  164. return stats, err
  165. }
  166. stats.RxDropped, err = sysFs.GetNetworkStatValue(name, "rx_dropped")
  167. if err != nil {
  168. return stats, err
  169. }
  170. stats.TxBytes, err = sysFs.GetNetworkStatValue(name, "tx_bytes")
  171. if err != nil {
  172. return stats, err
  173. }
  174. stats.TxPackets, err = sysFs.GetNetworkStatValue(name, "tx_packets")
  175. if err != nil {
  176. return stats, err
  177. }
  178. stats.TxErrors, err = sysFs.GetNetworkStatValue(name, "tx_errors")
  179. if err != nil {
  180. return stats, err
  181. }
  182. stats.TxDropped, err = sysFs.GetNetworkStatValue(name, "tx_dropped")
  183. if err != nil {
  184. return stats, err
  185. }
  186. return stats, nil
  187. }
  188. func GetSystemUUID(sysFs sysfs.SysFs) (string, error) {
  189. return sysFs.GetSystemUUID()
  190. }