-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add os release collector #2094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add os release collector #2094
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| NAME="Ubuntu" | ||
| VERSION="20.04.2 LTS (Focal Fossa)" | ||
| ID=ubuntu | ||
| ID_LIKE=debian | ||
| PRETTY_NAME="Ubuntu 20.04.2 LTS" | ||
| VERSION_ID="20.04" | ||
| HOME_URL="https://www.ubuntu.com/" | ||
| SUPPORT_URL="https://help.ubuntu.com/" | ||
| BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" | ||
| PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" | ||
| VERSION_CODENAME=focal | ||
| UBUNTU_CODENAME=focal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| // Copyright 2021 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 collector | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io" | ||
| "os" | ||
| "regexp" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/go-kit/log" | ||
| "github.com/go-kit/log/level" | ||
| envparse "github.com/hashicorp/go-envparse" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| const ( | ||
| etcOSRelease = "/etc/os-release" | ||
| usrLibOSRelease = "/usr/lib/os-release" | ||
| ) | ||
|
|
||
| var ( | ||
| versionRegex = regexp.MustCompile(`^[0-9]+\.?[0-9]*`) | ||
| ) | ||
|
|
||
| type osRelease struct { | ||
| Name string | ||
| ID string | ||
| IDLike string | ||
| PrettyName string | ||
| Variant string | ||
| VariantID string | ||
| Version string | ||
| VersionID string | ||
| VersionCodename string | ||
| BuildID string | ||
| ImageID string | ||
| ImageVersion string | ||
| } | ||
|
|
||
| type osReleaseCollector struct { | ||
| infoDesc *prometheus.Desc | ||
| logger log.Logger | ||
| os *osRelease | ||
| osFilename string // file name of cached release information | ||
| osMtime time.Time // mtime of cached release file | ||
| osMutex sync.Mutex | ||
| osReleaseFilenames []string // all os-release file names to check | ||
| version float64 | ||
| versionDesc *prometheus.Desc | ||
| } | ||
|
|
||
| func init() { | ||
| registerCollector("os", defaultEnabled, NewOSCollector) | ||
| } | ||
|
|
||
| // NewOSCollector returns a new Collector exposing os-release information. | ||
| func NewOSCollector(logger log.Logger) (Collector, error) { | ||
| return &osReleaseCollector{ | ||
| logger: logger, | ||
| infoDesc: prometheus.NewDesc( | ||
| prometheus.BuildFQName(namespace, "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.", | ||
| []string{"build_id", "id", "id_like", "image_id", "image_version", "name", "pretty_name", | ||
| "variant", "variant_id", "version", "version_codename", "version_id"}, nil, | ||
| ), | ||
| osReleaseFilenames: []string{etcOSRelease, usrLibOSRelease}, | ||
| versionDesc: prometheus.NewDesc( | ||
| prometheus.BuildFQName(namespace, "os", "version"), | ||
| "Metric containing the major.minor part of the OS version.", | ||
| []string{"id", "id_like", "name"}, nil, | ||
| ), | ||
| }, nil | ||
| } | ||
|
|
||
| func parseOSRelease(r io.Reader) (*osRelease, error) { | ||
| env, err := envparse.Parse(r) | ||
| return &osRelease{ | ||
| Name: env["NAME"], | ||
| ID: env["ID"], | ||
| IDLike: env["ID_LIKE"], | ||
| PrettyName: env["PRETTY_NAME"], | ||
| Variant: env["VARIANT"], | ||
| VariantID: env["VARIANT_ID"], | ||
| Version: env["VERSION"], | ||
| VersionID: env["VERSION_ID"], | ||
| VersionCodename: env["VERSION_CODENAME"], | ||
| BuildID: env["BUILD_ID"], | ||
| ImageID: env["IMAGE_ID"], | ||
| ImageVersion: env["IMAGE_VERSION"], | ||
| }, err | ||
| } | ||
|
|
||
| func (c *osReleaseCollector) UpdateStruct(path string) error { | ||
| releaseFile, err := os.Open(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer releaseFile.Close() | ||
|
|
||
| stat, err := releaseFile.Stat() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| t := stat.ModTime() | ||
| if path == c.osFilename && t == c.osMtime { | ||
| // osReleaseCollector struct is already up-to-date. | ||
| return nil | ||
| } | ||
|
|
||
| // Acquire a lock to update the osReleaseCollector struct. | ||
| c.osMutex.Lock() | ||
| defer c.osMutex.Unlock() | ||
|
|
||
| level.Debug(c.logger).Log("msg", "file modification time has changed", | ||
| "file", path, "old_value", c.osMtime, "new_value", t) | ||
| c.osFilename = path | ||
| c.osMtime = t | ||
|
|
||
| c.os, err = parseOSRelease(releaseFile) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| majorMinor := versionRegex.FindString(c.os.VersionID) | ||
| if majorMinor != "" { | ||
| c.version, err = strconv.ParseFloat(majorMinor, 64) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| c.version = 0 | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *osReleaseCollector) Update(ch chan<- prometheus.Metric) error { | ||
| for i, path := range c.osReleaseFilenames { | ||
| err := c.UpdateStruct(*rootfsPath + path) | ||
| if err == nil { | ||
| break | ||
| } | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| if i >= (len(c.osReleaseFilenames) - 1) { | ||
| level.Debug(c.logger).Log("msg", "no os-release file found", "files", strings.Join(c.osReleaseFilenames, ",")) | ||
| return ErrNoData | ||
| } | ||
| continue | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| ch <- prometheus.MustNewConstMetric(c.infoDesc, prometheus.GaugeValue, 1.0, | ||
| c.os.BuildID, c.os.ID, c.os.IDLike, c.os.ImageID, c.os.ImageVersion, c.os.Name, c.os.PrettyName, | ||
| c.os.Variant, c.os.VariantID, c.os.Version, c.os.VersionCodename, c.os.VersionID) | ||
| if c.version > 0 { | ||
bdrung marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ch <- prometheus.MustNewConstMetric(c.versionDesc, prometheus.GaugeValue, c.version, | ||
| c.os.ID, c.os.IDLike, c.os.Name) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Copyright 2021 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 collector | ||
|
|
||
| import ( | ||
| "os" | ||
| "reflect" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/go-kit/log" | ||
| ) | ||
|
|
||
| const debianBullseye string = `PRETTY_NAME="Debian GNU/Linux 11 (bullseye)" | ||
| NAME="Debian GNU/Linux" | ||
| VERSION_ID="11" | ||
| VERSION="11 (bullseye)" | ||
| VERSION_CODENAME=bullseye | ||
| ID=debian | ||
| HOME_URL="https://www.debian.org/" | ||
| SUPPORT_URL="https://www.debian.org/support" | ||
| BUG_REPORT_URL="https://bugs.debian.org/" | ||
| ` | ||
|
|
||
| func TestParseOSRelease(t *testing.T) { | ||
| want := &osRelease{ | ||
| Name: "Ubuntu", | ||
| ID: "ubuntu", | ||
| IDLike: "debian", | ||
| PrettyName: "Ubuntu 20.04.2 LTS", | ||
| Version: "20.04.2 LTS (Focal Fossa)", | ||
| VersionID: "20.04", | ||
| VersionCodename: "focal", | ||
| } | ||
|
|
||
| osReleaseFile, err := os.Open("fixtures" + usrLibOSRelease) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| got, err := parseOSRelease(osReleaseFile) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !reflect.DeepEqual(want, got) { | ||
| t.Fatalf("should have %+v osRelease: got %+v", want, got) | ||
| } | ||
|
|
||
| want = &osRelease{ | ||
| Name: "Debian GNU/Linux", | ||
| ID: "debian", | ||
| PrettyName: "Debian GNU/Linux 11 (bullseye)", | ||
| Version: "11 (bullseye)", | ||
| VersionID: "11", | ||
| VersionCodename: "bullseye", | ||
| } | ||
| got, err = parseOSRelease(strings.NewReader(debianBullseye)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !reflect.DeepEqual(want, got) { | ||
| t.Fatalf("should have %+v osRelease: got %+v", want, got) | ||
| } | ||
| } | ||
|
|
||
| func TestUpdateStruct(t *testing.T) { | ||
| wantedOS := &osRelease{ | ||
| Name: "Ubuntu", | ||
| ID: "ubuntu", | ||
| IDLike: "debian", | ||
| PrettyName: "Ubuntu 20.04.2 LTS", | ||
| Version: "20.04.2 LTS (Focal Fossa)", | ||
| VersionID: "20.04", | ||
| VersionCodename: "focal", | ||
| } | ||
| wantedVersion := 20.04 | ||
|
|
||
| collector, err := NewOSCollector(log.NewNopLogger()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| c := collector.(*osReleaseCollector) | ||
|
|
||
| err = c.UpdateStruct("fixtures" + usrLibOSRelease) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(wantedOS, c.os) { | ||
| t.Fatalf("should have %+v osRelease: got %+v", wantedOS, c.os) | ||
| } | ||
| if wantedVersion != c.version { | ||
| t.Errorf("Expected '%v' but got '%v'", wantedVersion, c.version) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.