Фикс багов
parent
0cfdd37702
commit
619996b913
|
@ -31,19 +31,23 @@ public class TelegramBotController extends TelegramLongPollingBot {
|
|||
String messageText = update.getMessage().getText();
|
||||
switch (messageText) {
|
||||
case "/start" -> {
|
||||
log.info("Получена команда /start");
|
||||
log.info("Получена команда '" + messageText
|
||||
+ "' от " + update.getMessage().getChat().getUserName());
|
||||
telegramBotService.firstUse(update, this);
|
||||
}
|
||||
case "Статус" -> {
|
||||
log.info("Получена команда 'Статус'");
|
||||
log.info("Получена команда '" + messageText
|
||||
+ "' от " + update.getMessage().getChat().getUserName());
|
||||
telegramBotService.getStatusSessions(update, this);
|
||||
}
|
||||
case "uname" -> {
|
||||
log.info("Получена команда 'uname'");
|
||||
log.info("Получена команда '" + messageText
|
||||
+ "' от " + update.getMessage().getChat().getUserName());
|
||||
telegramBotService.sendUnameAggregate(update, this);
|
||||
}
|
||||
default -> {
|
||||
log.warn("Неизвестная команда:" + messageText);
|
||||
log.warn("Неизвестная команда '" + messageText
|
||||
+ "' от " + update.getMessage().getChat().getUserName());
|
||||
telegramBotService.switchToMainMenu(update, this);
|
||||
}
|
||||
}
|
||||
|
@ -54,16 +58,19 @@ public class TelegramBotController extends TelegramLongPollingBot {
|
|||
if (tags.length > 1) {
|
||||
switch (tags[0]) {
|
||||
case "uname":
|
||||
log.info("Получена команда '" + messageText + "'");
|
||||
log.info("Получена команда '" + messageText
|
||||
+ "' от " + update.getCallbackQuery().getMessage().getChat().getUserName());
|
||||
telegramBotService.uname(update, this);
|
||||
break;
|
||||
default:
|
||||
log.warn("Неизвестная команда:" + messageText);
|
||||
log.warn("Неизвестная команда '" + messageText
|
||||
+ "' от " + update.getCallbackQuery().getMessage().getChat().getUserName());
|
||||
telegramBotService.switchToMainMenu(update, this);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
log.warn("Неверный формат команды:" + messageText);
|
||||
log.warn("Неверный формат команды '" + messageText
|
||||
+ "' от " + update.getCallbackQuery().getMessage().getChat().getUserName());
|
||||
telegramBotService.switchToMainMenu(update, this);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package ru.ldeloff.servermonitorbot.model;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class Command {
|
||||
@NotNull
|
||||
private String command;
|
||||
@NotNull
|
||||
@Min(100)
|
||||
private int timeout;
|
||||
@NotNull
|
||||
private SshServer sshServer;
|
||||
private String response;
|
||||
|
||||
public Command(String command, int timeout, SshServer sshServer) {
|
||||
this.command = command;
|
||||
this.timeout = timeout;
|
||||
this.sshServer = sshServer;
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@ import ru.ldeloff.servermonitorbot.model.SshServer;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
@Repository
|
||||
|
@ -65,7 +66,7 @@ public class SshRepositoryImpl implements SshRepository {
|
|||
sshServers.forEach(server -> {
|
||||
text.append(server.getName())
|
||||
.append(": ")
|
||||
.append(server.getSession().isConnected() ? "OK" : "отключён")
|
||||
.append(checkStatusServer(server))
|
||||
.append("\n");
|
||||
});
|
||||
return String.valueOf(text);
|
||||
|
@ -75,4 +76,13 @@ public class SshRepositoryImpl implements SshRepository {
|
|||
public List<SshServer> getSshServers() {
|
||||
return sshServers;
|
||||
}
|
||||
|
||||
private String checkStatusServer(SshServer server) {
|
||||
if (Objects.isNull(server.getSession())) {
|
||||
return "нет соединения";
|
||||
} else {
|
||||
return server.getSession().isConnected() ? "OK" : "отключён";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ public class TelegramBotServiceImpl implements TelegramBotService {
|
|||
SendMessage message = new SendMessage();
|
||||
long chatId = 0L;
|
||||
chatId = update.getMessage().getChatId();
|
||||
message.setText("Добро пожалвать " + update.getMessage().getChat().getUserName() + "!");
|
||||
message.setText("Добро пожаловать " + update.getMessage().getChat().getUserName() + "!");
|
||||
message.setChatId(chatId);
|
||||
sendMessage(telegramBotKeyboard.uiForm(message), bot);
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package ru.ldeloff.servermonitorbot.service.ssh;
|
|||
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import ru.ldeloff.servermonitorbot.model.Command;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -10,7 +11,6 @@ import java.util.List;
|
|||
public interface SshService {
|
||||
SendMessage getStatusSessions(Update update);
|
||||
List<SshServer> getSshServers();
|
||||
|
||||
String execute(String s, SshServer sshServer);
|
||||
String execute(String s, SshServer sshServer, int timeout);
|
||||
Command execute(Command command);
|
||||
List<Command> execute(List<Command> commands);
|
||||
}
|
||||
|
|
|
@ -8,10 +8,12 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import ru.ldeloff.servermonitorbot.model.Command;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
|
@ -33,34 +35,44 @@ public class SshServiceImpl implements SshService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String execute(String s, SshServer sshServer) {
|
||||
return execute(s, sshServer, 100);
|
||||
public Command execute(Command command) {
|
||||
List<Command> commands = new ArrayList<>();
|
||||
commands.add(command);
|
||||
return execute(commands).get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String s, SshServer sshServer, int timeout) {
|
||||
Session session = sshServer.getSession();
|
||||
public List<Command> execute(List<Command> commands) {
|
||||
Session session = commands.get(0).getSshServer().getSession(); // подразумевается, что в листе с командами сервер один и тот же
|
||||
ChannelExec channel = null;
|
||||
try {
|
||||
channel = (ChannelExec) session.openChannel("exec");
|
||||
channel.setCommand(s);
|
||||
ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
|
||||
channel.setOutputStream(responseStream);
|
||||
channel.connect();
|
||||
ChannelExec finalChannel = channel;
|
||||
|
||||
while (channel.isConnected()) {
|
||||
Thread.sleep(timeout);
|
||||
}
|
||||
|
||||
String responseString = new String(responseStream.toByteArray());
|
||||
return responseString;
|
||||
} catch (JSchException | InterruptedException e) {
|
||||
log.error(e.getMessage());
|
||||
return null;
|
||||
commands.forEach(command -> {
|
||||
try {
|
||||
finalChannel.setCommand(command.getCommand());
|
||||
ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
|
||||
finalChannel.setOutputStream(responseStream);
|
||||
finalChannel.connect();
|
||||
while (finalChannel.isConnected()) {
|
||||
Thread.sleep(command.getTimeout());
|
||||
}
|
||||
String responseString = new String(responseStream.toByteArray());
|
||||
command.setResponse(responseString);
|
||||
} catch (JSchException | InterruptedException e) {
|
||||
log.warn(e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
} catch (JSchException e) {
|
||||
log.warn(e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (channel != null) {
|
||||
channel.disconnect();
|
||||
}
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,10 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import ru.ldeloff.servermonitorbot.model.Command;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.utils.SshServerUtils;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.uname.UnameChatButtonAggregate;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
@ -49,7 +51,7 @@ public class UnameServiceImpl implements UnameService {
|
|||
}
|
||||
|
||||
private String unameAllHost() {
|
||||
List<SshServer> servers = sshService.getSshServers();
|
||||
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
|
||||
StringBuilder response = new StringBuilder();
|
||||
servers.forEach(server -> {
|
||||
response.append(unameSpecificHost(server.getName())).append("\n");
|
||||
|
@ -63,9 +65,9 @@ public class UnameServiceImpl implements UnameService {
|
|||
.filter(x -> x.getName().equals(serverName))
|
||||
.findFirst();
|
||||
if (server.isPresent()) {
|
||||
String result = sshService.execute("uname -a", server.get());
|
||||
return server.get().getName() + ": " + (Objects.isNull(result) ? "ошибка при выполнении команды" : result);
|
||||
|
||||
Command result = sshService.execute(new Command("uname -a", 100, server.get()));
|
||||
return server.get().getName() + ": " +
|
||||
(Objects.isNull(result.getResponse()) ? "ошибка при выполнении команды" : result.getResponse());
|
||||
} else {
|
||||
log.error("Ошибка при выполнении команды 'uname'. Искомый сервер (" + serverName + ") не найден");
|
||||
return null;
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package ru.ldeloff.servermonitorbot.utils;
|
||||
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class SshServerUtils {
|
||||
public static List<SshServer> filterGoodServers(List<SshServer> sshServers) {
|
||||
List<SshServer> goodSshServers = sshServers.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(server -> Objects.nonNull(server.getSession()))
|
||||
.filter(server -> server.getSession().isConnected())
|
||||
.toList();
|
||||
return goodSshServers;
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@ import ru.ldeloff.servermonitorbot.utils.ui.UiFormer;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
|
@ -26,15 +27,19 @@ public class UnameChatButtonAggregate implements UiFormer {
|
|||
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
|
||||
|
||||
sshService.getSshServers().forEach(sshServer -> {
|
||||
List<InlineKeyboardButton> keyboardRow = new ArrayList<>();
|
||||
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
|
||||
inlineKeyboardButton.setText(sshServer.getName());
|
||||
inlineKeyboardButton.setCallbackData("uname: " + sshServer.getName());
|
||||
keyboardRow.add(inlineKeyboardButton);
|
||||
keyboard.add(keyboardRow);
|
||||
if (Objects.nonNull(sshServer.getSession())) {
|
||||
if (sshServer.getSession().isConnected()) {
|
||||
List<InlineKeyboardButton> keyboardRow = new ArrayList<>();
|
||||
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
|
||||
inlineKeyboardButton.setText(sshServer.getName());
|
||||
inlineKeyboardButton.setCallbackData("uname: " + sshServer.getName());
|
||||
keyboardRow.add(inlineKeyboardButton);
|
||||
keyboard.add(keyboardRow);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (sshService.getSshServers().size() > 1) {
|
||||
if (keyboard.size() > 1) {
|
||||
List<InlineKeyboardButton> keyboardRow = new ArrayList<>();
|
||||
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
|
||||
inlineKeyboardButton.setText("Все сервера");
|
||||
|
|
Loading…
Reference in New Issue