master
go 118 lines 3.06 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package snmp
4
5 import (
6 "math"
7 "time"
8 )
9
10 type licenseAggregate struct {
11 remainingTime int64
12 hasRemainingTime bool
13 authRemainingTime int64
14 hasAuthRemaining bool
15 certRemainingTime int64
16 hasCertRemaining bool
17 graceRemainingTime int64
18 hasGraceRemaining bool
19 usagePercent int64
20 hasUsagePercent bool
21 stateHealthy int64
22 stateInformational int64
23 stateDegraded int64
24 stateBroken int64
25 stateIgnored int64
26 hasStateCounts bool
27 }
28
29 func aggregateLicenseRows(rows []licenseRow, now time.Time) licenseAggregate {
30 var agg licenseAggregate
31
32 for _, row := range rows {
33 switch row.StateBucket {
34 case licenseStateBucketHealthy:
35 agg.stateHealthy++
36 agg.hasStateCounts = true
37 case licenseStateBucketInformational:
38 agg.stateInformational++
39 agg.hasStateCounts = true
40 case licenseStateBucketDegraded:
41 agg.stateDegraded++
42 agg.hasStateCounts = true
43 case licenseStateBucketBroken:
44 agg.stateBroken++
45 agg.hasStateCounts = true
46 case licenseStateBucketIgnored:
47 agg.stateIgnored++
48 agg.hasStateCounts = true
49 }
50
51 if row.StateBucket == licenseStateBucketIgnored {
52 continue
53 }
54
55 if row.HasExpiry && !row.IsPerpetual {
56 agg.setMin(row.ExpiryTS-now.Unix(), &agg.remainingTime, &agg.hasRemainingTime)
57 }
58 if row.HasAuthorizationTime {
59 agg.setMin(row.AuthorizationExpiry-now.Unix(), &agg.authRemainingTime, &agg.hasAuthRemaining)
60 }
61 if row.HasCertificateTime {
62 agg.setMin(row.CertificateExpiry-now.Unix(), &agg.certRemainingTime, &agg.hasCertRemaining)
63 }
64 if row.HasGraceTime {
65 agg.setMin(row.GraceExpiry-now.Unix(), &agg.graceRemainingTime, &agg.hasGraceRemaining)
66 }
67 if row.HasUsagePct && !row.IsUnlimited {
68 pct := int64(math.Round(row.UsagePercent))
69 if !agg.hasUsagePercent || pct > agg.usagePercent {
70 agg.usagePercent = pct
71 agg.hasUsagePercent = true
72 }
73 }
74 }
75
76 return agg
77 }
78
79 func (agg *licenseAggregate) setMin(value int64, current *int64, seen *bool) {
80 if !*seen || value < *current {
81 *current = value
82 *seen = true
83 }
84 }
85
86 func (agg licenseAggregate) empty() bool {
87 return !agg.hasRemainingTime &&
88 !agg.hasAuthRemaining &&
89 !agg.hasCertRemaining &&
90 !agg.hasGraceRemaining &&
91 !agg.hasUsagePercent &&
92 !agg.hasStateCounts
93 }
94
95 func (agg licenseAggregate) writeTo(mx map[string]int64) {
96 if agg.hasRemainingTime {
97 mx[metricIDLicenseRemainingTime] = agg.remainingTime
98 }
99 if agg.hasAuthRemaining {
100 mx[metricIDLicenseAuthorizationRemainingTime] = agg.authRemainingTime
101 }
102 if agg.hasCertRemaining {
103 mx[metricIDLicenseCertificateRemainingTime] = agg.certRemainingTime
104 }
105 if agg.hasGraceRemaining {
106 mx[metricIDLicenseGraceRemainingTime] = agg.graceRemainingTime
107 }
108 if agg.hasUsagePercent {
109 mx[metricIDLicenseUsagePercent] = agg.usagePercent
110 }
111 if agg.hasStateCounts {
112 mx[metricIDLicenseStateHealthy] = agg.stateHealthy
113 mx[metricIDLicenseStateInformational] = agg.stateInformational
114 mx[metricIDLicenseStateDegraded] = agg.stateDegraded
115 mx[metricIDLicenseStateBroken] = agg.stateBroken
116 mx[metricIDLicenseStateIgnored] = agg.stateIgnored
117 }
118 }