108 lines
3.1 KiB
Java
108 lines
3.1 KiB
Java
package dev.dinauer.monitoring;
|
|
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import jakarta.annotation.PostConstruct;
|
|
import jakarta.enterprise.context.ApplicationScoped;
|
|
import jakarta.inject.Inject;
|
|
|
|
import org.jboss.logging.Logger;
|
|
|
|
import io.quarkus.runtime.Startup;
|
|
|
|
import dev.dinauer.monitoring.entity.MonitoringConfig;
|
|
import dev.dinauer.monitoring.entity.repo.MonitoringRepo;
|
|
import dev.dinauer.monitoring.log.Log;
|
|
import dev.dinauer.monitoring.log.LogRepo;
|
|
import dev.dinauer.monitoring.memory.MemoryMonitoringJobRunner;
|
|
import dev.dinauer.monitoring.nodes.NodeMonitoringService;
|
|
import dev.dinauer.monitoring.volume.VolumeMonitoringJobRunner;
|
|
import dev.dinauer.monitoring.volume.utils.Duration;
|
|
|
|
@Startup
|
|
@ApplicationScoped
|
|
public class MonitoringJobRunner
|
|
{
|
|
@Inject
|
|
Logger LOG;
|
|
|
|
@Inject
|
|
VolumeMonitoringJobRunner volumeMonitoringJobRunner;
|
|
|
|
@Inject
|
|
MemoryMonitoringJobRunner memoryMonitoringJobRunner;
|
|
|
|
@Inject
|
|
NodeMonitoringService nodeMonitoringService;
|
|
|
|
@Inject
|
|
MonitoringRepo monitoringRepo;
|
|
|
|
@Inject
|
|
LogRepo logRepo;
|
|
|
|
@PostConstruct
|
|
public void run()
|
|
{
|
|
List<MonitoringConfig> configs = monitoringRepo.listAll();
|
|
for (MonitoringConfig config : configs)
|
|
{
|
|
schedule(config);
|
|
}
|
|
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
|
Runnable task = () -> {
|
|
try
|
|
{
|
|
System.out.println("x");
|
|
nodeMonitoringService.run();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logRepo.persist(Log.init("Failed to node monitoring job"));
|
|
LOG.error("Failed to node monitoring job");
|
|
}
|
|
};
|
|
scheduler.scheduleAtFixedRate(task, 0, Duration.parse("5m"), TimeUnit.SECONDS);
|
|
}
|
|
|
|
private void schedule(MonitoringConfig config)
|
|
{
|
|
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
|
Runnable task = () -> {
|
|
try
|
|
{
|
|
run(config);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logRepo.persist(Log.init(String.format("Monitoring %s failed.", config.getConfigName())));
|
|
LOG.errorf("Monitoring %s failed.", config.getConfigName());
|
|
}
|
|
};
|
|
scheduler.scheduleAtFixedRate(task, 0, Duration.parse(config.getInterval()), TimeUnit.SECONDS);
|
|
}
|
|
|
|
public void run(MonitoringConfig config) throws IOException, InterruptedException
|
|
{
|
|
LOG.infof("Running %s %s monitoring.", config.getConfigName(), config.getType().toString().toLowerCase());
|
|
switch (config.getType())
|
|
{
|
|
case VOLUME:
|
|
{
|
|
volumeMonitoringJobRunner.run(config);
|
|
return;
|
|
}
|
|
case WORKLOAD:
|
|
{
|
|
memoryMonitoringJobRunner.run(config);
|
|
return;
|
|
}
|
|
}
|
|
throw new RuntimeException();
|
|
}
|
|
}
|