|
17 | 17 | package org.apache.cloudstack.metrics; |
18 | 18 |
|
19 | 19 | import java.math.BigDecimal; |
| 20 | +import java.security.cert.X509Certificate; |
20 | 21 | import java.util.ArrayList; |
21 | 22 | import java.util.HashMap; |
22 | 23 | import java.util.HashSet; |
|
26 | 27 |
|
27 | 28 | import javax.inject.Inject; |
28 | 29 |
|
| 30 | +import org.apache.cloudstack.ca.CAManager; |
29 | 31 | import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope; |
30 | 32 | import org.apache.cloudstack.storage.datastore.db.ImageStoreDao; |
31 | 33 | import org.apache.commons.lang3.StringUtils; |
@@ -133,6 +135,8 @@ public String toString() { |
133 | 135 | private ResourceCountDao _resourceCountDao; |
134 | 136 | @Inject |
135 | 137 | private HostTagsDao _hostTagsDao; |
| 138 | + @Inject |
| 139 | + private CAManager caManager; |
136 | 140 |
|
137 | 141 | public PrometheusExporterImpl() { |
138 | 142 | super(); |
@@ -216,6 +220,9 @@ private void addHostMetrics(final List<Item> metricsList, final long dcId, final |
216 | 220 | } |
217 | 221 |
|
218 | 222 | metricsList.add(new ItemHostVM(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), vmDao.listByHostId(host.getId()).size())); |
| 223 | + |
| 224 | + addSSLCertificateExpirationMetrics(metricsList, zoneName, zoneUuid, host); |
| 225 | + |
219 | 226 | final CapacityVO coreCapacity = capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_CPU_CORE); |
220 | 227 |
|
221 | 228 | if (coreCapacity == null && !host.isInMaintenanceStates()){ |
@@ -253,6 +260,18 @@ private void addHostMetrics(final List<Item> metricsList, final long dcId, final |
253 | 260 | addHostTagsMetrics(metricsList, dcId, zoneName, zoneUuid, totalHosts, upHosts, downHosts, total, up, down); |
254 | 261 | } |
255 | 262 |
|
| 263 | + private void addSSLCertificateExpirationMetrics(List<Item> metricsList, String zoneName, String zoneUuid, HostVO host) { |
| 264 | + if (caManager == null || caManager.getActiveCertificatesMap() == null) { |
| 265 | + return; |
| 266 | + } |
| 267 | + X509Certificate cert = caManager.getActiveCertificatesMap().getOrDefault(host.getPrivateIpAddress(), null); |
| 268 | + if (cert == null) { |
| 269 | + return; |
| 270 | + } |
| 271 | + long certExpiryEpoch = cert.getNotAfter().getTime() / 1000; // Convert to epoch seconds |
| 272 | + metricsList.add(new ItemHostCertExpiry(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), certExpiryEpoch)); |
| 273 | + } |
| 274 | + |
256 | 275 | private String markTagMaps(HostVO host, Map<String, Integer> totalHosts, Map<String, Integer> upHosts, Map<String, Integer> downHosts) { |
257 | 276 | List<HostTagVO> hostTagVOS = _hostTagsDao.getHostTags(host.getId()); |
258 | 277 | List<String> hostTags = new ArrayList<>(); |
@@ -1049,4 +1068,28 @@ public String toMetricsString() { |
1049 | 1068 | return String.format("%s{zone=\"%s\",cpu=\"%d\",memory=\"%d\"} %d", name, zoneName, cpu, memory, total); |
1050 | 1069 | } |
1051 | 1070 | } |
| 1071 | + |
| 1072 | + class ItemHostCertExpiry extends Item { |
| 1073 | + String zoneName; |
| 1074 | + String zoneUuid; |
| 1075 | + String hostName; |
| 1076 | + String hostUuid; |
| 1077 | + String hostIp; |
| 1078 | + long expiryTimestamp; |
| 1079 | + |
| 1080 | + public ItemHostCertExpiry(final String zoneName, final String zoneUuid, final String hostName, final String hostUuid, final String hostIp, final long expiry) { |
| 1081 | + super("cloudstack_host_cert_expiry_timestamp"); |
| 1082 | + this.zoneName = zoneName; |
| 1083 | + this.zoneUuid = zoneUuid; |
| 1084 | + this.hostName = hostName; |
| 1085 | + this.hostUuid = hostUuid; |
| 1086 | + this.hostIp = hostIp; |
| 1087 | + this.expiryTimestamp = expiry; |
| 1088 | + } |
| 1089 | + |
| 1090 | + @Override |
| 1091 | + public String toMetricsString() { |
| 1092 | + return String.format("%s{zone=\"%s\",hostname=\"%s\",ip=\"%s\"} %d", name, zoneName, hostName, hostIp, expiryTimestamp); |
| 1093 | + } |
| 1094 | + } |
1052 | 1095 | } |
0 commit comments