Skip to content

Commit d102c05

Browse files
author
Cody Kaczynski
committed
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
1 parent 3c1182d commit d102c05

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

get_nagios_version/get_nagios_version.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ package get_nagios_version
33
import (
44
"net/http"
55
"strings"
6+
"time"
67

78
"golang.org/x/net/html"
89
)
910

1011
func GetLatestNagiosXIVersion(NagiosXIURL string) (version string, err error) {
1112

13+
// Initialize a client with a timeout in case of connection issues
14+
client := &http.Client{
15+
Timeout: 10 * time.Second,
16+
}
17+
1218
// Fetch the HTML source data from the URL
13-
resp, err := http.Get(NagiosXIURL)
19+
resp, err := client.Get(NagiosXIURL)
1420
if err != nil {
1521
return "", err
1622
}

nagios_exporter.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,17 @@ func (e *Exporter) QueryAPIsAndUpdateMetrics(ch chan<- prometheus.Metric, sslVer
465465
log.Warn(err)
466466
}
467467

468-
updateMetric := CompareNagiosVersions(nagiosVersion, systemInfoObject.Version)
469-
ch <- prometheus.MustNewConstMetric(
470-
updateAvailable, prometheus.GaugeValue, updateMetric,
471-
// updateMetric 0 = no update, updateMetric 1 = update available
472-
)
468+
// Ensure that nagiosVersion is not empty before comparing versions
469+
if nagiosVersion != "" {
470+
updateMetric := CompareNagiosVersions(nagiosVersion, systemInfoObject.Version)
471+
ch <- prometheus.MustNewConstMetric(
472+
updateAvailable, prometheus.GaugeValue, updateMetric,
473+
// updateMetric 0 = no update, updateMetric 1 = update available
474+
)
475+
}
476+
477+
log.Warn("Nagios version wasn't found, skipping version comparison")
478+
473479
} else { // user did not want to compare nagios versions externally so just say there aren't any updates (0)
474480
ch <- prometheus.MustNewConstMetric(
475481
updateAvailable, prometheus.GaugeValue, 0,

0 commit comments

Comments
 (0)