108 lines
3.0 KiB
Java
108 lines
3.0 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.sql.SQLException;
|
|
|
|
/**
|
|
* Created by gurkengewuerz.de on 21.10.2017.
|
|
*/
|
|
public class StartUp {
|
|
private static Database db;
|
|
private static String private_key;
|
|
|
|
public static void main(String... args) {
|
|
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);
|
|
|
|
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;
|
|
}
|
|
|
|
try {
|
|
db = new Database(cmd.getOptionValue("d", Variables.DATABASE_NAME));
|
|
} catch (Exception e) {
|
|
Logger.error(e);
|
|
}
|
|
|
|
if (db == null) System.exit(1);
|
|
|
|
try {
|
|
createDatabase();
|
|
} catch (SQLException e) {
|
|
Logger.error(e);
|
|
System.exit(1);
|
|
}
|
|
|
|
private_key = cmd.getOptionValue("s", Variables.PRIVATE_KEY);
|
|
if (!new File(private_key).exists()) {
|
|
Logger.error(private_key + " wurde nicht gefunden!");
|
|
System.exit(1);
|
|
}
|
|
if (cmd.hasOption("p")) {
|
|
new Poller();
|
|
} else {
|
|
new GUI();
|
|
}
|
|
}
|
|
|
|
public static Database getDb() {
|
|
return db;
|
|
}
|
|
|
|
public static String getPrivate_key() {
|
|
return private_key;
|
|
}
|
|
|
|
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" +
|
|
");"
|
|
);
|
|
}
|
|
}
|