|
| 1 | +// Copyright 2019 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 | + |
| 14 | +package procfs |
| 15 | + |
| 16 | +import ( |
| 17 | + "bufio" |
| 18 | + "fmt" |
| 19 | + "io/ioutil" |
| 20 | + "os" |
| 21 | + "regexp" |
| 22 | + "strings" |
| 23 | +) |
| 24 | + |
| 25 | +// Regexp variables |
| 26 | +var ( |
| 27 | + rPos = regexp.MustCompile(`^pos:\s+(\d+)$`) |
| 28 | + rFlags = regexp.MustCompile(`^flags:\s+(\d+)$`) |
| 29 | + rMntID = regexp.MustCompile(`^mnt_id:\s+(\d+)$`) |
| 30 | + rInotify = regexp.MustCompile(`^inotify`) |
| 31 | +) |
| 32 | + |
| 33 | +// ProcFDInfo contains represents file descriptor information. |
| 34 | +type ProcFDInfo struct { |
| 35 | + // File descriptor |
| 36 | + FD string |
| 37 | + // File offset |
| 38 | + Pos string |
| 39 | + // File access mode and status flags |
| 40 | + Flags string |
| 41 | + // Mount point ID |
| 42 | + MntID string |
| 43 | + // List of inotify lines (structed) in the fdinfo file (kernel 3.8+ only) |
| 44 | + InotifyInfos []InotifyInfo |
| 45 | +} |
| 46 | + |
| 47 | +// FDInfo constructor. On kernels older than 3.8, InotifyInfos will always be empty. |
| 48 | +func (p Proc) FDInfo(fd string) (*ProcFDInfo, error) { |
| 49 | + f, err := os.Open(p.path("fdinfo", fd)) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + defer f.Close() |
| 54 | + |
| 55 | + fdinfo, err := ioutil.ReadAll(f) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("could not read %s: %s", f.Name(), err) |
| 58 | + } |
| 59 | + |
| 60 | + var text, pos, flags, mntid string |
| 61 | + var inotify []InotifyInfo |
| 62 | + |
| 63 | + scanner := bufio.NewScanner(strings.NewReader(string(fdinfo))) |
| 64 | + for scanner.Scan() { |
| 65 | + text = scanner.Text() |
| 66 | + if rPos.MatchString(text) { |
| 67 | + pos = rPos.FindStringSubmatch(text)[1] |
| 68 | + } else if rFlags.MatchString(text) { |
| 69 | + flags = rFlags.FindStringSubmatch(text)[1] |
| 70 | + } else if rMntID.MatchString(text) { |
| 71 | + mntid = rMntID.FindStringSubmatch(text)[1] |
| 72 | + } else if rInotify.MatchString(text) { |
| 73 | + newInotify, err := parseInotifyInfo(text) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + inotify = append(inotify, *newInotify) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + i := &ProcFDInfo{ |
| 82 | + FD: fd, |
| 83 | + Pos: pos, |
| 84 | + Flags: flags, |
| 85 | + MntID: mntid, |
| 86 | + InotifyInfos: inotify, |
| 87 | + } |
| 88 | + |
| 89 | + return i, nil |
| 90 | +} |
| 91 | + |
| 92 | +// InotifyInfo represents a single inotify line in the fdinfo file. |
| 93 | +type InotifyInfo struct { |
| 94 | + // Watch descriptor number |
| 95 | + WD string |
| 96 | + // Inode number |
| 97 | + Ino string |
| 98 | + // Device ID |
| 99 | + Sdev string |
| 100 | + // Mask of events being monitored |
| 101 | + Mask string |
| 102 | +} |
| 103 | + |
| 104 | +// InotifyInfo constructor. Only available on kernel 3.8+. |
| 105 | +func parseInotifyInfo(line string) (*InotifyInfo, error) { |
| 106 | + r := regexp.MustCompile(`^inotify\s+wd:([0-9a-f]+)\s+ino:([0-9a-f]+)\s+sdev:([0-9a-f]+)\s+mask:([0-9a-f]+)`) |
| 107 | + m := r.FindStringSubmatch(line) |
| 108 | + i := &InotifyInfo{ |
| 109 | + WD: m[1], |
| 110 | + Ino: m[2], |
| 111 | + Sdev: m[3], |
| 112 | + Mask: m[4], |
| 113 | + } |
| 114 | + return i, nil |
| 115 | +} |
| 116 | + |
| 117 | +// ProcFDInfos represents a list of ProcFDInfo structs. |
| 118 | +type ProcFDInfos []ProcFDInfo |
| 119 | + |
| 120 | +func (p ProcFDInfos) Len() int { return len(p) } |
| 121 | +func (p ProcFDInfos) Swap(i, j int) { p[i], p[j] = p[j], p[i] } |
| 122 | +func (p ProcFDInfos) Less(i, j int) bool { return p[i].FD < p[j].FD } |
| 123 | + |
| 124 | +// InotifyWatchLen returns the total number of inotify watches |
| 125 | +func (p ProcFDInfos) InotifyWatchLen() (int, error) { |
| 126 | + length := 0 |
| 127 | + for _, f := range p { |
| 128 | + length += len(f.InotifyInfos) |
| 129 | + } |
| 130 | + |
| 131 | + return length, nil |
| 132 | +} |
0 commit comments