-
Notifications
You must be signed in to change notification settings - Fork 1
/
awscli.go
76 lines (66 loc) · 2.12 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"time"
"strconv"
"strings"
"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/aws/aws-sdk-go/service/cloudformation"
"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"),
}))
// create client object to access cloudformation
svcCfn := cloudformation.New(sess)
stacks, err := svcCfn.DescribeStacks(nil)
if err != nil {
fmt.Println(err.Error())
}
// loop over all stacks to find the base-region stack and get the AccountName
var accountName string
for _, stack := range stacks.Stacks {
if strings.HasPrefix(*stack.StackName, "base-region") {
for _, tag := range stack.Parameters {
if *tag.ParameterKey == "AccountName" {
accountName = *tag.ParameterKey
}
}
}
}
svcEc2 := ec2.New(sess)
// inner function as go function to run endlessly but don't block the exporter itself
go func() {
for {
result, err := svcEc2.DescribeVpnConnections(nil)
if err != nil {
fmt.Println(err.Error())
return
}
// loop over all tunnel to get metrics
for _, connection := range result.VpnConnections {
// loop over tags to find the name of the tunnel
var name string
for _, tag := range connection.Tags {
if *tag.Key == "Name" {
name = *tag.Value
}
}
// each tunnel is really two tunnels so we need to check if both tunnels are working
for id, tunnel := range connection.VgwTelemetry {
if *tunnel.Status == "UP" {
tunnelMetric.With(prometheus.Labels{"name": name, "id": strconv.Itoa(id+1), "account": accountName}).Set(1)
} else {
tunnelMetric.With(prometheus.Labels{"name": name, "id": strconv.Itoa(id+1), "account": accountName}).Set(0)
}
}
}
// sleep for 10s and restart the loop
time.Sleep(10 * time.Second)
}
}()
}