-
Notifications
You must be signed in to change notification settings - Fork 62
Add hw-exporter #554
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 hw-exporter #554
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| apiVersion: apps/v1 | ||
| kind: DaemonSet | ||
| metadata: | ||
| name: hw-exporter | ||
| spec: | ||
| selector: | ||
| matchLabels: | ||
| app: hw-exporter | ||
| template: | ||
| metadata: | ||
| labels: | ||
| app: hw-exporter | ||
| spec: | ||
| containers: | ||
| - name: hw-exporter | ||
| image: {{ .Values.registry }}{{ .Values.images.hw_exporter }} | ||
| args: | ||
| - --metrics-port=9100 | ||
| - --chroot=/host | ||
| volumeMounts: | ||
| - mountPath: /host/proc | ||
| name: proc | ||
| readOnly: true | ||
| - mountPath: /host/sys | ||
| name: sys | ||
| readOnly: true | ||
| - mountPath: /host/usr/share | ||
| name: usr-share | ||
| readOnly: true | ||
| securityContext: | ||
| fsGroup: 65534 | ||
| runAsGroup: 65534 | ||
| runAsNonRoot: true | ||
| runAsUser: 65534 | ||
| tolerations: | ||
| - operator: Exists | ||
| effect: NoSchedule | ||
| volumes: | ||
| - hostPath: | ||
| path: /proc | ||
| type: "" | ||
| name: proc | ||
| - hostPath: | ||
| path: /sys | ||
| type: "" | ||
| name: sys | ||
| # Mount pcidb from host, which could be in /usr/share/misc or /usr/share/hwdata. | ||
| - hostPath: | ||
| path: /usr/share | ||
| type: "" | ||
| name: usr-share | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Service | ||
| metadata: | ||
| name: hw-exporter | ||
| labels: | ||
| app: hw-exporter | ||
| spec: | ||
| clusterIP: None | ||
| ports: | ||
| - port: 9100 | ||
| name: http-metrics | ||
| selector: | ||
| app: hw-exporter | ||
| type: ClusterIP | ||
| --- | ||
| apiVersion: monitoring.coreos.com/v1 | ||
| kind: ServiceMonitor | ||
| metadata: | ||
| name: hw-exporter | ||
| labels: | ||
| prometheus: kube-prometheus | ||
| spec: | ||
| endpoints: | ||
| - port: http-metrics | ||
| path: /metrics | ||
| interval: 60s | ||
| selector: | ||
| matchLabels: | ||
| app: hw-exporter |
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,52 @@ | ||
| load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") | ||
| load("@rules_oci//oci:defs.bzl", "oci_image") | ||
| load("@rules_pkg//pkg:tar.bzl", "pkg_tar") | ||
|
|
||
| go_library( | ||
| name = "go_default_library", | ||
| srcs = ["main.go"], | ||
| importpath = "github.com/googlecloudrobotics/core/src/go/cmd/hw-exporter", | ||
| visibility = ["//visibility:private"], | ||
| deps = [ | ||
| "@com_github_googlecloudrobotics_ilog//:go_default_library", | ||
| "@com_github_jaypipes_ghw//:go_default_library", | ||
| "@com_github_jaypipes_ghw//pkg/option:go_default_library", | ||
| "@com_github_jaypipes_ghw//pkg/util:go_default_library", | ||
| "@com_github_prometheus_client_golang//prometheus:go_default_library", | ||
| "@com_github_prometheus_client_golang//prometheus/promhttp:go_default_library", | ||
| ], | ||
| ) | ||
|
|
||
| go_binary( | ||
| name = "hw-exporter", | ||
| embed = [":go_default_library"], | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| pkg_tar( | ||
| name = "hw-exporter-image-layer", | ||
| srcs = [":hw-exporter"], | ||
| extension = "tar.gz", | ||
| ) | ||
|
|
||
| oci_image( | ||
| name = "hw-exporter-image", | ||
| base = "@distroless_base", | ||
| entrypoint = ["/hw-exporter"], | ||
| tars = [":hw-exporter-image-layer"], | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "go_default_test", | ||
| srcs = ["main_test.go"], | ||
| embed = [":go_default_library"], | ||
| deps = [ | ||
| "@com_github_jaypipes_ghw//:go_default_library", | ||
| "@com_github_jaypipes_ghw//pkg/option:go_default_library", | ||
| "@com_github_jaypipes_ghw//pkg/pci:go_default_library", | ||
| "@com_github_jaypipes_ghw//pkg/util:go_default_library", | ||
| "@com_github_jaypipes_pcidb//:go_default_library", | ||
| "@com_github_prometheus_client_golang//prometheus/testutil:go_default_library", | ||
| ], | ||
| ) |
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,122 @@ | ||
| // hw-exporter exposes a Prometheus metric pci_device_count that indicates the | ||
| // number of each PCI device type (vendor/product/class/driver) installed on | ||
| // this node. | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "log/slog" | ||
| "net/http" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
|
|
||
| "github.com/googlecloudrobotics/ilog" | ||
| "github.com/jaypipes/ghw" | ||
| "github.com/jaypipes/ghw/pkg/option" | ||
| "github.com/jaypipes/ghw/pkg/util" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||
| ) | ||
|
|
||
| var ( | ||
| metricsPort = flag.Int("metrics-port", 9999, "Port to expose Prometheus metrics on.") | ||
| logLevel = flag.Int("log-level", int(slog.LevelInfo), "the log message level required to be logged") | ||
| chroot = flag.String("chroot", "/", "Path to chroot into before collecting hardware info.") | ||
| ) | ||
|
|
||
| type pciCollector struct { | ||
| pciDeviceCount *prometheus.Desc | ||
| } | ||
|
|
||
| func newPciCollector() *pciCollector { | ||
| return &pciCollector{ | ||
| pciDeviceCount: prometheus.NewDesc( | ||
| "pci_device_count", | ||
| "Number of PCI devices by vendor, product, class, and driver.", | ||
| []string{"vendor", "product", "class", "driver"}, | ||
| nil, | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| // Describe implements the prometheus.Collector interface. | ||
| func (c *pciCollector) Describe(ch chan<- *prometheus.Desc) { | ||
| ch <- c.pciDeviceCount | ||
| } | ||
|
|
||
| // getNameOrID returns the name of a PCI device component, or its ID if the name is unknown. | ||
| func getNameOrID(name, id string) string { | ||
| if name == util.UNKNOWN { | ||
| return "0x" + id | ||
| } | ||
| return name | ||
| } | ||
|
|
||
| // Collect implements the prometheus.Collector interface, counting the number of | ||
| // devices by vendor/product/class/driver. | ||
| func (c *pciCollector) Collect(ch chan<- prometheus.Metric) { | ||
| pciInfo, err := ghw.PCI(&option.Option{Chroot: chroot}) | ||
| if err != nil { | ||
| slog.Error("Failed to get PCI info", ilog.Err(err)) | ||
| return | ||
| } | ||
|
|
||
| deviceCounts := make(map[[4]string]int) | ||
| for _, device := range pciInfo.Devices { | ||
| vendor := getNameOrID(device.Vendor.Name, device.Vendor.ID) | ||
| product := getNameOrID(device.Product.Name, device.Product.ID) | ||
| class := getNameOrID(device.Class.Name, device.Class.ID) | ||
| labels := [4]string{vendor, product, class, device.Driver} | ||
| deviceCounts[labels]++ | ||
| } | ||
|
|
||
| for labels, count := range deviceCounts { | ||
| ch <- prometheus.MustNewConstMetric(c.pciDeviceCount, prometheus.GaugeValue, float64(count), labels[0], labels[1], labels[2], labels[3]) | ||
| } | ||
| } | ||
|
|
||
| func main() { | ||
| flag.Parse() | ||
| logHandler := ilog.NewLogHandler(slog.Level(*logLevel), os.Stderr) | ||
| slog.SetDefault(slog.New(logHandler)) | ||
|
|
||
| // Run once on startup to test container setup, this is useful during development. | ||
| _, err := ghw.PCI(&option.Option{Chroot: chroot}) | ||
| if err != nil { | ||
| slog.Error("Failed to get PCI info", ilog.Err(err)) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| // Construct and run the metrics server until stopped by k8s (or Ctrl+C). | ||
| ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT) | ||
| defer cancel() | ||
|
|
||
| registry := prometheus.NewRegistry() | ||
| registry.MustRegister(newPciCollector()) | ||
|
|
||
| mux := http.NewServeMux() | ||
| mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{})) | ||
|
|
||
| server := &http.Server{ | ||
| Addr: fmt.Sprintf(":%d", *metricsPort), | ||
| Handler: mux, | ||
| } | ||
|
|
||
| go func() { | ||
|
drigz marked this conversation as resolved.
|
||
| slog.Info("Starting metrics server", slog.Int("port", *metricsPort)) | ||
| if err := server.ListenAndServe(); err != http.ErrServerClosed { | ||
| slog.Error("Metrics server failed", ilog.Err(err)) | ||
| os.Exit(1) | ||
| } | ||
| }() | ||
|
|
||
| // Call Shutdown() in the main goroutine because ListenAndServe() returns | ||
| // immediately but if the main goroutine ends then, the process will stop | ||
| // before finishing any ongoing requests. | ||
| <-ctx.Done() | ||
| slog.Info("Shutting down metrics server...") | ||
| server.Shutdown(context.Background()) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you need the map? Are there duplicates?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This counts up if we have two devices of the same type (eg a card with multiple NICs).