Started work on ServiceChecker

This commit is contained in:
Jerome van der Sar 2013-08-04 20:48:50 +02:00
parent 0901c02c7e
commit 5b34facdd3
7 changed files with 221 additions and 6 deletions

View File

@ -135,3 +135,7 @@ pet_protect_enabled: true
# Logs Registration
logs_register_password:
logs_register_url:
# Mojang service checker
service_checker_url: http://status.mojang.com/check
service_checker_timeout: 200

View File

@ -50,6 +50,7 @@ public class Command_status extends TFM_Command
playerMsg(String.format("World %d: %s - %d players.", i++, world.getName(), world.getPlayers().size()), ChatColor.BLUE);
}
/* Moved to TFM_ServiceChecker and Command_services
new BukkitRunnable()
{
@SuppressWarnings("unchecked")
@ -109,6 +110,7 @@ public class Command_status extends TFM_Command
}
}
}.runTaskAsynchronously(plugin);
*/
return true;
}

View File

@ -16,7 +16,7 @@ public class Command_tfm extends TFM_Command
playerMsg("TotalFreedomMod for 'TotalFreedom', the original all-op server.", ChatColor.GOLD);
playerMsg(String.format("Version " + ChatColor.BLUE + "%s.%s" + ChatColor.BLUE + ", built %s.", TotalFreedomMod.pluginVersion, TotalFreedomMod.buildNumber, TotalFreedomMod.buildDate), ChatColor.GOLD);
playerMsg("Created by Madgeek1450 and DarthSalamon.", ChatColor.GOLD);
playerMsg("Visit " + ChatColor.AQUA + "http://totalfreedom.me/" + ChatColor.GREEN + " for more information.", ChatColor.DARK_GREEN);
playerMsg("Visit " + ChatColor.AQUA + "http://totalfreedom.me/" + ChatColor.GREEN + " for more information.", ChatColor.GREEN);
return true;
}

View File

