Skip to content

Instantly share code, notes, and snippets.

@auckenox
Created October 13, 2018 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auckenox/d875c6545169f0ff6878e574119bb824 to your computer and use it in GitHub Desktop.
Save auckenox/d875c6545169f0ff6878e574119bb824 to your computer and use it in GitHub Desktop.
php parse iostat data and outputs it as json
<?php
/* requires sysstat: apt install sysstat */
function parseIostatLine($line)
{
$re = '/(?\'device\'[a-z0-9]{3}) \s* (?\'tps\'[\d\.]*) \s* (?\'readS\'[\d\.]*)\s*(?\'writeS\'[\d\.]*)\s*(?\'read\'[\d\.]*)\s*(?\'write\'[\d\.]*)/m';
preg_match_all($re, $line, $matches, PREG_SET_ORDER, 0);
if($matches[0]['read']==''&&$matches[0]['write']==''){return false;}
return array(
"device"=>$matches[0]['device'],
"write_per_second"=>$matches[0]['writeS'],
"read_per_second"=>$matches[0]['readS'],
"write"=>$matches[0]['write'],
"read"=>$matches[0]['read'],
"tps"=>$matches[0]['tps'] // transactions per second
);
}
$cmd = '/usr/bin/iostat -d -m';
exec($cmd,$lines,$ecode);
foreach ($lines as $line) {
$io_res = parseIostatLine($line);
$device=$io_res['device'];
if($device=='') {continue;}
$output[$device] = $io_res;
}
// json output
header("Content-Type: application/json; charset=utf-8");
echo json_encode($output);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment