ipvs.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. // NewIPVSStats reads the IPVS statistics.
  61. func NewIPVSStats() (IPVSStats, error) {
  62. fs, err := NewFS(DefaultMountPoint)
  63. if err != nil {
  64. return IPVSStats{}, err
  65. }
  66. return fs.NewIPVSStats()
  67. }
  68. // NewIPVSStats reads the IPVS statistics from the specified `proc` filesystem.
  69. func (fs FS) NewIPVSStats() (IPVSStats, error) {
  70. file, err := os.Open(fs.Path("net/ip_vs_stats"))
  71. if err != nil {
  72. return IPVSStats{}, err
  73. }
  74. defer file.Close()
  75. return parseIPVSStats(file)
  76. }
  77. // parseIPVSStats performs the actual parsing of `ip_vs_stats`.
  78. func parseIPVSStats(file io.Reader) (IPVSStats, error) {
  79. var (
  80. statContent []byte
  81. statLines []string
  82. statFields []string
  83. stats IPVSStats
  84. )
  85. statContent, err := ioutil.ReadAll(file)
  86. if err != nil {
  87. return IPVSStats{}, err
  88. }
  89. statLines = strings.SplitN(string(statContent), "\n", 4)
  90. if len(statLines) != 4 {
  91. return IPVSStats{}, errors.New("ip_vs_stats corrupt: too short")
  92. }
  93. statFields = strings.Fields(statLines[2])
  94. if len(statFields) != 5 {
  95. return IPVSStats{}, errors.New("ip_vs_stats corrupt: unexpected number of fields")
  96. }
  97. stats.Connections, err = strconv.ParseUint(statFields[0], 16, 64)
  98. if err != nil {
  99. return IPVSStats{}, err
  100. }
  101. stats.IncomingPackets, err = strconv.ParseUint(statFields[1], 16, 64)
  102. if err != nil {
  103. return IPVSStats{}, err
  104. }
  105. stats.OutgoingPackets, err = strconv.ParseUint(statFields[2], 16, 64)
  106. if err != nil {
  107. return IPVSStats{}, err
  108. }
  109. stats.IncomingBytes, err = strconv.ParseUint(statFields[3], 16, 64)
  110. if err != nil {
  111. return IPVSStats{}, err
  112. }
  113. stats.OutgoingBytes, err = strconv.ParseUint(statFields[4], 16, 64)
  114. if err != nil {
  115. return IPVSStats{}, err
  116. }
  117. return stats, nil
  118. }
  119. // NewIPVSBackendStatus reads and returns the status of all (virtual,real) server pairs.
  120. func NewIPVSBackendStatus() ([]IPVSBackendStatus, error) {
  121. fs, err := NewFS(DefaultMountPoint)
  122. if err != nil {
  123. return []IPVSBackendStatus{}, err
  124. }
  125. return fs.NewIPVSBackendStatus()
  126. }
  127. // NewIPVSBackendStatus reads and returns the status of all (virtual,real) server pairs from the specified `proc` filesystem.
  128. func (fs FS) NewIPVSBackendStatus() ([]IPVSBackendStatus, error) {
  129. file, err := os.Open(fs.Path("net/ip_vs"))
  130. if err != nil {
  131. return nil, err
  132. }
  133. defer file.Close()
  134. return parseIPVSBackendStatus(file)
  135. }
  136. func parseIPVSBackendStatus(file io.Reader) ([]IPVSBackendStatus, error) {
  137. var (
  138. status []IPVSBackendStatus
  139. scanner = bufio.NewScanner(file)
  140. proto string
  141. localMark string
  142. localAddress net.IP
  143. localPort uint16
  144. err error
  145. )
  146. for scanner.Scan() {
  147. fields := strings.Fields(scanner.Text())
  148. if len(fields) == 0 {
  149. continue
  150. }
  151. switch {
  152. case fields[0] == "IP" || fields[0] == "Prot" || fields[1] == "RemoteAddress:Port":
  153. continue
  154. case fields[0] == "TCP" || fields[0] == "UDP":
  155. if len(fields) < 2 {
  156. continue
  157. }
  158. proto = fields[0]
  159. localMark = ""
  160. localAddress, localPort, err = parseIPPort(fields[1])
  161. if err != nil {
  162. return nil, err
  163. }
  164. case fields[0] == "FWM":
  165. if len(fields) < 2 {
  166. continue
  167. }
  168. proto = fields[0]
  169. localMark = fields[1]
  170. localAddress = nil
  171. localPort = 0
  172. case fields[0] == "->":
  173. if len(fields) < 6 {
  174. continue
  175. }
  176. remoteAddress, remotePort, err := parseIPPort(fields[1])
  177. if err != nil {
  178. return nil, err
  179. }
  180. weight, err := strconv.ParseUint(fields[3], 10, 64)
  181. if err != nil {
  182. return nil, err
  183. }
  184. activeConn, err := strconv.ParseUint(fields[4], 10, 64)
  185. if err != nil {
  186. return nil, err
  187. }
  188. inactConn, err := strconv.ParseUint(fields[5], 10, 64)
  189. if err != nil {
  190. return nil, err
  191. }
  192. status = append(status, IPVSBackendStatus{
  193. LocalAddress: localAddress,
  194. LocalPort: localPort,
  195. LocalMark: localMark,
  196. RemoteAddress: remoteAddress,
  197. RemotePort: remotePort,
  198. Proto: proto,
  199. Weight: weight,
  200. ActiveConn: activeConn,
  201. InactConn: inactConn,
  202. })
  203. }
  204. }
  205. return status, nil
  206. }
  207. func parseIPPort(s string) (net.IP, uint16, error) {
  208. var (
  209. ip net.IP
  210. err error
  211. )
  212. switch len(s) {
  213. case 13:
  214. ip, err = hex.DecodeString(s[0:8])
  215. if err != nil {
  216. return nil, 0, err
  217. }
  218. case 46:
  219. ip = net.ParseIP(s[1:40])
  220. if ip == nil {
  221. return nil, 0, fmt.Errorf("invalid IPv6 address: %s", s[1:40])
  222. }
  223. default:
  224. return nil, 0, fmt.Errorf("unexpected IP:Port: %s", s)
  225. }
  226. portString := s[len(s)-4:]
  227. if len(portString) != 4 {
  228. return nil, 0, fmt.Errorf("unexpected port string format: %s", portString)
  229. }
  230. port, err := strconv.ParseUint(portString, 16, 16)
  231. if err != nil {
  232. return nil, 0, err
  233. }
  234. return ip, uint16(port), nil
  235. }