diff --git a/Makefile b/Makefile index 9a056f1..4d2910d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ # this is the what ends up in the RPM "Version" field and it is also used as suffix for the built binaries # if you want to commit to OBS it must be a remotely available Git reference -VERSION ?= $(shell git rev-parse --short HEAD) +VERSION ?= $(shell git describe --tags --abbrev=0)dev+git.$(shell git show -s --format=%ct.%h HEAD) +DATE = $(shell date --iso-8601=seconds) # we only use this to comply with RPM changelog conventions at SUSE AUTHOR ?= shap-staff@suse.de @@ -25,7 +26,7 @@ build-all: clean-bin $(ARCHS) $(ARCHS): @mkdir -p build/bin - CGO_ENABLED=0 GOOS=linux GOARCH=$@ go build -trimpath -ldflags "-s -w -X main.version=$(VERSION)" -o build/bin/ha_cluster_exporter-$(VERSION)-$@ + CGO_ENABLED=0 GOOS=linux GOARCH=$@ go build -trimpath -ldflags "-s -w -X main.version=$(VERSION) -X main.buildDate=$(DATE)" -o build/bin/ha_cluster_exporter-$(VERSION)-$@ install: go install diff --git a/ha_cluster_exporter.go b/ha_cluster_exporter.go index 1825fd7..c181982 100644 --- a/ha_cluster_exporter.go +++ b/ha_cluster_exporter.go @@ -3,6 +3,8 @@ package main import ( "fmt" "net/http" + "os" + "runtime" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -17,6 +19,17 @@ import ( "github.com/ClusterLabs/ha_cluster_exporter/internal" ) +var ( + // the released version + version string + // the time the binary was built + buildDate string + // global --help flag + helpFlag *bool + // global --version flag + versionFlag *bool +) + func init() { config.SetConfigName("ha_cluster_exporter") config.AddConfigPath("./") @@ -35,19 +48,35 @@ func init() { flag.String("sbd-config-path", "/etc/sysconfig/sbd", "path to sbd configuration") flag.String("drbdsetup-path", "/sbin/drbdsetup", "path to drbdsetup executable") flag.String("drbdsplitbrain-path", "/var/run/drbd/splitbrain", "path to drbd splitbrain hooks temporary files") - flag.Bool("enable-timestamps", false, "Add the timestamp to every metric line (hint: don't do this unless you really know what you are doing)") + flag.Bool("enable-timestamps", false, "Add the timestamp to every metric line") + flag.CommandLine.MarkDeprecated("enable-timestamps", "server-side metric timestamping is discouraged by Prometheus best-practices and should be avoided") + flag.CommandLine.SortFlags = false err := config.BindPFlags(flag.CommandLine) if err != nil { - log.Errorf("Could not bind config to CLI flags: %v", err) + log.Fatalf("Could not bind config to CLI flags: %v", err) } + + helpFlag = flag.BoolP("help", "h", false, "show this help message") + versionFlag = flag.Bool("version", false, "show version and build information") } func main() { - var err error - flag.Parse() + switch { + case *helpFlag: + showHelp() + case *versionFlag: + showVersion() + default: + run() + } +} + +func run() { + var err error + err = config.ReadInConfig() if err != nil { log.Warn(err) @@ -112,3 +141,16 @@ func main() { log.Infof("Serving metrics on %s", fullListenAddress) log.Fatal(http.ListenAndServe(fullListenAddress, nil)) } + +func showHelp() { + flag.Usage() + os.Exit(0) +} + +func showVersion() { + if buildDate == "" { + buildDate = "at unknown time" + } + fmt.Printf("version %s\nbuilt with %s %s/%s %s\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH, buildDate) + os.Exit(0) +} diff --git a/packaging/obs/prometheus-ha_cluster_exporter.spec b/packaging/obs/prometheus-ha_cluster_exporter.spec index 349a736..c8be199 100644 --- a/packaging/obs/prometheus-ha_cluster_exporter.spec +++ b/packaging/obs/prometheus-ha_cluster_exporter.spec @@ -44,12 +44,13 @@ Prometheus exporter for Pacemaker HA clusters metrics %setup -q -T -D -a 1 # unpack go dependencies in vendor.tar.gz, which was prepared by the source services %define shortname ha_cluster_exporter +%define build_date %(date --iso-8601=seconds) %build export CGO_ENABLED=0 go build -mod=vendor \ - -ldflags="-s -w -X main.version=%{version}" \ + -ldflags="-s -w -X main.version=%{version} -X main.buildDate=%{build_date}" \ -o %{shortname} %install