Skip to content

Commit

Permalink
In case calling the nft binary fails this exporter exits immediately. (
Browse files Browse the repository at this point in the history
  • Loading branch information
equinox0815 authored Aug 25, 2023
1 parent a173315 commit 2d748d0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
6 changes: 6 additions & 0 deletions descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package main
import "github.com/prometheus/client_golang/prometheus"

var (
upDesc = prometheus.NewDesc(
"nftables_up",
"'1' if reading the nft output was successful, '0' otherwise",
nil,
nil,
)
counterBytesDesc = prometheus.NewDesc(
"nftables_counter_bytes",
"Bytes, matched by counter",
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type nftablesManagerCollector struct {

// Describe sends the super-set of all possible descriptors of metrics
func (i nftablesManagerCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- upDesc
ch <- counterBytesDesc
ch <- counterPacketsDesc
ch <- tableChainsDesc
Expand All @@ -32,7 +33,9 @@ func (i nftablesManagerCollector) Collect(ch chan<- prometheus.Metric) {
json, err := readData(i.opts)
if err != nil {
log.Printf("failed parsing nftables data: %s", err)
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 0)
} else {
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 1)
nft := newNFTables(json, ch)
nft.Collect()
}
Expand Down
2 changes: 1 addition & 1 deletion readjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func readNFTables(opts options) (gjson.Result, error) {
nft := opts.Nft.NFTLocation
out, err := exec.Command(nft, "-j", "list", "ruleset").Output()
if err != nil {
log.Fatalf("nftables reading error: %s", err)
log.Printf("nftables reading error: %s", err)
}
return parseJSON(string(out))
}
Expand Down

0 comments on commit 2d748d0

Please sign in to comment.