diff --git a/pom.xml b/pom.xml index 06f23c8..889e73d 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,41 @@ org.springframework.boot spring-boot-starter-jdbc + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + io.micrometer + micrometer-core + + + io.micrometer + micrometer-registry-prometheus + + + + + io.prometheus + simpleclient_common + 0.16.0 + + + io.prometheus + simpleclient_hotspot + 0.16.0 + + + io.prometheus + simpleclient_servlet + 0.16.0 + + mysql diff --git a/src/main/java/com/geedge/common/PrometheusMetrics.java b/src/main/java/com/geedge/common/PrometheusMetrics.java new file mode 100644 index 0000000..57beb83 --- /dev/null +++ b/src/main/java/com/geedge/common/PrometheusMetrics.java @@ -0,0 +1,26 @@ +package com.geedge.common; + +import io.prometheus.client.exporter.MetricsServlet; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * TODO + * + * @Classname PrometheusMetrics + * @Date 2024/1/9 10:22 + * @Author wWei + */ +@RestController +public class PrometheusMetrics extends MetricsServlet { + + @GetMapping("/metrics") + public void redisMetrics(HttpServletRequest request, HttpServletResponse response) throws IOException { + this.doGet(request, response); + } +} + diff --git a/src/main/java/com/geedge/scheduler/FqdnScheduler.java b/src/main/java/com/geedge/scheduler/FqdnScheduler.java index c85ba04..ec34a6e 100644 --- a/src/main/java/com/geedge/scheduler/FqdnScheduler.java +++ b/src/main/java/com/geedge/scheduler/FqdnScheduler.java @@ -11,6 +11,8 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Record; +import io.prometheus.client.CollectorRegistry; +import io.prometheus.client.Counter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; @@ -45,10 +47,11 @@ public class FqdnScheduler { private Boolean cyberghostvpnServernameDeleteEnable; @Value("${tsg.object.fqdn.cyberghostvpn_servername.delete.offsetSecond}") private Integer cyberghostvpnServernameOffsetSecond; + public Counter cyberghostvpnServernameCounter = Counter.build("cyberghostvpn_servername_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.fqdn.cyberghostvpn_servername.update.cron}") public void updateCyberghostvpn() { - executeUpdate(cyberghostvpnServernameId, cyberghostvpnServernameName, cyberghostvpnServernameUpdateEnable, cyberghostvpnServernameSql); + executeUpdate(cyberghostvpnServernameId, cyberghostvpnServernameName, cyberghostvpnServernameUpdateEnable, cyberghostvpnServernameSql, cyberghostvpnServernameCounter); } @Scheduled(cron = "${tsg.object.fqdn.cyberghostvpn_servername.delete.cron}") @@ -68,10 +71,11 @@ public class FqdnScheduler { private Boolean ipvanishvpnServernameDeleteEnable; @Value("${tsg.object.fqdn.ipvanishvpn_servername.delete.offsetSecond}") private Integer ipvanishvpnServernameOffsetSecond; + public Counter ipvanishvpnServernameCounter = Counter.build("ipvanishvpn_servername_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.fqdn.ipvanishvpn_servername.update.cron}") public void updateIpvanishvpn() { - executeUpdate(ipvanishvpnServernameId, ipvanishvpnServernameName, ipvanishvpnServernameUpdateEnable, ipvanishvpnServernameSql); + executeUpdate(ipvanishvpnServernameId, ipvanishvpnServernameName, ipvanishvpnServernameUpdateEnable, ipvanishvpnServernameSql, ipvanishvpnServernameCounter); } @Scheduled(cron = "${tsg.object.fqdn.ipvanishvpn_servername.delete.cron}") @@ -92,11 +96,11 @@ public class FqdnScheduler { private Boolean ivacyvpnServernameDeleteEnable; @Value("${tsg.object.fqdn.ivacyvpn_servername.delete.offsetSecond}") private Integer ivacyvpnServernameOffsetSecond; - + public Counter ivacyvpnServernameCounter = Counter.build("ivacyvpn_servername_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.fqdn.ivacyvpn_servername.update.cron}") public void updateIvacyvpn() { - executeUpdate(ivacyvpnServernameId, ivacyvpnServernameName, ivacyvpnServernameUpdateEnable, ivacyvpnServernameSql); + executeUpdate(ivacyvpnServernameId, ivacyvpnServernameName, ivacyvpnServernameUpdateEnable, ivacyvpnServernameSql, ivacyvpnServernameCounter); } @Scheduled(cron = "${tsg.object.fqdn.ivacyvpn_servername.delete.cron}") @@ -116,11 +120,11 @@ public class FqdnScheduler { private Boolean vpnunlimitedServernameDeleteEnable; @Value("${tsg.object.fqdn.vpnunlimited_servername.delete.offsetSecond}") private Integer vpnunlimitedServernameOffsetSecond; - + public Counter vpnunlimitedServernameCounter = Counter.build("vpnunlimited_servername_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.fqdn.vpnunlimited_servername.update.cron}") public void updateVpnunlimited() { - executeUpdate(vpnunlimitedServernameId, vpnunlimitedServernameName, vpnunlimitedServernameUpdateEnable, vpnunlimitedServernameSql); + executeUpdate(vpnunlimitedServernameId, vpnunlimitedServernameName, vpnunlimitedServernameUpdateEnable, vpnunlimitedServernameSql, vpnunlimitedServernameCounter); } @Scheduled(cron = "${tsg.object.fqdn.vpnunlimited_servername.delete.cron}") @@ -140,11 +144,11 @@ public class FqdnScheduler { private Boolean vpn4fameServernameDeleteEnable; @Value("${tsg.object.fqdn.vpn4fame_servername.delete.offsetSecond}") private Integer vpn4fameServernameOffsetSecond; - + public Counter vpn4fameServernameCounter = Counter.build("vpn4fame_servername_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.fqdn.vpn4fame_servername.update.cron}") public void updateVpn4fame() { - executeUpdate(vpn4fameServernameId, vpn4fameServernameName, vpn4fameServernameUpdateEnable, vpn4fameServernameSql); + executeUpdate(vpn4fameServernameId, vpn4fameServernameName, vpn4fameServernameUpdateEnable, vpn4fameServernameSql, vpn4fameServernameCounter); } @Scheduled(cron = "${tsg.object.fqdn.vpn4fame_servername.delete.cron}") @@ -152,7 +156,7 @@ public class FqdnScheduler { executeDelete(vpn4fameServernameId, vpn4fameServernameDeleteEnable, vpn4fameServernameOffsetSecond); } - private static void executeUpdate(Integer id, String name, Boolean enable, String sql) { + private static void executeUpdate(Integer id, String name, Boolean enable, String sql, Counter counter) { log.info("{}: started update task.", id); if (BooleanUtil.isFalse(enable)) { log.warn("{}: interrupted update task. enable: {}", id, enable); @@ -170,7 +174,7 @@ public class FqdnScheduler { List> patterns = Lists.newArrayList(); Map pattern = Maps.newHashMap(); - pattern.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_STRING_PATTERNS_KEYWORDS, "$" + domain); + pattern.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_STRING_PATTERNS_KEYWORDS, domain); patterns.add(pattern); Map str = Maps.newHashMap(); @@ -197,7 +201,11 @@ public class FqdnScheduler { body.put(TsgObject.KEY_VSYS_ID, TsgObject.VALUE_VSYS_ID_1); body.put(TsgObject.KEY_OBJECT, obj); log.info("{}: build api params, items size: {}, cost {} seconds", id, items.size(), watch.elapsed(TimeUnit.SECONDS)); + if (items.isEmpty()) { + return; + } TsgUtil.updateObjectById(id, body); + counter.inc(items.size()); } catch (Exception e) { log.error("{}: failed to execute update task. message: {}", id, e.getMessage()); throw new RuntimeException(e); diff --git a/src/main/java/com/geedge/scheduler/IpScheduler.java b/src/main/java/com/geedge/scheduler/IpScheduler.java index 5215eeb..b13641d 100644 --- a/src/main/java/com/geedge/scheduler/IpScheduler.java +++ b/src/main/java/com/geedge/scheduler/IpScheduler.java @@ -15,6 +15,8 @@ import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Record; +import io.prometheus.client.CollectorRegistry; +import io.prometheus.client.Counter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; @@ -58,10 +60,11 @@ public class IpScheduler { private Boolean cyberghostvpnServeripDeleteEnable; @Value("${tsg.object.ip.cyberghostvpn_serverip.delete.offsetSecond}") private Integer cyberghostvpnServeripOffsetSecond; + public Counter cyberghostvpnServeripCounter = Counter.build("cyberghostvpn_serverip_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.ip.cyberghostvpn_serverip.update.cron}") public void updateCyberghostvpn() { - executeUpdate(cyberghostvpnServeripId, cyberghostvpnServeripName, cyberghostvpnServeripUpdateEnable, cyberghostvpnServeripSql); + executeUpdate(cyberghostvpnServeripId, cyberghostvpnServeripName, cyberghostvpnServeripUpdateEnable, cyberghostvpnServeripSql, cyberghostvpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.cyberghostvpn_serverip.delete.cron}") @@ -81,10 +84,11 @@ public class IpScheduler { private Boolean hotspotvpnServeripDeleteEnable; @Value("${tsg.object.ip.hotspotvpn_serverip.delete.offsetSecond}") private Integer hotspotvpnServeripOffsetSecond; + public Counter hotspotvpnServeripCounter = Counter.build("hotspotvpn_serverip_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.ip.hotspotvpn_serverip.update.cron}") public void updateHotspotvpn() { - executeUpdate(hotspotvpnServeripId, hotspotvpnServeripName, hotspotvpnServeripUpdateEnable, hotspotvpnServeripSql); + executeUpdate(hotspotvpnServeripId, hotspotvpnServeripName, hotspotvpnServeripUpdateEnable, hotspotvpnServeripSql, hotspotvpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.hotspotvpn_serverip.delete.cron}") @@ -104,10 +108,11 @@ public class IpScheduler { private Boolean ipvanishvpnServeripDeleteEnable; @Value("${tsg.object.ip.ipvanishvpn_serverip.delete.offsetSecond}") private Integer ipvanishvpnServeripOffsetSecond; + public Counter ipvanishvpnServeripCounter = Counter.build("ipvanishvpn_serverip_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.ip.ipvanishvpn_serverip.update.cron}") public void updateIpvanishvpn() { - executeUpdate(ipvanishvpnServeripId, ipvanishvpnServeripName, ipvanishvpnServeripUpdateEnable, ipvanishvpnServeripSql); + executeUpdate(ipvanishvpnServeripId, ipvanishvpnServeripName, ipvanishvpnServeripUpdateEnable, ipvanishvpnServeripSql, ipvanishvpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.ipvanishvpn_serverip.delete.cron}") @@ -127,10 +132,11 @@ public class IpScheduler { private Boolean geckovpnServeripDeleteEnable; @Value("${tsg.object.ip.geckovpn_serverip.delete.offsetSecond}") private Integer geckovpnServeripOffsetSecond; + public Counter geckovpnServeripCounter = Counter.build("geckovpn_serverip_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.ip.geckovpn_serverip.update.cron}") public void updateGeckovpn() { - executeUpdate(geckovpnServeripId, geckovpnServeripName, geckovpnServeripUpdateEnable, geckovpnServeripSql); + executeUpdate(geckovpnServeripId, geckovpnServeripName, geckovpnServeripUpdateEnable, geckovpnServeripSql, geckovpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.geckovpn_serverip.delete.cron}") @@ -150,10 +156,11 @@ public class IpScheduler { private Boolean ivacyvpnServeripDeleteEnable; @Value("${tsg.object.ip.ivacyvpn_serverip.delete.offsetSecond}") private Integer ivacyvpnServeripOffsetSecond; + public Counter ivacyvpnServeripCounter = Counter.build("ivacyvpn_serverip_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.ip.ivacyvpn_serverip.update.cron}") public void updateIvacyvpn() { - executeUpdate(ivacyvpnServeripId, ivacyvpnServeripName, ivacyvpnServeripUpdateEnable, ivacyvpnServeripSql); + executeUpdate(ivacyvpnServeripId, ivacyvpnServeripName, ivacyvpnServeripUpdateEnable, ivacyvpnServeripSql, ivacyvpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.ivacyvpn_serverip.delete.cron}") @@ -174,10 +181,11 @@ public class IpScheduler { private Boolean turbovpnServeripDeleteEnable; @Value("${tsg.object.ip.turbovpn_serverip.delete.offsetSecond}") private Integer turbovpnServeripOffsetSecond; + public Counter turbovpnServeripCounter = Counter.build("turbovpn_serverip_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.ip.turbovpn_serverip.update.cron}") public void updateTurbovpn() { - executeUpdate(turbovpnServeripId, turbovpnServeripName, turbovpnServeripUpdateEnable, turbovpnServeripSql); + executeUpdate(turbovpnServeripId, turbovpnServeripName, turbovpnServeripUpdateEnable, turbovpnServeripSql, turbovpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.turbovpn_serverip.delete.cron}") @@ -197,10 +205,11 @@ public class IpScheduler { private Boolean vpnunlimitedServeripDeleteEnable; @Value("${tsg.object.ip.vpnunlimited_serverip.delete.offsetSecond}") private Integer vpnunlimitedServeripOffsetSecond; + public Counter vpnunlimitedServeripCounter = Counter.build("vpnunlimited_serverip_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.ip.vpnunlimited_serverip.update.cron}") public void updateVpnunlimited() { - executeUpdate(vpnunlimitedServeripId, vpnunlimitedServeripName, vpnunlimitedServeripUpdateEnable, vpnunlimitedServeripSql); + executeUpdate(vpnunlimitedServeripId, vpnunlimitedServeripName, vpnunlimitedServeripUpdateEnable, vpnunlimitedServeripSql, vpnunlimitedServeripCounter); } @Scheduled(cron = "${tsg.object.ip.vpnunlimited_serverip.delete.cron}") @@ -220,10 +229,11 @@ public class IpScheduler { private Boolean windscribevpnServeripDeleteEnable; @Value("${tsg.object.ip.windscribevpn_serverip.delete.offsetSecond}") private Integer windscribevpnServeripOffsetSecond; + public Counter windscribevpnServeripCounter = Counter.build("windscribevpn_serverip_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.ip.windscribevpn_serverip.update.cron}") public void updateWindscribevpn() { - executeUpdate(windscribevpnServeripId, windscribevpnServeripName, windscribevpnServeripUpdateEnable, windscribevpnServeripSql); + executeUpdate(windscribevpnServeripId, windscribevpnServeripName, windscribevpnServeripUpdateEnable, windscribevpnServeripSql, windscribevpnServeripCounter); } @Scheduled(cron = "${tsg.object.ip.windscribevpn_serverip.delete.cron}") @@ -243,10 +253,11 @@ public class IpScheduler { private Boolean vpn4fameServeripDeleteEnable; @Value("${tsg.object.ip.vpn4fame_serverip.delete.offsetSecond}") private Integer vpn4fameServeripOffsetSecond; + public Counter vpn4fameServeripCounter = Counter.build("vpn4fame_serverip_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.ip.vpn4fame_serverip.update.cron}") public void updateVpn4fame() { - executeUpdate(vpn4fameServeripId, vpn4fameServeripName, vpn4fameServeripUpdateEnable, vpn4fameServeripSql); + executeUpdate(vpn4fameServeripId, vpn4fameServeripName, vpn4fameServeripUpdateEnable, vpn4fameServeripSql, vpn4fameServeripCounter); } @Scheduled(cron = "${tsg.object.ip.vpn4fame_serverip.delete.cron}") @@ -266,10 +277,11 @@ public class IpScheduler { private Boolean protonvpnIpDeleteEnable; @Value("${tsg.object.ip.protonvpn_ip.delete.offsetSecond}") private Integer protonvpnIpOffsetSecond; + public Counter protonvpnIpCounter = Counter.build("protonvpn_ip_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.ip.protonvpn_ip.update.cron}") public void updateProtonvpn() { - executeUpdate(protonvpnIpId, protonvpnIpName, protonvpnIpUpdateEnable, protonvpnIpSql); + executeUpdate(protonvpnIpId, protonvpnIpName, protonvpnIpUpdateEnable, protonvpnIpSql, protonvpnIpCounter); } @Scheduled(cron = "${tsg.object.ip.protonvpn_ip.delete.cron}") @@ -289,10 +301,11 @@ public class IpScheduler { private Boolean expressvpnIpDeleteEnable; @Value("${tsg.object.ip.expressvpn_ip.delete.offsetSecond}") private Integer expressvpnIpOffsetSecond; + public Counter expressvpnIpCounter = Counter.build("expressvpn_ip_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry); @Scheduled(cron = "${tsg.object.ip.expressvpn_ip.update.cron}") public void updateExpressvpn() { - executeUpdate(expressvpnIpId, expressvpnIpName, expressvpnIpUpdateEnable, expressvpnIpSql); + executeUpdate(expressvpnIpId, expressvpnIpName, expressvpnIpUpdateEnable, expressvpnIpSql, expressvpnIpCounter); } @Scheduled(cron = "${tsg.object.ip.expressvpn_ip.delete.cron}") @@ -301,7 +314,7 @@ public class IpScheduler { } - private static void executeUpdate(Integer id, String name, Boolean enable, String sql) { + private static void executeUpdate(Integer id, String name, Boolean enable, String sql, Counter counter) { log.info("{}: started update task.", id); if (BooleanUtil.isFalse(enable)) { log.warn("{}: interrupted update task. enable: {}", id, enable); @@ -365,7 +378,11 @@ public class IpScheduler { body.put(TsgObject.KEY_VSYS_ID, TsgObject.VALUE_VSYS_ID_1); body.put(TsgObject.KEY_OBJECT, obj); log.info("{}: build api params, items size: {}, cost {} seconds", id, items.size(), watch.elapsed(TimeUnit.SECONDS)); + if (items.isEmpty()) { + return; + } TsgUtil.updateObjectById(id, body); + counter.inc(items.size()); } catch (Exception e) { log.error("{}: failed to execute update task. message: {}", id, e.getMessage()); throw new RuntimeException(e);