forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
105 lines (81 loc) · 1.73 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"github.com/lxc/lxd/lxc/config"
"github.com/lxc/lxd/shared/gnuflag"
"github.com/lxc/lxd/shared/i18n"
)
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
}
type monitorCmd struct {
typeArgs typeList
}
func (c *monitorCmd) showByDefault() bool {
return false
}
func (c *monitorCmd) usage() string {
return i18n.G(
`Usage: lxc monitor [<remote>:] [--type=TYPE...]
Monitor a local or remote LXD server.
By default the monitor will listen to all message types.
Message types to listen for can be specified with --type.
*Examples*
lxc monitor --type=logging
Only show log message.`)
}
func (c *monitorCmd) flags() {
gnuflag.Var(&c.typeArgs, "type", i18n.G("Event type to listen for"))
}
func (c *monitorCmd) run(conf *config.Config, args []string) error {
var err error
var remote string
if len(args) > 1 {
return errArgs
}
if len(args) == 0 {
remote, _, err = conf.ParseRemote("")
if err != nil {
return err
}
} else {
remote, _, err = conf.ParseRemote(args[0])
if err != nil {
return err
}
}
d, err := conf.GetContainerServer(remote)
if err != nil {
return err
}
listener, err := d.GetEvents()
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)
}
_, err = listener.AddHandler(c.typeArgs, handler)
if err != nil {
return err
}
return listener.Wait()
}