From d0be1f0a6271fce5c8ea2d97b271cda41fd801e3 Mon Sep 17 00:00:00 2001 From: L_DelOff Date: Sun, 13 Aug 2023 19:36:12 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BA=D0=BD=D0=BE=D0=BF=D0=BA=D1=83=20=D1=81=D0=BB=D0=B5?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B7=D0=B0=20=D1=82=D0=B5?= =?UTF-8?q?=D0=BC=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D1=83=D1=80=D0=BE=D0=B9=20?= =?UTF-8?q?=D0=B4=D0=B8=D1=81=D0=BA=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TelegramBotController.java | 14 ++-- .../hddtemp/HddTempAggregateCommand.java | 24 +++++++ .../command/hddtemp/HddTempCommand.java | 64 +++++++++++++++++++ .../command/uname/UnameAggregateCommand.java | 10 +-- .../utils/ui/TelegramBotKeyboard.java | 2 + ...nAggregate.java => ServerListButtons.java} | 10 ++- 6 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 src/main/java/ru/ldeloff/servermonitorbot/service/command/hddtemp/HddTempAggregateCommand.java create mode 100644 src/main/java/ru/ldeloff/servermonitorbot/service/command/hddtemp/HddTempCommand.java rename src/main/java/ru/ldeloff/servermonitorbot/utils/ui/uname/{UnameChatButtonAggregate.java => ServerListButtons.java} (83%) diff --git a/src/main/java/ru/ldeloff/servermonitorbot/controller/TelegramBotController.java b/src/main/java/ru/ldeloff/servermonitorbot/controller/TelegramBotController.java index d5b17aa..a9147f3 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.hddtemp.HddTempAggregateCommand; +import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempCommand; import ru.ldeloff.servermonitorbot.service.command.uname.UnameAggregateCommand; import ru.ldeloff.servermonitorbot.service.command.uname.UnameCommand; @@ -17,7 +19,6 @@ import ru.ldeloff.servermonitorbot.service.command.uname.UnameCommand; @Slf4j @RequiredArgsConstructor public class TelegramBotController extends TelegramLongPollingBot { - final TelegramBot telegramBot; final SshRepository sshRepository; @@ -26,6 +27,8 @@ public class TelegramBotController extends TelegramLongPollingBot { final UnameAggregateCommand unameAggregateCommand; final SwitchToMainMenu switchToMainMenu; final UnameCommand unameCommand; + final HddTempAggregateCommand hddTempAggregateCommand; + final HddTempCommand hddTempCommand; @Override public void onUpdateReceived(Update update) { @@ -42,6 +45,9 @@ public class TelegramBotController extends TelegramLongPollingBot { case "uname" -> { unameAggregateCommand.execute(update, this); } + case "HDD.temp" -> { + hddTempAggregateCommand.execute(update, this); + } default -> { switchToMainMenu.execute(update, this); } @@ -54,6 +60,9 @@ public class TelegramBotController extends TelegramLongPollingBot { case "uname" -> { unameCommand.execute(update, this); } + case "HDD.temp" -> { + hddTempCommand.execute(update, this); + } default -> { switchToMainMenu.execute(update, this); } @@ -63,17 +72,14 @@ public class TelegramBotController extends TelegramLongPollingBot { } } } - @Override public String getBotUsername() { return telegramBot.getBotUsername(); } - @Override public String getBotToken() { return telegramBot.getBotToken(); } - @Override public void onClosing() { super.onClosing(); diff --git a/src/main/java/ru/ldeloff/servermonitorbot/service/command/hddtemp/HddTempAggregateCommand.java b/src/main/java/ru/ldeloff/servermonitorbot/service/command/hddtemp/HddTempAggregateCommand.java new file mode 100644 index 0000000..1f0ea18 --- /dev/null +++ b/src/main/java/ru/ldeloff/servermonitorbot/service/command/hddtemp/HddTempAggregateCommand.java @@ -0,0 +1,24 @@ +package ru.ldeloff.servermonitorbot.service.command.hddtemp; + +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 HddTempAggregateCommand extends CommandTemplate { + final ServerListButtons serverListButtons; + + public HddTempAggregateCommand(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/hddtemp/HddTempCommand.java b/src/main/java/ru/ldeloff/servermonitorbot/service/command/hddtemp/HddTempCommand.java new file mode 100644 index 0000000..48793cc --- /dev/null +++ b/src/main/java/ru/ldeloff/servermonitorbot/service/command/hddtemp/HddTempCommand.java @@ -0,0 +1,64 @@ +package ru.ldeloff.servermonitorbot.service.command.hddtemp; + +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 HddTempCommand extends CommandTemplate { + final SshService sshService; + public HddTempCommand(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("hddtemp /dev/sd*", 500, server.get())); + return server.get().getName() + ": \n" + + (Objects.isNull(result.getResponse()) ? "ошибка при выполнении команды" : result.getResponse()); + } else { + log.error("Ошибка при выполнении команды 'hddtemp'. Искомый сервер (" + serverName + ") не найден"); + return null; + } + } +} diff --git a/src/main/java/ru/ldeloff/servermonitorbot/service/command/uname/UnameAggregateCommand.java b/src/main/java/ru/ldeloff/servermonitorbot/service/command/uname/UnameAggregateCommand.java index 5bee9c1..2ac594e 100644 --- a/src/main/java/ru/ldeloff/servermonitorbot/service/command/uname/UnameAggregateCommand.java +++ b/src/main/java/ru/ldeloff/servermonitorbot/service/command/uname/UnameAggregateCommand.java @@ -6,19 +6,19 @@ 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.UnameChatButtonAggregate; +import ru.ldeloff.servermonitorbot.utils.ui.uname.ServerListButtons; @Service public class UnameAggregateCommand extends CommandTemplate { - final UnameChatButtonAggregate unameChatButtonAggregate; + final ServerListButtons serverListButtons; - public UnameAggregateCommand(UserService userService, RoleService roleService, UnameChatButtonAggregate unameChatButtonAggregate) { + public UnameAggregateCommand(UserService userService, RoleService roleService, ServerListButtons serverListButtons) { super(userService, roleService); - this.unameChatButtonAggregate = unameChatButtonAggregate; + this.serverListButtons = serverListButtons; } @Override public SendMessage actionForAuth(User user, SendMessage message) { - return unameChatButtonAggregate.uiForm(message); + return serverListButtons.uiForm(message); } } 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 a07a478..817c456 100644 --- a/src/main/java/ru/ldeloff/servermonitorbot/utils/ui/TelegramBotKeyboard.java +++ b/src/main/java/ru/ldeloff/servermonitorbot/utils/ui/TelegramBotKeyboard.java @@ -13,6 +13,7 @@ import java.util.ArrayList; public class TelegramBotKeyboard implements UiFormer { private final String STATUS = "Статус"; private final String UNAME = "uname"; + private final String HDD_TEMP = "HDD.temp"; @Override public SendMessage uiForm(SendMessage message) { @@ -27,6 +28,7 @@ public class TelegramBotKeyboard implements UiFormer { keyboardRow.add(new KeyboardButton(STATUS)); keyboardRow.add(new KeyboardButton(UNAME)); + keyboardRow.add(new KeyboardButton(HDD_TEMP)); replyKeyboardMarkup.setKeyboard(keyboardRows); message.setReplyMarkup(replyKeyboardMarkup); diff --git a/src/main/java/ru/ldeloff/servermonitorbot/utils/ui/uname/UnameChatButtonAggregate.java b/src/main/java/ru/ldeloff/servermonitorbot/utils/ui/uname/ServerListButtons.java similarity index 83% rename from src/main/java/ru/ldeloff/servermonitorbot/utils/ui/uname/UnameChatButtonAggregate.java rename to src/main/java/ru/ldeloff/servermonitorbot/utils/ui/uname/ServerListButtons.java index 99a9547..c6d0b4e 100644 --- a/src/main/java/ru/ldeloff/servermonitorbot/utils/ui/uname/UnameChatButtonAggregate.java +++ b/src/main/java/ru/ldeloff/servermonitorbot/utils/ui/uname/ServerListButtons.java @@ -1,12 +1,10 @@ package ru.ldeloff.servermonitorbot.utils.ui.uname; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup; import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton; -import ru.ldeloff.servermonitorbot.repository.SshRepository; import ru.ldeloff.servermonitorbot.service.ssh.SshService; import ru.ldeloff.servermonitorbot.utils.ui.UiFormer; @@ -16,7 +14,7 @@ import java.util.Objects; @Component @RequiredArgsConstructor -public class UnameChatButtonAggregate implements UiFormer { +public class ServerListButtons implements UiFormer { private final SshService sshService; @@ -32,7 +30,7 @@ public class UnameChatButtonAggregate implements UiFormer { List keyboardRow = new ArrayList<>(); InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText(sshServer.getName()); - inlineKeyboardButton.setCallbackData("uname: " + sshServer.getName()); + inlineKeyboardButton.setCallbackData(message.getText() + ": " + sshServer.getName()); keyboardRow.add(inlineKeyboardButton); keyboard.add(keyboardRow); } @@ -43,14 +41,14 @@ public class UnameChatButtonAggregate implements UiFormer { List keyboardRow = new ArrayList<>(); InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText("Все сервера"); - inlineKeyboardButton.setCallbackData("uname: all"); + inlineKeyboardButton.setCallbackData(message.getText() + ": all"); keyboardRow.add(inlineKeyboardButton); keyboard.add(keyboardRow); } inlineKeyboardMarkup.setKeyboard(keyboard); message.setReplyMarkup(inlineKeyboardMarkup); - message.setText("Выполнить uname для"); + message.setText("Выполнить " + message.getText() + " для"); return message; } -- 2.40.1 From c42781c5ee8db1fb94927b96cf30114b01d35b27 Mon Sep 17 00:00:00 2001 From: L_DelOff Date: Sun, 13 Aug 2023 21:00:43 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BA=D0=BD=D0=BE=D0=BF=D0=BA=D1=83=20=D1=81=D0=BB=D0=B5?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B7=D0=B0=20=D1=82=D0=B5?= =?UTF-8?q?=D0=BC=D0=BF=D0=B5=D1=80=D0=B0=D1=82=D1=83=D1=80=D0=BE=D0=B9=20?= =?UTF-8?q?CPU?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TelegramBotController.java | 10 +++ .../cputemp/CpuTempAggregateCommand.java | 24 +++++++ .../command/cputemp/CpuTempCommand.java | 67 +++++++++++++++++++ .../utils/ui/TelegramBotKeyboard.java | 3 + 4 files changed, 104 insertions(+) create mode 100644 src/main/java/ru/ldeloff/servermonitorbot/service/command/cputemp/CpuTempAggregateCommand.java create mode 100644 src/main/java/ru/ldeloff/servermonitorbot/service/command/cputemp/CpuTempCommand.java 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); -- 2.40.1