Instead of parsing the text output from the supervisorctl
command, you could talk directly to the supervisord
process using XML-RPC. The supervisor documentation includes the supported API calls.
It's easiest if you configure the daemon to listen on a local HTTP server; let's assume you configured it to listen on the default port 9001 on localhost, so you can connect over HTTP to localhost:9001. PHP has built-in support for XML-RPC but you do need to enable the feature. If you installed PHP using your system's package manager, look for a php-xmlrpc
package to install. Alternatively, install a pure PHP XML-RPC package like polyfill-xmlrpc.
To get the status of all managed processes, use the supervisor.getAllProcessInfo()
method; this returns an array of processes, and you'll need the name
and statename
columns from those:
define('SUPERVISOR_RPC_URL', 'http://localhost:9001/RPC2');
function supervisor_states() {
$request = xmlrpc_encode_request("supervisor.getAllProcessInfo", array());
$context = stream_context_create(['http' => [
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request
]]);
$file = file_get_contents(SUPERVISOR_RPC_URL, false, $context);
$response = xmlrpc_decode($file);
if (is_array($response) && xmlrpc_is_fault($response)) {
// Consider creating a custom exception class for this.
throw new Exception($response['faultString'], $response['faultCode']);
}
return array_map(
function($proc) {
return ['name'=>$proc['name'], 'status'=>$proc['statusname']];
},
$response,
);
}
This returns an multidimensional array with name
and status
keys. The latter is the process state, see the Process States documentation for possible values.