File: class.docker.inc.php

package info (click to toggle)
phpsysinfo 3.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,900 kB
  • sloc: javascript: 22,511; php: 20,651; xml: 18,293; sh: 196; python: 58; makefile: 12
file content (134 lines) | stat: -rw-r--r-- 5,160 bytes parent folder | download | duplicates (2)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
 * Docker plugin, which displays docker informations
 *
 * @category  PHP
 * @package   PSI_Plugin_Docker
 * @author    Mieczyslaw Nalewaj <namiltd@users.sourceforge.net>
 * @copyright 2014 phpSysInfo
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
 * @version   Release: 1.0
 * @link      http://phpsysinfo.sourceforge.net
 */

class Docker extends PSI_Plugin
{
    private $_lines;

    public function __construct($enc)
    {
        parent::__construct(__CLASS__, $enc);

        $this->_lines = array();
    }

    /**
     * get docker information
     *
     * @return array docker in array with label
     */

    private function getDocker()
    {
        $result = array();
        $i = 0;

        foreach ($this->_lines as $line) {
            if ($i > 0) {
                $buffer = preg_split("/\s\s+/", $line);
                $result[$i]['Name'] = $buffer[0];
                $result[$i]['CPUUsage'] = str_replace(',', '.', trim($buffer[1], '%'));
                preg_match('/([\d\.]+)(B|KiB|MiB|GiB|TiB|PiB)\s+\/\s+([\d\.]+)(B|KiB|MiB|GiB|TiB|PiB)/', str_replace(',', '.', trim($buffer[2])), $tmpbuf);
                switch ($tmpbuf[2]) {
                case 'B':
                    $result[$i]['MemoryUsed'] = $tmpbuf[1];
                    break;
                case 'KiB':
                    $result[$i]['MemoryUsed'] = 1024*$tmpbuf[1];
                    break;
                case 'MiB':
                    $result[$i]['MemoryUsed'] = 1024*1024*$tmpbuf[1];
                    break;
                case 'GiB':
                    $result[$i]['MemoryUsed'] = 1024*1024*1024*$tmpbuf[1];
                    break;
                case 'TiB':
                    $result[$i]['MemoryUsed'] = 1024*1024*1024*1024*$tmpbuf[1];
                    break;
                case 'PiB':
                    $result[$i]['MemoryUsed'] = 1024*1024*1024*1024*1025*$tmpbuf[1];
                }
                switch ($tmpbuf[4]) {
                case 'B':
                    $result[$i]['MemoryLimit'] = $tmpbuf[3];
                    break;
                case 'KiB':
                    $result[$i]['MemoryLimit'] = 1024*$tmpbuf[3];
                    break;
                case 'MiB':
                    $result[$i]['MemoryLimit'] = 1024*1024*$tmpbuf[3];
                    break;
                case 'GiB':
                    $result[$i]['MemoryLimit'] = 1024*1024*1024*$tmpbuf[3];
                    break;
                case 'TiB':
                    $result[$i]['MemoryLimit'] = 1024*1024*1024*1024*$tmpbuf[3];
                    break;
                case 'PiB':
                    $result[$i]['MemoryLimit'] = 1024*1024*1024*1024*1025*$tmpbuf[3];
                }
                $result[$i]['MemoryUsage'] = str_replace(',', '.', trim($buffer[3], '%'));
                $result[$i]['NetIO'] = trim($buffer[4]);
                $result[$i]['BlockIO'] = trim($buffer[5]);
                $result[$i]['PIDs'] = trim($buffer[6]);
            }
            $i++;
        }

        return $result;
    }

    public function execute()
    {
        $this->_lines = array();
        if ((PSI_OS != 'WINNT') && (!defined('PSI_EMU_HOSTNAME') || defined('PSI_EMU_PORT'))) {
            switch (strtolower(PSI_PLUGIN_DOCKER_ACCESS)) {
            case 'command':
                $lines = "";
                if (CommonFunctions::executeProgram('docker', 'stats --no-stream --format \'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDs}}\'', $lines) && !empty($lines))
                    $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
                break;
            case 'data':
                if (!defined('PSI_EMU_HOSTNAME') && CommonFunctions::rftsdata("docker.tmp", $lines) && !empty($lines))
                    $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
                break;
            default:
                $this->global_error->addConfigError("execute()", "[docker] ACCESS");
            }
        }
    }

    public function xml()
    {
        if (empty($this->_lines))
            return $this->xml->getSimpleXmlElement();

        $arrBuff = $this->getDocker();
        if (sizeof($arrBuff) > 0) {
            $docker = $this->xml->addChild("Docker");
            foreach ($arrBuff as $arrValue) {
                $item = $docker->addChild('Item');
                $item->addAttribute('Name', $arrValue['Name']);
                $item->addAttribute('CPUUsage', $arrValue['CPUUsage']);
                $item->addAttribute('MemoryUsage', $arrValue['MemoryUsage']);
                $item->addAttribute('MemoryUsed', Round($arrValue['MemoryUsed']));
                $item->addAttribute('MemoryLimit', Round($arrValue['MemoryLimit']));
                $item->addAttribute('NetIO', $arrValue['NetIO']);
                $item->addAttribute('BlockIO', $arrValue['BlockIO']);
                $item->addAttribute('PIDs', $arrValue['PIDs']);
            }
        }

        return $this->xml->getSimpleXmlElement();
    }
}