if (json) {
final Map<String, HostStatus> sorted = Maps.newTreeMap();
sorted.putAll(allAsMap(statuses));
out.println(Json.asPrettyStringUnchecked(sorted));
} else {
final Table table = table(out);
table.row("HOST", "STATUS", "DEPLOYED", "RUNNING",
"CPUS", "MEM", "LOAD AVG", "MEM USAGE", "OS", "HELIOS", "DOCKER");
for (final Map.Entry<String, ListenableFuture<HostStatus>> e : statuses.entrySet()) {
final String host = e.getKey();
final HostStatus s = e.getValue().get();
if (s == null) {
continue;
}
final Set<TaskStatus> runningDeployedJobs = Sets.newHashSet();
for (final JobId jobId : s.getJobs().keySet()) {
final TaskStatus taskStatus = s.getStatuses().get(jobId);
if (taskStatus == null) {
continue;
}
if (taskStatus.getState() == TaskStatus.State.RUNNING) {
runningDeployedJobs.add(taskStatus);
}
}
final HostInfo hi = s.getHostInfo();
final String memUsage;
final String cpus;
final String mem;
final String loadAvg;
final String os;
final String docker;
if (hi != null) {
final long free = hi.getMemoryFreeBytes();
final long total = hi.getMemoryTotalBytes();
memUsage = format("%.2f", (float) (total - free) / total);
cpus = String.valueOf(hi.getCpus());
mem = hi.getMemoryTotalBytes() / (1024 * 1024 * 1024) + " gb";
loadAvg = format("%.2f", hi.getLoadAvg());
os = hi.getOsName() + " " + hi.getOsVersion();
final DockerVersion dv = hi.getDockerVersion();
docker = (dv != null) ? format("%s (%s)", dv.getVersion(), dv.getApiVersion()) : "";
} else {
memUsage = cpus = mem = loadAvg = os = docker = "";
}
final String version;
if (s.getAgentInfo() != null) {
version = Optional.fromNullable(s.getAgentInfo().getVersion()).or("");
} else {
version = "";
}
String status = s.getStatus() == UP ? "Up" : "Down";
if (s.getAgentInfo() != null) {
final long startTime = s.getAgentInfo().getStartTime();
final long upTime = s.getAgentInfo().getUptime();
if (s.getStatus() == UP) {
status += " " + humanDuration(currentTimeMillis() - startTime);
} else {
status += " " + humanDuration(currentTimeMillis() - startTime - upTime);
}
}
table.row(formatHostname(full, host), status, s.getJobs().size(),
runningDeployedJobs.size(), cpus, mem, loadAvg, memUsage, os, version, docker);
}
table.print();
}
}
return 0;
}