|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/aws/aws-sdk-go/aws" |
| 9 | + "github.com/aws/aws-sdk-go/aws/session" |
| 10 | + "github.com/aws/aws-sdk-go/service/ec2" |
| 11 | + "github.com/aws/aws-sdk-go/service/servicequotas" |
| 12 | + "github.com/go-kit/kit/log" |
| 13 | + "github.com/go-kit/kit/log/level" |
| 14 | + "github.com/prometheus/client_golang/prometheus" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + transitGatewayPerAccountQuotaCode string = "L-A2478D36" |
| 19 | + ec2ServiceCode string = "ec2" |
| 20 | +) |
| 21 | + |
| 22 | +var TransitGatewaysQuota *prometheus.Desc = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "ec2_transitgatewaysperregion_quota"), "Quota for maximum number of Transitgateways in this account", []string{"aws_region"}, nil) |
| 23 | +var TransitGatewaysUsage *prometheus.Desc = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "ec2_transitgatewaysperregion_usage"), "Number of Tranitgatewyas in the AWS Account", []string{"aws_region"}, nil) |
| 24 | + |
| 25 | +type EC2Exporter struct { |
| 26 | + sessions []*session.Session |
| 27 | + |
| 28 | + logger log.Logger |
| 29 | + timeout time.Duration |
| 30 | +} |
| 31 | + |
| 32 | +func NewEC2Exporter(sessions []*session.Session, logger log.Logger, timeout time.Duration) *EC2Exporter { |
| 33 | + |
| 34 | + level.Info(logger).Log("msg", "Initializing EC2 exporter") |
| 35 | + return &EC2Exporter{ |
| 36 | + sessions: sessions, |
| 37 | + |
| 38 | + logger: logger, |
| 39 | + timeout: timeout, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (e *EC2Exporter) Collect(ch chan<- prometheus.Metric) { |
| 44 | + ctx, ctxCancel := context.WithTimeout(context.Background(), e.timeout) |
| 45 | + defer ctxCancel() |
| 46 | + wg := &sync.WaitGroup{} |
| 47 | + wg.Add(len(e.sessions)) |
| 48 | + |
| 49 | + for _, sess := range e.sessions { |
| 50 | + go collectInRegion(sess, e.logger, wg, ch, ctx) |
| 51 | + } |
| 52 | + wg.Wait() |
| 53 | +} |
| 54 | + |
| 55 | +func collectInRegion(sess *session.Session, logger log.Logger, wg *sync.WaitGroup, ch chan<- prometheus.Metric, ctx context.Context) { |
| 56 | + defer wg.Done() |
| 57 | + ec2Svc := ec2.New(sess) |
| 58 | + serviceQuotaSvc := servicequotas.New(sess) |
| 59 | + |
| 60 | + quota, err := getQuotaValueWithContext(serviceQuotaSvc, ec2ServiceCode, transitGatewayPerAccountQuotaCode, ctx) |
| 61 | + if err != nil { |
| 62 | + level.Error(logger).Log("msg", "Could not retrieve Transit Gateway quota", "error", err.Error()) |
| 63 | + exporterMetrics.IncrementErrors() |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + gateways, err := getAllTransitGatewaysWithContext(ec2Svc, ctx) |
| 68 | + if err != nil { |
| 69 | + level.Error(logger).Log("msg", "Could not retrieve Transit Gateway quota", "error", err.Error()) |
| 70 | + exporterMetrics.IncrementErrors() |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + ch <- prometheus.MustNewConstMetric(TransitGatewaysUsage, prometheus.GaugeValue, float64(len(gateways)), *sess.Config.Region) |
| 75 | + ch <- prometheus.MustNewConstMetric(TransitGatewaysQuota, prometheus.GaugeValue, quota, *sess.Config.Region) |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +func (e *EC2Exporter) Describe(ch chan<- *prometheus.Desc) { |
| 80 | + ch <- TransitGatewaysQuota |
| 81 | + ch <- TransitGatewaysUsage |
| 82 | +} |
| 83 | + |
| 84 | +func getAllTransitGatewaysWithContext(client *ec2.EC2, ctx context.Context) ([]*ec2.TransitGateway, error) { |
| 85 | + results := []*ec2.TransitGateway{} |
| 86 | + describeGatewaysInput := &ec2.DescribeTransitGatewaysInput{ |
| 87 | + DryRun: aws.Bool(false), |
| 88 | + MaxResults: aws.Int64(1000), |
| 89 | + } |
| 90 | + |
| 91 | + describeGatewaysOutput, err := client.DescribeTransitGatewaysWithContext(ctx, describeGatewaysInput) |
| 92 | + |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + results = append(results, describeGatewaysOutput.TransitGateways...) |
| 97 | + |
| 98 | + for describeGatewaysOutput.NextToken != nil { |
| 99 | + describeGatewaysInput.SetNextToken(*describeGatewaysOutput.NextToken) |
| 100 | + describeGatewaysOutput, err := client.DescribeTransitGatewaysWithContext(ctx, describeGatewaysInput) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + results = append(results, describeGatewaysOutput.TransitGateways...) |
| 105 | + } |
| 106 | + |
| 107 | + return results, nil |
| 108 | +} |
| 109 | + |
| 110 | +func getQuotaValueWithContext(client *servicequotas.ServiceQuotas, serviceCode string, quotaCode string, ctx context.Context) (float64, error) { |
| 111 | + sqOutput, err := client.GetServiceQuotaWithContext(ctx, &servicequotas.GetServiceQuotaInput{ |
| 112 | + QuotaCode: aws.String(quotaCode), |
| 113 | + ServiceCode: aws.String(serviceCode), |
| 114 | + }) |
| 115 | + |
| 116 | + if err != nil { |
| 117 | + return 0, err |
| 118 | + } |
| 119 | + |
| 120 | + return *sqOutput.Quota.Value, nil |
| 121 | +} |
0 commit comments