-
Notifications
You must be signed in to change notification settings - Fork 2
/
kullect.go
130 lines (116 loc) · 3.45 KB
/
kullect.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
129
130
package main
import (
"errors"
"github.com/influxdata/kapacitor/udf"
"github.com/influxdata/kapacitor/udf/agent"
"log"
"os"
_ "strconv"
)
// An Agent.Handler
type costHandler struct {
hourly_cost int
total_cpu int
total_memory int
agent *agent.Agent
}
func newCostHandler(a *agent.Agent) *costHandler {
return &costHandler{agent: a}
}
// Return the InfoResponse. Describing the properties of this UDF agent.
func (a *costHandler) Info() (*udf.InfoResponse, error) {
info := &udf.InfoResponse{
Wants: udf.EdgeType_STREAM,
Provides: udf.EdgeType_STREAM,
Options: map[string]*udf.OptionInfo{
"total_cpu": {ValueTypes: []udf.ValueType{udf.ValueType_INT}},
"total_memory": {ValueTypes: []udf.ValueType{udf.ValueType_INT}},
"hourly_cost": {ValueTypes: []udf.ValueType{udf.ValueType_DOUBLE}},
},
}
return info, nil
}
// Initialze the handler based of the provided options.
func (a *costHandler) Init(r *udf.InitRequest) (*udf.InitResponse, error) {
init := &udf.InitResponse{
Success: true,
Error: "",
}
for _, opt := range r.Options {
switch opt.Name {
case "hourly_cost":
a.hourly_cost = int(opt.Values[0].Value.(*udf.OptionValue_DoubleValue).DoubleValue)
case "total_cpu":
a.total_cpu = int(opt.Values[0].Value.(*udf.OptionValue_IntValue).IntValue)
case "total_memory":
a.total_memory = int(opt.Values[0].Value.(*udf.OptionValue_IntValue).IntValue)
}
}
if a.hourly_cost == 0.0 {
init.Success = false
init.Error += " must supply the hourly cost of your entire kubernetes cluster"
}
if a.total_cpu == 0 {
init.Success = false
init.Error += " must supply the total available millicores in your cluster"
}
if a.total_memory == 0 {
init.Success = false
init.Error += " must supply the total available memory in megabytes from your cluster"
}
return init, nil
}
// Create a snapshot of the running state of the process.
func (a *costHandler) Snaphost() (*udf.SnapshotResponse, error) {
return &udf.SnapshotResponse{}, nil
}
// Restore a previous snapshot.
func (a *costHandler) Restore(req *udf.RestoreRequest) (*udf.RestoreResponse, error) {
return &udf.RestoreResponse{
Success: true,
}, nil
}
// This handler does not do batching
func (a *costHandler) BeginBatch(*udf.BeginBatch) error {
return errors.New("batching not supported")
}
// Compute Cost
func (a *costHandler) Point(p *udf.Point) error {
// Re-use the existing point so we keep the same tags etc.
cpu_usage := float64(p.FieldsInt["cpu.value"])
memory_usage := float64(p.FieldsInt["memory.value"])
uptime := float64(p.FieldsInt["uptime.value"])
hourly_uptime := uptime / 36000000
uptime_cost := hourly_uptime * float64(a.hourly_cost)
cpu_cost := uptime_cost * (cpu_usage / float64(a.total_cpu))
memory_cost := uptime_cost * (memory_usage / float64(a.total_memory))
p.FieldsDouble = map[string]float64{"cpu_cost": cpu_cost, "memory_cost": memory_cost}
p.FieldsInt = nil
p.FieldsString = nil
// Send point with average value.
a.agent.Responses <- &udf.Response{
Message: &udf.Response_Point{
Point: p,
},
}
return nil
}
// This handler does not do batching
func (a *costHandler) EndBatch(*udf.EndBatch) error {
return errors.New("batching not supported")
}
// Stop the handler gracefully.
func (a *costHandler) Stop() {
close(a.agent.Responses)
}
func main() {
a := agent.New(os.Stdin, os.Stdout)
h := newCostHandler(a)
a.Handler = h
log.Println("Starting agent")
a.Start()
err := a.Wait()
if err != nil {
log.Fatal(err)
}
}