-
Notifications
You must be signed in to change notification settings - Fork 10
/
file_handles.go
99 lines (79 loc) · 2.47 KB
/
file_handles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
// +build !windows
// +build !freebsd
package filehandles
import (
"errors"
"io/ioutil"
"strconv"
"strings"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/aggregator"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/collector/check"
core "github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/collector/corechecks"
"k8s.io/klog/v2"
)
const fileHandlesCheckName = "file_handle"
// For testing
var fileNrHandle = "/proc/sys/fs/file-nr"
type fhCheck struct {
core.CheckBase
}
func (c *fhCheck) getFileNrValues(fn string) ([]string, error) {
dat, err := ioutil.ReadFile(fn)
if err != nil {
klog.Error(err.Error())
return nil, err
}
s := strings.Split(strings.TrimRight(string(dat), "\n"), "\t")
if len(s) != 3 {
klog.Errorf("Unexpected number of arguments in file-nr, expected %d, got %d", 3, len(s))
err := errors.New("Unexpected number of args in file-nr")
return nil, err
}
return s, err
}
// Run executes the check
func (c *fhCheck) Run() error {
fileNrValues, err := c.getFileNrValues(fileNrHandle)
if err != nil {
return err
}
sender, err := aggregator.GetSender(c.ID())
if err != nil {
return err
}
allocatedFh, err := strconv.ParseFloat(fileNrValues[0], 64)
if err != nil {
klog.Errorf("Could not gather \"allocated file handle\" value")
return err
}
allocatedUnusedFh, err := strconv.ParseFloat(fileNrValues[1], 64)
if err != nil {
klog.Errorf("Could not gather \"allocated unused file handle\" value")
return err
}
maxFh, err := strconv.ParseFloat(fileNrValues[2], 64)
if err != nil {
klog.Errorf("Could not parse \"maximum file handle\" value")
return err
}
fhInUse := (allocatedFh - allocatedUnusedFh) / maxFh
sender.Gauge("system.fs.file_handles.allocated", allocatedFh, "", nil)
sender.Gauge("system.fs.file_handles.allocated_unused", allocatedUnusedFh, "", nil)
sender.Gauge("system.fs.file_handles.in_use", fhInUse, "", nil)
sender.Gauge("system.fs.file_handles.used", allocatedFh-allocatedUnusedFh, "", nil)
sender.Gauge("system.fs.file_handles.max", maxFh, "", nil)
sender.Commit()
return nil
}
func fhFactory() check.Check {
return &fhCheck{
CheckBase: core.NewCheckBase(fileHandlesCheckName),
}
}
func init() {
core.RegisterCheck(fileHandlesCheckName, fhFactory)
}