-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (155 loc) · 6.89 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"time"
"github.com/gwos/tcg/connectors"
"github.com/gwos/tcg/milliseconds"
"github.com/gwos/tcg/transit"
)
//////////////////////////////////////////////////////////
// Examples of High Level API functions //
//////////////////////////////////////////////////////////
var enableTransit = false
const (
Resource1 = "tcg-host-1"
Resource2 = "tcg-host-1"
Service1 = "tcg-server-1"
Service2 = "tcg-server-2"
CpuMetric = "tcg-cpu"
CpuMetricWarning = "tcg-cpu-warning"
CpuMetricCritical = "tcg-cpu-critical"
PercentFreeMetric = "tcg-percent-free"
PercentFreeMetricWarning = "tcg-percent-free-warning"
PercentFreeMetricCritical = "tcg-percent-free-critical"
DiskUsed = "tcg-memory-used"
)
func main() {
//////////////////////////////////////////////////////////////////////////////////////////
// Inventory Examples //
//////////////////////////////////////////////////////////////////////////////////////////
var iServices []transit.DynamicInventoryService
is1 := connectors.CreateInventoryService(Service1, Resource1)
is2 := connectors.CreateInventoryService(Service2, Resource1)
iServices = append(iServices, is1, is2)
iResource1 := connectors.CreateInventoryResource(Resource1, iServices)
println(iResource1.Services[0].Description)
println(iResource1.Description)
//if (enableTransit) {
// connectors.SendInventory()
//}
//////////////////////////////////////////////////////////////////////////////////////////
// Metrics Examples //
//////////////////////////////////////////////////////////////////////////////////////////
// Create Integer Metric with Thresholds
metric1, _ := connectors.CreateMetric(CpuMetric, 75)
warning1, _ := connectors.CreateWarningThreshold(CpuMetricWarning, 60)
critical1, _ := connectors.CreateCriticalThreshold(CpuMetricCritical, 90)
metric1.Thresholds = &[]transit.ThresholdValue{*warning1, *critical1}
// Create Double Metric with Thresholds and Unit Type
metric2, _ := connectors.CreateMetric(PercentFreeMetric, 99.82, transit.PercentCPU)
warning2, _ := connectors.CreateWarningThreshold(PercentFreeMetricWarning, 80.5)
critical2, _ := connectors.CreateCriticalThreshold(PercentFreeMetricCritical, 90.8)
metric2.Thresholds = &[]transit.ThresholdValue{*warning2, *critical2}
// create with interval
now := time.Now()
interval := &transit.TimeInterval{
EndTime: milliseconds.MillisecondTimestamp{Time: now},
StartTime: milliseconds.MillisecondTimestamp{Time: now},
}
metric3, _ := connectors.CreateMetric(DiskUsed, 65.82, transit.GB, interval)
// add tags
metric3.CreateTag("myTag1", "myTagValue1")
metric3.CreateTag("myTag2", "myTagValue2")
// display ...
fmt.Printf("metric 1 created with thresholds: %+v\n", metric1)
fmt.Printf("metric 2 created with thresholds: %+v\n", metric2)
fmt.Printf("metric 3 created with thresholds: %+v\n", metric3)
// Create a Service and add metrics ...
service1, _ := connectors.CreateService(Service1, Resource1, []transit.TimeSeries{*metric1, *metric2, *metric3})
fmt.Printf("service 1 created with metrics: %+v\n", service1)
service2, _ := connectors.CreateService(Service2, Resource2, []transit.TimeSeries{*metric1})
fmt.Printf("service 1 created with metrics: %+v\n", service2)
// Create a Monitored Resource and Add Service
resource1, _ := connectors.CreateResource(Resource1, []transit.DynamicMonitoredService{*service1})
fmt.Printf("resource 1 created with services: %+v\n", resource1)
resource2, _ := connectors.CreateResource(Resource2, []transit.DynamicMonitoredService{*service2})
fmt.Printf("resource 2 created with services: %+v\n", resource2)
if enableTransit {
connectors.SendMetrics(context.Background(), []transit.DynamicMonitoredResource{*resource1, *resource2}, nil)
}
}
//////////////////////////////////////////////////////////
// Examples of Low Level API functions //
//////////////////////////////////////////////////////////
func LowLevelExamples() {
warningThreshold := transit.ThresholdValue{
SampleType: transit.Warning,
Label: "local_load_5_wn",
Value: &transit.TypedValue{ValueType: transit.DoubleType, DoubleValue: 70.0}}
errorThreshold := transit.ThresholdValue{
SampleType: transit.Critical,
Label: "local_load_5_cr",
Value: &transit.TypedValue{ValueType: transit.DoubleType, DoubleValue: 85.0}}
random := rand.Float64() * 100.0
now := milliseconds.MillisecondTimestamp{Time: time.Now()}
sampleValue := transit.TimeSeries{
MetricName: "local_load_5",
SampleType: transit.Value,
Interval: &transit.TimeInterval{EndTime: now, StartTime: now},
Value: &transit.TypedValue{ValueType: transit.DoubleType, DoubleValue: random},
Thresholds: &[]transit.ThresholdValue{warningThreshold, errorThreshold},
Unit: "%{cpu}",
}
// Example Service
var localLoadService = transit.DynamicMonitoredService{
BaseTransitData: transit.BaseTransitData{
Name: "local_load",
Type: transit.Service,
Properties: map[string]transit.TypedValue{
"stateType": {StringValue: "SOFT"},
"checkType": {StringValue: "ACTIVE"},
"PerformanceData": {StringValue: "007-321 RAD"},
"ExecutionTime": {DoubleValue: 3.0},
"CurrentAttempt": {IntegerValue: 2},
"InceptionTime": {TimeValue: &milliseconds.MillisecondTimestamp{Time: time.Now()}},
},
},
Status: transit.ServiceOk,
LastCheckTime: milliseconds.MillisecondTimestamp{Time: time.Now()},
NextCheckTime: milliseconds.MillisecondTimestamp{Time: time.Now().Add(time.Minute * 5)},
LastPlugInOutput: "foo | bar",
Metrics: []transit.TimeSeries{sampleValue},
}
geneva := transit.DynamicMonitoredResource{
BaseResource: transit.BaseResource{
BaseTransitData: transit.BaseTransitData{
Name: "geneva",
Type: transit.Host,
Properties: map[string]transit.TypedValue{
"stateType": {StringValue: "SOFT"},
"checkType": {StringValue: "ACTIVE"},
"PerformanceData": {StringValue: "007-321 RAD"},
"ExecutionTime": {DoubleValue: 3.0},
"CurrentAttempt": {IntegerValue: 2},
"InceptionTime": {TimeValue: &milliseconds.MillisecondTimestamp{Time: time.Now()}},
},
},
},
Status: transit.HostUp,
LastCheckTime: milliseconds.MillisecondTimestamp{Time: time.Now()},
NextCheckTime: milliseconds.MillisecondTimestamp{Time: time.Now().Add(time.Minute * 5)},
LastPlugInOutput: "44/55/888 QA00005-BC",
Services: []transit.DynamicMonitoredService{localLoadService},
}
// Build Monitored Resources
resources := []transit.DynamicMonitoredResource{geneva}
// TODO: call into API
b, err := json.Marshal(resources)
if err == nil {
s := string(b)
println(s)
}
}