1

How can i populate a variable holding the total value from below content extracted after running df -h --total on centos. Note: the result is not from command line but from a php file.

How do i only capture the percentage value in the last line using PHP?

Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 20G 685M 20G 4% /
devtmpfs 40G 0 40G 0% /dev
tmpfs 40G 0 40G 0% /dev/shm
tmpfs 40G 177M 40G 1% /run
tmpfs 40G 0 40G 0% /sys/fs/cg
/dev/mapper/rhel-usr 20G 3.2G 17G 16% /usr
/dev/sda2 1014M 223M 792M 22% /boot
/dev/mapper/rhel-opt_mycom 100G 37G 63G 37% /opt/my
/dev/mapper/oradb-nimsdb 2.0T 1.5T 614G 71% /opt/my/data
/dev/mapper/redo-logs 305G 72G 234G 24% /opt/my/data/
/dev/mapper/rhel-home 20G 472M 20G 3% /home
/dev/mapper/rhel-var 20G 1.4G 19G 7% /var
tmpfs 7.9G 0 7.9G 0% /run/user/1000
total 3.6T 2.3T 1.4T 63% -

I.E. i would want a variable that has the value 63%

$variable = 63%
Omari Victor Omosa
  • 2,814
  • 2
  • 24
  • 46

3 Answers3

5

You can add grep and awk to extract the info you want:

$result = $ssh->exec("df -h --total | grep '^total ' | awk '{print $5}'");

Note that you need to change the types of quoting you are doing for this to work properly. Just copy and paste the above and you should be good.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
2

Perhaps you can adapt this snippet for your needs:

$data = <<<EOF
Filesystem           1K-blocks      Used Available Use% Mounted on
ubi0_0                  143180     89740     53440  63% /
tmpfs                       64         0        64   0% /dev
tmpfs                   143124        76    143048   0% /tmp
tmpfs                     4096       912      3184  22% /var
tmpfs                       64         0        64   0% /mnt
ubi1_0                  468256     12144    456112   3% /opt/data/settings
EOF;

print_r(array_map(function($line){
    $elements=preg_split('/\s+/',$line);
    return(array(
    'filesystem' => $elements[0],
    '1k-blocks' => $elements[1],
    'used' => $elements[2],
    'available' => $elements[3],
    'use%' => $elements[4],
    'mounted_on' => $elements[5]
    ));
},explode("\n",$data)));

https://gist.github.com/cschaba/4740323

Nazariy
  • 6,028
  • 5
  • 37
  • 61
1

sorry if i misunderstood the question how ever i think this could be the answer you are looking for... :)

Version 1:

#executes the comand and stores in array
$testExec = exec("df -h --total" ,$testArray);
#moves to the end off the array
$testArrayEnd = end($testExec);
#splits the last line into an array
$stringArray = explode(' ',$testArrayEnd);
foreach($stringArray as $string){
    #checks if the string contains a % 
    if (preg_match("/%/", $string)) {
        #there is only one % so this is what you're looking for
        $percentage = $string;
        var_dump($percentage);
    }
}

PS. @ReynierPM you don't require a shell_exec to get a full output return... you just have to define a array where you want to store the data in... :) and granted its not a string but you could easily transform it into one by using implode()

Version 2:

Sorry If you disagree but i don't feel comfortable just editing @Nazariy answer as I'm adding/changing to much (at this stage see edit history:).

#thanks go out to ReynierPM
#returns string with every entry being separated by a newline 
$output = shell_exec('df -h --total');

$ArrayFull=(array_map(function($line){ /*<-- *¹ & *² */
    $elements=preg_split('/\s+/',$line); /*<--- *4  */>
    return(array(
    'filesystem' => $elements[0],
    '1k-blocks' => $elements[1],
    'used' => $elements[2],
    'available' => $elements[3],
    'use%' => $elements[4],
    'mounted_on' => $elements[5]
    ));
},explode("\n",$output))); /*(<--- *³)*/

#removes bloat
unset($ArrayFull[0]);
#Rebase array keys https://stackoverflow.com/questions/5943149/rebase-array-keys-after-unsetting-elements
$ArrayFull=array_values($ArrayFull);

#if you only want the last value ;)
$lastVallue = end($ArrayFull);

Explanation:


array_map applies it too all values in the array "array_map — Applies the callback to the elements of the given arrays"

We first give it a callback function which will be called for every element, and pass it the variable $line (we use this to store the line created by the explode)

We use the array_maps to explode by \n (to create a array entry for evry new line) (We offcourse do an explode on $data)
*4
So now that every line is separated... We split the separated lines into substrings and store them in new variables. preg_split('/\s+/',$line) splitting $line into an array without having to deal with the issue of multiple whitspaces. s standing for space.

Sorry for the formatting... will edit latter :)

  • Thank you for your answer since i can get to my final answer having the percentage value. Although i have mixed your answer with @sean_bright. i am going through your version 2 to see if i can learn from – Omari Victor Omosa Jun 12 '19 at 16:01
  • @OmariVictorOmosa Just glad i could help :) ... To be fair the answer given by sean_bright is just a lot more slick... If you have any further question concerning the code snippet just ask... – Lost Demondragon Jun 14 '19 at 17:18