-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
195 lines (171 loc) · 4.94 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/xh4n3/ucloud-sdk-go/service/umon"
"github.com/xh4n3/ucloud-sdk-go/service/unet"
"github.com/xh4n3/ucloud-sdk-go/ucloud"
"github.com/xh4n3/ucloud-sdk-go/ucloud/auth"
"github.com/xh4n3/unet_exporter/sdk"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
var (
ResourceBandwidthUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "resource_bandwidth_usage",
Help: "Bandwidth usage per resource",
},
[]string{"shareBandwidth", "resource"},
)
TotalBandwidthUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "total_bandwidth_usage",
Help: "Bandwidth usage in total",
},
[]string{"shareBandwidth"},
)
CurrentBandwidth = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "current_bandwidth",
Help: "Current bandwidth",
},
[]string{"shareBandwidth"},
)
BandwidthMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bandwidth_metric_up",
Help: "Bandwidth metric up",
},
[]string{"shareBandwidth"},
)
CanSetBandwidth = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "can_set_bandwidth",
Help: "ucloud api returns good when setting new bandwidth",
},
[]string{"shareBandwidth"},
)
config *sdk.Config
uNet *unet.UNet
uMon *umon.UMon
)
func init() {
prometheus.MustRegister(ResourceBandwidthUsage)
prometheus.MustRegister(TotalBandwidthUsage)
prometheus.MustRegister(CurrentBandwidth)
prometheus.MustRegister(BandwidthMetric)
prometheus.MustRegister(CanSetBandwidth)
}
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
configContent, err := ioutil.ReadFile("config.yml")
if err != nil {
log.Fatalf("Config file not found: %v", err)
}
config = &sdk.Config{}
err = yaml.Unmarshal(configContent, config)
if err != nil {
log.Fatalf("cannot unmarshal config file: %v", err)
}
uNet, uMon := envClient(config)
shareBandwidth := config.Targets[0]
collector := sdk.NewCollector(uNet, uMon, shareBandwidth)
collector.ListEIPs()
resizer := sdk.NewResizer(uNet, shareBandwidth, config)
log.Println("Collect looping started.")
go func() {
for {
resourceBandwidthMap := collector.ListBandwidthUsages()
currentBandwidth, err := collector.GetCurrentBandwidth()
bandwidthTotalUsed := collector.GetTotalBandwidth()
if err != nil {
log.Printf("getting bandwidth usage on ucloud error: %v\n", err.Error())
BandwidthMetric.WithLabelValues(shareBandwidth.Name).Set(float64(0))
} else {
BandwidthMetric.WithLabelValues(shareBandwidth.Name).Set(float64(1))
CurrentBandwidth.WithLabelValues(shareBandwidth.Name).Set(float64(currentBandwidth))
}
for resourceName, usage := range resourceBandwidthMap {
ResourceBandwidthUsage.WithLabelValues(shareBandwidth.Name, resourceName).Set(float64(usage))
}
TotalBandwidthUsage.WithLabelValues(shareBandwidth.Name).Set(float64(bandwidthTotalUsed))
err = resizer.SetToAdvisedBandwidth()
if err != nil {
log.Printf("setting advised bandwidth on ucloud error: %v\n", err.Error())
CanSetBandwidth.WithLabelValues(shareBandwidth.Name).Set(float64(0))
} else {
CanSetBandwidth.WithLabelValues(shareBandwidth.Name).Set(float64(1))
}
time.Sleep(time.Duration(time.Duration(config.Global.Interval) * time.Second))
}
}()
// Expose the registered metrics via HTTP.
http.Handle("/metrics", promhttp.Handler())
go func() {
err := http.ListenAndServe(config.Global.MertricPort, nil)
if err != nil {
switchToDefault()
log.Fatal(err)
}
}()
//catch exit signals
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
log.Println(<-ch)
switchToDefault()
}
//switch to default bandwidth before exit
func switchToDefault() {
log.Println("switching to defaultBandwidth before exits")
for _, target := range config.Targets {
resizer := sdk.NewResizer(uNet, target, config)
err := resizer.SetCurrentBandwidth(target.DefaultBandwidth)
if err != nil {
log.Printf("switching err: %v", err)
}
}
log.Println("Swiching to default finished, exiting.")
}
func envClient(config *sdk.Config) (*unet.UNet, *umon.UMon) {
var publicKey, privateKey string
for _, env := range os.Environ() {
pair := strings.Split(env, "=")
if pair[0] == "PUBLIC_KEY" {
publicKey = pair[1]
continue
}
if pair[0] == "PRIVATE_KEY" {
privateKey = pair[1]
continue
}
}
if publicKey == "" {
log.Println("Loading PUBLIC_KEY from config file")
publicKey = config.Global.PublicKey
}
if privateKey == "" {
log.Println("Loading PRIVATE_KEY from config file")
privateKey = config.Global.PrivateKey
}
uNet = unet.New(&ucloud.Config{
Credentials: &auth.KeyPair{
PublicKey: publicKey,
PrivateKey: privateKey,
},
})
uMon = umon.New(&ucloud.Config{
Credentials: &auth.KeyPair{
PublicKey: publicKey,
PrivateKey: privateKey,
},
})
return uNet, uMon
}