Skip to content

Commit

Permalink
Merge pull request #51 from cxdy/fix/issue-50/handle-timeouts
Browse files Browse the repository at this point in the history
fix: prevent failures when unable to connect to *.nagios.com
  • Loading branch information
cxdy authored Oct 21, 2024
2 parents 3c1182d + 5fdea25 commit dfbfc8c
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/updating the metric
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, not updating `nagios_update_available_info` metric")

} 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 dfbfc8c

Please sign in to comment.