servermanager/src/main/java/de/gurkengewuerz/monitoring/StartUp.java

131 lines
3.6 KiB
Java

package de.gurkengewuerz.monitoring;
import de.gurkengewuerz.monitoring.object.Database;
import org.apache.commons.cli.*;
import org.pmw.tinylog.Logger;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
/**
* Created by gurkengewuerz.de on 21.10.2017.
*/
public class StartUp {
private static Database db;
private static Config conf;
private static String private_key;
public static void main(String... args) throws IOException {
// START-ARGUMENTS
Options options = new Options();
Option poller = new Option("p", "poller", false, "Start only the poller");
poller.setRequired(false);
options.addOption(poller);
Option settingsfile = new Option("f", "file", true, "Set Settings file");
settingsfile.setRequired(false);
options.addOption(settingsfile);
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
Logger.error(e);
formatter.printHelp("utility-name", options);
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(conf.getString("sqlite"));
} catch (Exception e) {
Logger.error(e);
}
if (db == null) System.exit(1);
try {
createDatabase();
} catch (SQLException e) {
Logger.error(e);
System.exit(1);
}
// END-Datenbank
// 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 {
new GUI();
}
}
public static Database getDb() {
return db;
}
public static String getPrivate_key() {
return private_key;
}
public static Config getConfig() {
return conf;
}
private static void createDatabase() throws SQLException {
db.executeUpdate(
"CREATE TABLE IF NOT EXISTS server (" +
" id INTEGER PRIMARY KEY AUTOINCREMENT," +
" servername text NULL," +
" username text NULL," +
" ip text NULL," +
" port int NULL," +
" adddate float NOT NULL" +
");"
);
db.executeUpdate("CREATE TABLE IF NOT EXISTS status (" +
"server_id INTEGER," +
"os TEXT," +
"cpu TEXT," +
"cpucount INTEGER," +
"max_ram INTEGER," +
"current_ram INTEGER," +
"load float," +
"uptime INTEGER," +
"state text," +
"lastpoll INTEGER" +
");"
);
}
}