|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/advisor/armadvisor" |
| 7 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions" |
| 8 | + "github.com/prometheus/client_golang/prometheus" |
| 9 | + "github.com/webdevops/go-common/prometheus/collector" |
| 10 | + "github.com/webdevops/go-common/utils/to" |
| 11 | + "go.uber.org/zap" |
| 12 | +) |
| 13 | + |
| 14 | +type MetricsCollectorAzureRmAdvisor struct { |
| 15 | + collector.Processor |
| 16 | + |
| 17 | + prometheus struct { |
| 18 | + advisorRecommendation *prometheus.GaugeVec |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func (m *MetricsCollectorAzureRmAdvisor) Setup(collector *collector.Collector) { |
| 23 | + m.Processor.Setup(collector) |
| 24 | + |
| 25 | + m.prometheus.advisorRecommendation = prometheus.NewGaugeVec( |
| 26 | + prometheus.GaugeOpts{ |
| 27 | + Name: "azurerm_advisor_recommendation", |
| 28 | + Help: "Azure Advisor recommendation", |
| 29 | + }, |
| 30 | + []string{ |
| 31 | + "recommendationID", |
| 32 | + "resourceID", |
| 33 | + "resourceType", |
| 34 | + "category", |
| 35 | + "impact", |
| 36 | + "risk", |
| 37 | + "recommendationSubCategory", |
| 38 | + "problem", |
| 39 | + "solution", |
| 40 | + }, |
| 41 | + ) |
| 42 | + m.Collector.RegisterMetricList("advisorRecommendation", m.prometheus.advisorRecommendation, true) |
| 43 | +} |
| 44 | + |
| 45 | +func (m *MetricsCollectorAzureRmAdvisor) Reset() {} |
| 46 | + |
| 47 | +func (m *MetricsCollectorAzureRmAdvisor) Collect(callback chan<- func()) { |
| 48 | + err := AzureSubscriptionsIterator.ForEachAsync(m.Logger(), func(subscription *armsubscriptions.Subscription, logger *zap.SugaredLogger) { |
| 49 | + m.collectAzureAdvisorRecommendations(subscription, logger) |
| 50 | + }) |
| 51 | + if err != nil { |
| 52 | + m.Logger().Panic(err) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (m *MetricsCollectorAzureRmAdvisor) collectAzureAdvisorRecommendations(subscription *armsubscriptions.Subscription, logger *zap.SugaredLogger) { |
| 57 | + client, err := armadvisor.NewRecommendationsClient(*subscription.SubscriptionID, AzureClient.GetCred(), AzureClient.NewArmClientOptions()) |
| 58 | + if err != nil { |
| 59 | + logger.Panic(err) |
| 60 | + } |
| 61 | + |
| 62 | + // Generate recommendations first (async operation) |
| 63 | + // Note: This is a fire-and-forget operation. The generation is asynchronous, |
| 64 | + // so newly generated recommendations will be available in the next scrape cycle. |
| 65 | + _, err = client.Generate(m.Context(), nil) |
| 66 | + if err != nil { |
| 67 | + logger.Warnf("failed to generate recommendations for subscription %s: %v", to.StringLower(subscription.SubscriptionID), err) |
| 68 | + } |
| 69 | + |
| 70 | + recommendationMetrics := m.Collector.GetMetricList("advisorRecommendation") |
| 71 | + |
| 72 | + pager := client.NewListPager(nil) |
| 73 | + for pager.More() { |
| 74 | + result, err := pager.NextPage(m.Context()) |
| 75 | + if err != nil { |
| 76 | + logger.Panic(err) |
| 77 | + } |
| 78 | + |
| 79 | + for _, recommendation := range result.Value { |
| 80 | + category := "" |
| 81 | + if recommendation.Properties.Category != nil { |
| 82 | + category = strings.ToLower(string(*recommendation.Properties.Category)) |
| 83 | + } |
| 84 | + |
| 85 | + risk := "" |
| 86 | + if recommendation.Properties.Risk != nil { |
| 87 | + risk = strings.ToLower(string(*recommendation.Properties.Risk)) |
| 88 | + } |
| 89 | + |
| 90 | + impact := "" |
| 91 | + if recommendation.Properties.Impact != nil { |
| 92 | + impact = strings.ToLower(string(*recommendation.Properties.Impact)) |
| 93 | + } |
| 94 | + |
| 95 | + problem := "" |
| 96 | + solution := "" |
| 97 | + if recommendation.Properties.ShortDescription != nil { |
| 98 | + problem = to.String(recommendation.Properties.ShortDescription.Problem) |
| 99 | + solution = to.String(recommendation.Properties.ShortDescription.Solution) |
| 100 | + } |
| 101 | + |
| 102 | + // Truncate problem and solution if configured |
| 103 | + if Config.Collectors.Advisor.ProblemMaxLength > 0 { |
| 104 | + problem = truncateStrings(problem, Config.Collectors.Advisor.ProblemMaxLength, "...") |
| 105 | + } |
| 106 | + if Config.Collectors.Advisor.SolutionMaxLength > 0 { |
| 107 | + solution = truncateStrings(solution, Config.Collectors.Advisor.SolutionMaxLength, "...") |
| 108 | + } |
| 109 | + |
| 110 | + recommendationSubCategory := "" |
| 111 | + if recommendation.Properties.ExtendedProperties != nil { |
| 112 | + if subCat, ok := recommendation.Properties.ExtendedProperties["recommendationSubCategory"]; ok && subCat != nil { |
| 113 | + recommendationSubCategory = to.String(subCat) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + resourceID := "" |
| 118 | + if recommendation.Properties.ResourceMetadata != nil && recommendation.Properties.ResourceMetadata.ResourceID != nil { |
| 119 | + resourceID = to.String(recommendation.Properties.ResourceMetadata.ResourceID) |
| 120 | + } |
| 121 | + |
| 122 | + resourceType := to.StringLower(recommendation.Properties.ImpactedField) |
| 123 | + |
| 124 | + recommendationID := to.StringLower(recommendation.Name) |
| 125 | + |
| 126 | + infoLabels := prometheus.Labels{ |
| 127 | + "recommendationID": recommendationID, |
| 128 | + "resourceID": resourceID, |
| 129 | + "resourceType": resourceType, |
| 130 | + "category": category, |
| 131 | + "impact": impact, |
| 132 | + "risk": risk, |
| 133 | + "recommendationSubCategory": recommendationSubCategory, |
| 134 | + "problem": problem, |
| 135 | + "solution": solution, |
| 136 | + } |
| 137 | + recommendationMetrics.Add(infoLabels, 1) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments