forked from signalfx/signalfx-agent
/
processlist.go
117 lines (101 loc) · 2.96 KB
/
processlist.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
package processlist
import (
"bytes"
"compress/zlib"
"context"
"encoding/base64"
"fmt"
"math"
"runtime"
"time"
"github.com/signalfx/golib/event"
"github.com/signalfx/signalfx-agent/internal/core/config"
"github.com/signalfx/signalfx-agent/internal/monitors"
"github.com/signalfx/signalfx-agent/internal/monitors/types"
"github.com/signalfx/signalfx-agent/internal/utils"
log "github.com/sirupsen/logrus"
)
const (
monitorType = "processlist"
version = "0.0.30"
)
// MONITOR(processlist): (Windows Only) This monitor reports processlist
// information for Windows Hosts.
//
// Sample YAML configuration:
//
// ```yaml
// monitors:
// - type: processlist
// ```
// EVENT(objects.top-info): Process list event.
var logger = log.WithFields(log.Fields{"monitorType": monitorType})
var zlibCompressor = zlib.NewWriter(&bytes.Buffer{})
// Config for this monitor
type Config struct {
config.MonitorConfig `singleInstance:"true" acceptsEndpoints:"false"`
}
func init() {
monitors.Register(monitorType, func() interface{} { return &Monitor{} }, &Config{})
}
// compresses the given byte array
func compressBytes(in []byte) (buf bytes.Buffer, err error) {
zlibCompressor.Reset(&buf)
_, err = zlibCompressor.Write(in)
_ = zlibCompressor.Close()
return
}
// toTime returns the given seconds as a formatted string "min:sec.dec"
func toTime(secs float64) string {
minutes := int(secs) / 60
seconds := math.Mod(secs, 60.0)
dec := math.Mod(seconds, 1.0) * 100
return fmt.Sprintf("%02d:%02.f.%02.f", minutes, seconds, dec)
}
// Monitor for Utilization
type Monitor struct {
Output types.Output
cancel func()
}
// Configure configures the monitor and starts collecting on the configured interval
func (m *Monitor) Configure(conf *Config) error {
if runtime.GOOS != "windows" {
return fmt.Errorf("currently this monitor only supports windows")
}
// create contexts for managing the the plugin loop
var ctx context.Context
ctx, m.cancel = context.WithCancel(context.Background())
utils.RunOnInterval(
ctx,
func() {
// get the process list
processList, err := ProcessList()
if err != nil {
logger.WithError(err).Error("Couldn't get process list")
return
}
// escape and compress the process list
escapedBytes := bytes.Replace(processList.Bytes(), []byte{byte('\\')}, []byte{byte('\\'), byte('\\')}, -1)
compressedBytes, err := compressBytes(escapedBytes)
if err != nil {
logger.WithError(err).Error("Couldn't compress process list")
return
}
// format and emit the top-info event
message := fmt.Sprintf("{\"t\":\"%s\",\"v\":\"%s\"}", base64.StdEncoding.EncodeToString(compressedBytes.Bytes()), version)
m.Output.SendEvent(
&event.Event{
EventType: "objects.top-info",
Category: event.AGENT,
Dimensions: map[string]string{},
Properties: map[string]interface{}{
"message": message,
},
Timestamp: time.Now(),
},
)
},
time.Duration(conf.IntervalSeconds)*time.Second,
)
return nil
}