summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/linux-health/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:23:16 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:23:16 +0000
commit3e97c51418e6d27e9a81906f347fcb7c78e27d4f (patch)
treeee596ce1bc9840661386f96f9b8d1f919a106317 /vendor/gipfl/linux-health/src
parentInitial commit. (diff)
downloadicingaweb2-module-incubator-3e97c51418e6d27e9a81906f347fcb7c78e27d4f.tar.xz
icingaweb2-module-incubator-3e97c51418e6d27e9a81906f347fcb7c78e27d4f.zip
Adding upstream version 0.20.0.upstream/0.20.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gipfl/linux-health/src')
-rw-r--r--vendor/gipfl/linux-health/src/Cpu.php59
-rw-r--r--vendor/gipfl/linux-health/src/Memory.php52
-rw-r--r--vendor/gipfl/linux-health/src/Network.php36
3 files changed, 147 insertions, 0 deletions
diff --git a/vendor/gipfl/linux-health/src/Cpu.php b/vendor/gipfl/linux-health/src/Cpu.php
new file mode 100644
index 0000000..43f2e50
--- /dev/null
+++ b/vendor/gipfl/linux-health/src/Cpu.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace gipfl\LinuxHealth;
+
+class Cpu
+{
+ public static function getCounters($procFile = '/proc/stat')
+ {
+ $info = [];
+ $cpus = [];
+
+ $cpuKeys = [ // From 'man proc':
+ 'user', // Time spent in user mode.
+ 'nice', // Time spent in user mode with low priority (nice).
+ 'system', // Time spent in system mode.
+ 'idle', // Time spent in the idle task.
+ // This value should be USER_HZ times the second entry in the
+ // /proc/uptime pseudo-file.
+ 'iowait', // Time waiting for I/O to complete. (Linux >= 2.5.41)
+ 'irq', // Time servicing interrupts. (Linux >= 2.6.0-test4)
+ 'softirq', // Time servicing softirqs. (Linux >= 2.6.0-test4)
+ 'steal', // Stolen time, which is the time spent in other operating
+ // systems when running in a virtualized environment
+ // (Linux >= 2.6.11)
+ 'guest', // Time spent running a virtual CPU for guest operating systems
+ // under the control of the Linux kernel. (Linux >= 2.6.24)
+ 'guest_nice' // Time spent running a niced guest (virtual CPU for guest
+ // operating systems under the control of the Linux kernel).
+ // (Linux >= 2.6.33)
+ ];
+
+ // TODO:
+ // ctxt 891299797 -> The number of context switches that the system underwent
+ // btime 1540828526 -> boot time, in seconds since the Epoch
+ // processes 2079015 -> Number of forks since boot
+ // procs_running 6 -> Number of processes in runnable state
+ // procs_blocked 0 -> Number of processes blocked waiting for I/O to complete
+
+ foreach (file($procFile, FILE_IGNORE_NEW_LINES) as $line) {
+ $parts = preg_split('/\s+/', $line);
+ $key = array_shift($parts);
+ if (substr($key, 0, 3) === 'cpu') {
+ // TODO: handle count mismatch
+ $cpus[$key] = array_combine(
+ array_slice($cpuKeys, 0, count($parts), true),
+ $parts
+ );
+
+ for ($i = count($cpus[$key]) - 1; $i < count($cpuKeys); $i++) {
+ $cpus[$key][$cpuKeys[$i]] = 0;
+ }
+ } else {
+ $info[$key] = $parts;
+ }
+ }
+
+ return $cpus;
+ }
+}
diff --git a/vendor/gipfl/linux-health/src/Memory.php b/vendor/gipfl/linux-health/src/Memory.php
new file mode 100644
index 0000000..0c6f197
--- /dev/null
+++ b/vendor/gipfl/linux-health/src/Memory.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace gipfl\LinuxHealth;
+
+class Memory
+{
+ protected static $pageSize;
+
+ public static function getUsageForPid($pid)
+ {
+ $pid = (int) $pid;
+ $content = @file_get_contents("/proc/$pid/statm");
+ if ($content === false) {
+ return false;
+ }
+
+ $pageSize = static::getPageSize();
+ if ($pageSize === null) {
+ return false;
+ }
+ $parts = explode(' ', $content);
+
+ return (object) [
+ 'size' => $pageSize * (int) $parts[0],
+ 'rss' => $pageSize * (int) $parts[1],
+ 'shared' => $pageSize * (int) $parts[3],
+ ];
+ }
+
+ /**
+ * @return int
+ */
+ public static function getPageSize()
+ {
+ if (self::$pageSize === null) {
+ $output = trim(`getconf PAGESIZE 2>&1`);
+ if (strlen($output)) {
+ self::$pageSize = (int) $output;
+ }
+ }
+
+ return self::$pageSize;
+ }
+
+ /**
+ * @param int $pageSize
+ */
+ public static function setPageSize($pageSize)
+ {
+ self::$pageSize = (int) $pageSize;
+ }
+}
diff --git a/vendor/gipfl/linux-health/src/Network.php b/vendor/gipfl/linux-health/src/Network.php
new file mode 100644
index 0000000..e0bad7d
--- /dev/null
+++ b/vendor/gipfl/linux-health/src/Network.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace gipfl\LinuxHealth;
+
+class Network
+{
+ public static function getInterfaceCounters($procFile = '/proc/net/dev')
+ {
+ // Header looks like this:
+ // Inter-| Receive | Transmit
+ // face |bytes packets errs drop fifo frame compressed multicast|bytes packets
+ // (...from above line) errs drop fifo colls carrier compressed
+
+ $lines = \file($procFile, FILE_IGNORE_NEW_LINES);
+ \array_shift($lines);
+ $headers = preg_split('/\|/', array_shift($lines));
+ $rxHeaders = preg_split('/\s+/', $headers[1]);
+ $txHeaders = preg_split('/\s+/', $headers[2]);
+
+ $headers = [];
+ foreach ($rxHeaders as $rx) {
+ $headers[] = 'rx' . ucfirst($rx);
+ }
+ foreach ($txHeaders as $tx) {
+ $headers[] = 'tx' . ucfirst($tx);
+ }
+ $interfaces = [];
+ foreach ($lines as $line) {
+ $parts = preg_split('/\s+|\|/', trim($line));
+ $ifName = rtrim(array_shift($parts), ':');
+ $interfaces[$ifName] = (object) array_combine($headers, $parts);
+ }
+
+ return $interfaces;
+ }
+}