Read /proc/* file contents with ioutil.ReadFile() instead of opening it with os.Open() and scanning with bufio.Scanner

This simplifies the code a bit.
This commit is contained in:
Aliaksandr Valialkin 2021-02-08 14:23:37 +02:00
parent c070763356
commit 43691b65a0

View File

@ -1,7 +1,6 @@
package metrics package metrics
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
@ -123,15 +122,13 @@ func getOpenFDsCount(path string) (uint64, error) {
} }
func getMaxFilesLimit(path string) (uint64, error) { func getMaxFilesLimit(path string) (uint64, error) {
f, err := os.Open(path) data, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer f.Close() lines := strings.Split(string(data), "\n")
prefix := "Max open files" const prefix = "Max open files"
scan := bufio.NewScanner(f) for _, s := range lines {
for scan.Scan() {
s := scan.Text()
if !strings.HasPrefix(s, prefix) { if !strings.HasPrefix(s, prefix) {
continue continue
} }