-
-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Linux only. Enable Systemd's watchdog integration when running under Systemd and the service unit has enabled the watchdog feature.
- Loading branch information
Showing
5 changed files
with
85 additions
and
0 deletions.
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,32 @@ | ||
# Systemd's Watchdog integration | ||
|
||
Integrates Beehive with the Systemd's watchdog. | ||
|
||
See http://0pointer.de/blog/projects/watchdog.html | ||
|
||
## Configuration | ||
|
||
As a system service: | ||
|
||
* Add the service unit to `/etc/systemd/system/beehive.conf` | ||
|
||
Sample service unit: | ||
|
||
``` | ||
[Unit] | ||
Description=Beehive with Systemd's watchdog | ||
[Service] | ||
Type=simple | ||
ExecStart=/usr/bin/beehive --config /path/to/beehive.conf | ||
Restart=on-failure | ||
WatchdogSec=30s | ||
[Install] | ||
WantedBy=multi-user.target | ||
``` | ||
*Note: change `/path/to/beehive.conf` to a real path pointing to Beehive's config* | ||
|
||
* Enable the new service: `systemctl enable beehive` | ||
* Start the service: `systemctl start beehive` | ||
|
||
Beehive will automatically detect it's running under Systemd and notify Systemd's watchdog every WatchdogSec/3 seconds (10 seconds in this particular case). Note the notification interval is hardcoded to WatchdogSec/3. |
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,47 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/coreos/go-systemd/daemon" | ||
"github.com/muesli/beehive/api" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// Notifies Systemd's watchdog every WatchdogSec/3 seconds when running | ||
// under Systemd and the watchdog feature has been enabled in | ||
// the service unit. | ||
// | ||
// This will no-op when not running under Systemd. | ||
// | ||
// See http://0pointer.de/blog/projects/watchdog.html | ||
// and https://www.freedesktop.org/software/systemd/man/systemd.service.html | ||
// | ||
func init() { | ||
// returns the configured WatchdogSec in the service unit as time.Duration | ||
interval, err := daemon.SdWatchdogEnabled(false) | ||
if err != nil || interval == 0 { | ||
log.Printf("Systemd watchdog not enabled") | ||
return | ||
} | ||
|
||
// We want to notify the watchdog every WatchdogSec/3, that is, if WatchdogSec is | ||
// set to 30 seconds, we'll send a notification to systemd every 10 seconds. | ||
runEvery := interval / 3 | ||
log.Printf("Systemd watchdog notifications every %.2f seconds", runEvery.Seconds()) | ||
|
||
go func() { | ||
for { | ||
select { | ||
case <-time.After(runEvery): | ||
resp, err := http.Get(api.CanonicalURL().String()) | ||
if err == nil { | ||
resp.Body.Close() | ||
log.Debugf("Systemd watchdog notify") | ||
daemon.SdNotify(false, daemon.SdNotifyWatchdog) | ||
} | ||
} | ||
} | ||
}() | ||
} |