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..c4baa3c740 --- /dev/null +++ b/collector/release_opensuse.go @@ -0,0 +1,90 @@ +// 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(), "=") + parts[1] = strings.Replace(parts[1], "\"", "", -1) + 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)"