// Copyright 2019 The Prometheus Authors// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.package procfsimport ()// Swap represents an entry in /proc/swaps.typeSwapstruct { Filename string Type string Size int Used int Priority int}// Swaps returns a slice of all configured swap devices on the system.func ( FS) () ([]*Swap, error) { , := util.ReadFileNoStat(.proc.Path("swaps"))if != nil {returnnil, }returnparseSwaps()}func parseSwaps( []byte) ([]*Swap, error) { := []*Swap{} := bufio.NewScanner(bytes.NewReader()) .Scan() // ignore header linefor .Scan() { := .Text() , := parseSwapString()if != nil {returnnil, } = append(, ) } := .Err()return , }func parseSwapString( string) (*Swap, error) {varerror := strings.Fields() := len()if < 5 {returnnil, fmt.Errorf("%w: too few fields in swap string: %s", ErrFileParse, ) } := &Swap{Filename: [0],Type: [1], } .Size, = strconv.Atoi([2])if != nil {returnnil, fmt.Errorf("%w: invalid swap size: %s: %w", ErrFileParse, [2], ) } .Used, = strconv.Atoi([3])if != nil {returnnil, fmt.Errorf("%w: invalid swap used: %s: %w", ErrFileParse, [3], ) } .Priority, = strconv.Atoi([4])if != nil {returnnil, fmt.Errorf("%w: invalid swap priority: %s: %w", ErrFileParse, [4], ) }return , nil}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.