|
| 1 | +// Copyright 2019 Tetrate |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package debug |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "syscall" |
| 23 | + |
| 24 | + "github.com/shirou/gopsutil/process" |
| 25 | + |
| 26 | + "github.com/tetratelabs/getenvoy/pkg/binary" |
| 27 | + "github.com/tetratelabs/getenvoy/pkg/binary/envoy" |
| 28 | + "github.com/tetratelabs/log" |
| 29 | +) |
| 30 | + |
| 31 | +// OpenFileStat defines the structure of statistics about a single opened file |
| 32 | +type OpenFileStat struct { |
| 33 | + // string enclosed in `` are known as struct tags |
| 34 | + // this particular tag is used by json.Marshal() to encode Command field in a specific manner |
| 35 | + // process dependent information |
| 36 | + Command string `json:"command"` |
| 37 | + Pid string `json:"pid"` |
| 38 | + User string `json:"user"` |
| 39 | + Fd string `json:"fd"` |
| 40 | + // non process dependent information |
| 41 | + Type string `json:"type"` // type of node associated with the file / FIFO for FIFO special file |
| 42 | + Device string `json:"device"` // device numbers associated with the file, separated by commas |
| 43 | + Size string `json:"size"` |
| 44 | + Node string `json:"node"` // inode number of a local file/ Internet protocol type |
| 45 | + Name string `json:"name"` // name of the mount point and file system on which the file resides |
| 46 | +} |
| 47 | + |
| 48 | +// EnableOpenFilesDataCollection is a preset option that registers collection of statistics of files opened by envoy instance(s) |
| 49 | +func EnableOpenFilesDataCollection(r *envoy.Runtime) { |
| 50 | + if err := os.Mkdir(filepath.Join(r.DebugStore(), "lsof"), os.ModePerm); err != nil { |
| 51 | + log.Errorf("error in creating a directory to write open file data of envoy to: %v", err) |
| 52 | + } |
| 53 | + r.RegisterPreTermination(retrieveOpenFilesData) |
| 54 | +} |
| 55 | + |
| 56 | +// retrieveOpenFilesData writes statistics of open files associated with envoy instance(s) to a json file |
| 57 | +// if succeeded, return nil, else return an error instance |
| 58 | +func retrieveOpenFilesData(r binary.Runner) error { |
| 59 | + // get pid of envoy instance |
| 60 | + pid, err := r.GetPid() |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("error in getting pid of envoy instance: %v", err) |
| 63 | + } |
| 64 | + envoyProcess, err := process.NewProcess(int32(pid)) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("error in creating an envoy instance: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + f, err := os.Create(filepath.Join(r.DebugStore(), "lsof/lsof.json")) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("error in creating a file to write open file statistics to: %v", err) |
| 72 | + } |
| 73 | + defer f.Close() //nolint |
| 74 | + |
| 75 | + result := make([]OpenFileStat, 0) |
| 76 | + // print open file stats for all envoy instances |
| 77 | + // relevant fields of the process |
| 78 | + username, _ := envoyProcess.Username() |
| 79 | + name, _ := envoyProcess.Name() |
| 80 | + |
| 81 | + openFiles, err := envoyProcess.OpenFiles() |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("error in getting open file statistics: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + for _, stat := range openFiles { |
| 87 | + ofStat := OpenFileStat{ |
| 88 | + Command: name, |
| 89 | + Pid: fmt.Sprint(pid), |
| 90 | + User: username, |
| 91 | + Fd: fmt.Sprint(stat.Fd), |
| 92 | + Name: stat.Path, |
| 93 | + } |
| 94 | + |
| 95 | + var fstat syscall.Stat_t |
| 96 | + if errSyscall := syscall.Stat(stat.Path, &fstat); errSyscall != nil { |
| 97 | + // continue if the path is invalid |
| 98 | + result = append(result, ofStat) |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + ofStat.Node = fmt.Sprint(fstat.Ino) |
| 103 | + ofStat.Size = fmt.Sprint(fstat.Size) |
| 104 | + result = append(result, ofStat) |
| 105 | + } |
| 106 | + |
| 107 | + out, err := json.Marshal(result) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("unable to convert to json representation: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + fmt.Fprintln(f, string(out)) |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
0 commit comments