os_release.go: Added support end parsing support. (#2982)

* os_release.go: Added support end parsing support.

Fixes: #2977

Signed-off-by: Jonathan Davies <jpds@protonmail.com>

* os_release_test.go: Added TestParseOSSupportEnd.

Signed-off-by: Jonathan Davies <jpds@protonmail.com>

---------

Signed-off-by: Jonathan Davies <jpds@protonmail.com>
This commit is contained in:
Jonathan Davies 2024-04-03 10:23:03 +00:00 committed by GitHub
parent 3accd4cf82
commit b6227af54b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 65 additions and 0 deletions

View File

@ -53,6 +53,7 @@ type osRelease struct {
BuildID string
ImageID string
ImageVersion string
SupportEnd string
}
type osReleaseCollector struct {
@ -65,6 +66,8 @@ type osReleaseCollector struct {
osReleaseFilenames []string // all os-release file names to check
version float64
versionDesc *prometheus.Desc
supportEnd time.Time
supportEndDesc *prometheus.Desc
}
type Plist struct {
@ -97,6 +100,11 @@ func NewOSCollector(logger log.Logger) (Collector, error) {
"Metric containing the major.minor part of the OS version.",
[]string{"id", "id_like", "name"}, nil,
),
supportEndDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "os", "support_end_timestamp_seconds"),
"Metric containing the end-of-life date timestamp of the OS.",
nil, nil,
),
}, nil
}
@ -115,6 +123,7 @@ func parseOSRelease(r io.Reader) (*osRelease, error) {
BuildID: env["BUILD_ID"],
ImageID: env["IMAGE_ID"],
ImageVersion: env["IMAGE_VERSION"],
SupportEnd: env["SUPPORT_END"],
}, err
}
@ -169,6 +178,15 @@ func (c *osReleaseCollector) UpdateStruct(path string) error {
} else {
c.version = 0
}
if c.os.SupportEnd != "" {
c.supportEnd, err = time.Parse("2006-01-02", c.os.SupportEnd)
if err != nil {
return err
}
}
return nil
}
@ -195,6 +213,11 @@ func (c *osReleaseCollector) Update(ch chan<- prometheus.Metric) error {
ch <- prometheus.MustNewConstMetric(c.versionDesc, prometheus.GaugeValue, c.version,
c.os.ID, c.os.IDLike, c.os.Name)
}
if c.os.SupportEnd != "" {
ch <- prometheus.MustNewConstMetric(c.supportEndDesc, prometheus.GaugeValue, float64(c.supportEnd.Unix()))
}
return nil
}

View File

@ -33,12 +33,28 @@ SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
`
const nixosTapir string = `BUG_REPORT_URL="https://github.com/NixOS/nixpkgs/issues"
BUILD_ID="23.11.20240328.219951b"
DOCUMENTATION_URL="https://nixos.org/learn.html"
HOME_URL="https://nixos.org/"
ID=nixos
LOGO="nix-snowflake"
NAME=NixOS
PRETTY_NAME="NixOS 23.11 (Tapir)"
SUPPORT_END="2024-06-30"
SUPPORT_URL="https://nixos.org/community.html"
VERSION="23.11 (Tapir)"
VERSION_CODENAME=tapir
VERSION_ID="23.11"
`
func TestParseOSRelease(t *testing.T) {
want := &osRelease{
Name: "Ubuntu",
ID: "ubuntu",
IDLike: "debian",
PrettyName: "Ubuntu 20.04.2 LTS",
SupportEnd: "",
Version: "20.04.2 LTS (Focal Fossa)",
VersionID: "20.04",
VersionCodename: "focal",
@ -75,6 +91,32 @@ func TestParseOSRelease(t *testing.T) {
}
}
func TestParseOSSupportEnd(t *testing.T) {
want := &osRelease{
BuildID: "23.11.20240328.219951b",
Name: "NixOS",
ID: "nixos",
IDLike: "",
ImageID: "",
ImageVersion: "",
PrettyName: "NixOS 23.11 (Tapir)",
SupportEnd: "2024-06-30",
Variant: "",
VariantID: "",
Version: "23.11 (Tapir)",
VersionID: "23.11",
VersionCodename: "tapir",
}
got, err := parseOSRelease(strings.NewReader(nixosTapir))
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",