From 6ca0482870b134dcf239e519b0f85afed53d37d8 Mon Sep 17 00:00:00 2001 From: Jeremy Figgins Date: Sat, 2 Mar 2019 11:11:37 -0600 Subject: [PATCH 1/2] add release info --- collector/paths.go | 5 ++ collector/release_opensuse.go | 89 +++++++++++++++++++++++++++++++++++ end-to-end-test.sh | 1 + 3 files changed, 95 insertions(+) create mode 100644 collector/release_opensuse.go diff --git a/collector/paths.go b/collector/paths.go index 5057f75639..1e4606b33c 100644 --- a/collector/paths.go +++ b/collector/paths.go @@ -22,11 +22,16 @@ import ( var ( // The path of the proc filesystem. + etcPath = kingpin.Flag("path.etcfs", "etcfs mountpoint.").Default("/etc").String() procPath = kingpin.Flag("path.procfs", "procfs mountpoint.").Default(procfs.DefaultMountPoint).String() sysPath = kingpin.Flag("path.sysfs", "sysfs mountpoint.").Default("/sys").String() rootfsPath = kingpin.Flag("path.rootfs", "rootfs mountpoint.").Default("/").String() ) +func etcFilePath(name string) string { + return filepath.Join(*etcPath, name) +} + func procFilePath(name string) string { return filepath.Join(*procPath, name) } diff --git a/collector/release_opensuse.go b/collector/release_opensuse.go new file mode 100644 index 0000000000..3d6feceeb7 --- /dev/null +++ b/collector/release_opensuse.go @@ -0,0 +1,89 @@ +// Copyright 2016 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. + +// +build !novmstat + +package collector + +import ( + "bufio" + // "fmt" + "os" + // "regexp" + // "strconv" + "strings" + + "github.com/prometheus/client_golang/prometheus" + // "gopkg.in/alecthomas/kingpin.v2" +) + +const ( + releaseSubsystem = "release" +) + +type releaseCollector struct{} +type release struct { + ID string + VersionID string +} + +func init() { + registerCollector("release", defaultEnabled, newReleaseCollector) +} + +func newReleaseCollector() (Collector, error) { + return &releaseCollector{}, nil +} + +func (c *releaseCollector) Update(ch chan<- prometheus.Metric) error { + release, err := getRelease() + if err != nil { + return err + } + + ch <- prometheus.MustNewConstMetric( + prometheus.NewDesc( + prometheus.BuildFQName(namespace, releaseSubsystem, "info"), + "/etc/os-release information", + []string{"id", "versionid"}, nil), + prometheus.GaugeValue, + 1, + release.ID, + release.VersionID, + ) + return nil +} + +func getRelease() (release, error) { + release := release{ + ID: "", + VersionID: "", + } + file, err := os.Open(etcFilePath("os-release")) + if err != nil { + return release, err + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + parts := strings.Split(scanner.Text(), "=") + if "ID" == parts[0] { + release.ID = parts[1] + } else if "VERSION_ID" == parts[0] { + release.VersionID = parts[1] + } + } + + return release, scanner.Err() +} diff --git a/end-to-end-test.sh b/end-to-end-test.sh index ea24cf513e..7efc51d418 100755 --- a/end-to-end-test.sh +++ b/end-to-end-test.sh @@ -45,6 +45,7 @@ disabled_collectors=$(cat << COLLECTORS time timex uname + release COLLECTORS ) cd "$(dirname $0)" From 29943e10bff188afcb90bae201ef6346ba6d2058 Mon Sep 17 00:00:00 2001 From: Jeremy Figgins Date: Sat, 2 Mar 2019 18:00:01 -0600 Subject: [PATCH 2/2] fix double quotations --- collector/release_opensuse.go | 1 + 1 file changed, 1 insertion(+) diff --git a/collector/release_opensuse.go b/collector/release_opensuse.go index 3d6feceeb7..c4baa3c740 100644 --- a/collector/release_opensuse.go +++ b/collector/release_opensuse.go @@ -78,6 +78,7 @@ func getRelease() (release, error) { scanner := bufio.NewScanner(file) for scanner.Scan() { parts := strings.Split(scanner.Text(), "=") + parts[1] = strings.Replace(parts[1], "\"", "", -1) if "ID" == parts[0] { release.ID = parts[1] } else if "VERSION_ID" == parts[0] {