diff --git a/collector/cgroups_linux.go b/collector/cgroups_linux.go new file mode 100644 index 00000000..80ae7da1 --- /dev/null +++ b/collector/cgroups_linux.go @@ -0,0 +1,65 @@ +// Copyright 2022 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. + +//go:build !nostat +// +build !nostat + +package collector + +import ( + "fmt" + "strconv" + + "github.com/go-kit/log" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/procfs" +) + +type cgroupSummaryCollector struct { + fs procfs.FS + cgroups *prometheus.Desc + logger log.Logger +} + +func init() { + registerCollector("cgroupSummary", defaultEnabled, NewCgroupSummaryCollector) +} + +// NewCgroupSummaryCollector returns a new Collector exposing a summary of cgroups. +func NewCgroupSummaryCollector(logger log.Logger) (Collector, error) { + fs, err := procfs.NewFS(*procPath) + if err != nil { + return nil, fmt.Errorf("failed to open procfs: %w", err) + } + return &cgroupSummaryCollector{ + fs: fs, + cgroups: prometheus.NewDesc( + prometheus.BuildFQName(namespace, "", "cgroups_total"), + "Current cgroup number of the subsystem.", + []string{"subsys_name", "enabled"}, nil, + ), + logger: logger, + }, nil +} + +// Update implements Collector and exposes cgroup statistics. +func (c *cgroupSummaryCollector) Update(ch chan<- prometheus.Metric) error { + cgroupSummarys, err := c.fs.CgroupSummarys() + if err != nil { + return err + } + for _, cs := range cgroupSummarys { + ch <- prometheus.MustNewConstMetric(c.cgroups, prometheus.CounterValue, float64(cs.Cgroups), cs.SubsysName, strconv.Itoa(cs.Enabled)) + } + return nil +} diff --git a/collector/fixtures/e2e-output.txt b/collector/fixtures/e2e-output.txt index 62a1ba37..09523012 100644 --- a/collector/fixtures/e2e-output.txt +++ b/collector/fixtures/e2e-output.txt @@ -296,6 +296,20 @@ node_buddyinfo_blocks{node="0",size="8",zone="Normal"} 0 node_buddyinfo_blocks{node="0",size="9",zone="DMA"} 1 node_buddyinfo_blocks{node="0",size="9",zone="DMA32"} 0 node_buddyinfo_blocks{node="0",size="9",zone="Normal"} 0 +# HELP node_cgroups_total Current cgroup number of the subsystem. +# TYPE node_cgroups_total counter +node_cgroups_total{enabled="1",subsys_name="blkio"} 170 +node_cgroups_total{enabled="1",subsys_name="cpu"} 172 +node_cgroups_total{enabled="1",subsys_name="cpuacct"} 172 +node_cgroups_total{enabled="1",subsys_name="cpuset"} 47 +node_cgroups_total{enabled="1",subsys_name="devices"} 170 +node_cgroups_total{enabled="1",subsys_name="freezer"} 47 +node_cgroups_total{enabled="1",subsys_name="hugetlb"} 47 +node_cgroups_total{enabled="1",subsys_name="memory"} 234 +node_cgroups_total{enabled="1",subsys_name="net_cls"} 47 +node_cgroups_total{enabled="1",subsys_name="perf_event"} 47 +node_cgroups_total{enabled="1",subsys_name="pids"} 170 +node_cgroups_total{enabled="1",subsys_name="rdma"} 1 # HELP node_context_switches_total Total number of context switches. # TYPE node_context_switches_total counter node_context_switches_total 3.8014093e+07 @@ -3031,6 +3045,7 @@ node_scrape_collector_success{collector="bcache"} 1 node_scrape_collector_success{collector="bonding"} 1 node_scrape_collector_success{collector="btrfs"} 1 node_scrape_collector_success{collector="buddyinfo"} 1 +node_scrape_collector_success{collector="cgroupSummary"} 1 node_scrape_collector_success{collector="conntrack"} 1 node_scrape_collector_success{collector="cpu"} 1 node_scrape_collector_success{collector="cpufreq"} 1 diff --git a/collector/fixtures/proc/cgroups b/collector/fixtures/proc/cgroups new file mode 100644 index 00000000..99382e31 --- /dev/null +++ b/collector/fixtures/proc/cgroups @@ -0,0 +1,13 @@ +#subsys_name hierarchy num_cgroups enabled +cpuset 5 47 1 +cpu 3 172 1 +cpuacct 3 172 1 +blkio 6 170 1 +memory 7 234 1 +devices 11 170 1 +freezer 9 47 1 +net_cls 2 47 1 +perf_event 8 47 1 +hugetlb 12 47 1 +pids 10 170 1 +rdma 4 1 1 \ No newline at end of file diff --git a/go.mod b/go.mod index 6bcf9dc4..cb4a76f6 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/prometheus/client_model v0.2.0 github.com/prometheus/common v0.33.0 github.com/prometheus/exporter-toolkit v0.7.1 - github.com/prometheus/procfs v0.7.4-0.20211011103944-1a7a2bd3279f + github.com/prometheus/procfs v0.7.4-0.20211209105546-0f8a320e1e1f github.com/safchain/ethtool v0.2.0 github.com/soundcloud/go-runit v0.0.0-20150630195641-06ad41a06c4a golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f diff --git a/go.sum b/go.sum index 22864c5d..dec8ab01 100644 --- a/go.sum +++ b/go.sum @@ -235,8 +235,8 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= -github.com/prometheus/procfs v0.7.4-0.20211011103944-1a7a2bd3279f h1:ncXqc93eJV1Ncr3f6GA3MrIDNkNHvcPonRC2QgZaVkQ= -github.com/prometheus/procfs v0.7.4-0.20211011103944-1a7a2bd3279f/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= +github.com/prometheus/procfs v0.7.4-0.20211209105546-0f8a320e1e1f h1:tg+5gOV1AkhTfWNi94UA5rvn+Mat/thyB2WASYAzPZo= +github.com/prometheus/procfs v0.7.4-0.20211209105546-0f8a320e1e1f/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=