cubefs
98 строк · 3.0 Кб
1// Copyright 2021 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package procfs15
16import (17"bufio"18"bytes"19"fmt"20"strconv"21"strings"22
23"github.com/prometheus/procfs/internal/util"24)
25
26// CgroupSummary models one line from /proc/cgroups.
27// This file contains information about the controllers that are compiled into the kernel.
28//
29// Also see http://man7.org/linux/man-pages/man7/cgroups.7.html
30type CgroupSummary struct {31// The name of the controller. controller is also known as subsystem.32SubsysName string33// The unique ID of the cgroup hierarchy on which this controller is mounted.34Hierarchy int35// The number of control groups in this hierarchy using this controller.36Cgroups int37// This field contains the value 1 if this controller is enabled, or 0 if it has been disabled38Enabled int39}
40
41// parseCgroupSummary parses each line of the /proc/cgroup file
42// Line format is `subsys_name hierarchy num_cgroups enabled`.
43func parseCgroupSummaryString(CgroupSummaryStr string) (*CgroupSummary, error) {44var err error45
46fields := strings.Fields(CgroupSummaryStr)47// require at least 4 fields48if len(fields) < 4 {49return nil, fmt.Errorf("at least 4 fields required, found %d fields in cgroup info string: %s", len(fields), CgroupSummaryStr)50}51
52CgroupSummary := &CgroupSummary{53SubsysName: fields[0],54}55CgroupSummary.Hierarchy, err = strconv.Atoi(fields[1])56if err != nil {57return nil, fmt.Errorf("failed to parse hierarchy ID")58}59CgroupSummary.Cgroups, err = strconv.Atoi(fields[2])60if err != nil {61return nil, fmt.Errorf("failed to parse Cgroup Num")62}63CgroupSummary.Enabled, err = strconv.Atoi(fields[3])64if err != nil {65return nil, fmt.Errorf("failed to parse Enabled")66}67return CgroupSummary, nil68}
69
70// parseCgroupSummary reads each line of the /proc/cgroup file.
71func parseCgroupSummary(data []byte) ([]CgroupSummary, error) {72var CgroupSummarys []CgroupSummary73scanner := bufio.NewScanner(bytes.NewReader(data))74for scanner.Scan() {75CgroupSummaryString := scanner.Text()76// ignore comment lines77if strings.HasPrefix(CgroupSummaryString, "#") {78continue79}80CgroupSummary, err := parseCgroupSummaryString(CgroupSummaryString)81if err != nil {82return nil, err83}84CgroupSummarys = append(CgroupSummarys, *CgroupSummary)85}86
87err := scanner.Err()88return CgroupSummarys, err89}
90
91// CgroupSummarys returns information about current /proc/cgroups.
92func (fs FS) CgroupSummarys() ([]CgroupSummary, error) {93data, err := util.ReadFileNoStat(fs.proc.Path("cgroups"))94if err != nil {95return nil, err96}97return parseCgroupSummary(data)98}
99