Added Config and custom Commands
also refactor
This commit is contained in:
parent
33abcaccc5
commit
b2b6a8cae3
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
||||
target/
|
||||
*.sqlite3
|
||||
*.iml
|
||||
settings.json
|
||||
|
118
src/main/java/de/gurkengewuerz/monitoring/Config.java
Normal file
118
src/main/java/de/gurkengewuerz/monitoring/Config.java
Normal file
@ -0,0 +1,118 @@
|
||||
package de.gurkengewuerz.monitoring;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
import org.pmw.tinylog.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.AccessDeniedException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
/**
|
||||
* Created by gurkengewuerz.de on 24.10.2017.
|
||||
*/
|
||||
public class Config extends JSONObject {
|
||||
private File file;
|
||||
private boolean firstRun = false;
|
||||
|
||||
public Config(File file) throws IOException {
|
||||
this.file = file;
|
||||
|
||||
if (!file.exists()) {
|
||||
file.createNewFile();
|
||||
firstRun = true;
|
||||
}
|
||||
|
||||
if (!file.isFile()) {
|
||||
throw new FileNotFoundException(file.getAbsolutePath() + " not found");
|
||||
}
|
||||
|
||||
if (!file.canRead() || !file.canWrite()) {
|
||||
throw new AccessDeniedException(file.getAbsolutePath() + " is not accessable");
|
||||
}
|
||||
|
||||
this.put("debug", true);
|
||||
this.put("sqlite", Variables.DATABASE_NAME);
|
||||
this.put("private_key", Variables.PRIVATE_KEY);
|
||||
|
||||
JSONObject custom_commands = new JSONObject();
|
||||
custom_commands.put("Screen installieren", "apt-get install screen");
|
||||
this.put("commands", custom_commands);
|
||||
}
|
||||
|
||||
public void save() {
|
||||
try {
|
||||
FileWriter fw = new FileWriter(file.getAbsolutePath());
|
||||
fw.write(this.toString(4));
|
||||
fw.close();
|
||||
} catch (IOException e) {
|
||||
Logger.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void load() {
|
||||
try {
|
||||
String content = new String(Files.readAllBytes(file.toPath()), "UTF-8");
|
||||
if (content.isEmpty()) {
|
||||
save();
|
||||
return;
|
||||
}
|
||||
JSONTokener jt = new JSONTokener(content);
|
||||
if (jt.nextClean() != 123) {
|
||||
throw jt.syntaxError("A JSONObject text must begin with '{'");
|
||||
} else {
|
||||
while (jt.more()) {
|
||||
char c = jt.nextClean();
|
||||
switch (c) {
|
||||
case '\u0000':
|
||||
throw jt.syntaxError("A JSONObject text must end with '}'");
|
||||
case '}':
|
||||
return;
|
||||
default:
|
||||
jt.back();
|
||||
String key = jt.nextValue().toString();
|
||||
c = jt.nextClean();
|
||||
if (c != 58) {
|
||||
throw jt.syntaxError("Expected a ':' after a key");
|
||||
}
|
||||
|
||||
this.remove(key);
|
||||
this.putOnce(key, jt.nextValue());
|
||||
switch (jt.nextClean()) {
|
||||
case ',':
|
||||
case ';':
|
||||
if (jt.nextClean() == 125) {
|
||||
return;
|
||||
}
|
||||
|
||||
jt.back();
|
||||
break;
|
||||
case '}':
|
||||
save();
|
||||
return;
|
||||
default:
|
||||
throw jt.syntaxError("Expected a ',' or '}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Logger.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean debug() {
|
||||
return getBoolean("debug");
|
||||
}
|
||||
|
||||
public JSONObject getCustomCommands() {
|
||||
return getJSONObject("commands");
|
||||
}
|
||||
|
||||
public boolean isFirstRun() {
|
||||
return firstRun;
|
||||
}
|
||||
}
|
@ -17,6 +17,7 @@ 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.*;
|
||||
@ -162,7 +163,7 @@ public class GUI {
|
||||
List<Server> serverarray = new ArrayList<>();
|
||||
checkedItems.forEach(s -> serverarray.add(serverlist.get(s)));
|
||||
|
||||
new ActionListDialogBuilder()
|
||||
ActionListDialogBuilder pre = new ActionListDialogBuilder()
|
||||
.setTitle("Aktion ausführen")
|
||||
.setDescription("Was möchtest du unternhmen")
|
||||
.addAction("Löschen", () -> {
|
||||
@ -191,9 +192,17 @@ public class GUI {
|
||||
})
|
||||
.addAction("Updaten", () -> MassCommand.run("apt-get update && apt-get upgrade -y", gui, serverarray, StartUp.getPrivate_key()))
|
||||
.addAction("Herunterfahren", () -> MassCommand.run("shutdown -h now", gui, serverarray, StartUp.getPrivate_key()))
|
||||
.addAction("Neustart", () -> MassCommand.run("reboot", gui, serverarray, StartUp.getPrivate_key()))
|
||||
.build()
|
||||
.showDialog(gui);
|
||||
.addAction("Neustart", () -> MassCommand.run("reboot", gui, serverarray, StartUp.getPrivate_key()));
|
||||
|
||||
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.getPrivate_key()));
|
||||
}
|
||||
|
||||
pre.build().showDialog(gui);
|
||||
|
||||
}).addTo(rightPanel);
|
||||
|
||||
new Button("Aktualisieren", () -> {
|
||||
@ -215,6 +224,20 @@ public class GUI {
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
@ -256,20 +279,6 @@ public class GUI {
|
||||
// "Name", "OS", "CPU", "RAM", "Uptime", "Last", "Status"
|
||||
}
|
||||
|
||||
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 double round(double value, int places) {
|
||||
if (places < 0) throw new IllegalArgumentException();
|
||||
|
||||
|
@ -5,6 +5,7 @@ import org.apache.commons.cli.*;
|
||||
import org.pmw.tinylog.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
@ -12,22 +13,20 @@ import java.sql.SQLException;
|
||||
*/
|
||||
public class StartUp {
|
||||
private static Database db;
|
||||
private static Config conf;
|
||||
private static String private_key;
|
||||
|
||||
public static void main(String... args) {
|
||||
public static void main(String... args) throws IOException {
|
||||
// START-ARGUMENTS
|
||||
Options options = new Options();
|
||||
|
||||
Option database = new Option("d", "database", true, "Database Path");
|
||||
database.setRequired(false);
|
||||
options.addOption(database);
|
||||
|
||||
Option poller = new Option("p", "poller", false, "Start only the poller");
|
||||
poller.setRequired(false);
|
||||
options.addOption(poller);
|
||||
|
||||
Option sshkey = new Option("s", "key", true, "Set ssh private key");
|
||||
sshkey.setRequired(false);
|
||||
options.addOption(sshkey);
|
||||
Option settingsfile = new Option("f", "file", true, "Set Settings file");
|
||||
settingsfile.setRequired(false);
|
||||
options.addOption(settingsfile);
|
||||
|
||||
CommandLineParser parser = new DefaultParser();
|
||||
HelpFormatter formatter = new HelpFormatter();
|
||||
@ -42,9 +41,25 @@ public class StartUp {
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
// END-ARGUMENTS
|
||||
|
||||
// START-CONFIG
|
||||
String settingsFile = "." + File.separator + "settings.json";
|
||||
File f = new File(cmd.getOptionValue("f", settingsFile));
|
||||
|
||||
conf = new Config(f);
|
||||
conf.load();
|
||||
|
||||
if (conf.debug())
|
||||
Logger.info("Benutze Konfig: " + f.getAbsolutePath());
|
||||
Logger.info("Wechseln der Konfig mit --file <Pfad>");
|
||||
|
||||
if (conf.isFirstRun()) System.exit(0);
|
||||
// END-CONFIG
|
||||
|
||||
// START-Datenbank
|
||||
try {
|
||||
db = new Database(cmd.getOptionValue("d", Variables.DATABASE_NAME));
|
||||
db = new Database(conf.getString("sqlite"));
|
||||
} catch (Exception e) {
|
||||
Logger.error(e);
|
||||
}
|
||||
@ -57,12 +72,16 @@ public class StartUp {
|
||||
Logger.error(e);
|
||||
System.exit(1);
|
||||
}
|
||||
// END-Datenbank
|
||||
|
||||
private_key = cmd.getOptionValue("s", Variables.PRIVATE_KEY);
|
||||
// START-PRIV KEY
|
||||
private_key = conf.getString("private_key");
|
||||
if (!new File(private_key).exists()) {
|
||||
Logger.error(private_key + " wurde nicht gefunden!");
|
||||
System.exit(1);
|
||||
}
|
||||
// END-PRIV KEY
|
||||
|
||||
if (cmd.hasOption("p")) {
|
||||
new Poller();
|
||||
} else {
|
||||
@ -78,6 +97,10 @@ public class StartUp {
|
||||
return private_key;
|
||||
}
|
||||
|
||||
public static Config getConfig() {
|
||||
return conf;
|
||||
}
|
||||
|
||||
private static void createDatabase() throws SQLException {
|
||||
db.executeUpdate(
|
||||
"CREATE TABLE IF NOT EXISTS server (" +
|
||||
|
Loading…
Reference in New Issue
Block a user