@ -0,0 +1,207 @@
package me.StevenLawson.TotalFreedomMod;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bukkit.ChatColor;
import org.bukkit.scheduler.BukkitRunnable;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class TFM_ServiceChecker
{
public final Map<String, TFM_ServiceChecker_ServiceStatus> services = new HashMap<String, TFM_ServiceChecker_ServiceStatus>();
public String lastCheck = "never";
public String version = "1.0-Mojang";
public TFM_ServiceChecker()
{
services.put("minecraft.net", new TFM_ServiceChecker_ServiceStatus("Minecraft.net"));
services.put("login.minecraft.net", new TFM_ServiceChecker_ServiceStatus("Minecraft Logins"));
services.put("session.minecraft.net", new TFM_ServiceChecker_ServiceStatus("Minecraft Multiplayer Sessions"));
services.put("account.mojang.com", new TFM_ServiceChecker_ServiceStatus("Mojang Accounts Website"));
services.put("auth.mojang.com", new TFM_ServiceChecker_ServiceStatus("Mojang Accounts Login"));
services.put("skins.minecraft.net", new TFM_ServiceChecker_ServiceStatus("Minecraft Skins"));
}
public void update()
{
new BukkitRunnable()
{
@Override
public void run()
{
try
{
URL mojang_status = new URL(TotalFreedomMod.serviceCheckerURL);
BufferedReader in = new BufferedReader(new InputStreamReader(mojang_status.openStream()));
JSONArray status_json = (JSONArray) JSONValue.parse(in.readLine());
in.close();
TFM_ServiceChecker serviceChecker = TFM_ServiceChecker.getInstance();
Iterator status_it = status_json.iterator();
while (status_it.hasNext())
{
JSONObject service = (JSONObject) status_it.next();
Iterator service_it = service.entrySet().iterator();
while (service_it.hasNext())
{
Entry<String, String> pair = (Entry<String, String>) service_it.next();
if ("lastcheck".equals(pair.getKey()))
{
serviceChecker.lastCheck = pair.getValue();
continue;
}
if ("version".equals(pair.getKey()))
{
serviceChecker.version = pair.getValue();
continue;
}
if (pair.getValue().contains(":"))
{
String[] statusString = pair.getValue().split(":");
TFM_ServiceChecker_ServiceStatus status = serviceChecker.services.get(pair.getKey());
status.setColor(statusString[0]);
status.setMessage(statusString[1]);
status.setUptime(statusString[2]);
}
else
{
TFM_ServiceChecker_ServiceStatus status = serviceChecker.services.get(pair.getKey());
status.setColor(pair.getValue());
status.setMessage(("red".equals(pair.getValue()) ? "Offline" : ("yellow".equals(pair.getValue()) ? "Problem" : "Offline")));
}
}
}
TFM_Log.info("Mojang Service pull completed.");
}
catch (Exception ex)
{
TFM_Log.severe("Error updating mojang services from " + TotalFreedomMod.serviceCheckerURL);
}
}
}.runTaskAsynchronously(TotalFreedomMod.plugin);
}
public List<TFM_ServiceChecker_ServiceStatus> getAllStatuses()
{
List<TFM_ServiceChecker_ServiceStatus> ServicesList = new ArrayList<TFM_ServiceChecker_ServiceStatus>();
for (String key : services.keySet())
{
ServicesList.add(services.get(key));
}
return ServicesList;
}
public static TFM_ServiceChecker getInstance()
{
return TFM_ServiceCheckerHolder.INSTANCE;
}
private static class TFM_ServiceCheckerHolder
{
private static final TFM_ServiceChecker INSTANCE = new TFM_ServiceChecker();
}
public class TFM_ServiceChecker_ServiceStatus
{
private String name;
private String uptime = "100.0"; // skins.minecraft.net, minecraft.net, etc..
private ChatColor color = ChatColor.GREEN;
private String message = "Online"; // Online, Offline, Quite Slow, 404 Error, 500 Error, etc..
public TFM_ServiceChecker_ServiceStatus(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public String getUptime()
{
return uptime;
}
public float getUptimeFloat()
{
return Float.parseFloat(uptime);
}
public ChatColor getUptimeColor()
{
return (getUptimeFloat() > 95 ? ChatColor.GREEN : (getUptimeFloat() > 90 ? ChatColor.GOLD : ChatColor.RED));
}
public ChatColor getColor()
{
return color;
}
public String getMessage()
{
return message;
}
public String getFormattedStatus()
{
String status = ChatColor.BLUE + "- " + name + ChatColor.WHITE + ": " + color + message + ChatColor.WHITE;
if (!TFM_ServiceChecker.getInstance().version.contains("Mojang"))
{
status += " (" + getUptimeColor() + getUptime() + ChatColor.WHITE + "%)";
}
return status;
}
public void setUptime(String uptime)
{
this.uptime = uptime;
}
public void setColor(ChatColor color)
{
this.color = color;
}
public void setColor(String color)
{
if ("green".equals(color))
{
this.color = ChatColor.DARK_GREEN;
}
else if ("yellow".equals(color))
{
this.color = ChatColor.YELLOW;
}
else
{
this.color = ChatColor.RED;
}
}
public void setMessage(String message)
{
this.message = message;
}
}
}

View File

@ -49,7 +49,7 @@ public class TFM_TwitterHandler
String line = "failed";
try
{
URL getUrl = new URL(TotalFreedomMod.twitterbotUrl + "?auth=" + TotalFreedomMod.twitterbotSecret + "&" + queryString);
URL getUrl = new URL(TotalFreedomMod.twitterbotURL + "?auth=" + TotalFreedomMod.twitterbotSecret + "&" + queryString);
URLConnection urlConnection = getUrl.openConnection();
// Read the response
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

View File

@ -262,11 +262,12 @@ public class TotalFreedomMod extends JavaPlugin
public static double autoProtectRadius = 25.0D;
public static List<String> host_sender_names = Arrays.asList("rcon", "remotebukkit");
public static boolean twitterbotEnabled = false;
public static String twitterbotUrl = "";
public static String twitterbotURL = "";
public static String twitterbotSecret = "";
public static boolean petProtectEnabled = true;
public static String logsRegisterPassword = "";
public static String logsRegisterURL = "";
public static String serviceCheckerURL = "http://status.mojang.com/check";
public static void loadMainConfig()
{
@ -311,11 +312,12 @@ public class TotalFreedomMod extends JavaPlugin
autoProtectRadius = config.getDouble("auto_protect_radius", autoProtectRadius);
host_sender_names = config.getStringList("host_sender_names");
twitterbotEnabled = config.getBoolean("twitterbot_enabled", twitterbotEnabled);
twitterbotUrl = config.getString("twitterbot_url", twitterbotUrl);
twitterbotURL = config.getString("twitterbot_url", twitterbotURL);
twitterbotSecret = config.getString("twitterbot_secret", twitterbotSecret);
petProtectEnabled = config.getBoolean("pet_protect_enabled", petProtectEnabled);
logsRegisterPassword = config.getString("logs_register_password", logsRegisterPassword);
logsRegisterURL = config.getString("logs_register_url", logsRegisterURL);
serviceCheckerURL = config.getString("service_checker_url", serviceCheckerURL);
}
catch (Exception ex)
{