Skip to content

Commit

Permalink
Merge pull request #21 from infrawatch/proclimits
Browse files Browse the repository at this point in the history
Add system.GetOpenedFiles
  • Loading branch information
paramite authored Aug 9, 2021
2 parents 995e6ad + dfd6a51 commit 3573b29
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
32 changes: 26 additions & 6 deletions system/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ import (

// Modifiable constants
var (
ProcLimitColumns = 4
ProcLimitPathFmt = "/proc/%d/limits"
splitRex = regexp.MustCompile(" +")
ProcLimitColumns = 4
ProcLimitPathFmt = "/proc/%d/limits"
ProcOpenedFilesFmt = "/proc/%d/fd"
splitRex = regexp.MustCompile(" +")
)

// GetProcLimits returns limits for the given process. Use -1 for actual process.
func GetProcLimits(PID int) (map[string]map[string]interface{}, error) {
func getProcPath(pathFmt string, PID int) string {
if PID == -1 {
PID = os.Getpid()
}
return fmt.Sprintf(pathFmt, PID)
}

data, err := ioutil.ReadFile(fmt.Sprintf(ProcLimitPathFmt, PID))
// GetProcLimits returns limits for the given process. Use -1 for actual process.
func GetProcLimits(PID int) (map[string]map[string]interface{}, error) {
data, err := ioutil.ReadFile(getProcPath(ProcLimitPathFmt, PID))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -56,3 +60,19 @@ func GetProcLimits(PID int) (map[string]map[string]interface{}, error) {

return out, nil
}

// GetOpenedFiles returns count of opened files by the given process. Use -1 for actual process.
func GetOpenedFiles(PID int) (int, error) {
dir, err := os.Open(getProcPath(ProcOpenedFilesFmt, PID))
if err != nil {
return -1, err
}

files, err := dir.Readdir(-1)
dir.Close()
if err != nil {
return -1, err
}

return len(files), nil
}
29 changes: 23 additions & 6 deletions tests/system_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"fmt"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -34,13 +35,13 @@ func TestProcLimits(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(tmpdir)

// save test content
file, err := os.Create(path.Join(tmpdir, "0_limits"))
require.NoError(t, err)
file.WriteString(procLimitData)
require.NoError(t, file.Close())

t.Run("Test parsed limit file", func(t *testing.T) {
// save test content
file, err := os.Create(path.Join(tmpdir, "0_limits"))
require.NoError(t, err)
file.WriteString(procLimitData)
require.NoError(t, file.Close())

system.ProcLimitPathFmt = path.Join(tmpdir, "%d_limits")

expected := map[string]map[string]interface{}{
Expand Down Expand Up @@ -131,4 +132,20 @@ func TestProcLimits(t *testing.T) {
assert.Equal(t, expected, parsed, "Did not parse correctly")
})

t.Run("Test opened files from process", func(t *testing.T) {
fdir := path.Join(tmpdir, "0")
err := os.Mkdir(fdir, 0755)
require.NoError(t, err)

for i := 0; i < 10; i++ {
file, err := os.Create(path.Join(fdir, fmt.Sprintf("socket%d", i)))
require.NoError(t, err)
require.NoError(t, file.Close())
}

system.ProcOpenedFilesFmt = path.Join(tmpdir, "%d")
fcount, err := system.GetOpenedFiles(0)
require.NoError(t, err)
assert.Equal(t, 10, fcount)
})
}

0 comments on commit 3573b29

Please sign in to comment.