Рефактор
parent
39c2d6e6fc
commit
c3711bb571
|
@ -5,8 +5,10 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import ru.ldeloff.servermonitorbot.model.Command;
|
||||
import ru.ldeloff.servermonitorbot.model.TelegramBot;
|
||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.command.FirstUseCommand;
|
||||
import ru.ldeloff.servermonitorbot.service.command.GetStatusSessions;
|
||||
import ru.ldeloff.servermonitorbot.service.command.SwitchToMainMenu;
|
||||
|
@ -19,13 +21,15 @@ import ru.ldeloff.servermonitorbot.service.command.mdadm.MdadmStatusCommand;
|
|||
import ru.ldeloff.servermonitorbot.service.command.uname.UnameAggregateCommand;
|
||||
import ru.ldeloff.servermonitorbot.service.command.uname.UnameCommand;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class TelegramBotController extends TelegramLongPollingBot {
|
||||
//private final Map<String, CommandTemplate> commands;
|
||||
final TelegramBot telegramBot;
|
||||
final SshRepository sshRepository;
|
||||
|
||||
final FirstUseCommand firstUseCommand;
|
||||
final GetStatusSessions getStatusSessions;
|
||||
final UnameAggregateCommand unameAggregateCommand;
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package ru.ldeloff.servermonitorbot.init;
|
||||
|
||||
public class AddCommands {
|
||||
public static void fillCommandTable() {
|
||||
|
||||
}
|
||||
}
|
|
@ -15,6 +15,8 @@ import ru.ldeloff.servermonitorbot.model.User;
|
|||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
|
||||
import static ru.ldeloff.servermonitorbot.init.AddCommands.fillCommandTable;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@NoArgsConstructor
|
||||
|
@ -31,12 +33,12 @@ public class StartBot implements ApplicationRunner {
|
|||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
try {
|
||||
fillCommandTable();
|
||||
TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
|
||||
botsApi.registerBot(telegramBot);
|
||||
userService.getAllUsers().forEach(this::SendInitMessage);
|
||||
log.info("Бот запущен");
|
||||
} catch (TelegramApiException e) {
|
||||
//e.printStackTrace();
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,25 +1,19 @@
|
|||
package ru.ldeloff.servermonitorbot.model;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Data;
|
||||
@Entity
|
||||
@Data
|
||||
@Table(name = "commands")
|
||||
public class Command {
|
||||
@NotNull
|
||||
private String command;
|
||||
@NotNull
|
||||
@Min(100)
|
||||
private int timeout;
|
||||
@NotNull
|
||||
private SshServer sshServer;
|
||||
private String response;
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
public Command(String command, int timeout, SshServer sshServer) {
|
||||
this.command = command;
|
||||
this.timeout = timeout;
|
||||
this.sshServer = sshServer;
|
||||
}
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@Column(name = "level")
|
||||
private int level;
|
||||
}
|
||||
|
|
|
@ -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 SshCommand {
|
||||
@NotNull
|
||||
private String command;
|
||||
@NotNull
|
||||
@Min(100)
|
||||
private int timeout;
|
||||
@NotNull
|
||||
private SshServer sshServer;
|
||||
private String response;
|
||||
|
||||
public SshCommand(String command, int timeout, SshServer sshServer) {
|
||||
this.command = command;
|
||||
this.timeout = timeout;
|
||||
this.sshServer = sshServer;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package ru.ldeloff.servermonitorbot.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.ldeloff.servermonitorbot.model.Command;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface CommandRepository extends JpaRepository<Command, Long> {
|
||||
Optional<Command> findCommandByName(String name);
|
||||
Optional<Command> findCommandById(Long id);
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
public interface CommandService {
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package ru.ldeloff.servermonitorbot.service.command;
|
||||
|
||||
import org.jvnet.hk2.annotations.Service;
|
||||
|
||||
@Service
|
||||
public class CommandServiceImpl implements CommandService {
|
||||
|
||||
}
|
|
@ -6,7 +6,7 @@ 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;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
@Service
|
||||
public class CpuTempAggregateCommand extends CommandTemplate {
|
||||
|
|
|
@ -3,7 +3,7 @@ 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.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
|
@ -53,7 +53,7 @@ public class CpuTempCommand extends CommandTemplate {
|
|||
.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()));
|
||||
SshCommand result = sshService.execute(new SshCommand("cat /sys/class/thermal/thermal_zone0/temp", 100, server.get()));
|
||||
return server.get().getName() + ": \n" +
|
||||
(Objects.isNull(result.getResponse()) ? "ошибка при выполнении команды" :
|
||||
String.format("%.1f",
|
||||
|
|
|
@ -6,7 +6,7 @@ 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;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
@Service
|
||||
public class HddTempAggregateCommand extends CommandTemplate {
|
||||
|
|
|
@ -3,7 +3,7 @@ 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.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
|
@ -53,7 +53,7 @@ public class HddTempCommand extends CommandTemplate {
|
|||
.filter(x -> x.getName().equals(serverName))
|
||||
.findFirst();
|
||||
if (server.isPresent()) {
|
||||
Command result = sshService.execute(new Command("hddtemp /dev/sd*", 500, server.get()));
|
||||
SshCommand result = sshService.execute(new SshCommand("hddtemp /dev/sd*", 500, server.get()));
|
||||
return server.get().getName() + ": \n" +
|
||||
(Objects.isNull(result.getResponse()) ? "ошибка при выполнении команды" : result.getResponse());
|
||||
} else {
|
||||
|
|
|
@ -6,7 +6,7 @@ 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;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
@Service
|
||||
public class MdadmStatusAggregateCommand extends CommandTemplate {
|
||||
|
|
|
@ -3,7 +3,7 @@ package ru.ldeloff.servermonitorbot.service.command.mdadm;
|
|||
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.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
|
@ -13,9 +13,6 @@ import ru.ldeloff.servermonitorbot.service.user.UserService;
|
|||
import ru.ldeloff.servermonitorbot.utils.SshServerUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
|
@ -56,14 +53,14 @@ public class MdadmStatusCommand extends CommandTemplate {
|
|||
.findFirst();
|
||||
if (server.isPresent()) {
|
||||
// узнаю скок рейдов
|
||||
Command commandRaid = sshService.execute(new Command("mdadm --detail --scan", 500, server.get()));
|
||||
List<String> raidNames = parseRaidNames(commandRaid);
|
||||
SshCommand sshCommandRaid = sshService.execute(new SshCommand("mdadm --detail --scan", 500, server.get()));
|
||||
List<String> raidNames = parseRaidNames(sshCommandRaid);
|
||||
// Для каждого рейда надо выяснить статус
|
||||
StringBuilder result = new StringBuilder();
|
||||
raidNames.forEach(raid -> {
|
||||
result.append(raid).append(":\n");
|
||||
Command commandHdd = sshService.execute(new Command("mdadm --detail " + raid, 500, server.get()));
|
||||
List<String> hdds = parseHddState(commandHdd);
|
||||
SshCommand sshCommandHdd = sshService.execute(new SshCommand("mdadm --detail " + raid, 500, server.get()));
|
||||
List<String> hdds = parseHddState(sshCommandHdd);
|
||||
hdds.forEach(hdd -> result.append(hdd).append("\n"));
|
||||
});
|
||||
return server.get().getName() + ": \n" + result;
|
||||
|
@ -72,7 +69,7 @@ public class MdadmStatusCommand extends CommandTemplate {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
private List<String> parseRaidNames(Command result) {
|
||||
private List<String> parseRaidNames(SshCommand result) {
|
||||
List<String> names = new ArrayList<>();
|
||||
Arrays.stream(result.getResponse().split("\n"))
|
||||
.map(line -> line.split(" "))
|
||||
|
@ -82,7 +79,7 @@ public class MdadmStatusCommand extends CommandTemplate {
|
|||
return names;
|
||||
}
|
||||
|
||||
private List<String> parseHddState(Command result) {
|
||||
private List<String> parseHddState(SshCommand result) {
|
||||
List<String> names = new ArrayList<>();
|
||||
names.add("Number Name State");
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ 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;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.ServerListButtons;
|
||||
|
||||
@Service
|
||||
public class UnameAggregateCommand extends CommandTemplate {
|
||||
|
|
|
@ -3,7 +3,7 @@ package ru.ldeloff.servermonitorbot.service.command.uname;
|
|||
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.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.command.CommandTemplate;
|
||||
|
@ -52,7 +52,7 @@ public class UnameCommand extends CommandTemplate {
|
|||
.filter(x -> x.getName().equals(serverName))
|
||||
.findFirst();
|
||||
if (server.isPresent()) {
|
||||
Command result = sshService.execute(new Command("uname -a", 100, server.get()));
|
||||
SshCommand result = sshService.execute(new SshCommand("uname -a", 100, server.get()));
|
||||
return server.get().getName() + ": " +
|
||||
(Objects.isNull(result.getResponse()) ? "ошибка при выполнении команды" : result.getResponse());
|
||||
} else {
|
||||
|
|
|
@ -2,7 +2,7 @@ package ru.ldeloff.servermonitorbot.service.ssh;
|
|||
|
||||
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.Command;
|
||||
import ru.ldeloff.servermonitorbot.model.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -11,6 +11,6 @@ public interface SshService {
|
|||
SendMessage getStatusSessions(SendMessage update);
|
||||
|
||||
List<SshServer> getSshServers();
|
||||
Command execute(Command command);
|
||||
List<Command> execute(List<Command> commands);
|
||||
SshCommand execute(SshCommand sshCommand);
|
||||
List<SshCommand> execute(List<SshCommand> sshCommands);
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import lombok.RequiredArgsConstructor;
|
|||
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.SshCommand;
|
||||
import ru.ldeloff.servermonitorbot.model.SshServer;
|
||||
import ru.ldeloff.servermonitorbot.repository.SshRepository;
|
||||
|
||||
|
@ -31,21 +31,21 @@ public class SshServiceImpl implements SshService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Command execute(Command command) {
|
||||
List<Command> commands = new ArrayList<>();
|
||||
commands.add(command);
|
||||
return execute(commands).get(0);
|
||||
public SshCommand execute(SshCommand sshCommand) {
|
||||
List<SshCommand> sshCommands = new ArrayList<>();
|
||||
sshCommands.add(sshCommand);
|
||||
return execute(sshCommands).get(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Command> execute(List<Command> commands) {
|
||||
Session session = commands.get(0).getSshServer().getSession(); // подразумевается, что в листе с командами сервер один и тот же
|
||||
public List<SshCommand> execute(List<SshCommand> sshCommands) {
|
||||
Session session = sshCommands.get(0).getSshServer().getSession(); // подразумевается, что в листе с командами сервер один и тот же
|
||||
ChannelExec channel = null;
|
||||
try {
|
||||
channel = (ChannelExec) session.openChannel("exec");
|
||||
ChannelExec finalChannel = channel;
|
||||
|
||||
commands.forEach(command -> {
|
||||
sshCommands.forEach(command -> {
|
||||
try {
|
||||
finalChannel.setCommand(command.getCommand());
|
||||
ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
|
||||
|
@ -69,6 +69,6 @@ public class SshServiceImpl implements SshService {
|
|||
channel.disconnect();
|
||||
}
|
||||
}
|
||||
return commands;
|
||||
return sshCommands;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package ru.ldeloff.servermonitorbot.utils;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import ru.ldeloff.servermonitorbot.model.Role;
|
||||
import ru.ldeloff.servermonitorbot.model.User;
|
||||
import ru.ldeloff.servermonitorbot.service.user.UserService;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Component
|
||||
public class RoleChecker {
|
||||
final UserService userService;
|
||||
public Role getRole(SendMessage sendMessage) {
|
||||
String chatId = sendMessage.getChatId();
|
||||
User user = userService.getByTelegramId(Long.parseLong(chatId));
|
||||
return user.getRole();
|
||||
}
|
||||
}
|
|
@ -1,11 +1,13 @@
|
|||
package ru.ldeloff.servermonitorbot.utils.ui.uname;
|
||||
package ru.ldeloff.servermonitorbot.utils.ui;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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.model.Role;
|
||||
import ru.ldeloff.servermonitorbot.service.ssh.SshService;
|
||||
import ru.ldeloff.servermonitorbot.utils.RoleChecker;
|
||||
import ru.ldeloff.servermonitorbot.utils.ui.UiFormer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -21,7 +23,6 @@ public class ServerListButtons implements UiFormer {
|
|||
@Override
|
||||
public SendMessage uiForm(SendMessage message) {
|
||||
List<List<InlineKeyboardButton>> keyboard = new ArrayList<>();
|
||||
|
||||
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
|
||||
|
||||
sshService.getSshServers().forEach(sshServer -> {
|
|
@ -1,25 +1,33 @@
|
|||
package ru.ldeloff.servermonitorbot.utils.ui;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton;
|
||||
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
|
||||
import ru.ldeloff.servermonitorbot.model.Role;
|
||||
import ru.ldeloff.servermonitorbot.utils.RoleChecker;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
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";
|
||||
private final String MDADM = "MDADM";
|
||||
private final RoleChecker roleChecker;
|
||||
|
||||
|
||||
@Override
|
||||
public SendMessage uiForm(SendMessage message) {
|
||||
|
||||
Role role = roleChecker.getRole(message);
|
||||
|
||||
ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
|
||||
replyKeyboardMarkup.setResizeKeyboard(true);
|
||||
replyKeyboardMarkup.setOneTimeKeyboard(false);
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
CREATE TABLE commands
|
||||
(
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(128),
|
||||
level integer
|
||||
);
|
||||
|
||||
COMMENT ON TABLE commands IS 'Команды';
|
||||
COMMENT ON COLUMN commands.id IS 'ID';
|
||||
COMMENT ON COLUMN commands.name IS 'Имя команды';
|
||||
COMMENT ON COLUMN commands.level IS 'Уровень доступа';
|
Loading…
Reference in New Issue