feature/task-11-mdadm_status #35

Merged
L_DelOff merged 2 commits from feature/task-11-mdadm_status into master 2023-11-22 20:10:08 +03:00
5 changed files with 143 additions and 0 deletions

View File

@ -14,6 +14,8 @@ import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempAggregateComma
import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempCommand;
import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempAggregateCommand;
import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempCommand;
import ru.ldeloff.servermonitorbot.service.command.mdadm.MdadmStatusAggregateCommand;
import ru.ldeloff.servermonitorbot.service.command.mdadm.MdadmStatusCommand;
import ru.ldeloff.servermonitorbot.service.command.uname.UnameAggregateCommand;
import ru.ldeloff.servermonitorbot.service.command.uname.UnameCommand;
@ -33,6 +35,8 @@ public class TelegramBotController extends TelegramLongPollingBot {
final CpuTempCommand cpuTempCommand;
final HddTempAggregateCommand hddTempAggregateCommand;
final HddTempCommand hddTempCommand;
final MdadmStatusAggregateCommand mdadmStatusAggregateCommand;
final MdadmStatusCommand mdadmStatusCommand;
@Override
public void onUpdateReceived(Update update) {
@ -55,6 +59,9 @@ public class TelegramBotController extends TelegramLongPollingBot {
case "HDD.temp" -> {
hddTempAggregateCommand.execute(update, this);
}
case "MDADM" -> {
mdadmStatusAggregateCommand.execute(update,this);
}
default -> {
switchToMainMenu.execute(update, this);
}
@ -73,6 +80,9 @@ public class TelegramBotController extends TelegramLongPollingBot {
case "HDD.temp" -> {
hddTempCommand.execute(update, this);
}
case "MDADM" -> {
mdadmStatusCommand.execute(update,this);
}
default -> {
switchToMainMenu.execute(update, this);
}

View File

@ -0,0 +1,25 @@
package ru.ldeloff.servermonitorbot.service.command.mdadm;
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 MdadmStatusAggregateCommand extends CommandTemplate {
final ServerListButtons serverListButtons;
public MdadmStatusAggregateCommand(UserService userService, RoleService roleService, ServerListButtons serverListButtons) {
super(userService, roleService);
this.serverListButtons = serverListButtons;
}
@Override
public SendMessage actionForAuth(User user, SendMessage message) {
return serverListButtons.uiForm(message);
}
}

View File

@ -0,0 +1,100 @@
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.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.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service
@Slf4j
public class MdadmStatusCommand extends CommandTemplate {
final SshService sshService;
public MdadmStatusCommand(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(mdadmStatusAllHost());
} else {
message.setText(Objects.requireNonNull(mdadmStatusSpecificHost(tags[1])));
}
return message;
}
private String mdadmStatusAllHost() {
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
StringBuilder response = new StringBuilder();
servers.forEach(server -> response.append(mdadmStatusSpecificHost(server.getName())).append("\n"));
return response.toString();
}
private String mdadmStatusSpecificHost(String serverName) {
Optional<SshServer> server = sshService.getSshServers()
.stream()
.filter(x -> x.getName().equals(serverName))
.findFirst();
if (server.isPresent()) {
// узнаю скок рейдов
Command commandRaid = sshService.execute(new Command("mdadm --monitor", 500, server.get()));
List<String> raidNames = parseRaidNames(commandRaid);
// Для каждого рейда надо выяснить статус
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);
hdds.forEach(hdd -> result.append(hdd).append("\n"));
});
return server.get().getName() + ": \n" + result;
} else {
log.error("Ошибка при выполнении команды 'mdadm'. Искомый сервер (" + serverName + ") не найден");
return null;
}
}
private List<String> parseRaidNames(Command result) {
String raidInfo = result.getResponse();
List<String> names = new ArrayList<>();
Arrays.stream(raidInfo.split("\n"))
.map(line -> line.split(" "))
.filter(lineArray -> lineArray.length >= 2)
.map(lineArray -> lineArray[1])
.forEach(names::add);
return names;
}
private List<String> parseHddState(Command result) {
String raidInfo = result.getResponse();
List<String> names = new ArrayList<>();
Pattern pattern = Pattern.compile("^\\s*(\\d+)\\s+\\d+\\s+\\d+\\s+\\d+\\s+\\w+\\s+(.+)$");
Matcher matcher = pattern.matcher(raidInfo);
while (matcher.find()) {
String number = matcher.group(1);
String state = matcher.group(2);
names.add("Number: " + number + ", State: " + state);
}
return names;
}
}

View File

@ -15,6 +15,7 @@ public class TelegramBotKeyboard implements UiFormer {
private final String UNAME = "uname";
private final String CPU_TEMP = "CPU.temp";
private final String HDD_TEMP = "HDD.temp";
private final String MDADM = "MDADM";
@Override
@ -32,6 +33,7 @@ public class TelegramBotKeyboard implements UiFormer {
keyboardRow.add(new KeyboardButton(UNAME));
keyboardRow.add(new KeyboardButton(CPU_TEMP));
keyboardRow.add(new KeyboardButton(HDD_TEMP));
keyboardRow.add(new KeyboardButton(MDADM));
replyKeyboardMarkup.setKeyboard(keyboardRows);
message.setReplyMarkup(replyKeyboardMarkup);

View File

@ -24,6 +24,12 @@ ssh:
port: 22
user: "user"
password: "pass"
-
name: "Server Name 2"
host: "url 2"
port: 22
user: "user 2"
password: "pass 2"