Добавил кнопку слежения за температурой CPU

pull/29/head
L_DelOff 2023-08-13 21:00:43 +03:00
parent daf9e9cb06
commit c42781c5ee
4 changed files with 104 additions and 0 deletions

View File

@ -10,6 +10,8 @@ import ru.ldeloff.servermonitorbot.repository.SshRepository;
import ru.ldeloff.servermonitorbot.service.command.FirstUseCommand; import ru.ldeloff.servermonitorbot.service.command.FirstUseCommand;
import ru.ldeloff.servermonitorbot.service.command.GetStatusSessions; import ru.ldeloff.servermonitorbot.service.command.GetStatusSessions;
import ru.ldeloff.servermonitorbot.service.command.SwitchToMainMenu; import ru.ldeloff.servermonitorbot.service.command.SwitchToMainMenu;
import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempAggregateCommand;
import ru.ldeloff.servermonitorbot.service.command.cputemp.CpuTempCommand;
import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempAggregateCommand; import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempAggregateCommand;
import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempCommand; import ru.ldeloff.servermonitorbot.service.command.hddtemp.HddTempCommand;
import ru.ldeloff.servermonitorbot.service.command.uname.UnameAggregateCommand; import ru.ldeloff.servermonitorbot.service.command.uname.UnameAggregateCommand;
@ -27,6 +29,8 @@ public class TelegramBotController extends TelegramLongPollingBot {
final UnameAggregateCommand unameAggregateCommand; final UnameAggregateCommand unameAggregateCommand;
final SwitchToMainMenu switchToMainMenu; final SwitchToMainMenu switchToMainMenu;
final UnameCommand unameCommand; final UnameCommand unameCommand;
final CpuTempAggregateCommand cpuTempAggregateCommand;
final CpuTempCommand cpuTempCommand;
final HddTempAggregateCommand hddTempAggregateCommand; final HddTempAggregateCommand hddTempAggregateCommand;
final HddTempCommand hddTempCommand; final HddTempCommand hddTempCommand;
@ -45,6 +49,9 @@ public class TelegramBotController extends TelegramLongPollingBot {
case "uname" -> { case "uname" -> {
unameAggregateCommand.execute(update, this); unameAggregateCommand.execute(update, this);
} }
case "CPU.temp" -> {
cpuTempAggregateCommand.execute(update, this);
}
case "HDD.temp" -> { case "HDD.temp" -> {
hddTempAggregateCommand.execute(update, this); hddTempAggregateCommand.execute(update, this);
} }
@ -60,6 +67,9 @@ public class TelegramBotController extends TelegramLongPollingBot {
case "uname" -> { case "uname" -> {
unameCommand.execute(update, this); unameCommand.execute(update, this);
} }
case "CPU.temp" -> {
cpuTempCommand.execute(update, this);
}
case "HDD.temp" -> { case "HDD.temp" -> {
hddTempCommand.execute(update, this); hddTempCommand.execute(update, this);
} }

View File

@ -0,0 +1,24 @@
package ru.ldeloff.servermonitorbot.service.command.cputemp;
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 CpuTempAggregateCommand extends CommandTemplate {
final ServerListButtons serverListButtons;
public CpuTempAggregateCommand(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,67 @@
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.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.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@Service
@Slf4j
public class CpuTempCommand extends CommandTemplate {
final SshService sshService;
public CpuTempCommand(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(hddTempAllHost());
} else {
message.setText(Objects.requireNonNull(hddTempSpecificHost(tags[1])));
}
return message;
}
private String hddTempAllHost() {
List<SshServer> servers = SshServerUtils.filterGoodServers(sshService.getSshServers());
StringBuilder response = new StringBuilder();
servers.forEach(server -> response.append(hddTempSpecificHost(server.getName())).append("\n"));
return response.toString();
}
private String hddTempSpecificHost(String serverName) {
Optional<SshServer> server = sshService.getSshServers()
.stream()
.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()));
return server.get().getName() + ": \n" +
(Objects.isNull(result.getResponse()) ? "ошибка при выполнении команды" :
String.format("%.1f",
Double.parseDouble(result.getResponse().replaceAll("\n", ""))/1000))
+ "°C";
} else {
log.error("Ошибка при выполнении команды 'CPUtemp'. Искомый сервер (" + serverName + ") не найден");
return null;
}
}
}

View File

@ -13,8 +13,10 @@ import java.util.ArrayList;
public class TelegramBotKeyboard implements UiFormer { public class TelegramBotKeyboard implements UiFormer {
private final String STATUS = "Статус"; private final String STATUS = "Статус";
private final String UNAME = "uname"; private final String UNAME = "uname";
private final String CPU_TEMP = "CPU.temp";
private final String HDD_TEMP = "HDD.temp"; private final String HDD_TEMP = "HDD.temp";
@Override @Override
public SendMessage uiForm(SendMessage message) { public SendMessage uiForm(SendMessage message) {
ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup(); ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
@ -28,6 +30,7 @@ public class TelegramBotKeyboard implements UiFormer {
keyboardRow.add(new KeyboardButton(STATUS)); keyboardRow.add(new KeyboardButton(STATUS));
keyboardRow.add(new KeyboardButton(UNAME)); keyboardRow.add(new KeyboardButton(UNAME));
keyboardRow.add(new KeyboardButton(CPU_TEMP));
keyboardRow.add(new KeyboardButton(HDD_TEMP)); keyboardRow.add(new KeyboardButton(HDD_TEMP));
replyKeyboardMarkup.setKeyboard(keyboardRows); replyKeyboardMarkup.setKeyboard(keyboardRows);