ipvs.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. "bytes"
  17. "encoding/hex"
  18. "errors"
  19. "fmt"
  20. "io"
  21. "net"
  22. "os"
  23. "strconv"
  24. "strings"
  25. "github.com/prometheus/procfs/internal/util"
  26. )
  27. // IPVSStats holds IPVS statistics, as exposed by the kernel in `/proc/net/ip_vs_stats`.
  28. type IPVSStats struct {
  29. // Total count of connections.
  30. Connections uint64
  31. // Total incoming packages processed.
  32. IncomingPackets uint64
  33. // Total outgoing packages processed.
  34. OutgoingPackets uint64
  35. // Total incoming traffic.
  36. IncomingBytes uint64
  37. // Total outgoing traffic.
  38. OutgoingBytes uint64
  39. }
  40. // IPVSBackendStatus holds current metrics of one virtual / real address pair.
  41. type IPVSBackendStatus struct {
  42. // The local (virtual) IP address.
  43. LocalAddress net.IP
  44. // The remote (real) IP address.
  45. RemoteAddress net.IP
  46. // The local (virtual) port.
  47. LocalPort uint16
  48. // The remote (real) port.
  49. RemotePort uint16
  50. // The local firewall mark
  51. LocalMark string
  52. // The transport protocol (TCP, UDP).
  53. Proto string
  54. // The current number of active connections for this virtual/real address pair.
  55. ActiveConn uint64
  56. // The current number of inactive connections for this virtual/real address pair.
  57. InactConn uint64
  58. // The current weight of this virtual/real address pair.
  59. Weight uint64
  60. }
  61. // IPVSStats reads the IPVS statistics from the specified `proc` filesystem.
  62. func (fs FS) IPVSStats() (IPVSStats, error) {
  63. data, err := util.ReadFileNoStat(fs.proc.Path("net/ip_vs_stats"))
  64. if err != nil {
  65. return IPVSStats{}, err
  66. }
  67. return parseIPVSStats(bytes.NewReader(data))
  68. }
  69. // parseIPVSStats performs the actual parsing of `ip_vs_stats`.
  70. func parseIPVSStats(r io.Reader) (IPVSStats, error) {
  71. var (
  72. statContent []byte
  73. statLines []string
  74. statFields []string
  75. stats IPVSStats
  76. )
  77. statContent, err := io.ReadAll(r)
  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("%s: Invalid IPv6 addr %s: %w", ErrFileParse, s[1:40], err)
  206. }
  207. default:
  208. return nil, 0, fmt.Errorf("%s: Unexpected IP:Port %s: %w", ErrFileParse, s, err)
  209. }
  210. portString := s[len(s)-4:]
  211. if len(portString) != 4 {
  212. return nil, 0,
  213. fmt.Errorf("%s: Unexpected port string format %s: %w", ErrFileParse, portString, err)
  214. }
  215. port, err := strconv.ParseUint(portString, 16, 16)
  216. if err != nil {
  217. return nil, 0, err
  218. }
  219. return ip, uint16(port), nil
  220. }