Skip to content

Commit

Permalink
fix: prevent failures when unable to connect to *.nagios.com
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Cody Kaczynski committed Oct 19, 2024
1 parent 3c1182d commit d102c05
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
8 changes: 7 additions & 1 deletion get_nagios_version/get_nagios_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 11 additions & 5 deletions nagios_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d102c05

Please sign in to comment.