/*
* Copyright 2010-2014, CloudBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cloudbees.sdk.server;
import com.cloudbees.api.ApplicationInfo;
import com.cloudbees.api.BeesClient;
import com.cloudbees.api.ServerInfo;
import com.cloudbees.api.ServerPoolInfo;
import com.cloudbees.sdk.pool.ServerPoolBase;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author Fabian Donze
*/
public abstract class ServerStatsBase extends ServerPoolBase {
public ServerStatsBase() {
super();
}
protected void printServerStats(String serverId, boolean details) throws Exception {
BeesClient client = getBeesClient(BeesClient.class);
ServerInfo serverInfo = client.serverInfo(serverId);
printServerStats(serverInfo, details, null);
}
protected void printServerStats(ServerInfo serverInfo, boolean details, ServerPoolInfo serverPoolInfo) throws Exception {
BeesClient client = getBeesClient(BeesClient.class);
System.out.println("Server ID : " + serverInfo.getId());
String containerSize = serverInfo.getSize();
double serverSize = getServerSize(containerSize);
double serverAppcells = getServerAppcell(containerSize);
if (containerSize == null) containerSize = "m1.small?";
System.out.println(String.format(" size : %s %.2f MB %.1f app-cells", containerSize, serverSize, serverAppcells));
if (details) {
String poolName = serverInfo.getParameters().get("poolName");
String msg = " pool ID : " + serverInfo.getPoolId();
if (poolName != null) msg += " (" + poolName + ")";
System.out.println(msg);
}
if (serverInfo.getApplicationIDs() != null) {
int totalSize = 0;
int totalAppCells = 0;
List<ApplicationInfo> apps = new ArrayList<ApplicationInfo>();
String totalSizeStr = serverInfo.getParameters().get("memoryUsage");
if (details || totalSizeStr == null) {
for (String appId : serverInfo.getApplicationIDs()) {
try {
ApplicationInfo res = getApplicationInfo(serverPoolInfo, appId, client);
apps.add(res);
totalSize += getApplicationSize(res);
totalAppCells += getApplicationAppCell(res);
} catch (Exception e) {
System.err.println("WARNING: Cannot get application information: " + appId + " >" + e.getMessage());
}
}
} else {
totalSize = Integer.parseInt(totalSizeStr);
totalAppCells = totalSize / 128;
}
double ratio = totalSize / serverSize * 100;
System.out.println(String.format(" applications : %s using %d MB %d app-cells", serverInfo.getApplicationIDs().size(), totalSize, totalAppCells));
if (details) {
for (ApplicationInfo app : apps) {
System.out.println(String.format(" %s (%d MB %d app-cells) %s", app.getId(), getApplicationSize(app), getApplicationAppCell(app), getApplicationContainer(app)));
}
}
System.out.println(String.format(" capacity used : %.2f %%", ratio));
System.out.println(String.format(" free capacity : %.2f MB %.1f app-cells", (serverSize-totalSize), (serverAppcells-totalAppCells)));
} else {
System.out.println(" no applications");
System.out.println(" capacity used : 0 %");
}
}
private int getApplicationSize(ApplicationInfo applicationInfo) {
Map<String, String> settings = applicationInfo.getSettings();
if (settings != null) {
return Integer.parseInt(settings.get("maxMemory"));
} else
return 0;
}
private String getApplicationContainer(ApplicationInfo applicationInfo) {
Map<String, String> settings = applicationInfo.getSettings();
if (settings != null) {
return settings.get("container");
} else
return "";
}
private int getApplicationAppCell(ApplicationInfo applicationInfo) {
Map<String, String> settings = applicationInfo.getSettings();
if (settings != null) {
return Integer.parseInt(settings.get("maxMemory")) / 128;
} else
return 0;
}
private double getServerSize(String size) {
if (size == null)
return 1024 * 1.7;
else if (size.equalsIgnoreCase("m1.small"))
return 1024 * 1.7;
else if (size.equalsIgnoreCase("m1.medium"))
return 1024 * 3.75;
else if (size.equalsIgnoreCase("m1.large"))
return 1024 * 7.5;
else if (size.equalsIgnoreCase("c1.medium"))
return 1024 * 1.7;
else throw new IllegalArgumentException("Unknown size: " + size);
}
private double getServerAppcell(String size) {
return getServerSize(size) / 128;
}
private ApplicationInfo getApplicationInfo(ServerPoolInfo serverPoolInfo, String appId, BeesClient client) throws Exception {
if (serverPoolInfo != null && serverPoolInfo.getApplications() != null) {
for (ApplicationInfo applicationInfo : serverPoolInfo.getApplications()) {
if (applicationInfo.getId().equals(appId) && getApplicationSize(applicationInfo) > 0)
return applicationInfo;
}
}
return client.applicationInfo(appId);
}
}