Created
February 7, 2013 16:35
-
-
Save furkanmustafa/4732200 to your computer and use it in GitHub Desktop.
PHP Function for parsing vmstat output
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 | |
// TEST | |
// $res[] = "procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----"; | |
// $res[] = " r b swpd free buff cache si so bi bo in cs us sy id wa"; | |
// $res[] = " 0 0 0 10376 148188 291056 0 0 4 29 85 9 1 0 98 1"; | |
// // or | |
// // exec('vmstat', $res); | |
// print_r(parseVMStatOutput($res)); | |
function parseVMStatOutput($lines) { | |
$titles = explode(' ', $lines[0]); | |
foreach ($titles as $n => $_t) { | |
$titles[$n] = array( | |
'name'=>trim($_t, '-'), | |
'len'=>strlen($_t) | |
); | |
} | |
$subtitleLine = $lines[1]; | |
foreach ($titles as $n => $_t) { | |
$subtitles = substr($subtitleLine, 0, $_t['len']); | |
$subtitleLine = substr($subtitleLine, $_t['len']+1); | |
preg_match_all('/(?:[ \t]*([0-9a-zA-Z]+))/', $subtitles, $subtitleMatch); | |
$titles[$n]['subtitles'] = $subtitleMatch[1]; | |
} | |
$results = array(); | |
for ($i = 2; $i<count($lines); $i++) { | |
preg_match_all('/(?:[ \t]*([0-9a-zA-Z]+))/', $lines[$i], $valuesMatch); | |
$values = $valuesMatch[1]; | |
$_line = array(); | |
$j = 0; | |
foreach ($titles as $_t) { | |
foreach ($_t['subtitles'] as $_s) { | |
$_line[$_t['name']][$_s] = $values[$j++]; | |
} | |
} | |
$results[] = $_line; | |
} | |
return count($results)==1 ? $results[0] : $results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
poor function
i catch only 9 parameters from 19
Array
(
[procs] => Array
(
[r] => 0
[b] => 0
[w] => 0
)
)
and if u using regex, it is poor performance