|
| 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 | +//go:build linux && !nodmi |
| 15 | +// +build linux,!nodmi |
| 16 | + |
| 17 | +package collector |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/go-kit/log" |
| 25 | + "github.com/go-kit/log/level" |
| 26 | + "github.com/prometheus/client_golang/prometheus" |
| 27 | + "github.com/prometheus/procfs/sysfs" |
| 28 | +) |
| 29 | + |
| 30 | +type dmiCollector struct { |
| 31 | + infoDesc *prometheus.Desc |
| 32 | + values []string |
| 33 | +} |
| 34 | + |
| 35 | +func init() { |
| 36 | + registerCollector("dmi", defaultEnabled, NewDMICollector) |
| 37 | +} |
| 38 | + |
| 39 | +// NewDMICollector returns a new Collector exposing DMI information. |
| 40 | +func NewDMICollector(logger log.Logger) (Collector, error) { |
| 41 | + fs, err := sysfs.NewFS(*sysPath) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("failed to open sysfs: %w", err) |
| 44 | + } |
| 45 | + |
| 46 | + dmi, err := fs.DMIClass() |
| 47 | + if err != nil { |
| 48 | + if errors.Is(err, os.ErrNotExist) { |
| 49 | + level.Debug(logger).Log("msg", "Platform does not support Desktop Management Interface (DMI) information", "err", err) |
| 50 | + dmi = &sysfs.DMIClass{} |
| 51 | + } else { |
| 52 | + return nil, fmt.Errorf("failed to read Desktop Management Interface (DMI) information: %w", err) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + var labels, values []string |
| 57 | + for label, value := range map[string]*string{ |
| 58 | + "bios_date": dmi.BiosDate, |
| 59 | + "bios_release": dmi.BiosRelease, |
| 60 | + "bios_vendor": dmi.BiosVendor, |
| 61 | + "bios_version": dmi.BiosVersion, |
| 62 | + "board_asset_tag": dmi.BoardAssetTag, |
| 63 | + "board_name": dmi.BoardName, |
| 64 | + "board_serial": dmi.BoardSerial, |
| 65 | + "board_vendor": dmi.BoardVendor, |
| 66 | + "board_version": dmi.BoardVersion, |
| 67 | + "chassis_asset_tag": dmi.ChassisAssetTag, |
| 68 | + "chassis_serial": dmi.ChassisSerial, |
| 69 | + "chassis_vendor": dmi.ChassisVendor, |
| 70 | + "chassis_version": dmi.ChassisVersion, |
| 71 | + "product_family": dmi.ProductFamily, |
| 72 | + "product_name": dmi.ProductName, |
| 73 | + "product_serial": dmi.ProductSerial, |
| 74 | + "product_sku": dmi.ProductSKU, |
| 75 | + "product_uuid": dmi.ProductUUID, |
| 76 | + "product_version": dmi.ProductVersion, |
| 77 | + "system_vendor": dmi.SystemVendor, |
| 78 | + } { |
| 79 | + if value != nil { |
| 80 | + labels = append(labels, label) |
| 81 | + values = append(values, *value) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Construct DMI metric only once since it will not change until the next reboot. |
| 86 | + return &dmiCollector{ |
| 87 | + infoDesc: prometheus.NewDesc( |
| 88 | + prometheus.BuildFQName(namespace, "dmi", "info"), |
| 89 | + "A metric with a constant '1' value labeled by bios_date, bios_release, bios_vendor, bios_version, "+ |
| 90 | + "board_asset_tag, board_name, board_serial, board_vendor, board_version, chassis_asset_tag, "+ |
| 91 | + "chassis_serial, chassis_vendor, chassis_version, product_family, product_name, product_serial, "+ |
| 92 | + "product_sku, product_uuid, product_version, system_vendor if provided by DMI.", |
| 93 | + labels, nil, |
| 94 | + ), |
| 95 | + values: values, |
| 96 | + }, nil |
| 97 | +} |
| 98 | + |
| 99 | +func (c *dmiCollector) Update(ch chan<- prometheus.Metric) error { |
| 100 | + if len(c.values) == 0 { |
| 101 | + return ErrNoData |
| 102 | + } |
| 103 | + ch <- prometheus.MustNewConstMetric(c.infoDesc, prometheus.GaugeValue, 1.0, c.values...) |
| 104 | + return nil |
| 105 | +} |
0 commit comments