297 lines
13 KiB
Java
297 lines
13 KiB
Java
package de.gurkengewuerz.monitoring;
|
|
|
|
import com.googlecode.lanterna.TerminalSize;
|
|
import com.googlecode.lanterna.TextColor;
|
|
import com.googlecode.lanterna.gui2.*;
|
|
import com.googlecode.lanterna.gui2.dialogs.ActionListDialogBuilder;
|
|
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
|
|
import com.googlecode.lanterna.gui2.dialogs.MessageDialogButton;
|
|
import com.googlecode.lanterna.gui2.dialogs.TextInputDialog;
|
|
import com.googlecode.lanterna.gui2.table.Table;
|
|
import com.googlecode.lanterna.screen.Screen;
|
|
import com.googlecode.lanterna.screen.TerminalScreen;
|
|
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
|
|
import com.googlecode.lanterna.terminal.Terminal;
|
|
import com.googlecode.lanterna.terminal.swing.SwingTerminalFrame;
|
|
import de.gurkengewuerz.monitoring.object.MassCommand;
|
|
import de.gurkengewuerz.monitoring.object.Server;
|
|
import de.gurkengewuerz.monitoring.object.ServerStatus;
|
|
import org.apache.commons.validator.routines.InetAddressValidator;
|
|
import org.json.JSONObject;
|
|
import org.pmw.tinylog.Logger;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.event.WindowAdapter;
|
|
import java.awt.event.WindowEvent;
|
|
import java.io.IOException;
|
|
import java.math.BigDecimal;
|
|
import java.math.RoundingMode;
|
|
import java.net.URI;
|
|
import java.net.URISyntaxException;
|
|
import java.net.URL;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Created by gurkengewuerz.de on 21.10.2017.
|
|
*/
|
|
public class GUI {
|
|
|
|
private HashMap<String, Server> serverlist;
|
|
|
|
public GUI() {
|
|
serverlist = Server.getServerList(StartUp.getDb());
|
|
Poller poller = new Poller();
|
|
try {
|
|
Terminal terminal = new DefaultTerminalFactory()
|
|
.setTerminalEmulatorTitle("Servermanagment").createTerminal();
|
|
if (terminal instanceof SwingTerminalFrame) {
|
|
SwingTerminalFrame frame = (SwingTerminalFrame) terminal;
|
|
URL iconURL = getClass().getResource("/icon.png");
|
|
frame.setIconImage(new ImageIcon(iconURL).getImage());
|
|
|
|
frame.setSize(930, 500);
|
|
frame.addWindowListener(new WindowAdapter() {
|
|
@Override
|
|
public void windowClosing(WindowEvent e) {
|
|
poller.stop();
|
|
StartUp.getDb().closeConnection();
|
|
System.exit(0);
|
|
}
|
|
});
|
|
|
|
frame.repaint();
|
|
}
|
|
terminal.flush();
|
|
|
|
Screen screen = new TerminalScreen(terminal);
|
|
screen.startScreen();
|
|
|
|
Panel mainPanel = new Panel();
|
|
mainPanel.setLayoutManager(new LinearLayout(Direction.HORIZONTAL));
|
|
mainPanel.setPreferredSize(new TerminalSize(40, 20));
|
|
|
|
Panel leftPanel = new Panel();
|
|
mainPanel.addComponent(leftPanel.withBorder(Borders.singleLine("Serverliste")));
|
|
|
|
Panel rightPanel = new Panel();
|
|
mainPanel.addComponent(rightPanel.withBorder(Borders.singleLine("Aktionen")));
|
|
|
|
BasicWindow window = new BasicWindow();
|
|
window.setComponent(mainPanel.withBorder(Borders.singleLine("Servermanagment")));
|
|
window.setHints(Arrays.asList(Window.Hint.CENTERED));
|
|
|
|
MultiWindowTextGUI gui = new MultiWindowTextGUI(screen, new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE));
|
|
|
|
if (!StartUp.getPrivateKey().exists()) {
|
|
Logger.error(StartUp.getPrivateKey().getPath() + " wurde nicht gefunden!");
|
|
MessageDialog.showMessageDialog(gui, "ERROR!", StartUp.getPrivateKey().getPath() + " wurde nicht gefunden!", MessageDialogButton.Cancel);
|
|
gui.addWindowAndWait(window);
|
|
return;
|
|
}
|
|
|
|
CheckBoxList<String> checkBoxList = new CheckBoxList<String>().addTo(leftPanel);
|
|
updateList(checkBoxList, serverlist);
|
|
|
|
new Button("Server hinzufügen", () -> {
|
|
String servername = TextInputDialog.showDialog(gui, "Servername", "Wie soll der Identifiziert werden (Servername)", "Server_Cloud");
|
|
if (servername == null) return;
|
|
|
|
if (serverlist.containsKey(servername)) {
|
|
MessageDialog.showMessageDialog(gui, "ERROR!", "Server bereits vorhanden", MessageDialogButton.Cancel);
|
|
return;
|
|
}
|
|
|
|
String stringIPPort = TextInputDialog.showDialog(gui, "username@IP:Port", "Wie lautet der Hostname und Port des SSH Servers", "root@10.10.2.156:22");
|
|
if (stringIPPort == null) return;
|
|
|
|
String ss = "ssh://" + stringIPPort;
|
|
|
|
URI uri;
|
|
try {
|
|
uri = new URI(ss);
|
|
} catch (URISyntaxException e) {
|
|
MessageDialog.showMessageDialog(gui, "ERROR!", "Fehler beim lesen der URL", MessageDialogButton.Cancel);
|
|
return;
|
|
}
|
|
|
|
String ip = uri.getHost();
|
|
int port = uri.getPort();
|
|
String username = uri.getUserInfo() == null ? "root" : uri.getUserInfo();
|
|
|
|
if (port <= 0 || port > 65535) {
|
|
MessageDialog.showMessageDialog(gui, "ERROR!", "Port muss größer NUll und kleiner 65535 sein", MessageDialogButton.Cancel);
|
|
return;
|
|
}
|
|
|
|
InetAddressValidator inetAddressValidator = new InetAddressValidator();
|
|
if (!inetAddressValidator.isValid(ip)) {
|
|
MessageDialog.showMessageDialog(gui, "ERROR!", "Inkorrekte IP Adresse", MessageDialogButton.Cancel);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
PreparedStatement ps = StartUp.getDb().getPreparedStatement("INSERT INTO server (servername, username, ip, port, adddate) VALUES (?, ?, ?, ?, ?);");
|
|
ps.setString(1, servername);
|
|
ps.setString(2, username);
|
|
ps.setString(3, ip);
|
|
ps.setInt(4, port);
|
|
ps.setLong(5, System.currentTimeMillis() / 1000);
|
|
ps.execute();
|
|
|
|
ResultSet rs = ps.getGeneratedKeys();
|
|
rs.next();
|
|
int serverid = rs.getInt(1);
|
|
Server server = new Server(serverid, servername, username, ip, port);
|
|
serverlist.put(servername, server);
|
|
checkBoxList.addItem(servername);
|
|
Logger.info("Added: " + ip + " " + port);
|
|
MessageDialog.showMessageDialog(gui, "INFO", "Server hinzugefügt.\n" +
|
|
"Gehe sicher das der Public Key von " + StartUp.getPrivateKey().getPath() + " auf dem Server abgelegt wurde.", MessageDialogButton.Continue);
|
|
} catch (SQLException e) {
|
|
Logger.error(e);
|
|
}
|
|
}).addTo(rightPanel).takeFocus();
|
|
|
|
new Button("Alle auswählen", () -> checkBoxList.getItems().forEach(s -> checkBoxList.setChecked(s, !checkBoxList.isChecked(s)))).addTo(rightPanel);
|
|
|
|
new Button("Aktion ausführen", () -> {
|
|
List<String> checkedItems = checkBoxList.getCheckedItems();
|
|
if (checkedItems.size() <= 0) {
|
|
MessageDialog.showMessageDialog(gui, "ERROR!", "Bitte wähle zuerst Server aus!");
|
|
return;
|
|
}
|
|
|
|
List<Server> serverarray = new ArrayList<>();
|
|
checkedItems.forEach(s -> serverarray.add(serverlist.get(s)));
|
|
|
|
ActionListDialogBuilder pre = new ActionListDialogBuilder()
|
|
.setTitle("Aktion ausführen")
|
|
.setDescription("Was möchtest du unternhmen")
|
|
.addAction("Löschen", () -> {
|
|
try {
|
|
PreparedStatement ps = StartUp.getDb().getPreparedStatement("DELETE FROM server WHERE id = ?");
|
|
serverarray.forEach(server -> {
|
|
try {
|
|
ps.setInt(1, server.getId());
|
|
ps.addBatch();
|
|
serverlist.remove(server.getName());
|
|
} catch (SQLException e) {
|
|
Logger.error(e);
|
|
}
|
|
});
|
|
ps.executeBatch();
|
|
} catch (SQLException e) {
|
|
Logger.error(e);
|
|
}
|
|
updateList(checkBoxList, serverlist);
|
|
})
|
|
.addAction("Info", () -> serverarray.forEach(server -> MessageDialog.showMessageDialog(gui, server.getName(), server.toString(), MessageDialogButton.OK)))
|
|
.addAction("Status", () -> {
|
|
BasicWindow windowStatus = getStatusWindow(checkBoxList, serverlist);
|
|
gui.addWindow(windowStatus);
|
|
gui.setActiveWindow(windowStatus);
|
|
})
|
|
.addAction("Updaten", () -> MassCommand.run("apt-get update && apt-get upgrade -y", gui, serverarray, StartUp.getPrivateKey()))
|
|
.addAction("Herunterfahren", () -> MassCommand.run("shutdown -h now", gui, serverarray, StartUp.getPrivateKey()))
|
|
.addAction("Neustart", () -> MassCommand.run("reboot", gui, serverarray, StartUp.getPrivateKey()));
|
|
|
|
JSONObject customCommands = StartUp.getConfig().getCustomCommands();
|
|
for (int i = 0; i < customCommands.names().length(); i++) {
|
|
String key = customCommands.names().getString(i);
|
|
String value = customCommands.getString(key);
|
|
pre.addAction(key, () -> MassCommand.run(value, gui, serverarray, StartUp.getPrivateKey()));
|
|
}
|
|
|
|
pre.build().showDialog(gui);
|
|
|
|
}).addTo(rightPanel);
|
|
|
|
new Button("Aktualisieren", () -> {
|
|
serverlist = Server.getServerList(StartUp.getDb());
|
|
updateList(checkBoxList, serverlist);
|
|
}).addTo(rightPanel);
|
|
|
|
new Button("Beenden", () -> {
|
|
poller.stop();
|
|
StartUp.getDb().closeConnection();
|
|
System.exit(0);
|
|
}).addTo(rightPanel);
|
|
|
|
screen.setCursorPosition(null);
|
|
screen.doResizeIfNecessary();
|
|
gui.addWindowAndWait(window);
|
|
} catch (IOException e) {
|
|
Logger.error(e);
|
|
}
|
|
}
|
|
|
|
public static BasicWindow getCallbackWindow(Label label, Button abort) {
|
|
Panel statusPanel = new Panel();
|
|
statusPanel.setLayoutManager(new LinearLayout(Direction.VERTICAL));
|
|
|
|
BasicWindow windowStatus = new BasicWindow();
|
|
windowStatus.setComponent(statusPanel.withBorder(Borders.singleLine("Update Status")));
|
|
windowStatus.setHints(Arrays.asList(Window.Hint.CENTERED));
|
|
|
|
label.addTo(statusPanel);
|
|
|
|
abort.addTo(statusPanel);
|
|
return windowStatus;
|
|
}
|
|
|
|
private void updateList(CheckBoxList<String> checkBoxList, HashMap<String, Server> serverlist) {
|
|
checkBoxList.clearItems();
|
|
serverlist.forEach((s, server) -> checkBoxList.addItem(s));
|
|
}
|
|
|
|
private BasicWindow getStatusWindow(CheckBoxList<String> checkBoxList, HashMap<String, Server> serverlist) {
|
|
Panel statusPanel = new Panel();
|
|
statusPanel.setLayoutManager(new LinearLayout(Direction.VERTICAL));
|
|
|
|
BasicWindow windowStatus = new BasicWindow();
|
|
windowStatus.setComponent(statusPanel.withBorder(Borders.singleLine("Server Status")));
|
|
windowStatus.setHints(Arrays.asList(Window.Hint.CENTERED));
|
|
|
|
Table<String> table = new Table<String>("Name", "OS", "CPU", "RAM", "Uptime", "Last", "Status").addTo(statusPanel);
|
|
|
|
updateTable(table, checkBoxList, serverlist);
|
|
|
|
Button back = new Button("Zurück", windowStatus::close).addTo(statusPanel);
|
|
|
|
back.takeFocus();
|
|
return windowStatus;
|
|
}
|
|
|
|
private void updateTable(Table<String> table, CheckBoxList<String> checkBoxList, HashMap<String, Server> serverlist) {
|
|
for (String s : checkBoxList.getCheckedItems()) {
|
|
Server server = serverlist.get(s);
|
|
ServerStatus status = server.getStatus();
|
|
if (status.getLastpoll() == 0) {
|
|
return;
|
|
}
|
|
double totalMB = round(status.getMemTotal() / 1024.0, 2);
|
|
double freeMB = round(status.getMemFree() / 1024.0, 2);
|
|
|
|
table.getTableModel().addRow(server.getName(),
|
|
status.getOS(), status.getCPUcount() + "x " + status.getCPUModel(),
|
|
freeMB + "MB/" + totalMB + "MB", status.getUptimeFormatted(),
|
|
String.valueOf(status.getLoad()), status.getState().toString());
|
|
}
|
|
// "Name", "OS", "CPU", "RAM", "Uptime", "Last", "Status"
|
|
}
|
|
|
|
private double round(double value, int places) {
|
|
if (places < 0) throw new IllegalArgumentException();
|
|
|
|
BigDecimal bd = new BigDecimal(value);
|
|
bd = bd.setScale(places, RoundingMode.HALF_UP);
|
|
return bd.doubleValue();
|
|
}
|
|
}
|