forked from m3philis/prometheus_aws_ipsec_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awscli.go
56 lines (47 loc) · 1.41 KB
/
awscli.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
package main
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/prometheus/client_golang/prometheus"
)
func ipsecMetrics() {
// create a session to AWS with a set region
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String("eu-west-1"),
}))
svc := ec2.New(sess)
// inner function as go function to run endlessly but don't block the exporter itself
go func() {
for {
result, err := svc.DescribeVpnConnections(nil)
if err != nil {
fmt.Println("Error", err)
return
}
for _, connection := range result.VpnConnections {
var name string
for _, tags := range connection.Tags {
if *tags.Key == "Name" {
name = *tags.Value
}
}
// Set state of primary tunnel
if *connection.VgwTelemetry[0].Status == "UP" {
tunnelMetric1.With(prometheus.Labels{"name": name}).Set(1)
} else {
tunnelMetric1.With(prometheus.Labels{"name": name}).Set(0)
}
// Set state of secondary tunnel
if *connection.VgwTelemetry[1].Status == "UP" {
tunnelMetric2.With(prometheus.Labels{"name": name}).Set(1)
} else {
tunnelMetric2.With(prometheus.Labels{"name": name}).Set(0)
}
}
time.Sleep(10 * time.Second)
}
}()
}