PS API

Code

php
├── api
│   └── v1
│       ├── index.php
│       └── util.php
└── docker-compose.yml

docker-compose.yml:

version: "3"

services:
  web:
    container_name: web
    image: php:7.3-apache
    ports:
      - 8080:80
    volumes:
      - ./:/var/www/html/

api/v1/index.php:

<?php

require 'util.php';

$result = shell_exec("ps aux");
$ps = ps_encode($result);

header("Content-type: application/json; charset=UTF-8");
header("Access-Control-Allow-Origin: *");
echo json_encode($ps);

api/v1/util.php:

<?php

function ps_encode($result)
{
  $processes = [];

  $regex = "/(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.+)/";
  preg_match_all($regex, $result, $matches);

  foreach ($matches[1] as $index => $user) {
    $processes[] = [
      "user"    => $matches[1][$index],
      "pid"     => $matches[2][$index],
      "cpu"     => $matches[3][$index],
      "mem"     => $matches[4][$index],
      "vsz"     => $matches[5][$index],
      "rss"     => $matches[6][$index],
      "tty"     => $matches[7][$index],
      "stat"    => $matches[8][$index],
      "start"   => $matches[9][$index],
      "time"    => $matches[10][$index],
      "command" => $matches[11][$index],
    ];
  }

  return $processes;
}