You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
5.9 KiB

  1. // Copyright 2018 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package procfs
  14. import (
  15. "bufio"
  16. "encoding/hex"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "net"
  22. "os"
  23. "strconv"
  24. "strings"
  25. )
  26. // IPVSStats holds IPVS statistics, as exposed by the kernel in `/proc/net/ip_vs_stats`.
  27. type IPVSStats struct {
  28. // Total count of connections.
  29. Connections uint64
  30. // Total incoming packages processed.
  31. IncomingPackets uint64
  32. // Total outgoing packages processed.
  33. OutgoingPackets uint64
  34. // Total incoming traffic.
  35. IncomingBytes uint64
  36. // Total outgoing traffic.
  37. OutgoingBytes uint64
  38. }
  39. // IPVSBackendStatus holds current metrics of one virtual / real address pair.
  40. type IPVSBackendStatus struct {
  41. // The local (virtual) IP address.
  42. LocalAddress net.IP
  43. // The remote (real) IP address.
  44. RemoteAddress net.IP
  45. // The local (virtual) port.
  46. LocalPort uint16
  47. // The remote (real) port.
  48. RemotePort uint16
  49. // The local firewall mark
  50. LocalMark string
  51. // The transport protocol (TCP, UDP).
  52. Proto string
  53. // The current number of active connections for this virtual/real address pair.
  54. ActiveConn uint64
  55. // The current number of inactive connections for this virtual/real address pair.
  56. InactConn uint64
  57. // The current weight of this virtual/real address pair.
  58. Weight uint64
  59. }
  60. // IPVSStats reads the IPVS statistics from the specified `proc` filesystem.
  61. func (fs FS) IPVSStats() (IPVSStats, error) {
  62. file, err := os.Open(fs.proc.Path("net/ip_vs_stats"))
  63. if err != nil {
  64. return IPVSStats{}, err
  65. }
  66. defer file.Close()
  67. return parseIPVSStats(file)
  68. }
  69. // parseIPVSStats performs the actual parsing of `ip_vs_stats`.
  70. func parseIPVSStats(file io.Reader) (IPVSStats, error) {
  71. var (
  72. statContent []byte
  73. statLines []string
  74. statFields []string
  75. stats IPVSStats
  76. )
  77. statContent, err := ioutil.ReadAll(file)
  78. if err != nil {
  79. return IPVSStats{}, err
  80. }
  81. statLines = strings.SplitN(string(statContent), "\n", 4)
  82. if len(statLines) != 4 {
  83. return IPVSStats{}, errors.New("ip_vs_stats corrupt: too short")
  84. }
  85. statFields = strings.Fields(statLines[2])
  86. if len(statFields) != 5 {
  87. return IPVSStats{}, errors.New("ip_vs_stats corrupt: unexpected number of fields")
  88. }
  89. stats.Connections, err = strconv.ParseUint(statFields[0], 16, 64)
  90. if err != nil {
  91. return IPVSStats{}, err
  92. }
  93. stats.IncomingPackets, err = strconv.ParseUint(statFields[1], 16, 64)
  94. if err != nil {
  95. return IPVSStats{}, err
  96. }
  97. stats.OutgoingPackets, err = strconv.ParseUint(statFields[2], 16, 64)
  98. if err != nil {
  99. return IPVSStats{}, err
  100. }
  101. stats.IncomingBytes, err = strconv.ParseUint(statFields[3], 16, 64)
  102. if err != nil {
  103. return IPVSStats{}, err
  104. }
  105. stats.OutgoingBytes, err = strconv.ParseUint(statFields[4], 16, 64)
  106. if err != nil {
  107. return IPVSStats{}, err
  108. }
  109. return stats, nil
  110. }
  111. // IPVSBackendStatus reads and returns the status of all (virtual,real) server pairs from the specified `proc` filesystem.
  112. func (fs FS) IPVSBackendStatus() ([]IPVSBackendStatus, error) {
  113. file, err := os.Open(fs.proc.Path("net/ip_vs"))
  114. if err != nil {
  115. return nil, err
  116. }
  117. defer file.Close()
  118. return parseIPVSBackendStatus(file)
  119. }
  120. func parseIPVSBackendStatus(file io.Reader) ([]IPVSBackendStatus, error) {
  121. var (
  122. status []IPVSBackendStatus
  123. scanner = bufio.NewScanner(file)
  124. proto string
  125. localMark string
  126. localAddress net.IP
  127. localPort uint16
  128. err error
  129. )
  130. for scanner.Scan() {
  131. fields := strings.Fields(scanner.Text())
  132. if len(fields) == 0 {
  133. continue
  134. }
  135. switch {
  136. case fields[0] == "IP" || fields[0] == "Prot" || fields[1] == "RemoteAddress:Port":
  137. continue
  138. case fields[0] == "TCP" || fields[0] == "UDP":
  139. if len(fields) < 2 {
  140. continue
  141. }
  142. proto = fields[0]
  143. localMark = ""
  144. localAddress, localPort, err = parseIPPort(fields[1])
  145. if err != nil {
  146. return nil, err
  147. }
  148. case fields[0] == "FWM":
  149. if len(fields) < 2 {
  150. continue
  151. }
  152. proto = fields[0]
  153. localMark = fields[1]
  154. localAddress = nil
  155. localPort = 0
  156. case fields[0] == "->":
  157. if len(fields) < 6 {
  158. continue
  159. }
  160. remoteAddress, remotePort, err := parseIPPort(fields[1])
  161. if err != nil {
  162. return nil, err
  163. }
  164. weight, err := strconv.ParseUint(fields[3], 10, 64)
  165. if err != nil {
  166. return nil, err
  167. }
  168. activeConn, err := strconv.ParseUint(fields[4], 10, 64)
  169. if err != nil {
  170. return nil, err
  171. }
  172. inactConn, err := strconv.ParseUint(fields[5], 10, 64)
  173. if err != nil {
  174. return nil, err
  175. }
  176. status = append(status, IPVSBackendStatus{
  177. LocalAddress: localAddress,
  178. LocalPort: localPort,
  179. LocalMark: localMark,
  180. RemoteAddress: remoteAddress,
  181. RemotePort: remotePort,
  182. Proto: proto,
  183. Weight: weight,
  184. ActiveConn: activeConn,
  185. InactConn: inactConn,
  186. })
  187. }
  188. }
  189. return status, nil
  190. }
  191. func parseIPPort(s string) (net.IP, uint16, error) {
  192. var (
  193. ip net.IP
  194. err error
  195. )
  196. switch len(s) {
  197. case 13:
  198. ip, err = hex.DecodeString(s[0:8])
  199. if err != nil {
  200. return nil, 0, err
  201. }
  202. case 46:
  203. ip = net.ParseIP(s[1:40])
  204. if ip == nil {
  205. return nil, 0, fmt.Errorf("invalid IPv6 address: %s", s[1:40])
  206. }
  207. default:
  208. return nil, 0, fmt.Errorf("unexpected IP:Port: %s", s)
  209. }
  210. portString := s[len(s)-4:]
  211. if len(portString) != 4 {
  212. return nil, 0, fmt.Errorf("unexpected port string format: %s", portString)
  213. }
  214. port, err := strconv.ParseUint(portString, 16, 16)
  215. if err != nil {
  216. return nil, 0, err
  217. }
  218. return ip, uint16(port), nil
  219. }