forked from tych0/lxd-pkg-ubuntu
/
monitor.go
88 lines (65 loc) · 1.52 KB
/
monitor.go
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
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"github.com/lxc/lxd"
"github.com/lxc/lxd/i18n"
"github.com/lxc/lxd/shared/gnuflag"
)
type monitorCmd struct{}
func (c *monitorCmd) showByDefault() bool {
return false
}
func (c *monitorCmd) usage() string {
return i18n.G(
`Monitor activity on the LXD server.
lxc monitor [remote:] [--type=TYPE...]
Connects to the monitoring interface of the specified LXD server.
By default will listen to all message types.
Specific types to listen to can be specified with --type.
Example:
lxc monitor --type=logging`)
}
type typeList []string
func (f *typeList) String() string {
return fmt.Sprint(*f)
}
func (f *typeList) Set(value string) error {
if value == "" {
return fmt.Errorf("Invalid type: %s", value)
}
if f == nil {
*f = make(typeList, 1)
} else {
*f = append(*f, value)
}
return nil
}
var typeArgs typeList
func (c *monitorCmd) flags() {
gnuflag.Var(&typeArgs, "type", i18n.G("Event type to listen for"))
}
func (c *monitorCmd) run(config *lxd.Config, args []string) error {
var remote string
if len(args) > 1 {
return errArgs
}
if len(args) == 0 {
remote, _ = config.ParseRemoteAndContainer("")
} else {
remote, _ = config.ParseRemoteAndContainer(args[0])
}
d, err := lxd.NewClient(config, remote)
if err != nil {
return err
}
handler := func(message interface{}) {
render, err := yaml.Marshal(&message)
if err != nil {
fmt.Printf("error: %s\n", err)
return
}
fmt.Printf("%s\n\n", render)
}
return d.Monitor(typeArgs, handler)
}