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