-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy pathmetrics.go
128 lines (105 loc) · 2.69 KB
/
metrics.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package vql
import (
"context"
"fmt"
"sync"
"time"
"github.com/Velocidex/ordereddict"
"www.velocidex.com/golang/velociraptor/services/debug"
"www.velocidex.com/golang/velociraptor/utils"
"www.velocidex.com/golang/vfilter"
"www.velocidex.com/golang/vfilter/types"
)
var (
pluginMonitor PluginMonitor
)
type PluginMonitorEntry struct {
Name string
ArgsFunc func() *ordereddict.Dict
Start time.Time
id uint64
}
type PluginMonitor struct {
mu sync.Mutex
entries map[uint64]*PluginMonitorEntry
}
func (self *PluginMonitor) report(
ctx context.Context, scope vfilter.Scope,
output_chan chan vfilter.Row) {
self.mu.Lock()
defer self.mu.Unlock()
for _, item := range self.entries {
select {
case <-ctx.Done():
return
case output_chan <- ordereddict.NewDict().
Set("Started", item.Start).
Set("Plugin", item.Name).
Set("Args", item.ArgsFunc()).
Set("Duration", utils.GetTime().Now().Sub(item.Start).String()):
}
}
}
func (self *PluginMonitor) Register(name string, args *ordereddict.Dict) func() {
id := utils.GetId()
self.mu.Lock()
defer self.mu.Unlock()
self.entries[id] = &PluginMonitorEntry{
Name: name,
ArgsFunc: renderArgs(args),
Start: utils.GetTime().Now(),
}
return func() {
self.mu.Lock()
defer self.mu.Unlock()
delete(self.entries, id)
}
}
func renderValue(v interface{}) interface{} {
// Format the args in a safe way to ensure they do not get expanded.
switch t := v.(type) {
case string, uint64, uint32, uint16, uint8,
int64, int32, int16, int8, float64,
vfilter.Null, *vfilter.Null, bool:
return t
case *ordereddict.Dict:
return renderArgs(t)()
case types.StringProtocol:
scope := MakeScope()
return t.ToString(scope)
case types.StoredQuery, types.LazyExpr:
scope := MakeScope()
return vfilter.FormatToString(scope, t)
default:
// Second check based on string type. We can not
// reference the type directly due to import
// restrictions.
type_str := fmt.Sprintf("%T", v)
switch type_str {
case "*accessors.OSPath":
return v
}
}
return fmt.Sprintf("Variable of type %T", v)
}
func renderArgs(args *ordereddict.Dict) func() *ordereddict.Dict {
return func() *ordereddict.Dict {
result := ordereddict.NewDict()
for _, k := range args.Keys() {
v, _ := args.Get(k)
result.Set(k, renderValue(v))
}
return result
}
}
func RegisterMonitor(name string, args *ordereddict.Dict) func() {
return pluginMonitor.Register(name, args)
}
func init() {
pluginMonitor.entries = make(map[uint64]*PluginMonitorEntry)
debug.RegisterProfileWriter(debug.ProfileWriterInfo{
Name: "Plugin Monitor",
Description: "See currently running VQL plugins",
ProfileWriter: pluginMonitor.report,
})
}