proc_snmp6.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. "errors"
  18. "io"
  19. "os"
  20. "strconv"
  21. "strings"
  22. "github.com/prometheus/procfs/internal/util"
  23. )
  24. // ProcSnmp6 models the content of /proc/<pid>/net/snmp6.
  25. type ProcSnmp6 struct {
  26. // The process ID.
  27. PID int
  28. Ip6
  29. Icmp6
  30. Udp6
  31. UdpLite6
  32. }
  33. type Ip6 struct { // nolint:revive
  34. InReceives float64
  35. InHdrErrors float64
  36. InTooBigErrors float64
  37. InNoRoutes float64
  38. InAddrErrors float64
  39. InUnknownProtos float64
  40. InTruncatedPkts float64
  41. InDiscards float64
  42. InDelivers float64
  43. OutForwDatagrams 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. InMcastPkts float64
  55. OutMcastPkts float64
  56. InOctets float64
  57. OutOctets float64
  58. InMcastOctets float64
  59. OutMcastOctets float64
  60. InBcastOctets float64
  61. OutBcastOctets float64
  62. InNoECTPkts float64
  63. InECT1Pkts float64
  64. InECT0Pkts float64
  65. InCEPkts float64
  66. }
  67. type Icmp6 struct {
  68. InMsgs float64
  69. InErrors float64
  70. OutMsgs float64
  71. OutErrors float64
  72. InCsumErrors float64
  73. InDestUnreachs float64
  74. InPktTooBigs float64
  75. InTimeExcds float64
  76. InParmProblems float64
  77. InEchos float64
  78. InEchoReplies float64
  79. InGroupMembQueries float64
  80. InGroupMembResponses float64
  81. InGroupMembReductions float64
  82. InRouterSolicits float64
  83. InRouterAdvertisements float64
  84. InNeighborSolicits float64
  85. InNeighborAdvertisements float64
  86. InRedirects float64
  87. InMLDv2Reports float64
  88. OutDestUnreachs float64
  89. OutPktTooBigs float64
  90. OutTimeExcds float64
  91. OutParmProblems float64
  92. OutEchos float64
  93. OutEchoReplies float64
  94. OutGroupMembQueries float64
  95. OutGroupMembResponses float64
  96. OutGroupMembReductions float64
  97. OutRouterSolicits float64
  98. OutRouterAdvertisements float64
  99. OutNeighborSolicits float64
  100. OutNeighborAdvertisements float64
  101. OutRedirects float64
  102. OutMLDv2Reports float64
  103. InType1 float64
  104. InType134 float64
  105. InType135 float64
  106. InType136 float64
  107. InType143 float64
  108. OutType133 float64
  109. OutType135 float64
  110. OutType136 float64
  111. OutType143 float64
  112. }
  113. type Udp6 struct { // nolint:revive
  114. InDatagrams float64
  115. NoPorts float64
  116. InErrors float64
  117. OutDatagrams float64
  118. RcvbufErrors float64
  119. SndbufErrors float64
  120. InCsumErrors float64
  121. IgnoredMulti float64
  122. }
  123. type UdpLite6 struct { // nolint:revive
  124. InDatagrams float64
  125. NoPorts float64
  126. InErrors float64
  127. OutDatagrams float64
  128. RcvbufErrors float64
  129. SndbufErrors float64
  130. InCsumErrors float64
  131. }
  132. func (p Proc) Snmp6() (ProcSnmp6, error) {
  133. filename := p.path("net/snmp6")
  134. data, err := util.ReadFileNoStat(filename)
  135. if err != nil {
  136. // On systems with IPv6 disabled, this file won't exist.
  137. // Do nothing.
  138. if errors.Is(err, os.ErrNotExist) {
  139. return ProcSnmp6{PID: p.PID}, nil
  140. }
  141. return ProcSnmp6{PID: p.PID}, err
  142. }
  143. procSnmp6, err := parseSNMP6Stats(bytes.NewReader(data))
  144. procSnmp6.PID = p.PID
  145. return procSnmp6, err
  146. }
  147. // parseSnmp6 parses the metrics from proc/<pid>/net/snmp6 file
  148. // and returns a map contains those metrics.
  149. func parseSNMP6Stats(r io.Reader) (ProcSnmp6, error) {
  150. var (
  151. scanner = bufio.NewScanner(r)
  152. procSnmp6 = ProcSnmp6{}
  153. )
  154. for scanner.Scan() {
  155. stat := strings.Fields(scanner.Text())
  156. if len(stat) < 2 {
  157. continue
  158. }
  159. // Expect to have "6" in metric name, skip line otherwise
  160. if sixIndex := strings.Index(stat[0], "6"); sixIndex != -1 {
  161. protocol := stat[0][:sixIndex+1]
  162. key := stat[0][sixIndex+1:]
  163. value, err := strconv.ParseFloat(stat[1], 64)
  164. if err != nil {
  165. return procSnmp6, err
  166. }
  167. switch protocol {
  168. case "Ip6":
  169. switch key {
  170. case "InReceives":
  171. procSnmp6.Ip6.InReceives = value
  172. case "InHdrErrors":
  173. procSnmp6.Ip6.InHdrErrors = value
  174. case "InTooBigErrors":
  175. procSnmp6.Ip6.InTooBigErrors = value
  176. case "InNoRoutes":
  177. procSnmp6.Ip6.InNoRoutes = value
  178. case "InAddrErrors":
  179. procSnmp6.Ip6.InAddrErrors = value
  180. case "InUnknownProtos":
  181. procSnmp6.Ip6.InUnknownProtos = value
  182. case "InTruncatedPkts":
  183. procSnmp6.Ip6.InTruncatedPkts = value
  184. case "InDiscards":
  185. procSnmp6.Ip6.InDiscards = value
  186. case "InDelivers":
  187. procSnmp6.Ip6.InDelivers = value
  188. case "OutForwDatagrams":
  189. procSnmp6.Ip6.OutForwDatagrams = value
  190. case "OutRequests":
  191. procSnmp6.Ip6.OutRequests = value
  192. case "OutDiscards":
  193. procSnmp6.Ip6.OutDiscards = value
  194. case "OutNoRoutes":
  195. procSnmp6.Ip6.OutNoRoutes = value
  196. case "ReasmTimeout":
  197. procSnmp6.Ip6.ReasmTimeout = value
  198. case "ReasmReqds":
  199. procSnmp6.Ip6.ReasmReqds = value
  200. case "ReasmOKs":
  201. procSnmp6.Ip6.ReasmOKs = value
  202. case "ReasmFails":
  203. procSnmp6.Ip6.ReasmFails = value
  204. case "FragOKs":
  205. procSnmp6.Ip6.FragOKs = value
  206. case "FragFails":
  207. procSnmp6.Ip6.FragFails = value
  208. case "FragCreates":
  209. procSnmp6.Ip6.FragCreates = value
  210. case "InMcastPkts":
  211. procSnmp6.Ip6.InMcastPkts = value
  212. case "OutMcastPkts":
  213. procSnmp6.Ip6.OutMcastPkts = value
  214. case "InOctets":
  215. procSnmp6.Ip6.InOctets = value
  216. case "OutOctets":
  217. procSnmp6.Ip6.OutOctets = value
  218. case "InMcastOctets":
  219. procSnmp6.Ip6.InMcastOctets = value
  220. case "OutMcastOctets":
  221. procSnmp6.Ip6.OutMcastOctets = value
  222. case "InBcastOctets":
  223. procSnmp6.Ip6.InBcastOctets = value
  224. case "OutBcastOctets":
  225. procSnmp6.Ip6.OutBcastOctets = value
  226. case "InNoECTPkts":
  227. procSnmp6.Ip6.InNoECTPkts = value
  228. case "InECT1Pkts":
  229. procSnmp6.Ip6.InECT1Pkts = value
  230. case "InECT0Pkts":
  231. procSnmp6.Ip6.InECT0Pkts = value
  232. case "InCEPkts":
  233. procSnmp6.Ip6.InCEPkts = value
  234. }
  235. case "Icmp6":
  236. switch key {
  237. case "InMsgs":
  238. procSnmp6.Icmp6.InMsgs = value
  239. case "InErrors":
  240. procSnmp6.Icmp6.InErrors = value
  241. case "OutMsgs":
  242. procSnmp6.Icmp6.OutMsgs = value
  243. case "OutErrors":
  244. procSnmp6.Icmp6.OutErrors = value
  245. case "InCsumErrors":
  246. procSnmp6.Icmp6.InCsumErrors = value
  247. case "InDestUnreachs":
  248. procSnmp6.Icmp6.InDestUnreachs = value
  249. case "InPktTooBigs":
  250. procSnmp6.Icmp6.InPktTooBigs = value
  251. case "InTimeExcds":
  252. procSnmp6.Icmp6.InTimeExcds = value
  253. case "InParmProblems":
  254. procSnmp6.Icmp6.InParmProblems = value
  255. case "InEchos":
  256. procSnmp6.Icmp6.InEchos = value
  257. case "InEchoReplies":
  258. procSnmp6.Icmp6.InEchoReplies = value
  259. case "InGroupMembQueries":
  260. procSnmp6.Icmp6.InGroupMembQueries = value
  261. case "InGroupMembResponses":
  262. procSnmp6.Icmp6.InGroupMembResponses = value
  263. case "InGroupMembReductions":
  264. procSnmp6.Icmp6.InGroupMembReductions = value
  265. case "InRouterSolicits":
  266. procSnmp6.Icmp6.InRouterSolicits = value
  267. case "InRouterAdvertisements":
  268. procSnmp6.Icmp6.InRouterAdvertisements = value
  269. case "InNeighborSolicits":
  270. procSnmp6.Icmp6.InNeighborSolicits = value
  271. case "InNeighborAdvertisements":
  272. procSnmp6.Icmp6.InNeighborAdvertisements = value
  273. case "InRedirects":
  274. procSnmp6.Icmp6.InRedirects = value
  275. case "InMLDv2Reports":
  276. procSnmp6.Icmp6.InMLDv2Reports = value
  277. case "OutDestUnreachs":
  278. procSnmp6.Icmp6.OutDestUnreachs = value
  279. case "OutPktTooBigs":
  280. procSnmp6.Icmp6.OutPktTooBigs = value
  281. case "OutTimeExcds":
  282. procSnmp6.Icmp6.OutTimeExcds = value
  283. case "OutParmProblems":
  284. procSnmp6.Icmp6.OutParmProblems = value
  285. case "OutEchos":
  286. procSnmp6.Icmp6.OutEchos = value
  287. case "OutEchoReplies":
  288. procSnmp6.Icmp6.OutEchoReplies = value
  289. case "OutGroupMembQueries":
  290. procSnmp6.Icmp6.OutGroupMembQueries = value
  291. case "OutGroupMembResponses":
  292. procSnmp6.Icmp6.OutGroupMembResponses = value
  293. case "OutGroupMembReductions":
  294. procSnmp6.Icmp6.OutGroupMembReductions = value
  295. case "OutRouterSolicits":
  296. procSnmp6.Icmp6.OutRouterSolicits = value
  297. case "OutRouterAdvertisements":
  298. procSnmp6.Icmp6.OutRouterAdvertisements = value
  299. case "OutNeighborSolicits":
  300. procSnmp6.Icmp6.OutNeighborSolicits = value
  301. case "OutNeighborAdvertisements":
  302. procSnmp6.Icmp6.OutNeighborAdvertisements = value
  303. case "OutRedirects":
  304. procSnmp6.Icmp6.OutRedirects = value
  305. case "OutMLDv2Reports":
  306. procSnmp6.Icmp6.OutMLDv2Reports = value
  307. case "InType1":
  308. procSnmp6.Icmp6.InType1 = value
  309. case "InType134":
  310. procSnmp6.Icmp6.InType134 = value
  311. case "InType135":
  312. procSnmp6.Icmp6.InType135 = value
  313. case "InType136":
  314. procSnmp6.Icmp6.InType136 = value
  315. case "InType143":
  316. procSnmp6.Icmp6.InType143 = value
  317. case "OutType133":
  318. procSnmp6.Icmp6.OutType133 = value
  319. case "OutType135":
  320. procSnmp6.Icmp6.OutType135 = value
  321. case "OutType136":
  322. procSnmp6.Icmp6.OutType136 = value
  323. case "OutType143":
  324. procSnmp6.Icmp6.OutType143 = value
  325. }
  326. case "Udp6":
  327. switch key {
  328. case "InDatagrams":
  329. procSnmp6.Udp6.InDatagrams = value
  330. case "NoPorts":
  331. procSnmp6.Udp6.NoPorts = value
  332. case "InErrors":
  333. procSnmp6.Udp6.InErrors = value
  334. case "OutDatagrams":
  335. procSnmp6.Udp6.OutDatagrams = value
  336. case "RcvbufErrors":
  337. procSnmp6.Udp6.RcvbufErrors = value
  338. case "SndbufErrors":
  339. procSnmp6.Udp6.SndbufErrors = value
  340. case "InCsumErrors":
  341. procSnmp6.Udp6.InCsumErrors = value
  342. case "IgnoredMulti":
  343. procSnmp6.Udp6.IgnoredMulti = value
  344. }
  345. case "UdpLite6":
  346. switch key {
  347. case "InDatagrams":
  348. procSnmp6.UdpLite6.InDatagrams = value
  349. case "NoPorts":
  350. procSnmp6.UdpLite6.NoPorts = value
  351. case "InErrors":
  352. procSnmp6.UdpLite6.InErrors = value
  353. case "OutDatagrams":
  354. procSnmp6.UdpLite6.OutDatagrams = value
  355. case "RcvbufErrors":
  356. procSnmp6.UdpLite6.RcvbufErrors = value
  357. case "SndbufErrors":
  358. procSnmp6.UdpLite6.SndbufErrors = value
  359. case "InCsumErrors":
  360. procSnmp6.UdpLite6.InCsumErrors = value
  361. }
  362. }
  363. }
  364. }
  365. return procSnmp6, scanner.Err()
  366. }