Created
October 13, 2018 15:24
-
-
Save auckenox/d875c6545169f0ff6878e574119bb824 to your computer and use it in GitHub Desktop.
php parse iostat data and outputs it as json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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