forked from beluga-php/docker-php-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerStats.php
More file actions
113 lines (101 loc) · 4.56 KB
/
Copy pathContainerStats.php
File metadata and controls
113 lines (101 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace Docker\API\Endpoint;
class ContainerStats extends \Docker\API\Runtime\Client\BaseEndpoint implements \Docker\API\Runtime\Client\Endpoint
{
use \Docker\API\Runtime\Client\EndpointTrait;
protected $id;
/**
* This endpoint returns a live stream of a container’s resource usage
* statistics.
*
* The `precpu_stats` is the CPU statistic of the *previous* read, and is
* used to calculate the CPU usage percentage. It is not an exact copy
* of the `cpu_stats` field.
*
* If either `precpu_stats.online_cpus` or `cpu_stats.online_cpus` is
* nil then for compatibility with older daemons the length of the
* corresponding `cpu_usage.percpu_usage` array should be used.
*
* On a cgroup v2 host, the following fields are not set
* `blkio_stats`: all fields other than `io_service_bytes_recursive`
* `cpu_stats`: `cpu_usage.percpu_usage`
* `memory_stats`: `max_usage` and `failcnt`
* Also, `memory_stats.stats` fields are incompatible with cgroup v1.
*
* To calculate the values shown by the `stats` command of the docker cli tool
* the following formulas can be used:
* used_memory = `memory_stats.usage - memory_stats.stats.cache`
* available_memory = `memory_stats.limit`
* Memory usage % = `(used_memory / available_memory) * 100.0`
* cpu_delta = `cpu_stats.cpu_usage.total_usage - precpu_stats.cpu_usage.total_usage`
* system_cpu_delta = `cpu_stats.system_cpu_usage - precpu_stats.system_cpu_usage`
* number_cpus = `lenght(cpu_stats.cpu_usage.percpu_usage)` or `cpu_stats.online_cpus`
* CPU usage % = `(cpu_delta / system_cpu_delta) * number_cpus * 100.0`
*
* @param string $id ID or name of the container
* @param array $queryParameters {
*
* @var bool $stream Stream the output. If false, the stats will be output once and then
* it will disconnect.
* @var bool $one-shot Only get a single stat instead of waiting for 2 cycles. Must be used
* with `stream=false`.
*
* }
*/
public function __construct(string $id, array $queryParameters = [])
{
$this->id = $id;
$this->queryParameters = $queryParameters;
}
public function getMethod(): string
{
return 'GET';
}
public function getUri(): string
{
return str_replace(['{id}'], [$this->id], '/containers/{id}/stats');
}
public function getBody(\Symfony\Component\Serializer\SerializerInterface $serializer, $streamFactory = null): array
{
return [[], null];
}
public function getExtraHeaders(): array
{
return ['Accept' => ['application/json']];
}
protected function getQueryOptionsResolver(): \Symfony\Component\OptionsResolver\OptionsResolver
{
$optionsResolver = parent::getQueryOptionsResolver();
$optionsResolver->setDefined(['stream', 'one-shot']);
$optionsResolver->setRequired([]);
$optionsResolver->setDefaults(['stream' => true, 'one-shot' => false]);
$optionsResolver->addAllowedTypes('stream', ['bool']);
$optionsResolver->addAllowedTypes('one-shot', ['bool']);
return $optionsResolver;
}
/**
* @throws \Docker\API\Exception\ContainerStatsNotFoundException
* @throws \Docker\API\Exception\ContainerStatsInternalServerErrorException
*
* @return null
*/
protected function transformResponseBody(\Psr\Http\Message\ResponseInterface $response, \Symfony\Component\Serializer\SerializerInterface $serializer, ?string $contentType = null)
{
$status = $response->getStatusCode();
$body = (string) $response->getBody();
if ((null === $contentType) === false && (200 === $status && false !== mb_strpos($contentType, 'application/json'))) {
return json_decode($body);
}
if ((null === $contentType) === false && (404 === $status && false !== mb_strpos($contentType, 'application/json'))) {
throw new \Docker\API\Exception\ContainerStatsNotFoundException($serializer->deserialize($body, 'Docker\\API\\Model\\ErrorResponse', 'json'), $response);
}
if ((null === $contentType) === false && (500 === $status && false !== mb_strpos($contentType, 'application/json'))) {
throw new \Docker\API\Exception\ContainerStatsInternalServerErrorException($serializer->deserialize($body, 'Docker\\API\\Model\\ErrorResponse', 'json'), $response);
}
}
public function getAuthenticationScopes(): array
{
return [];
}
}