Skip to content

Commit b6215e6

Browse files
bdrungdiscordianfish
authored andcommitted
Add os release collector
Currently Node Exporter has a metric called `node_uname_info` which of course exposes uname info. While this is nice, it does not help if you are running different OSes which could have similar uname info. Therefore parse `/etc/os-release` or `/usr/lib/os-release` and expose a `node_os_info` metric which provide information regarding the OS release/version of the node. Also expose the major.minor part of the OS release version as `node_os_version`. Since the os-release files will not change often, cache the parsed content and only refresh the cache if the modification time changes. This `os` collector will read files outside of `/proc` and `/sys`, but the os-release file is widely used and the format is standardized: https://www.freedesktop.org/software/systemd/man/os-release.html Bug: #1574 Signed-off-by: Benjamin Drung <[email protected]>
1 parent aea88e4 commit b6215e6

File tree

9 files changed

+314
-0
lines changed

9 files changed

+314
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ netstat | Exposes network statistics from `/proc/net/netstat`. This is the same
114114
nfs | Exposes NFS client statistics from `/proc/net/rpc/nfs`. This is the same information as `nfsstat -c`. | Linux
115115
nfsd | Exposes NFS kernel server statistics from `/proc/net/rpc/nfsd`. This is the same information as `nfsstat -s`. | Linux
116116
nvme | Exposes NVMe info from `/sys/class/nvme/` | Linux
117+
os | Expose OS release info from `/etc/os-release` or `/usr/lib/os-release` | _any_
117118
powersupplyclass | Exposes Power Supply statistics from `/sys/class/power_supply` | Linux
118119
pressure | Exposes pressure stall statistics from `/proc/pressure/`. | Linux (kernel 4.20+ and/or [CONFIG\_PSI](https://www.kernel.org/doc/html/latest/accounting/psi.html))
119120
rapl | Exposes various statistics from `/sys/class/powercap`. | Linux

collector/fixtures/e2e-64k-page-output.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,6 +2442,12 @@ node_nfsd_server_threads 8
24422442
# HELP node_nvme_info Non-numeric data from /sys/class/nvme/<device>, value is always 1.
24432443
# TYPE node_nvme_info gauge
24442444
node_nvme_info{device="nvme0",firmware_revision="1B2QEXP7",model="Samsung SSD 970 PRO 512GB",serial="S680HF8N190894I",state="live"} 1
2445+
# HELP node_os_info A metric with a constant '1' value labeled by build_id, id, id_like, image_id, image_version, name, pretty_name, variant, variant_id, version, version_codename, version_id.
2446+
# TYPE node_os_info gauge
2447+
node_os_info{build_id="",id="ubuntu",id_like="debian",image_id="",image_version="",name="Ubuntu",pretty_name="Ubuntu 20.04.2 LTS",variant="",variant_id="",version="20.04.2 LTS (Focal Fossa)",version_codename="focal",version_id="20.04"} 1
2448+
# HELP node_os_version Metric containing the major.minor part of the OS version.
2449+
# TYPE node_os_version gauge
2450+
node_os_version{id="ubuntu",id_like="debian",name="Ubuntu"} 20.04
24452451
# HELP node_power_supply_capacity capacity value of /sys/class/power_supply/<power_supply>.
24462452
# TYPE node_power_supply_capacity gauge
24472453
node_power_supply_capacity{power_supply="BAT0"} 81
@@ -2590,6 +2596,7 @@ node_scrape_collector_success{collector="netstat"} 1
25902596
node_scrape_collector_success{collector="nfs"} 1
25912597
node_scrape_collector_success{collector="nfsd"} 1
25922598
node_scrape_collector_success{collector="nvme"} 1
2599+
node_scrape_collector_success{collector="os"} 1
25932600
node_scrape_collector_success{collector="powersupplyclass"} 1
25942601
node_scrape_collector_success{collector="pressure"} 1
25952602
node_scrape_collector_success{collector="processes"} 1

collector/fixtures/e2e-output.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,6 +2640,12 @@ node_nfsd_server_threads 8
26402640
# HELP node_nvme_info Non-numeric data from /sys/class/nvme/<device>, value is always 1.
26412641
# TYPE node_nvme_info gauge
26422642
node_nvme_info{device="nvme0",firmware_revision="1B2QEXP7",model="Samsung SSD 970 PRO 512GB",serial="S680HF8N190894I",state="live"} 1
2643+
# HELP node_os_info A metric with a constant '1' value labeled by build_id, id, id_like, image_id, image_version, name, pretty_name, variant, variant_id, version, version_codename, version_id.
2644+
# TYPE node_os_info gauge
2645+
node_os_info{build_id="",id="ubuntu",id_like="debian",image_id="",image_version="",name="Ubuntu",pretty_name="Ubuntu 20.04.2 LTS",variant="",variant_id="",version="20.04.2 LTS (Focal Fossa)",version_codename="focal",version_id="20.04"} 1
2646+
# HELP node_os_version Metric containing the major.minor part of the OS version.
2647+
# TYPE node_os_version gauge
2648+
node_os_version{id="ubuntu",id_like="debian",name="Ubuntu"} 20.04
26432649
# HELP node_power_supply_capacity capacity value of /sys/class/power_supply/<power_supply>.
26442650
# TYPE node_power_supply_capacity gauge
26452651
node_power_supply_capacity{power_supply="BAT0"} 81
@@ -2791,6 +2797,7 @@ node_scrape_collector_success{collector="netstat"} 1
27912797
node_scrape_collector_success{collector="nfs"} 1
27922798
node_scrape_collector_success{collector="nfsd"} 1
27932799
node_scrape_collector_success{collector="nvme"} 1
2800+
node_scrape_collector_success{collector="os"} 1
27942801
node_scrape_collector_success{collector="powersupplyclass"} 1
27952802
node_scrape_collector_success{collector="pressure"} 1
27962803
node_scrape_collector_success{collector="processes"} 1

collector/fixtures/usr/lib/os-release

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
NAME="Ubuntu"
2+
VERSION="20.04.2 LTS (Focal Fossa)"
3+
ID=ubuntu
4+
ID_LIKE=debian
5+
PRETTY_NAME="Ubuntu 20.04.2 LTS"
6+
VERSION_ID="20.04"
7+
HOME_URL="https://www.ubuntu.com/"
8+
SUPPORT_URL="https://help.ubuntu.com/"
9+
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
10+
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
11+
VERSION_CODENAME=focal
12+
UBUNTU_CODENAME=focal

collector/os_release.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
14+
package collector
15+
16+
import (
17+
"errors"
18+
"io"
19+
"os"
20+
"regexp"
21+
"strconv"
22+
"strings"
23+
"sync"
24+
"time"
25+
26+
"github.com/go-kit/log"
27+
"github.com/go-kit/log/level"
28+
envparse "github.com/hashicorp/go-envparse"
29+
"github.com/prometheus/client_golang/prometheus"
30+
)
31+
32+
const (
33+
etcOSRelease = "/etc/os-release"
34+
usrLibOSRelease = "/usr/lib/os-release"
35+
)
36+
37+
var (
38+
versionRegex = regexp.MustCompile(`^[0-9]+\.?[0-9]*`)
39+
)
40+
41+
type osRelease struct {
42+
Name string
43+
ID string
44+
IDLike string
45+
PrettyName string
46+
Variant string
47+
VariantID string
48+
Version string
49+
VersionID string
50+
VersionCodename string
51+
BuildID string
52+
ImageID string
53+
ImageVersion string
54+
}
55+
56+
type osReleaseCollector struct {
57+
infoDesc *prometheus.Desc
58+
logger log.Logger
59+
os *osRelease
60+
osFilename string // file name of cached release information
61+
osMtime time.Time // mtime of cached release file
62+
osMutex sync.Mutex
63+
osReleaseFilenames []string // all os-release file names to check
64+
version float64
65+
versionDesc *prometheus.Desc
66+
}
67+
68+
func init() {
69+
registerCollector("os", defaultEnabled, NewOSCollector)
70+
}
71+
72+
// NewOSCollector returns a new Collector exposing os-release information.
73+
func NewOSCollector(logger log.Logger) (Collector, error) {
74+
return &osReleaseCollector{
75+
logger: logger,
76+
infoDesc: prometheus.NewDesc(
77+
prometheus.BuildFQName(namespace, "os", "info"),
78+
"A metric with a constant '1' value labeled by build_id, id, id_like, image_id, image_version, "+
79+
"name, pretty_name, variant, variant_id, version, version_codename, version_id.",
80+
[]string{"build_id", "id", "id_like", "image_id", "image_version", "name", "pretty_name",
81+
"variant", "variant_id", "version", "version_codename", "version_id"}, nil,
82+
),
83+
osReleaseFilenames: []string{etcOSRelease, usrLibOSRelease},
84+
versionDesc: prometheus.NewDesc(
85+
prometheus.BuildFQName(namespace, "os", "version"),
86+
"Metric containing the major.minor part of the OS version.",
87+
[]string{"id", "id_like", "name"}, nil,
88+
),
89+
}, nil
90+
}
91+
92+
func parseOSRelease(r io.Reader) (*osRelease, error) {
93+
env, err := envparse.Parse(r)
94+
return &osRelease{
95+
Name: env["NAME"],
96+
ID: env["ID"],
97+
IDLike: env["ID_LIKE"],
98+
PrettyName: env["PRETTY_NAME"],
99+
Variant: env["VARIANT"],
100+
VariantID: env["VARIANT_ID"],
101+
Version: env["VERSION"],
102+
VersionID: env["VERSION_ID"],
103+
VersionCodename: env["VERSION_CODENAME"],
104+
BuildID: env["BUILD_ID"],
105+
ImageID: env["IMAGE_ID"],
106+
ImageVersion: env["IMAGE_VERSION"],
107+
}, err
108+
}
109+
110+
func (c *osReleaseCollector) UpdateStruct(path string) error {
111+
releaseFile, err := os.Open(path)
112+
if err != nil {
113+
return err
114+
}
115+
defer releaseFile.Close()
116+
117+
stat, err := releaseFile.Stat()
118+
if err != nil {
119+
return err
120+
}
121+
122+
t := stat.ModTime()
123+
if path == c.osFilename && t == c.osMtime {
124+
// osReleaseCollector struct is already up-to-date.
125+
return nil
126+
}
127+
128+
// Acquire a lock to update the osReleaseCollector struct.
129+
c.osMutex.Lock()
130+
defer c.osMutex.Unlock()
131+
132+
level.Debug(c.logger).Log("msg", "file modification time has changed",
133+
"file", path, "old_value", c.osMtime, "new_value", t)
134+
c.osFilename = path
135+
c.osMtime = t
136+
137+
c.os, err = parseOSRelease(releaseFile)
138+
if err != nil {
139+
return err
140+
}
141+
142+
majorMinor := versionRegex.FindString(c.os.VersionID)
143+
if majorMinor != "" {
144+
c.version, err = strconv.ParseFloat(majorMinor, 64)
145+
if err != nil {
146+
return err
147+
}
148+
} else {
149+
c.version = 0
150+
}
151+
return nil
152+
}
153+
154+
func (c *osReleaseCollector) Update(ch chan<- prometheus.Metric) error {
155+
for i, path := range c.osReleaseFilenames {
156+
err := c.UpdateStruct(*rootfsPath + path)
157+
if err == nil {
158+
break
159+
}
160+
if errors.Is(err, os.ErrNotExist) {
161+
if i >= (len(c.osReleaseFilenames) - 1) {
162+
level.Debug(c.logger).Log("msg", "no os-release file found", "files", strings.Join(c.osReleaseFilenames, ","))
163+
return ErrNoData
164+
}
165+
continue
166+
}
167+
return err
168+
}
169+
170+
ch <- prometheus.MustNewConstMetric(c.infoDesc, prometheus.GaugeValue, 1.0,
171+
c.os.BuildID, c.os.ID, c.os.IDLike, c.os.ImageID, c.os.ImageVersion, c.os.Name, c.os.PrettyName,
172+
c.os.Variant, c.os.VariantID, c.os.Version, c.os.VersionCodename, c.os.VersionID)
173+
if c.version > 0 {
174+
ch <- prometheus.MustNewConstMetric(c.versionDesc, prometheus.GaugeValue, c.version,
175+
c.os.ID, c.os.IDLike, c.os.Name)
176+
}
177+
return nil
178+
}

collector/os_release_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
14+
package collector
15+
16+
import (
17+
"os"
18+
"reflect"
19+
"strings"
20+
"testing"
21+
22+
"github.com/go-kit/log"
23+
)
24+
25+
const debianBullseye string = `PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
26+
NAME="Debian GNU/Linux"
27+
VERSION_ID="11"
28+
VERSION="11 (bullseye)"
29+
VERSION_CODENAME=bullseye
30+
ID=debian
31+
HOME_URL="https://www.debian.org/"
32+
SUPPORT_URL="https://www.debian.org/support"
33+
BUG_REPORT_URL="https://bugs.debian.org/"
34+
`
35+
36+
func TestParseOSRelease(t *testing.T) {
37+
want := &osRelease{
38+
Name: "Ubuntu",
39+
ID: "ubuntu",
40+
IDLike: "debian",
41+
PrettyName: "Ubuntu 20.04.2 LTS",
42+
Version: "20.04.2 LTS (Focal Fossa)",
43+
VersionID: "20.04",
44+
VersionCodename: "focal",
45+
}
46+
47+
osReleaseFile, err := os.Open("fixtures" + usrLibOSRelease)
48+
if err != nil {
49+
t.Fatal(err)
50+
}
51+
got, err := parseOSRelease(osReleaseFile)
52+
if err != nil {
53+
t.Fatal(err)
54+
}
55+
if !reflect.DeepEqual(want, got) {
56+
t.Fatalf("should have %+v osRelease: got %+v", want, got)
57+
}
58+
59+
want = &osRelease{
60+
Name: "Debian GNU/Linux",
61+
ID: "debian",
62+
PrettyName: "Debian GNU/Linux 11 (bullseye)",
63+
Version: "11 (bullseye)",
64+
VersionID: "11",
65+
VersionCodename: "bullseye",
66+
}
67+
got, err = parseOSRelease(strings.NewReader(debianBullseye))
68+
if err != nil {
69+
t.Fatal(err)
70+
}
71+
if !reflect.DeepEqual(want, got) {
72+
t.Fatalf("should have %+v osRelease: got %+v", want, got)
73+
}
74+
}
75+
76+
func TestUpdateStruct(t *testing.T) {
77+
wantedOS := &osRelease{
78+
Name: "Ubuntu",
79+
ID: "ubuntu",
80+
IDLike: "debian",
81+
PrettyName: "Ubuntu 20.04.2 LTS",
82+
Version: "20.04.2 LTS (Focal Fossa)",
83+
VersionID: "20.04",
84+
VersionCodename: "focal",
85+
}
86+
wantedVersion := 20.04
87+
88+
collector, err := NewOSCollector(log.NewNopLogger())
89+
if err != nil {
90+
t.Fatal(err)
91+
}
92+
c := collector.(*osReleaseCollector)
93+
94+
err = c.UpdateStruct("fixtures" + usrLibOSRelease)
95+
if err != nil {
96+
t.Fatal(err)
97+
}
98+
99+
if !reflect.DeepEqual(wantedOS, c.os) {
100+
t.Fatalf("should have %+v osRelease: got %+v", wantedOS, c.os)
101+
}
102+
if wantedVersion != c.version {
103+
t.Errorf("Expected '%v' but got '%v'", wantedVersion, c.version)
104+
}
105+
}

end-to-end-test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ then
100100
fi
101101

102102
./node_exporter \
103+
--path.rootfs="collector/fixtures" \
103104
--path.procfs="collector/fixtures/proc" \
104105
--path.sysfs="collector/fixtures/sys" \
105106
$(for c in ${enabled_collectors}; do echo --collector.${c} ; done) \

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/ema/qdisc v0.0.0-20200603082823-62d0308e3e00
77
github.com/go-kit/log v0.1.0
88
github.com/godbus/dbus v0.0.0-20190402143921-271e53dc4968
9+
github.com/hashicorp/go-envparse v0.0.0-20200406174449-d9cfd743a15e
910
github.com/hodgesds/perf-utils v0.2.5
1011
github.com/illumos/go-kstat v0.0.0-20210513183136-173c9b0a9973
1112
github.com/jsimonetti/rtnetlink v0.0.0-20210713125558-2bfdf1dbdbd6

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hf
139139
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
140140
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
141141
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
142+
github.com/hashicorp/go-envparse v0.0.0-20200406174449-d9cfd743a15e h1:v1d9+AJMP6i4p8BSKNU0InuvmIAdZjQLNN19V86AG4Q=
143+
github.com/hashicorp/go-envparse v0.0.0-20200406174449-d9cfd743a15e/go.mod h1:/NlxCzN2D4C4L2uDE6ux/h6jM+n98VFQM14nnCIfHJU=
142144
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
143145
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
144146
github.com/hodgesds/perf-utils v0.2.5 h1:X992/V3OaNJRM8Ivcram8Hhxz4JhWiKI0T8iGCJwk2k=

0 commit comments

Comments
 (0)