mirror of
https://github.com/prometheus/node_exporter.git
synced 2024-12-03 15:31:05 +01:00
read /proc/net files with a single read syscall (#1380)
Signed-off-by: Hanaasagi <ambiguous404@gmail.com>
This commit is contained in:
parent
3d504bc5cb
commit
777b751f90
@ -16,9 +16,9 @@
|
|||||||
package collector
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -108,23 +108,21 @@ func getTCPStats(statsFile string) (map[tcpConnectionState]float64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseTCPStats(r io.Reader) (map[tcpConnectionState]float64, error) {
|
func parseTCPStats(r io.Reader) (map[tcpConnectionState]float64, error) {
|
||||||
var (
|
tcpStats := map[tcpConnectionState]float64{}
|
||||||
tcpStats = map[tcpConnectionState]float64{}
|
contents, err := ioutil.ReadAll(r)
|
||||||
scanner = bufio.NewScanner(r)
|
if err != nil {
|
||||||
)
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
for scanner.Scan() {
|
for _, line := range strings.Split(string(contents), "\n")[1:] {
|
||||||
parts := strings.Fields(scanner.Text())
|
parts := strings.Fields(line)
|
||||||
if len(parts) == 0 {
|
if len(parts) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(parts) < 4 {
|
if len(parts) < 4 {
|
||||||
return nil, fmt.Errorf("invalid TCP stats line: %q", scanner.Text())
|
return nil, fmt.Errorf("invalid TCP stats line: %q", line)
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(parts[0], "sl") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
st, err := strconv.ParseInt(parts[3], 16, 8)
|
st, err := strconv.ParseInt(parts[3], 16, 8)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -133,7 +131,7 @@ func parseTCPStats(r io.Reader) (map[tcpConnectionState]float64, error) {
|
|||||||
tcpStats[tcpConnectionState(st)]++
|
tcpStats[tcpConnectionState(st)]++
|
||||||
}
|
}
|
||||||
|
|
||||||
return tcpStats, scanner.Err()
|
return tcpStats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (st tcpConnectionState) String() string {
|
func (st tcpConnectionState) String() string {
|
||||||
|
@ -26,7 +26,7 @@ func Test_parseTCPStatsError(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "too few fields",
|
name: "too few fields",
|
||||||
in: "hello world",
|
in: "sl local_address\n 0: 00000000:0016",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user