Реализована команда uname
parent
fcd4929e0c
commit
b6147e9e91
|
@ -1,5 +1,6 @@
|
|||
package ru.ldeloff.servermonitorbot.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||
|
@ -14,6 +15,7 @@ import ru.ldeloff.servermonitorbot.utils.ui.uname.UnameChatButtonAggregate;
|
|||
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class TelegramBotController extends TelegramLongPollingBot {
|
||||
|
||||
final TelegramBot telegramBot;
|
||||
|
@ -22,14 +24,6 @@ public class TelegramBotController extends TelegramLongPollingBot {
|
|||
final UnameChatButtonAggregate unameChatButtonAggregate;
|
||||
final TelegramBotService telegramBotService;
|
||||
|
||||
public TelegramBotController(TelegramBot telegramBot, SshRepository sshRepository, TelegramBotKeyboard telegramBotKeyboard, UnameChatButtonAggregate unameChatButtonAggregate, TelegramBotService telegramBotService) {
|
||||
this.telegramBot = telegramBot;
|
||||
this.sshRepository = sshRepository;
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
this.unameChatButtonAggregate = unameChatButtonAggregate;
|
||||
this.telegramBotService = telegramBotService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdateReceived(Update update) {
|
||||
if (update.hasMessage()) {
|
||||
|
@ -38,7 +32,7 @@ public class TelegramBotController extends TelegramLongPollingBot {
|
|||
switch (messageText) {
|
||||
case "/start" -> {
|
||||
log.info("Получена команда /start");
|
||||
telegramBotService.switchToMainMenu(update, this);
|
||||
telegramBotService.firstUse(update, this);
|
||||
}
|
||||
case "Статус" -> {
|
||||
log.info("Получена команда 'Статус'");
|
||||
|
@ -57,26 +51,21 @@ public class TelegramBotController extends TelegramLongPollingBot {
|
|||
} else if (update.hasCallbackQuery()) {
|
||||
String messageText = update.getCallbackQuery().getData();
|
||||
String [] tags = messageText.split(":");
|
||||
if (tags.length > 1) {
|
||||
switch (tags[0]) {
|
||||
case "uname":
|
||||
if (tags.length > 1) {
|
||||
switch (tags[1]) {
|
||||
default -> {
|
||||
log.warn("Неверный формат команды:" + messageText);
|
||||
log.info("Получена команда '" + messageText + "'");
|
||||
telegramBotService.uname(update, this);
|
||||
break;
|
||||
default:
|
||||
log.warn("Неизвестная команда:" + messageText);
|
||||
telegramBotService.switchToMainMenu(update, this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.warn("Неверный формат команды:" + messageText);
|
||||
telegramBotService.switchToMainMenu(update, this);
|
||||
}
|
||||
break;
|
||||
default :
|
||||
log.warn("Неизвестная команда:" + messageText);
|
||||
telegramBotService.switchToMainMenu(update, this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service;
|
||||
|
||||
|
||||
public interface SshService {
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
package ru.ldeloff.servermonitorbot.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class SshServiceImpl implements SshService {
|
||||
|
||||
}
|
|
@ -8,4 +8,6 @@ public interface TelegramBotService {
|
|||
void switchToMainMenu(Update update, TelegramBotController bot);
|
||||
void getStatusSessions(Update update, TelegramBotController bot);
|
||||
void sendUnameAggregate(Update update, TelegramBotController bot);
|
||||
void uname(Update update, TelegramBotController bot);
|
||||
void firstUse(Update update, TelegramBotController telegramBotController);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package ru.ldeloff.servermonitorbot.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
|
@ -7,20 +8,28 @@ import org.telegram.telegrambots.meta.api.objects.Update;
|
|||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
import ru.ldeloff.servermonitorbot.controller.TelegramBotController;
|
||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.service.uname.UnameService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.TelegramBotKeyboard;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.uname.UnameChatButtonAggregate;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TelegramBotServiceImpl implements TelegramBotService {
|
||||
final TelegramBotKeyboard telegramBotKeyboard;
|
||||
final SshRepository sshRepository;
|
||||
final UnameChatButtonAggregate unameChatButtonAggregate;
|
||||
final UnameService unameService;
|
||||
final SshService sshService;
|
||||
|
||||
public TelegramBotServiceImpl(TelegramBotKeyboard telegramBotKeyboard, SshRepository sshRepository, UnameChatButtonAggregate unameChatButtonAggregate) {
|
||||
this.telegramBotKeyboard = telegramBotKeyboard;
|
||||
this.sshRepository = sshRepository;
|
||||
this.unameChatButtonAggregate = unameChatButtonAggregate;
|
||||
@Override
|
||||
public void firstUse(Update update, TelegramBotController bot) {
|
||||
SendMessage message = new SendMessage();
|
||||
long chatId = 0L;
|
||||
chatId = update.getMessage().getChatId();
|
||||
message.setText("Добро пожалвать " + update.getMessage().getChat().getUserName() + "!");
|
||||
message.setChatId(chatId);
|
||||
sendMessage(telegramBotKeyboard.uiForm(message), bot);
|
||||
}
|
||||
@Override
|
||||
public void switchToMainMenu(Update update, TelegramBotController bot) {
|
||||
|
@ -30,7 +39,6 @@ public class TelegramBotServiceImpl implements TelegramBotService {
|
|||
chatId = update.getMessage().getChatId();
|
||||
} catch (Exception e) {
|
||||
chatId = update.getCallbackQuery().getMessage().getChatId();
|
||||
|
||||
}
|
||||
message.setText("Неверная команда и или формат. Попробуем сначала.");
|
||||
message.setChatId(chatId);
|
||||
|
@ -38,20 +46,18 @@ public class TelegramBotServiceImpl implements TelegramBotService {
|
|||
}
|
||||
@Override
|
||||
public void getStatusSessions(Update update, TelegramBotController bot) {
|
||||
SendMessage message = new SendMessage();
|
||||
long chatId = update.getMessage().getChatId();
|
||||
message.setText(String.valueOf(sshRepository.getStatusSessions()));
|
||||
message.setChatId(chatId);
|
||||
sendMessage(message, bot);
|
||||
sendMessage(sshService.getStatusSessions(update), bot);
|
||||
}
|
||||
@Override
|
||||
public void sendUnameAggregate(Update update, TelegramBotController bot) {
|
||||
SendMessage message = new SendMessage();
|
||||
long chatId = update.getMessage().getChatId();
|
||||
message.setText(String.valueOf(sshRepository.getStatusSessions()));
|
||||
message.setChatId(chatId);
|
||||
sendMessage(unameChatButtonAggregate.uiForm(message), bot);
|
||||
sendMessage(unameService.sendUnameAggregate(update), bot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void uname(Update update, TelegramBotController bot) {
|
||||
sendMessage(unameService.uname(update), bot);
|
||||
}
|
||||
|
||||
private void sendMessage(SendMessage message, TelegramBotController bot) {
|
||||
try {
|
||||
bot.execute(message);
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
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.SshServer;
|
||||
|
||||
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);
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package ru.ldeloff.servermonitorbot.service.ssh;
|
||||
|
||||
import com.jcraft.jsch.ChannelExec;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.Session;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class SshServiceImpl implements SshService {
|
||||
final SshRepository sshRepository;
|
||||
@Override
|
||||
public SendMessage getStatusSessions(Update update) {
|
||||
SendMessage message = new SendMessage();
|
||||
long chatId = update.getMessage().getChatId();
|
||||
message.setText(sshRepository.getStatusSessions());
|
||||
message.setChatId(chatId);
|
||||
return message;
|
||||
}
|
||||
@Override
|
||||
public List<SshServer> getSshServers() {
|
||||
return sshRepository.getSshServers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String s, SshServer sshServer) {
|
||||
return execute(s, sshServer, 100);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(String s, SshServer sshServer, int timeout) {
|
||||
Session session = sshServer.getSession();
|
||||
ChannelExec channel = null;
|
||||
try {
|
||||
channel = (ChannelExec) session.openChannel("exec");
|
||||
channel.setCommand(s);
|
||||
ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
|
||||
channel.setOutputStream(responseStream);
|
||||
channel.connect();
|
||||
|
||||
while (channel.isConnected()) {
|
||||
Thread.sleep(timeout);
|
||||
}
|
||||
|
||||
String responseString = new String(responseStream.toByteArray());
|
||||
return responseString;
|
||||
} catch (JSchException | InterruptedException e) {
|
||||
log.error(e.getMessage());
|
||||
return null;
|
||||
} finally {
|
||||
if (channel != null) {
|
||||
channel.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package ru.ldeloff.servermonitorbot.service.uname;
|
||||
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
|
||||
public interface UnameService {
|
||||
SendMessage sendUnameAggregate(Update update);
|
||||
|
||||
SendMessage uname(Update update);
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package ru.ldeloff.servermonitorbot.service.uname;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.uname.UnameChatButtonAggregate;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class UnameServiceImpl implements UnameService {
|
||||
final SshService sshService;
|
||||
final UnameChatButtonAggregate unameChatButtonAggregate;
|
||||
@Override
|
||||
public SendMessage sendUnameAggregate(Update update) {
|
||||
SendMessage message = new SendMessage();
|
||||
long chatId = update.getMessage().getChatId();
|
||||
message.setChatId(chatId);
|
||||
return unameChatButtonAggregate.uiForm(message);
|
||||
}
|
||||
@Override
|
||||
public SendMessage uname(Update update) {
|
||||
String messageText = update.getCallbackQuery().getData();
|
||||
|
||||
String [] tags = Arrays.stream(messageText.split(":"))
|
||||
.map(String::trim)
|
||||
.toArray(String[]::new);
|
||||
|
||||
String result;
|
||||
if (tags[1].equals("all")) {
|
||||
result = unameAllHost();
|
||||
} else {
|
||||
result = unameSpecificHost(tags[1]);
|
||||
}
|
||||
SendMessage message = new SendMessage();
|
||||
long chatId = update.getCallbackQuery().getMessage().getChatId();
|
||||
message.setChatId(chatId);
|
||||
message.setText(result);
|
||||
return message;
|
||||
}
|
||||
|
||||
private String unameAllHost() {
|
||||
List<SshServer> servers = sshService.getSshServers();
|
||||
StringBuilder response = new StringBuilder();
|
||||
servers.forEach(server -> {
|
||||
response.append(unameSpecificHost(server.getName())).append("\n");
|
||||
});
|
||||
return response.toString();
|
||||
}
|
||||
|
||||
private String unameSpecificHost(String serverName) {
|
||||
Optional<SshServer> server = sshService.getSshServers()
|
||||
.stream()
|
||||
.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);
|
||||
|
||||
} else {
|
||||
log.error("Ошибка при выполнении команды 'uname'. Искомый сервер (" + serverName + ") не найден");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,25 +1,23 @@
|
|||
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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class UnameChatButtonAggregate implements UiFormer {
|
||||
|
||||
private final SshRepository sshRepository;
|
||||
|
||||
@Autowired
|
||||
public UnameChatButtonAggregate(SshRepository sshRepository) {
|
||||
this.sshRepository = sshRepository;
|
||||
}
|
||||
private final SshService sshService;
|
||||
|
||||
@Override
|
||||
public SendMessage uiForm(SendMessage message) {
|
||||
|
@ -27,7 +25,7 @@ public class UnameChatButtonAggregate implements UiFormer {
|
|||
|
||||
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
|
||||
|
||||
sshRepository.getSshServers().forEach(sshServer -> {
|
||||
sshService.getSshServers().forEach(sshServer -> {
|
||||
List<InlineKeyboardButton> keyboardRow = new ArrayList<>();
|
||||
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
|
||||
inlineKeyboardButton.setText(sshServer.getName());
|
||||
|
@ -44,6 +42,7 @@ public class UnameChatButtonAggregate implements UiFormer {
|
|||
|
||||
inlineKeyboardMarkup.setKeyboard(keyboard);
|
||||
message.setReplyMarkup(inlineKeyboardMarkup);
|
||||
message.setText("Выполнить uname для");
|
||||
|
||||
return message;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue