github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/utilities/metrics/disk_linux.go (about)

     1  package metrics
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"strconv"
     9  	"strings"
    10  )
    11  
    12  func ReadDiskStats(stats *DiskStats) error {
    13  
    14  	inf, err := os.Open(fmt.Sprintf("/proc/%d/io", os.Getpid()))
    15  	if err != nil {
    16  		return err
    17  	}
    18  	defer inf.Close()
    19  	in := bufio.NewReader(inf)
    20  
    21  	for {
    22  
    23  		line, err := in.ReadString('\n')
    24  		if err != nil {
    25  			if err == io.EOF {
    26  				return nil
    27  			}
    28  			return err
    29  		}
    30  		parts := strings.Split(line, ":")
    31  		if len(parts) != 2 {
    32  			continue
    33  		}
    34  		key := strings.TrimSpace(parts[0])
    35  		value, err := strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 64)
    36  		if err != nil {
    37  			return err
    38  		}
    39  
    40  		switch key {
    41  		case "syscr":
    42  			stats.ReadCount = value
    43  		case "syscw":
    44  			stats.WriteCount = value
    45  		case "rchar":
    46  			stats.ReadBytes = value
    47  		case "wchar":
    48  			stats.WriteBytes = value
    49  		}
    50  	}
    51  }