File: README.md

package info (click to toggle)
golang-github-iguanesolutions-go-systemd 5.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 168 kB
  • sloc: makefile: 2
file content (299 lines) | stat: -rw-r--r-- 7,412 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# go-systemd

[![Go Report Card](https://goreportcard.com/badge/github.com/iguanesolutions/go-systemd)](https://goreportcard.com/report/github.com/iguanesolutions/go-systemd) [![PkgGoDev](https://pkg.go.dev/badge/github.com/iguanesolutions/go-systemd/v5)](https://pkg.go.dev/github.com/iguanesolutions/go-systemd/v5)

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

## Notifier

[![PkgGoDev](https://pkg.go.dev/badge/github.com/iguanesolutions/go-systemd/v5/notify)](https://pkg.go.dev/github.com/iguanesolutions/go-systemd/v5/notify)

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 (noop call).

```systemdunit
[Service]
Type=notify
```

```go
import (
    sysdnotify "github.com/iguanesolutions/go-systemd/v5/notify"
)

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

/*
    Do some more inits
*/

// Notify ready to systemd
if err = sysdnotify.Ready(); 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

```go
import (
    sysdnotify "github.com/iguanesolutions/go-systemd/v5/notify"
)

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

/*
    Stop others 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

```go
import (
    sysdnotify "github.com/iguanesolutions/go-systemd/v5/notify"
)

if err := sysdnotify.Status(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:

```systemctlstatus
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

[![PkgGoDev](https://pkg.go.dev/badge/github.com/iguanesolutions/go-systemd/v5/notify/watchdog)](https://pkg.go.dev/github.com/iguanesolutions/go-systemd/v5/notify/watchdog)

```systemdunit
[Service]
Type=notify
WatchdogSec=30s
```

```go
import (
    sysdwatchdog "github.com/iguanesolutions/go-systemd/v5/notify/watchdog"
)

// Init systemd watchdog, same as the notifier, it can be nil if your os does not support it
watchdog, err := sysdwatchdog.New()
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
            }
        }
    }()
}
```

## Resolved

[![PkgGoDev](https://pkg.go.dev/badge/github.com/iguanesolutions/go-systemd/resolved/resolved)](https://pkg.go.dev/github.com/iguanesolutions/go-systemd/v5/resolved)

This package is still under development and very experimental, do not use it in production.
We started this package in order to go deep into the DNS world. So we are opened to any suggestions/contributions on this.
DNS is not trivial at all so there can be some stuff that are not rfc compliant (like sorting addresses etc...).

The resolved package features:
 * Pure Go implementation of `org.freedesktop.resolve1` dbus interface
 * Resolver type (which uses the underlying dbus interface) that tries to implement the same methods as `net.Resolver` from Go standard library
 * Unit tests (make sure Go resolver and systemd-resolved query the same dns server)

### Dbus

The following example shows how to use the resolve1 dbus connection to resolve an host:

```go
package main

import (
	"context"
	"fmt"
	"log"
	"syscall"

	"github.com/iguanesolutions/go-systemd/v5/resolved"
)

func main() {
	c, err := resolved.NewConn()
	if err != nil {
		log.Fatal("ERROR: ", err)
	}
	ctx := context.Background()
	addrs, canonical, flags, err := c.ResolveHostname(ctx, 0, "google.com", syscall.AF_UNSPEC, 0)
	if err != nil {
		log.Println("ERROR: ", err)
	} else {
		fmt.Println("Addresses: ", addrs)
		fmt.Println("Canonical: ", canonical)
		fmt.Println("OutputFlags: ", flags)
	}
	err = c.Close()
	if err != nil {
		log.Println("ERROR: ", err)
	}
}
```

Output:

```output
Addresses:  [{
        IfIndex: 2,
        Family:  2,
        IP:      142.250.74.238,
} {
        IfIndex: 2,
        Family:  10,
        IP:      2a00:1450:4007:80b::200e,
}]
Canonical:  google.com
Flags:  1
```

### Resolver

The following example shows how to use the resolved Resolver to resolve an host:

```go
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/iguanesolutions/go-systemd/v5/resolved"
)

func main() {
	r, err := resolved.NewResolver()
	if err != nil {
		log.Fatal("ERROR: ", err)
	}
	ctx := context.Background()
	addrs, err := r.LookupHost(ctx, "google.com")
	if err != nil {
		log.Println("ERROR: ", err)
	} else {
		fmt.Println("Addresses: ", addrs)
	}
	err = r.Close()
	if err != nil {
		log.Println("ERROR: ", err)
	}
}
```

Output:

```output
Addresses:  [2a00:1450:4007:80b::200e 142.250.74.238]
```

### HTTP Client

The following example shows how to use the systemd-resolved Resolver with the Go http client from the standard library:

```go
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/iguanesolutions/go-systemd/v5/resolved"
)

func main() {
	r, err := resolved.NewResolver()
	if err != nil {
		log.Fatal("ERROR: ", err)
	}
	// if you want to make a custom http client using systemd-resolved as resolver
	httpCli := &http.Client{
		Transport: &http.Transport{
			DialContext: r.DialContext,
		},
	}
	// or if you don't have an http client you can call HTTPClient method on resolver
	// it comes with some nice default values.
	httpCli = r.HTTPClient()
	resp, err := httpCli.Get("https://google.com")
	if err != nil {
		log.Println("ERROR: ", err)
	} else {
		fmt.Println("Status: ", resp.Status)
		err = resp.Body.Close()
		if err != nil {
			log.Println("ERROR: ", err)
		}
	}
	err = r.Close()
	if err != nil {
		log.Println("ERROR: ", err)
	}
}
```

Output:

```output
Status:  200 OK
```