net_dev_snmp6.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "errors"
  17. "io"
  18. "os"
  19. "strconv"
  20. "strings"
  21. )
  22. // NetDevSNMP6 is parsed from files in /proc/net/dev_snmp6/ or /proc/<PID>/net/dev_snmp6/.
  23. // The outer map's keys are interface names and the inner map's keys are stat names.
  24. //
  25. // If you'd like a total across all interfaces, please use the Snmp6() method of the Proc type.
  26. type NetDevSNMP6 map[string]map[string]uint64
  27. // Returns kernel/system statistics read from interface files within the /proc/net/dev_snmp6/
  28. // directory.
  29. func (fs FS) NetDevSNMP6() (NetDevSNMP6, error) {
  30. return newNetDevSNMP6(fs.proc.Path("net/dev_snmp6"))
  31. }
  32. // Returns kernel/system statistics read from interface files within the /proc/<PID>/net/dev_snmp6/
  33. // directory.
  34. func (p Proc) NetDevSNMP6() (NetDevSNMP6, error) {
  35. return newNetDevSNMP6(p.path("net/dev_snmp6"))
  36. }
  37. // newNetDevSNMP6 creates a new NetDevSNMP6 from the contents of the given directory.
  38. func newNetDevSNMP6(dir string) (NetDevSNMP6, error) {
  39. netDevSNMP6 := make(NetDevSNMP6)
  40. // The net/dev_snmp6 folders contain one file per interface
  41. ifaceFiles, err := os.ReadDir(dir)
  42. if err != nil {
  43. // On systems with IPv6 disabled, this directory won't exist.
  44. // Do nothing.
  45. if errors.Is(err, os.ErrNotExist) {
  46. return netDevSNMP6, err
  47. }
  48. return netDevSNMP6, err
  49. }
  50. for _, iFaceFile := range ifaceFiles {
  51. f, err := os.Open(dir + "/" + iFaceFile.Name())
  52. if err != nil {
  53. return netDevSNMP6, err
  54. }
  55. defer f.Close()
  56. netDevSNMP6[iFaceFile.Name()], err = parseNetDevSNMP6Stats(f)
  57. if err != nil {
  58. return netDevSNMP6, err
  59. }
  60. }
  61. return netDevSNMP6, nil
  62. }
  63. func parseNetDevSNMP6Stats(r io.Reader) (map[string]uint64, error) {
  64. m := make(map[string]uint64)
  65. scanner := bufio.NewScanner(r)
  66. for scanner.Scan() {
  67. stat := strings.Fields(scanner.Text())
  68. if len(stat) < 2 {
  69. continue
  70. }
  71. key, val := stat[0], stat[1]
  72. // Expect stat name to contain "6" or be "ifIndex"
  73. if strings.Contains(key, "6") || key == "ifIndex" {
  74. v, err := strconv.ParseUint(val, 10, 64)
  75. if err != nil {
  76. return m, err
  77. }
  78. m[key] = v
  79. }
  80. }
  81. return m, scanner.Err()
  82. }