proc_snmp.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // Copyright 2022 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. "fmt"
  18. "io"
  19. "strconv"
  20. "strings"
  21. "github.com/prometheus/procfs/internal/util"
  22. )
  23. // ProcSnmp models the content of /proc/<pid>/net/snmp.
  24. type ProcSnmp struct {
  25. // The process ID.
  26. PID int
  27. Ip
  28. Icmp
  29. IcmpMsg
  30. Tcp
  31. Udp
  32. UdpLite
  33. }
  34. type Ip struct { // nolint:revive
  35. Forwarding *float64
  36. DefaultTTL *float64
  37. InReceives *float64
  38. InHdrErrors *float64
  39. InAddrErrors *float64
  40. ForwDatagrams *float64
  41. InUnknownProtos *float64
  42. InDiscards *float64
  43. InDelivers *float64
  44. OutRequests *float64
  45. OutDiscards *float64
  46. OutNoRoutes *float64
  47. ReasmTimeout *float64
  48. ReasmReqds *float64
  49. ReasmOKs *float64
  50. ReasmFails *float64
  51. FragOKs *float64
  52. FragFails *float64
  53. FragCreates *float64
  54. }
  55. type Icmp struct { // nolint:revive
  56. InMsgs *float64
  57. InErrors *float64
  58. InCsumErrors *float64
  59. InDestUnreachs *float64
  60. InTimeExcds *float64
  61. InParmProbs *float64
  62. InSrcQuenchs *float64
  63. InRedirects *float64
  64. InEchos *float64
  65. InEchoReps *float64
  66. InTimestamps *float64
  67. InTimestampReps *float64
  68. InAddrMasks *float64
  69. InAddrMaskReps *float64
  70. OutMsgs *float64
  71. OutErrors *float64
  72. OutDestUnreachs *float64
  73. OutTimeExcds *float64
  74. OutParmProbs *float64
  75. OutSrcQuenchs *float64
  76. OutRedirects *float64
  77. OutEchos *float64
  78. OutEchoReps *float64
  79. OutTimestamps *float64
  80. OutTimestampReps *float64
  81. OutAddrMasks *float64
  82. OutAddrMaskReps *float64
  83. }
  84. type IcmpMsg struct {
  85. InType3 *float64
  86. OutType3 *float64
  87. }
  88. type Tcp struct { // nolint:revive
  89. RtoAlgorithm *float64
  90. RtoMin *float64
  91. RtoMax *float64
  92. MaxConn *float64
  93. ActiveOpens *float64
  94. PassiveOpens *float64
  95. AttemptFails *float64
  96. EstabResets *float64
  97. CurrEstab *float64
  98. InSegs *float64
  99. OutSegs *float64
  100. RetransSegs *float64
  101. InErrs *float64
  102. OutRsts *float64
  103. InCsumErrors *float64
  104. }
  105. type Udp struct { // nolint:revive
  106. InDatagrams *float64
  107. NoPorts *float64
  108. InErrors *float64
  109. OutDatagrams *float64
  110. RcvbufErrors *float64
  111. SndbufErrors *float64
  112. InCsumErrors *float64
  113. IgnoredMulti *float64
  114. }
  115. type UdpLite struct { // nolint:revive
  116. InDatagrams *float64
  117. NoPorts *float64
  118. InErrors *float64
  119. OutDatagrams *float64
  120. RcvbufErrors *float64
  121. SndbufErrors *float64
  122. InCsumErrors *float64
  123. IgnoredMulti *float64
  124. }
  125. func (p Proc) Snmp() (ProcSnmp, error) {
  126. filename := p.path("net/snmp")
  127. data, err := util.ReadFileNoStat(filename)
  128. if err != nil {
  129. return ProcSnmp{PID: p.PID}, err
  130. }
  131. procSnmp, err := parseSnmp(bytes.NewReader(data), filename)
  132. procSnmp.PID = p.PID
  133. return procSnmp, err
  134. }
  135. // parseSnmp parses the metrics from proc/<pid>/net/snmp file
  136. // and returns a map contains those metrics (e.g. {"Ip": {"Forwarding": 2}}).
  137. func parseSnmp(r io.Reader, fileName string) (ProcSnmp, error) {
  138. var (
  139. scanner = bufio.NewScanner(r)
  140. procSnmp = ProcSnmp{}
  141. )
  142. for scanner.Scan() {
  143. nameParts := strings.Split(scanner.Text(), " ")
  144. scanner.Scan()
  145. valueParts := strings.Split(scanner.Text(), " ")
  146. // Remove trailing :.
  147. protocol := strings.TrimSuffix(nameParts[0], ":")
  148. if len(nameParts) != len(valueParts) {
  149. return procSnmp, fmt.Errorf("%w: mismatch field count mismatch in %s: %s",
  150. ErrFileParse, fileName, protocol)
  151. }
  152. for i := 1; i < len(nameParts); i++ {
  153. value, err := strconv.ParseFloat(valueParts[i], 64)
  154. if err != nil {
  155. return procSnmp, err
  156. }
  157. key := nameParts[i]
  158. switch protocol {
  159. case "Ip":
  160. switch key {
  161. case "Forwarding":
  162. procSnmp.Ip.Forwarding = &value
  163. case "DefaultTTL":
  164. procSnmp.Ip.DefaultTTL = &value
  165. case "InReceives":
  166. procSnmp.Ip.InReceives = &value
  167. case "InHdrErrors":
  168. procSnmp.Ip.InHdrErrors = &value
  169. case "InAddrErrors":
  170. procSnmp.Ip.InAddrErrors = &value
  171. case "ForwDatagrams":
  172. procSnmp.Ip.ForwDatagrams = &value
  173. case "InUnknownProtos":
  174. procSnmp.Ip.InUnknownProtos = &value
  175. case "InDiscards":
  176. procSnmp.Ip.InDiscards = &value
  177. case "InDelivers":
  178. procSnmp.Ip.InDelivers = &value
  179. case "OutRequests":
  180. procSnmp.Ip.OutRequests = &value
  181. case "OutDiscards":
  182. procSnmp.Ip.OutDiscards = &value
  183. case "OutNoRoutes":
  184. procSnmp.Ip.OutNoRoutes = &value
  185. case "ReasmTimeout":
  186. procSnmp.Ip.ReasmTimeout = &value
  187. case "ReasmReqds":
  188. procSnmp.Ip.ReasmReqds = &value
  189. case "ReasmOKs":
  190. procSnmp.Ip.ReasmOKs = &value
  191. case "ReasmFails":
  192. procSnmp.Ip.ReasmFails = &value
  193. case "FragOKs":
  194. procSnmp.Ip.FragOKs = &value
  195. case "FragFails":
  196. procSnmp.Ip.FragFails = &value
  197. case "FragCreates":
  198. procSnmp.Ip.FragCreates = &value
  199. }
  200. case "Icmp":
  201. switch key {
  202. case "InMsgs":
  203. procSnmp.Icmp.InMsgs = &value
  204. case "InErrors":
  205. procSnmp.Icmp.InErrors = &value
  206. case "InCsumErrors":
  207. procSnmp.Icmp.InCsumErrors = &value
  208. case "InDestUnreachs":
  209. procSnmp.Icmp.InDestUnreachs = &value
  210. case "InTimeExcds":
  211. procSnmp.Icmp.InTimeExcds = &value
  212. case "InParmProbs":
  213. procSnmp.Icmp.InParmProbs = &value
  214. case "InSrcQuenchs":
  215. procSnmp.Icmp.InSrcQuenchs = &value
  216. case "InRedirects":
  217. procSnmp.Icmp.InRedirects = &value
  218. case "InEchos":
  219. procSnmp.Icmp.InEchos = &value
  220. case "InEchoReps":
  221. procSnmp.Icmp.InEchoReps = &value
  222. case "InTimestamps":
  223. procSnmp.Icmp.InTimestamps = &value
  224. case "InTimestampReps":
  225. procSnmp.Icmp.InTimestampReps = &value
  226. case "InAddrMasks":
  227. procSnmp.Icmp.InAddrMasks = &value
  228. case "InAddrMaskReps":
  229. procSnmp.Icmp.InAddrMaskReps = &value
  230. case "OutMsgs":
  231. procSnmp.Icmp.OutMsgs = &value
  232. case "OutErrors":
  233. procSnmp.Icmp.OutErrors = &value
  234. case "OutDestUnreachs":
  235. procSnmp.Icmp.OutDestUnreachs = &value
  236. case "OutTimeExcds":
  237. procSnmp.Icmp.OutTimeExcds = &value
  238. case "OutParmProbs":
  239. procSnmp.Icmp.OutParmProbs = &value
  240. case "OutSrcQuenchs":
  241. procSnmp.Icmp.OutSrcQuenchs = &value
  242. case "OutRedirects":
  243. procSnmp.Icmp.OutRedirects = &value
  244. case "OutEchos":
  245. procSnmp.Icmp.OutEchos = &value
  246. case "OutEchoReps":
  247. procSnmp.Icmp.OutEchoReps = &value
  248. case "OutTimestamps":
  249. procSnmp.Icmp.OutTimestamps = &value
  250. case "OutTimestampReps":
  251. procSnmp.Icmp.OutTimestampReps = &value
  252. case "OutAddrMasks":
  253. procSnmp.Icmp.OutAddrMasks = &value
  254. case "OutAddrMaskReps":
  255. procSnmp.Icmp.OutAddrMaskReps = &value
  256. }
  257. case "IcmpMsg":
  258. switch key {
  259. case "InType3":
  260. procSnmp.IcmpMsg.InType3 = &value
  261. case "OutType3":
  262. procSnmp.IcmpMsg.OutType3 = &value
  263. }
  264. case "Tcp":
  265. switch key {
  266. case "RtoAlgorithm":
  267. procSnmp.Tcp.RtoAlgorithm = &value
  268. case "RtoMin":
  269. procSnmp.Tcp.RtoMin = &value
  270. case "RtoMax":
  271. procSnmp.Tcp.RtoMax = &value
  272. case "MaxConn":
  273. procSnmp.Tcp.MaxConn = &value
  274. case "ActiveOpens":
  275. procSnmp.Tcp.ActiveOpens = &value
  276. case "PassiveOpens":
  277. procSnmp.Tcp.PassiveOpens = &value
  278. case "AttemptFails":
  279. procSnmp.Tcp.AttemptFails = &value
  280. case "EstabResets":
  281. procSnmp.Tcp.EstabResets = &value
  282. case "CurrEstab":
  283. procSnmp.Tcp.CurrEstab = &value
  284. case "InSegs":
  285. procSnmp.Tcp.InSegs = &value
  286. case "OutSegs":
  287. procSnmp.Tcp.OutSegs = &value
  288. case "RetransSegs":
  289. procSnmp.Tcp.RetransSegs = &value
  290. case "InErrs":
  291. procSnmp.Tcp.InErrs = &value
  292. case "OutRsts":
  293. procSnmp.Tcp.OutRsts = &value
  294. case "InCsumErrors":
  295. procSnmp.Tcp.InCsumErrors = &value
  296. }
  297. case "Udp":
  298. switch key {
  299. case "InDatagrams":
  300. procSnmp.Udp.InDatagrams = &value
  301. case "NoPorts":
  302. procSnmp.Udp.NoPorts = &value
  303. case "InErrors":
  304. procSnmp.Udp.InErrors = &value
  305. case "OutDatagrams":
  306. procSnmp.Udp.OutDatagrams = &value
  307. case "RcvbufErrors":
  308. procSnmp.Udp.RcvbufErrors = &value
  309. case "SndbufErrors":
  310. procSnmp.Udp.SndbufErrors = &value
  311. case "InCsumErrors":
  312. procSnmp.Udp.InCsumErrors = &value
  313. case "IgnoredMulti":
  314. procSnmp.Udp.IgnoredMulti = &value
  315. }
  316. case "UdpLite":
  317. switch key {
  318. case "InDatagrams":
  319. procSnmp.UdpLite.InDatagrams = &value
  320. case "NoPorts":
  321. procSnmp.UdpLite.NoPorts = &value
  322. case "InErrors":
  323. procSnmp.UdpLite.InErrors = &value
  324. case "OutDatagrams":
  325. procSnmp.UdpLite.OutDatagrams = &value
  326. case "RcvbufErrors":
  327. procSnmp.UdpLite.RcvbufErrors = &value
  328. case "SndbufErrors":
  329. procSnmp.UdpLite.SndbufErrors = &value
  330. case "InCsumErrors":
  331. procSnmp.UdpLite.InCsumErrors = &value
  332. case "IgnoredMulti":
  333. procSnmp.UdpLite.IgnoredMulti = &value
  334. }
  335. }
  336. }
  337. }
  338. return procSnmp, scanner.Err()
  339. }