50 lines
1.5 KiB
Java
50 lines
1.5 KiB
Java
package com.educoder.api;
|
|
import java.net.URI;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import com.spotify.docker.client.DefaultDockerClient;
|
|
import com.spotify.docker.client.DockerClient;
|
|
import com.spotify.docker.client.DockerClient.ListContainersParam;
|
|
import com.spotify.docker.client.auth.FixedRegistryAuthSupplier;
|
|
import com.spotify.docker.client.exceptions.DockerException;
|
|
import com.spotify.docker.client.messages.Container;
|
|
import com.spotify.docker.client.messages.ContainerState;
|
|
import com.spotify.docker.client.messages.ContainerStats;
|
|
import com.spotify.docker.client.messages.RegistryAuth;
|
|
/**
|
|
* 提供docker的各种接口供资源监控使用
|
|
* @author kevin
|
|
*"http://106.75.96.108:4243"
|
|
*/
|
|
public class Docker {
|
|
private DockerClient dockerClient;
|
|
|
|
|
|
public List<Container> getAllContainers(String name,String value) throws DockerException, InterruptedException
|
|
{
|
|
List containerList = new ArrayList<Container>();
|
|
ListContainersParam params = new ListContainersParam(name, value);
|
|
containerList = dockerClient.listContainers(params);
|
|
return containerList;
|
|
}
|
|
|
|
public String getCpuUsage(String containerId) throws DockerException, InterruptedException
|
|
{
|
|
ContainerStats stats = this.dockerClient.stats(containerId);
|
|
Long cpuUsage = stats.cpuStats().cpuUsage().totalUsage();
|
|
return cpuUsage/1024 + "";
|
|
}
|
|
|
|
|
|
Docker(String URL)
|
|
{
|
|
this.dockerClient = DefaultDockerClient.builder()
|
|
.uri(URI.create(URL))
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
}
|