|
|
@ -16,12 +16,11 @@
|
|
|
|
package collector
|
|
|
|
package collector
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -41,8 +40,8 @@ func NewFileFDStatCollector() (Collector, error) {
|
|
|
|
return &fileFDStatCollector{}, nil
|
|
|
|
return &fileFDStatCollector{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
|
|
|
func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) error {
|
|
|
|
fileFDStat, err := getFileFDStats(procFilePath("sys/fs/file-nr"))
|
|
|
|
fileFDStat, err := parseFileFDStats(procFilePath("sys/fs/file-nr"))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't get file-nr: %s", err)
|
|
|
|
return fmt.Errorf("couldn't get file-nr: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -63,25 +62,27 @@ func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getFileFDStats(fileName string) (map[string]string, error) {
|
|
|
|
func parseFileFDStats(filename string) (map[string]string, error) {
|
|
|
|
file, err := os.Open(fileName)
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
defer file.Close()
|
|
|
|
return parseFileFDStats(file, fileName)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func parseFileFDStats(r io.Reader, fileName string) (map[string]string, error) {
|
|
|
|
content, err := ioutil.ReadAll(file)
|
|
|
|
var scanner = bufio.NewScanner(r)
|
|
|
|
if err != nil {
|
|
|
|
scanner.Scan()
|
|
|
|
return nil, err
|
|
|
|
// The file-nr proc file is separated by tabs, not spaces.
|
|
|
|
}
|
|
|
|
line := strings.Split(scanner.Text(), "\u0009")
|
|
|
|
parts := bytes.Split(bytes.TrimSpace(content), []byte("\u0009"))
|
|
|
|
|
|
|
|
if len(parts) < 3 {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unexpected number of file stats in %q", filename)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var fileFDStat = map[string]string{}
|
|
|
|
var fileFDStat = map[string]string{}
|
|
|
|
// The file-nr proc is only 1 line with 3 values.
|
|
|
|
// The file-nr proc is only 1 line with 3 values.
|
|
|
|
fileFDStat["allocated"] = line[0]
|
|
|
|
fileFDStat["allocated"] = string(parts[0])
|
|
|
|
// The second value is skipped as it will always be zero in linux 2.6.
|
|
|
|
// The second value is skipped as it will always be zero in linux 2.6.
|
|
|
|
fileFDStat["maximum"] = line[2]
|
|
|
|
fileFDStat["maximum"] = string(parts[2])
|
|
|
|
|
|
|
|
|
|
|
|
return fileFDStat, nil
|
|
|
|
return fileFDStat, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|