use of com.spotify.helios.common.descriptors.HostInfo in project helios by spotify.
the class HostListCommand method run.
@Override
int run(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final BufferedReader stdin) throws ExecutionException, InterruptedException {
final String pattern = options.getString(patternArg.getDest());
final List<String> selectorArgValue = options.getList(hostSelectorsArg.getDest());
final Set<String> selectors = ImmutableSet.copyOf(selectorArgValue);
final List<String> hosts;
if (pattern.isEmpty() && selectors.isEmpty()) {
hosts = client.listHosts().get();
} else if (!pattern.isEmpty() && selectors.isEmpty()) {
hosts = client.listHosts(pattern).get();
} else if (pattern.isEmpty() && !selectors.isEmpty()) {
hosts = client.listHosts(selectors).get();
} else {
hosts = client.listHosts(pattern, selectors).get();
}
final Map<String, String> queryParams = Maps.newHashMap();
final String statusFilter = options.getString(statusArg.getDest());
if (!isNullOrEmpty(statusFilter)) {
try {
HostStatus.Status.valueOf(statusFilter);
queryParams.put("status", statusFilter);
} catch (IllegalArgumentException ignored) {
throw new IllegalArgumentException("Invalid status. Valid statuses are: " + statusChoicesString);
}
}
final boolean full = options.getBoolean(fullArg.getDest());
final boolean quiet = options.getBoolean(quietArg.getDest());
if (hosts.isEmpty()) {
if (json) {
out.println("{ }");
} else if (!quiet && !isNullOrEmpty(pattern)) {
out.printf("host pattern %s matched no hosts%n", pattern);
}
return 1;
}
final List<String> sortedHosts = natural().sortedCopy(hosts);
if (quiet) {
if (json) {
out.println(Json.asPrettyStringUnchecked(sortedHosts));
} else {
for (final String host : sortedHosts) {
out.println(formatHostname(full, host));
}
}
} else {
final Map<String, HostStatus> statuses = client.hostStatuses(hosts, queryParams).get();
if (json) {
final Map<String, HostStatus> sorted = Maps.newTreeMap();
sorted.putAll(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", "LABELS");
for (final Map.Entry<String, HostStatus> e : statuses.entrySet()) {
final String host = e.getKey();
final HostStatus s = e.getValue();
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);
}
}
final String hostLabels = Joiner.on(", ").withKeyValueSeparator("=").join(s.getLabels());
table.row(formatHostname(full, host), status, s.getJobs().size(), runningDeployedJobs.size(), cpus, mem, loadAvg, memUsage, os, version, docker, hostLabels);
}
table.print();
}
}
return 0;
}
use of com.spotify.helios.common.descriptors.HostInfo in project helios by spotify.
the class ZooKeeperMasterModel method getHostStatus.
/**
* Returns the current status of the host named by {@code host}.
*/
@Override
public HostStatus getHostStatus(final String host) {
final ZooKeeperClient client = provider.get("getHostStatus");
if (!ZooKeeperRegistrarUtil.isHostRegistered(client, host)) {
log.warn("Host {} isn't registered in ZooKeeper.", host);
return null;
}
final boolean up = checkHostUp(client, host);
final HostInfo hostInfo = getHostInfo(client, host);
final AgentInfo agentInfo = getAgentInfo(client, host);
final Map<JobId, Deployment> tasks = getTasks(client, host);
final Map<JobId, TaskStatus> statuses = getTaskStatuses(client, host);
final Map<String, String> environment = getEnvironment(client, host);
final Map<String, String> labels = getLabels(client, host);
return HostStatus.newBuilder().setJobs(tasks).setStatuses(fromNullable(statuses).or(EMPTY_STATUSES)).setHostInfo(hostInfo).setAgentInfo(agentInfo).setStatus(up ? UP : DOWN).setEnvironment(environment).setLabels(labels).build();
}
use of com.spotify.helios.common.descriptors.HostInfo in project helios by spotify.
the class HostInfoReporter method runOneIteration.
@Override
protected void runOneIteration() throws InterruptedException {
final String hostname = exec("uname -n").trim();
final String uname = exec("uname -a").trim();
final HostInfo hostInfo = HostInfo.newBuilder().setArchitecture(operatingSystemMxBean.getArch()).setCpus(Runtime.getRuntime().availableProcessors()).setHostname(hostname).setLoadAvg(operatingSystemMxBean.getSystemLoadAverage()).setOsName(operatingSystemMxBean.getName()).setOsVersion(operatingSystemMxBean.getVersion()).setMemoryFreeBytes(operatingSystemMxBean.getFreePhysicalMemorySize()).setMemoryTotalBytes(operatingSystemMxBean.getTotalPhysicalMemorySize()).setSwapFreeBytes(operatingSystemMxBean.getFreeSwapSpaceSize()).setSwapTotalBytes(operatingSystemMxBean.getTotalSwapSpaceSize()).setUname(uname).setDockerVersion(dockerVersion()).setDockerHost(dockerHost()).setDockerCertPath(dockerHost.dockerCertPath()).build();
nodeUpdater.update(hostInfo.toJsonBytes());
}
use of com.spotify.helios.common.descriptors.HostInfo in project helios by spotify.
the class HostListCommandTest method setUp.
@Before
public void setUp() throws ParseException {
when(client.listHosts()).thenReturn(immediateFuture(HOSTS));
final HostInfo hostInfo = HostInfo.newBuilder().setCpus(4).setMemoryTotalBytes((long) Math.pow(1024, 3)).setMemoryFreeBytes(500000000).setLoadAvg(0.1).setOsName("OS foo").setOsVersion("0.1.0").setDockerVersion(DockerVersion.builder().version("1.7.0").apiVersion("1.18").build()).build();
final long dayMilliseconds = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
final long startTime = System.currentTimeMillis() - 2 * dayMilliseconds;
final AgentInfo agentInfo = AgentInfo.newBuilder().setVersion("0.8.420").setUptime(dayMilliseconds).setStartTime(startTime).build();
upStatus = HostStatus.newBuilder().setJobs(JOBS).setStatuses(JOB_STATUSES).setStatus(UP).setHostInfo(hostInfo).setAgentInfo(agentInfo).setLabels(LABELS).build();
downStatus = HostStatus.newBuilder().setJobs(JOBS).setStatuses(JOB_STATUSES).setStatus(DOWN).setHostInfo(hostInfo).setAgentInfo(agentInfo).setLabels(LABELS).build();
final Map<String, HostStatus> statuses = ImmutableMap.of(HOSTS.get(0), upStatus, HOSTS.get(1), upStatus, HOSTS.get(2), downStatus);
when(client.hostStatuses(eq(HOSTS), anyMapOf(String.class, String.class))).thenReturn(immediateFuture(statuses));
}
Aggregations