pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/system/info_loadavg_linux.go (about)

     1  package system
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"bufio"
    12  	"errors"
    13  	"io"
    14  	"os"
    15  	"strconv"
    16  	"strings"
    17  
    18  	"pkg.re/essentialkaos/ek.v12/strutil"
    19  )
    20  
    21  // ////////////////////////////////////////////////////////////////////////////////// //
    22  
    23  // Path to file with LA info in procfs
    24  var procLoadAvgFile = "/proc/loadavg"
    25  
    26  // ////////////////////////////////////////////////////////////////////////////////// //
    27  
    28  // GetLA returns loadavg
    29  func GetLA() (*LoadAvg, error) {
    30  	fd, err := os.OpenFile(procLoadAvgFile, os.O_RDONLY, 0)
    31  
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	defer fd.Close()
    37  
    38  	text, err := bufio.NewReader(fd).ReadString('\n')
    39  
    40  	if err != nil && err != io.EOF {
    41  		return nil, err
    42  	}
    43  
    44  	return parseLAInfo(text)
    45  }
    46  
    47  // ////////////////////////////////////////////////////////////////////////////////// //
    48  
    49  // codebeat:disable[LOC,ABC]
    50  
    51  // parseLAInfo parses loadavg data
    52  func parseLAInfo(text string) (*LoadAvg, error) {
    53  	var err error
    54  
    55  	if len(text) < 20 {
    56  		return nil, errors.New("Can't parse file " + procLoadAvgFile)
    57  	}
    58  
    59  	la := &LoadAvg{}
    60  
    61  	la.Min1, err = strconv.ParseFloat(strutil.ReadField(text, 0, true), 64)
    62  
    63  	if err != nil {
    64  		return nil, errors.New("Can't parse field 0 as float number in " + procLoadAvgFile)
    65  	}
    66  
    67  	la.Min5, err = strconv.ParseFloat(strutil.ReadField(text, 1, true), 64)
    68  
    69  	if err != nil {
    70  		return nil, errors.New("Can't parse field 1 as float number in " + procLoadAvgFile)
    71  	}
    72  
    73  	la.Min15, err = strconv.ParseFloat(strutil.ReadField(text, 2, true), 64)
    74  
    75  	if err != nil {
    76  		return nil, errors.New("Can't parse field 2 as float number in " + procLoadAvgFile)
    77  	}
    78  
    79  	procs := strutil.ReadField(text, 3, true)
    80  	delimPosition := strings.IndexRune(procs, '/')
    81  
    82  	if delimPosition == -1 {
    83  		return nil, errors.New("Can't parse field 3 in " + procLoadAvgFile)
    84  	}
    85  
    86  	la.RProc, err = strconv.Atoi(procs[:delimPosition])
    87  
    88  	if err != nil {
    89  		return nil, errors.New("Can't parse processes number in " + procLoadAvgFile)
    90  	}
    91  
    92  	la.TProc, err = strconv.Atoi(procs[delimPosition+1:])
    93  
    94  	if err != nil {
    95  		return nil, errors.New("Can't parse processes number in " + procLoadAvgFile)
    96  	}
    97  
    98  	return la, nil
    99  }
   100  
   101  // codebeat:enable[LOC,ABC]