Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gregorychalenko/01dd5742aad71478ad687c94d1311f62 to your computer and use it in GitHub Desktop.
Save gregorychalenko/01dd5742aad71478ad687c94d1311f62 to your computer and use it in GitHub Desktop.

Revisions

  1. @rlemon rlemon revised this gist Feb 9, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ function GetCpuPercentages($stat1, $stat2) {
    }


    /* get core information */
    /* get core information (snapshot) */
    $stat1 = GetCoreInformation();
    /* sleep on server for one second */
    sleep(1);
  2. @rlemon rlemon revised this gist Feb 9, 2012. 1 changed file with 16 additions and 6 deletions.
    22 changes: 16 additions & 6 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    <?php
    /* Gets individual core information */
    function GetCoreInformation() {
    $data = file('/proc/stat');
    $cores = array();
    @@ -16,7 +17,7 @@ function GetCoreInformation() {
    }
    return $cores;
    }

    /* compares two information snapshots and returns the cpu percentage */
    function GetCpuPercentages($stat1, $stat2) {
    if( count($stat1) !== count($stat2) ) {
    return;
    @@ -36,6 +37,19 @@ function GetCpuPercentages($stat1, $stat2) {
    return $cpus;
    }


    /* get core information */
    $stat1 = GetCoreInformation();
    /* sleep on server for one second */
    sleep(1);
    /* take second snapshot */
    $stat2 = GetCoreInformation();
    /* get the cpu percentage based off two snapshots */
    $data = GetCpuPercentages($stat1, $stat2);



    /* makes a google image chart url */
    function makeImageUrl($title, $data) {
    $url = 'http://chart.apis.google.com/chart?chs=440x240&cht=pc&chco=0062FF|498049|F2CAEC|D7D784&chd=t:';
    $url .= $data['user'] . ',';
    @@ -50,11 +64,7 @@ function makeImageUrl($title, $data) {
    $url .= '&chtt=Core+' . $title;
    return $url;
    }

    $stat1 = GetCoreInformation();
    sleep(1);
    $stat2 = GetCoreInformation();
    $data = GetCpuPercentages($stat1, $stat2);
    /* ouput pretty images */
    foreach( $data as $k => $v ) {
    echo '<img src="' . makeImageUrl( $k, $v ) . '" />';
    }
  3. @rlemon rlemon revised this gist Feb 9, 2012. 1 changed file with 19 additions and 3 deletions.
    22 changes: 19 additions & 3 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -36,10 +36,26 @@ function GetCpuPercentages($stat1, $stat2) {
    return $cpus;
    }

    function makeImageUrl($title, $data) {
    $url = 'http://chart.apis.google.com/chart?chs=440x240&cht=pc&chco=0062FF|498049|F2CAEC|D7D784&chd=t:';
    $url .= $data['user'] . ',';
    $url .= $data['nice'] . ',';
    $url .= $data['sys'] . ',';
    $url .= $data['idle'];
    $url .= '&chdl=User|Nice|Sys|Idle&chdlp=b&chl=';
    $url .= $data['user'] . '%25|';
    $url .= $data['nice'] . '%25|';
    $url .= $data['sys'] . '%25|';
    $url .= $data['idle'] . '%25';
    $url .= '&chtt=Core+' . $title;
    return $url;
    }

    $stat1 = GetCoreInformation();
    sleep(1);
    $stat2 = GetCoreInformation();

    print_r( GetCpuPercentages($stat1, $stat2) );

    $data = GetCpuPercentages($stat1, $stat2);
    foreach( $data as $k => $v ) {
    echo '<img src="' . makeImageUrl( $k, $v ) . '" />';
    }
    ?>
  4. @rlemon rlemon created this gist Feb 9, 2012.
    45 changes: 45 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?php
    function GetCoreInformation() {
    $data = file('/proc/stat');
    $cores = array();
    foreach( $data as $line ) {
    if( preg_match('/^cpu[0-9]/', $line) )
    {
    $info = explode(' ', $line );
    $cores[] = array(
    'user' => $info[1],
    'nice' => $info[2],
    'sys' => $info[3],
    'idle' => $info[4]
    );
    }
    }
    return $cores;
    }

    function GetCpuPercentages($stat1, $stat2) {
    if( count($stat1) !== count($stat2) ) {
    return;
    }
    $cpus = array();
    for( $i = 0, $l = count($stat1); $i < $l; $i++) {
    $dif = array();
    $dif['user'] = $stat2[$i]['user'] - $stat1[$i]['user'];
    $dif['nice'] = $stat2[$i]['nice'] - $stat1[$i]['nice'];
    $dif['sys'] = $stat2[$i]['sys'] - $stat1[$i]['sys'];
    $dif['idle'] = $stat2[$i]['idle'] - $stat1[$i]['idle'];
    $total = array_sum($dif);
    $cpu = array();
    foreach($dif as $x=>$y) $cpu[$x] = round($y / $total * 100, 1);
    $cpus['cpu' . $i] = $cpu;
    }
    return $cpus;
    }

    $stat1 = GetCoreInformation();
    sleep(1);
    $stat2 = GetCoreInformation();

    print_r( GetCpuPercentages($stat1, $stat2) );

    ?>