From d102c0555f386665fc9637520c79393be4383332 Mon Sep 17 00:00:00 2001 From: Cody Kaczynski Date: Sat, 19 Oct 2024 12:04:46 -0400 Subject: [PATCH] fix: prevent failures when unable to connect to *.nagios.com Fixes #50 - Creates an HTTP client with a specified timeout to catch 'connection timeout' errors instead of only waiting for an HTTP status code - Skip updating the metric if a version isn't returned from GetLatestNagiosXIVersion --- get_nagios_version/get_nagios_version.go | 8 +++++++- nagios_exporter.go | 16 +++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/get_nagios_version/get_nagios_version.go b/get_nagios_version/get_nagios_version.go index 20b7469..ce6905e 100755 --- a/get_nagios_version/get_nagios_version.go +++ b/get_nagios_version/get_nagios_version.go @@ -3,14 +3,20 @@ package get_nagios_version import ( "net/http" "strings" + "time" "golang.org/x/net/html" ) func GetLatestNagiosXIVersion(NagiosXIURL string) (version string, err error) { + // Initialize a client with a timeout in case of connection issues + client := &http.Client{ + Timeout: 10 * time.Second, + } + // Fetch the HTML source data from the URL - resp, err := http.Get(NagiosXIURL) + resp, err := client.Get(NagiosXIURL) if err != nil { return "", err } diff --git a/nagios_exporter.go b/nagios_exporter.go index 148c1c2..8f9cd99 100644 --- a/nagios_exporter.go +++ b/nagios_exporter.go @@ -465,11 +465,17 @@ func (e *Exporter) QueryAPIsAndUpdateMetrics(ch chan<- prometheus.Metric, sslVer log.Warn(err) } - updateMetric := CompareNagiosVersions(nagiosVersion, systemInfoObject.Version) - ch <- prometheus.MustNewConstMetric( - updateAvailable, prometheus.GaugeValue, updateMetric, - // updateMetric 0 = no update, updateMetric 1 = update available - ) + // Ensure that nagiosVersion is not empty before comparing versions + if nagiosVersion != "" { + updateMetric := CompareNagiosVersions(nagiosVersion, systemInfoObject.Version) + ch <- prometheus.MustNewConstMetric( + updateAvailable, prometheus.GaugeValue, updateMetric, + // updateMetric 0 = no update, updateMetric 1 = update available + ) + } + + log.Warn("Nagios version wasn't found, skipping version comparison") + } else { // user did not want to compare nagios versions externally so just say there aren't any updates (0) ch <- prometheus.MustNewConstMetric( updateAvailable, prometheus.GaugeValue, 0,