-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add systemd unit active state gauge #4
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a91015a
feat: add systemd unit active state gauge
LaurentMontBlanc d6f2627
fix: add default return for failing gauge
LaurentMontBlanc 4624a5a
fix: sanitize gauge name string
LaurentMontBlanc 0cdafce
docs: update README.md with service unit examples
LaurentMontBlanc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"strings" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
elementsrpc "github.com/rddl-network/elements-rpc" | ||
"github.com/rddl-network/rddl-prometheus-exporter/config" | ||
"github.com/rddl-network/rddl-prometheus-exporter/elements" | ||
"github.com/rddl-network/rddl-prometheus-exporter/system" | ||
) | ||
|
||
func setGauge(name string, help string, namespace string, subsystem string, callback func() float64) { | ||
gaugeFunc := prometheus.NewGaugeFunc(prometheus.GaugeOpts{ | ||
Namespace: namespace, | ||
Subsystem: subsystem, | ||
Name: name, | ||
Help: help, | ||
}, callback) | ||
prometheus.MustRegister(gaugeFunc) | ||
} | ||
|
||
func registerGauges(ctx context.Context, logger *log.Logger, cfg *config.Config) { | ||
units := cfg.GetServiceUnits() | ||
for _, unit := range units { | ||
unit := unit | ||
logger.Print("registering gauge for service unit: " + unit) | ||
unitName := strings.Split(unit, ".")[0] | ||
sanitizedUnit := strings.ReplaceAll(unitName, "-", "_") | ||
setGauge("service_active_state_"+sanitizedUnit, "ActiveState for network relevant service: "+unitName, "systemd", "units", func() float64 { | ||
return system.CheckUnitActiveState(ctx, unit) | ||
}) | ||
} | ||
|
||
wallets, err := elementsrpc.ListWallets(cfg.GetElementsURL(""), []string{}) | ||
if err != nil { | ||
log.Fatalf("fatal error fetching wallets: %s", err.Error()) | ||
} | ||
|
||
for _, wallet := range wallets { | ||
wallet := strings.TrimSpace(wallet) | ||
sanitizedWallet := strings.ReplaceAll(wallet, "-", "_") | ||
logger.Printf("registering gauge for wallet: " + wallet) | ||
setGauge("balance_"+sanitizedWallet, "Bitcoin balance for network relevant wallet: "+wallet, "elementsd", "wallets", func() float64 { | ||
url := cfg.GetElementsURL(wallet) | ||
balance, err := elements.GetWalletBalance(url, wallet) | ||
if err != nil { | ||
log.Print(err.Error()) | ||
return 0 | ||
} | ||
return balance | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package system | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/coreos/go-systemd/v22/dbus" | ||
) | ||
|
||
// CheckUnitActiveState takes the (unescaped) unit name and returns 1 if the unit is active or 0 if inactive | ||
func CheckUnitActiveState(ctx context.Context, unit string) float64 { | ||
logger := log.Default() | ||
|
||
conn, err := dbus.NewWithContext(ctx) | ||
if err != nil { | ||
logger.Print(err) | ||
return 0 | ||
} | ||
|
||
unitProps, err := conn.GetUnitPropertiesContext(ctx, unit) | ||
if err != nil { | ||
logger.Print(err) | ||
return 0 | ||
} | ||
|
||
state, ok := unitProps["ActiveState"] | ||
if !ok { | ||
logger.Printf("%s properties do not contain ActiveState", unit) | ||
} | ||
|
||
if state != "active" { | ||
return 0 | ||
} | ||
|
||
return 1 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to give a real example here, because i can see that the string passed here "only" splits at
,
. So if one puts inservice-units = 'foo;bar'
, which looks ok, the exporter won't work correctlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and as it seems the units need to be specified with
.service
extension for another string split. so a good example imho would beservice-units = 'foo.service,bar.service'
.