systemd

package module
v3.1.2+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2019 License: MIT Imports: 7 Imported by: 0

README

go-systemd

Go Report Card GoDoc

Easily communicate with systemd when run as daemon within a service unit.

Notifier

With notifier you can notify to systemd that your program is starting, stopping, reloading...

For example, if your daemon needs some time for initializing its controllers before really being considered as ready, you can specify to systemd that this is a "notify" service and send it a notification when ready.

It is safe to use it even if systemd notify support is disabled.

[Service]
Type=notify
import (
    systemd "github.com/iguanesolutions/go-systemd"
)

// Init http server
server := &http.Server{
    Addr:    "host:port",
    Handler: myHTTPHandler,
}

// Do some more inits

// Notify ready to systemd
if err = systemd.NotifyReady(); err != nil {
    log.Printf("failed to notify ready to systemd: %v\n", err)
}


// Start the server
if err = server.ListenAndServe(); err != nil {
    log.Printf("failed to start http server: %v\n", err)
}

When stopping, you can notify systemd that you have indeed received the SIGTERM and you have launched the stop procedure

import (
    systemd "github.com/iguanesolutions/go-systemd"
)

// Notify to systemd that we are stopping
var err error
if err = systemd.NotifyStopping(); err != nil {
    log.Printf("failed to notify stopping to systemd: %v\n", err)
}

// Stop some more things

// Stop the server (with timeout)
ctx, cancelCtx := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelCtx()
if err = server.Shutdown(ctx); err != nil {
    log.Printf("failed to shutdown http server: %v\n", err)
}

You can also notify status to systemd

import (
    systemd "github.com/iguanesolutions/go-systemd"
)

if err := systemd.NotifyStatus(fmt.Sprintf("There is currently %d active connections", activeConns)); err != nil {
    log.Printf("failed to notify status to systemd: %v\n", err)
}

systemctl status output example:

user@host:~$ systemctl status superapp.service
● superapp.service - superapp
   Loaded: loaded (/lib/systemd/system/superapp.service; enabled)
   Active: active (running) since Mon 2018-06-25 08:54:35 UTC; 3 days ago
 Main PID: 2604 (superapp)
   Status: "There is currently 1506 active connections"
   ...

Watchdog

[Service]
Type=notify
WatchdogSec=30s
// Init systemd watchdog, same as the notifier, it can be nil if your os does not support it
watchdog, err := systemd.NewWatchdog()
if err != nil {
    log.Printf("failed to initialize systemd watchdog controller: %v\n", err)
}

if watchdog != nil {
    // Then start a watcher worker
    go func() {
        ticker := watchdog.NewTicker()
        defer ticker.Stop()
        for {
            select {
            // Ticker chan
            case <-ticker.C:
                // Check if something wrong, if not send heartbeat
                if allGood {
                    if err = watchdog.SendHeartbeat(); err != nil {
                        log.Printf("failed to send systemd watchdog heartbeat: %v\n", err)
                    }
                }
            // Some stop signal chan
            case <-stopSig:
                return
            }
        }
    }()
}

Socket activation

See: http://0pointer.de/blog/projects/socket-activation.html for more information.

With the following socket file:

[Socket]
ListenStream=9091
BindIPv6Only=both

[Install]
WantedBy=sockets.target

Retrieve the socket and serve it.

listener, err := activation.Listen(":9091")
if err != nil {
    log.Fatal(err)
}
http.Serve(listener, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello from 9091")
}))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNotifyEnabled

func IsNotifyEnabled() bool

IsNotifyEnabled tells if systemd notify is enabled or not.

func NotifyBusError

func NotifyBusError(buserror string) error

NotifyBusError sends systemd notify BUSERROR=%s{buserror}

func NotifyErrNo

func NotifyErrNo(errno int) error

NotifyErrNo sends systemd notify ERRNO=%d{errno}

func NotifyMainPID

func NotifyMainPID(mainpid int) error

NotifyMainPID sends systemd notify MAINPID=%d{mainpid}

func NotifyReady

func NotifyReady() error

NotifyReady sends systemd notify READY=1

func NotifyReloading

func NotifyReloading() error

NotifyReloading sends systemd notify RELOADING=1

func NotifyStatus

func NotifyStatus(status string) error

NotifyStatus sends systemd notify STATUS=%s{status}

func NotifyStopping

func NotifyStopping() error

NotifyStopping sends systemd notify STOPPING=1

func NotifyWatchDog

func NotifyWatchDog() error

NotifyWatchDog sends systemd notify WATCHDOG=1

func NotifyWatchDogUSec

func NotifyWatchDogUSec(usec int64) error

NotifyWatchDogUSec sends systemd notify WATCHDOG_USEC=%d{µsec}

Types

type WatchDog

type WatchDog struct {
	// contains filtered or unexported fields
}

WatchDog is an interface to the systemd watchdog mechanism

func NewWatchdog

func NewWatchdog() (wd *WatchDog, err error)

NewWatchdog returns an initialized and ready to use WatchDog

func (*WatchDog) GetChecksDuration

func (c *WatchDog) GetChecksDuration() time.Duration

GetChecksDuration returns the ideal time for a client to perform (active or passive collect) checks. Is is equal at 1/3 of watchdogInterval

func (*WatchDog) GetLimitDuration

func (c *WatchDog) GetLimitDuration() time.Duration

GetLimitDuration returns the systemd watchdog limit provided by systemd

func (*WatchDog) NewTicker

func (c *WatchDog) NewTicker() *time.Ticker

NewTicker initializes and returns a ticker set at watchdogChecks (which is set at 1/3 of watchdogInterval). It can be used by clients to trigger checks before using SendHeartbeat().

func (*WatchDog) SendHeartbeat

func (c *WatchDog) SendHeartbeat() error

SendHeartbeat sends a keepalive notification to systemd watchdog

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL