diff --git a/src/main/java/ru/ldeloff/servermonitorbot/controller/TelegramBotController.java b/src/main/java/ru/ldeloff/servermonitorbot/controller/TelegramBotController.java index a9147f3..4ec8c43 100644 --- a/src/main/java/ru/ldeloff/servermonitorbot/controller/TelegramBotController.java +++ b/src/main/java/ru/ldeloff/servermonitorbot/controller/TelegramBotController.java @@ -10,6 +10,8 @@ import ru.ldeloff.servermonitorbot.repository.SshRepository; import ru.ldeloff.servermonitorbot.service.command.FirstUseCommand; import ru.ldeloff.servermonitorbot.service.command.GetStatusSessions; import ru.ldeloff.servermonitorbot.service.command.SwitchToMainMenu; +import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempAggregateCommand; +import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempCommand; import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempAggregateCommand; import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempCommand; import ru.ldeloff.servermonitorbot.service.command.uname.UnameAggregateCommand; @@ -27,6 +29,8 @@ public class TelegramBotController extends TelegramLongPollingBot { final UnameAggregateCommand unameAggregateCommand; final SwitchToMainMenu switchToMainMenu; final UnameCommand unameCommand; + final CpuTempAggregateCommand cpuTempAggregateCommand; + final CpuTempCommand cpuTempCommand; final HddTempAggregateCommand hddTempAggregateCommand; final HddTempCommand hddTempCommand; @@ -45,6 +49,9 @@ public class TelegramBotController extends TelegramLongPollingBot { case "uname" -> { unameAggregateCommand.execute(update, this); } + case "CPU.temp" -> { + cpuTempAggregateCommand.execute(update, this); + } case "HDD.temp" -> { hddTempAggregateCommand.execute(update, this); } @@ -60,6 +67,9 @@ public class TelegramBotController extends TelegramLongPollingBot { case "uname" -> { unameCommand.execute(update, this); } + case "CPU.temp" -> { + cpuTempCommand.execute(update, this); + } case "HDD.temp" -> { hddTempCommand.execute(update, this); } diff --git a/src/main/java/ru/ldeloff/servermonitorbot/service/command/cputemp/CpuTempAggregateCommand.java b/src/main/java/ru/ldeloff/servermonitorbot/service/command/cputemp/CpuTempAggregateCommand.java new file mode 100644 index 0000000..90d2eb2 --- /dev/null +++ b/src/main/java/ru/ldeloff/servermonitorbot/service/command/cputemp/CpuTempAggregateCommand.java @@ -0,0 +1,24 @@ +package ru.ldeloff.servermonitorbot.service.command.cputemp; + +import org.springframework.stereotype.Service; +import org.telegram.telegrambots.meta.api.methods.send.SendMessage; +import ru.ldeloff.servermonitorbot.model.User; +import ru.ldeloff.servermonitorbot.service.command.CommandTemplate; +import ru.ldeloff.servermonitorbot.service.role.RoleService; +import ru.ldeloff.servermonitorbot.service.user.UserService; +import ru.ldeloff.servermonitorbot.utils.ui.uname.ServerListButtons; + +@Service +public class CpuTempAggregateCommand extends CommandTemplate { + final ServerListButtons serverListButtons; + + public CpuTempAggregateCommand(UserService userService, RoleService roleService, ServerListButtons serverListButtons) { + super(userService, roleService); + this.serverListButtons = serverListButtons; + } + + @Override + public SendMessage actionForAuth(User user, SendMessage message) { + return serverListButtons.uiForm(message); + } +} diff --git a/src/main/java/ru/ldeloff/servermonitorbot/service/command/cputemp/CpuTempCommand.java b/src/main/java/ru/ldeloff/servermonitorbot/service/command/cputemp/CpuTempCommand.java new file mode 100644 index 0000000..fb046c1 --- /dev/null +++ b/src/main/java/ru/ldeloff/servermonitorbot/service/command/cputemp/CpuTempCommand.java @@ -0,0 +1,67 @@ +package ru.ldeloff.servermonitorbot.service.command.cputemp; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.telegram.telegrambots.meta.api.methods.send.SendMessage; +import ru.ldeloff.servermonitorbot.model.Command; +import ru.ldeloff.servermonitorbot.model.SshServer; +import ru.ldeloff.servermonitorbot.model.User; +import ru.ldeloff.servermonitorbot.service.command.CommandTemplate; +import ru.ldeloff.servermonitorbot.service.role.RoleService; +import ru.ldeloff.servermonitorbot.service.ssh.SshService; +import ru.ldeloff.servermonitorbot.service.user.UserService; +import ru.ldeloff.servermonitorbot.utils.SshServerUtils; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +@Service +@Slf4j +public class CpuTempCommand extends CommandTemplate { + final SshService sshService; + public CpuTempCommand(UserService userService, RoleService roleService, SshService sshService) { + super(userService, roleService); + this.sshService = sshService; + } + + @Override + public SendMessage actionForAuth(User user, SendMessage message) { + String [] tags = Arrays.stream(message.getText().split(":")) + .map(String::trim) + .toArray(String[]::new); + + if (tags[1].equals("all")) { + message.setText(hddTempAllHost()); + } else { + message.setText(Objects.requireNonNull(hddTempSpecificHost(tags[1]))); + } + return message; + } + + private String hddTempAllHost() { + List servers = SshServerUtils.filterGoodServers(sshService.getSshServers()); + StringBuilder response = new StringBuilder(); + servers.forEach(server -> response.append(hddTempSpecificHost(server.getName())).append("\n")); + return response.toString(); + } + + private String hddTempSpecificHost(String serverName) { + Optional server = sshService.getSshServers() + .stream() + .filter(x -> x.getName().equals(serverName)) + .findFirst(); + if (server.isPresent()) { + Command result = sshService.execute(new Command("cat /sys/class/thermal/thermal_zone0/temp", 100, server.get())); + return server.get().getName() + ": \n" + + (Objects.isNull(result.getResponse()) ? "ошибка при выполнении команды" : + String.format("%.1f", + Double.parseDouble(result.getResponse().replaceAll("\n", ""))/1000)) + + "°C"; + } else { + log.error("Ошибка при выполнении команды 'CPUtemp'. Искомый сервер (" + serverName + ") не найден"); + return null; + } + } +} diff --git a/src/main/java/ru/ldeloff/servermonitorbot/utils/ui/TelegramBotKeyboard.java b/src/main/java/ru/ldeloff/servermonitorbot/utils/ui/TelegramBotKeyboard.java index 817c456..a7f776c 100644 --- a/src/main/java/ru/ldeloff/servermonitorbot/utils/ui/TelegramBotKeyboard.java +++ b/src/main/java/ru/ldeloff/servermonitorbot/utils/ui/TelegramBotKeyboard.java @@ -13,8 +13,10 @@ import java.util.ArrayList; public class TelegramBotKeyboard implements UiFormer { private final String STATUS = "Статус"; private final String UNAME = "uname"; + private final String CPU_TEMP = "CPU.temp"; private final String HDD_TEMP = "HDD.temp"; + @Override public SendMessage uiForm(SendMessage message) { ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup(); @@ -28,6 +30,7 @@ public class TelegramBotKeyboard implements UiFormer { keyboardRow.add(new KeyboardButton(STATUS)); keyboardRow.add(new KeyboardButton(UNAME)); + keyboardRow.add(new KeyboardButton(CPU_TEMP)); keyboardRow.add(new KeyboardButton(HDD_TEMP)); replyKeyboardMarkup.setKeyboard(keyboardRows);