Hallo Zusammen,
habe per Suche nichts gefunden ob das Thema schon jemand angegangen ist.
Falls Interesse besteht, ich habe ein Skript geschrieben womit man die Docker Infos abrufen kann (Laufende Container mit Infos, Docker Infos).
Das Skript nimmt den weg über die Unix Socket.
Bei mir läuft alles auf einem Raspberry Pi 4 in Containern (IP Symcon, PiHole, Zigbee2Mqtt, Mosquitto, …)
Wichtig ist das IP Symcon unter Linux oder im Container auf das Verzeichnis /var/run/docker.sock zugreifen kann. Unter Windows gibt es da glaube ich einen anderen Weg. Mangels Aufbau kann ich das nicht testen. Aber das sollte mit paar anpassungen auch gehen.
In meiner Docker-Compose File habe ich unter Volumes einen weiteren Eintrag erstellt.
Hier das momentane Skript. Da ich eher im C# Umfeld unterwegs bin muss ich mich noch bisschen an das PHP Kauderwelsch gewöhnen.
Gibt noch paar Sachen die man schöner machen kann und es gibt definitiv noch mehr Daten zu sammeln und zu überwachen.
<?php
$DummyInstanceId = "{485D0419-BE97-4548-AA9C-C083EB82E61E}";
$ParentFolder = ID-FOLDER;
$client = new DockerClient('/var/run/docker.sock');
$dockerContainers = $client->dispatchCommand('/containers/json');
foreach ($dockerContainers as $container)
{
$instanceId = AddOrUpdateContainerInfos($container);
//$containerDetails = $client->dispatchCommand('/containers/'.$container['Id'].'/json');
//AddOrUpdateContainerDetails($instanceId, $containerDetails);
}
$dockerInfo = $client->dispatchCommand('/info');
if($dockerInfo === false) return;
AddOrUpdateDockerInfo("Container gesamt", VARIABLETYPE_INTEGER, $ParentFolder, $dockerInfo['Containers']);
AddOrUpdateDockerInfo("Container werden Ausgeführt", VARIABLETYPE_INTEGER, $ParentFolder, $dockerInfo['ContainersRunning']);
AddOrUpdateDockerInfo("Container die Pausiert sind", VARIABLETYPE_INTEGER, $ParentFolder, $dockerInfo['ContainersPaused']);
AddOrUpdateDockerInfo("Container die Gestoppt sind", VARIABLETYPE_INTEGER, $ParentFolder, $dockerInfo['ContainersStopped']);
AddOrUpdateDockerInfo("Vorhandene Images", VARIABLETYPE_INTEGER, $ParentFolder, $dockerInfo['Images']);
AddOrUpdateDockerInfo("Kernel Version", VARIABLETYPE_STRING, $ParentFolder, $dockerInfo['KernelVersion']);
AddOrUpdateDockerInfo("Betriebssystem", VARIABLETYPE_STRING, $ParentFolder, $dockerInfo['OperatingSystem']);
AddOrUpdateDockerInfo("Betriebsystem Version", VARIABLETYPE_STRING, $ParentFolder, $dockerInfo['OSVersion']);
AddOrUpdateDockerInfo("Docker Version", VARIABLETYPE_STRING, $ParentFolder, $dockerInfo['ServerVersion']);
/* Functions */
function AddOrUpdateContainerDetails($instanceId, $containerDetails)
{
//AddOrUpdateRestartCount($instanceId, $containerDetails);
}
function AddOrUpdateContainerInfos($container)
{
$instanceId = AddOrUpdateInstance($container);
AddOrUpdateCreated($instanceId, $container);
AddOrUpdateState($instanceId, $container);
AddOrUpdateStatus($instanceId, $container);
AddOrUpdateImage($instanceId, $container);
return $instanceId;
}
function AddOrUpdateDockerInfo($propertyName, $type, $parentId, $value)
{
$propertyId = @IPS_GetVariableIDByName($propertyName, $parentId);
if($propertyId === false)
{
$propertyId = IPS_CreateVariable($type);
IPS_SetName($propertyId, $propertyName);
IPS_SetParent($propertyId, $parentId);
}
$oldValue = GetValue($propertyId);
if($oldValue != $value)
SetValue($propertyId, $value);
}
function AddOrUpdateRestartCount($instanceId, $containerDetails)
{
AddOrUpdate($instanceId, $containerDetails, "Neustarts", VARIABLETYPE_INTEGER, "", $containerDetails['RestartCount']);
}
function AddOrUpdateImage($instanceId, $container)
{
AddOrUpdate($instanceId, $container, "Container Image", VARIABLETYPE_STRING, "", $container['Image']);
}
function AddOrUpdateStatus($instanceId, $container)
{
AddOrUpdate($instanceId, $container, "Status", VARIABLETYPE_STRING, "", $container['Status']);
}
function AddOrUpdateState($instanceId, $container)
{
AddOrUpdate($instanceId, $container, "Ausführung", VARIABLETYPE_STRING, "", $container['State']);
}
function AddOrUpdateCreated($instanceId, $container)
{
AddOrUpdate($instanceId, $container, "Erstellt", VARIABLETYPE_INTEGER, "~UnixTimestamp", $container['Created']);
}
function AddOrUpdate($instanceId, $container, $varName, $varType, $varProfile, $arrayProperty)
{
$createdId = @IPS_GetVariableIDByName($varName, $instanceId);
if($createdId === false)
{
$createdId = IPS_CreateVariable($varType);
IPS_SetParent($createdId, $instanceId);
IPS_SetName($createdId, $varName);
IPS_SetVariableCustomProfile($createdId, $varProfile);
}
if(GetValue($createdId) != $arrayProperty)
{
SetValue($createdId, $arrayProperty);
}
}
function GetName($container)
{
return substr($container['Names'][0], 1);
}
function AddOrUpdateInstance($container)
{
global $ParentFolder;
global $DummyInstanceId;
$containerName = GetName($container);
$instanceId = @IPS_GetInstanceIDByName($containerName, $ParentFolder);
if($instanceId === false)
{
$instanceId = IPS_CreateInstance($DummyInstanceId);
IPS_SetParent($instanceId, $ParentFolder);
IPS_SetName($instanceId, $containerName);
}
return $instanceId;
}
/*Credits https://gist.github.com/FergusInLondon/c3eb96f1f6565a81e509ece6b14b9a78 */
class DockerClient {
/** @param resource */
private $curlClient;
/** @param string */
private $socketPath;
/** @param string|null */
private $curlError = null;
/**
* Constructor: Initialises the Curl Resource, making it usable for subsequent
* API requests.
*
* @param string
*/
public function __construct(string $socketPath)
{
$this->curlClient = curl_init();
$this->socketPath = $socketPath;
curl_setopt($this->curlClient, CURLOPT_UNIX_SOCKET_PATH, $socketPath);
curl_setopt($this->curlClient, CURLOPT_RETURNTRANSFER, true);
}
/**
* Deconstructor: Ensure the Curl Resource is correctly closed.
*/
public function __destruct()
{
curl_close($this->curlClient);
}
private function generateRequestUri(string $requestPath)
{
/* Please note that Curl doesn't use http+unix:// or any other mechanism for
* specifying Unix Sockets; once the CURLOPT_UNIX_SOCKET_PATH option is set,
* Curl will simply ignore the domain of the request. Hence why this works,
* despite looking as though it should attempt to connect to a host found at
* the domain "unixsocket". See L14 where this is set.
*
* @see Client.php:L14
* @see https://github.com/curl/curl/issues/1338
*/
return sprintf("http://unixsocket%s", $requestPath);
}
/**
* Dispatches a command - via Curl - to Commander's Unix Socket.
*
* @param string Docker Engine endpoint to hit.
* @param array Data to post to $endpoint.
* @return array JSON decoded response from Commander.
*/
public function dispatchCommand(string $endpoint, array $parameters = null): array
{
curl_setopt($this->curlClient, CURLOPT_URL, $this->generateRequestUri($endpoint));
if (!is_null($parameters)) {
$payload = json_encode($parameters);
curl_setopt($this->curlClient, CURLOPT_POSTFIELDS, $payload);
}
$result = curl_exec($this->curlClient);
if ($result === FALSE) {
$this->curlError = curl_error($this->curlClient);
return array();
}
return json_decode($result, true);
}
/**
* Returns a human readable string from Curl in the event of an error.
*
* @return bool|string
*/
public function getCurlError()
{
return is_null($this->curlError) ? false : $this->curlError;
}
}
Das Resultat sieht ungefähr so aus
Man könnte das natürlich weiter spinnen und Container über IPS Starten, Beenden und Neu starten. Theoretisch könnte man so auch IPS Neu starten. Das wären die nächsten Schritte.
Gruß
Dennis