Добавил кнопку слежения за температурой дисков
parent
e75c496ea8
commit
d0be1f0a62
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<SshServer> 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<SshServer> 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<InlineKeyboardButton> 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<InlineKeyboardButton> 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;
|
||||
}
|
Loading…
Reference in New Issue