forked from webdevops/azure-resourcemanager-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_azurerm_security.go
134 lines (112 loc) · 4.2 KB
/
metrics_azurerm_security.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
package main
import (
"context"
"fmt"
"github.com/Azure/azure-sdk-for-go/profiles/latest/advisor/mgmt/advisor"
"github.com/Azure/azure-sdk-for-go/profiles/latest/resources/mgmt/subscriptions"
"github.com/Azure/azure-sdk-for-go/profiles/preview/preview/security/mgmt/security"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
prometheusCommon "github.com/webdevops/go-prometheus-common"
"time"
)
type MetricsCollectorAzureRmSecurity struct {
CollectorProcessorGeneral
prometheus struct {
securitycenterCompliance *prometheus.GaugeVec
advisorRecommendations *prometheus.GaugeVec
}
}
func (m *MetricsCollectorAzureRmSecurity) Setup(collector *CollectorGeneral) {
m.CollectorReference = collector
m.prometheus.securitycenterCompliance = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azurerm_securitycenter_compliance",
Help: "Azure Audit SecurityCenter compliance status",
},
[]string{
"subscriptionID",
"assessmentType",
},
)
m.prometheus.advisorRecommendations = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "azurerm_advisor_recommendation",
Help: "Azure Audit Advisor recommendation",
},
[]string{
"subscriptionID",
"category",
"resourceType",
"resourceName",
"resourceGroup",
"problem",
"impact",
"risk",
},
)
prometheus.MustRegister(m.prometheus.securitycenterCompliance)
prometheus.MustRegister(m.prometheus.advisorRecommendations)
}
func (m *MetricsCollectorAzureRmSecurity) Reset() {
m.prometheus.securitycenterCompliance.Reset()
m.prometheus.advisorRecommendations.Reset()
}
func (m *MetricsCollectorAzureRmSecurity) Collect(ctx context.Context, logger *log.Entry, callback chan<- func(), subscription subscriptions.Subscription) {
m.collectAzureAdvisorRecommendations(ctx, logger, callback, subscription)
for _, location := range m.CollectorReference.AzureLocations {
m.collectAzureSecurityCompliance(ctx, logger, callback, subscription, location)
}
}
func (m *MetricsCollectorAzureRmSecurity) collectAzureSecurityCompliance(ctx context.Context, logger *log.Entry, callback chan<- func(), subscription subscriptions.Subscription, location string) {
subscriptionResourceId := fmt.Sprintf("/subscriptions/%v", *subscription.SubscriptionID)
client := security.NewCompliancesClient(subscriptionResourceId, location)
client.Authorizer = AzureAuthorizer
complienceResult, err := client.Get(ctx, subscriptionResourceId, time.Now().Format("2006-01-02Z"))
if err != nil {
logger.Error(err)
return
}
infoMetric := prometheusCommon.NewMetricsList()
if complienceResult.AssessmentResult != nil {
for _, result := range *complienceResult.AssessmentResult {
segmentType := ""
if result.SegmentType != nil {
segmentType = *result.SegmentType
}
infoLabels := prometheus.Labels{
"subscriptionID": *subscription.SubscriptionID,
"assessmentType": segmentType,
}
infoMetric.Add(infoLabels, *result.Percentage)
}
}
callback <- func() {
infoMetric.GaugeSet(m.prometheus.securitycenterCompliance)
}
}
func (m *MetricsCollectorAzureRmSecurity) collectAzureAdvisorRecommendations(ctx context.Context, logger *log.Entry, callback chan<- func(), subscription subscriptions.Subscription) {
client := advisor.NewRecommendationsClient(*subscription.SubscriptionID)
client.Authorizer = AzureAuthorizer
recommendationResult, err := client.ListComplete(ctx, "", nil, "")
if err != nil {
logger.Panic(err)
}
infoMetric := prometheusCommon.NewMetricsList()
for _, item := range *recommendationResult.Response().Value {
infoLabels := prometheus.Labels{
"subscriptionID": *subscription.SubscriptionID,
"category": string(item.RecommendationProperties.Category),
"resourceType": stringPtrToString(item.RecommendationProperties.ImpactedField),
"resourceName": stringPtrToString(item.RecommendationProperties.ImpactedValue),
"resourceGroup": extractResourceGroupFromAzureId(*item.ID),
"problem": stringPtrToString(item.RecommendationProperties.ShortDescription.Problem),
"impact": string(item.Impact),
"risk": string(item.Risk),
}
infoMetric.AddInfo(infoLabels)
}
callback <- func() {
infoMetric.GaugeSet(m.prometheus.advisorRecommendations)
}
}