Repository
Munin (contrib)
Last change
2017-02-28
Graph Categories
Capabilities
Keywords
Language
PHP

multicpu

Sadly there is no documentation for this plugin.

#!/usr/bin/php
<?php
  /*
    multicpu monitors the cpu usage of multiple processes.
    you can configure the list of processes via munin's configuration

    [multicpu]
      env.ps_filter apache2 mysqld

    I have very little knowledge about munin plugins, it just 'works fine for me'.
    Please feel free to improve :)

    0.1 initial release - Florian Fida
  */

  // make sure that paht to ps is valid for your system
  $ps_cmd = '/bin/ps -eo "%C,%a"';

  // list of processes to monitor, can also be set by env.ps_filter in munin config
  $ps_filter = array(
    'apache2', 'mysqld', 'exim', 'php', 'perl', 'ruby', 'bftpd'
  );


  $cmdl = implode(' ', $argv);
  $config = stripos($cmdl, 'config') !== false;

  if(!empty($_ENV['ps_filter'])){
    $ps_filter = explode(' ', $_ENV['ps_filter']);
  }

  if($config){
    $out  = "multigraph multicpu_pcpu\n";
    $out .= "graph_title CPU usage per process\n";
    $out .= "graph_vlabel CPU usage [%]\n";
    $out .= "graph_category processes\n";
    foreach($ps_filter as $process) $out .= "$process.label $process\n";

    $out .= "multigraph multicpu_cnt\n";
    $out .= "graph_title Instances per process\n";
    $out .= "graph_vlabel count\n";
    $out .= "graph_category processes\n";
    foreach($ps_filter as $process) $out .= "$process.label $process\n";

    echo $out."\n";
    exit;
  }

  $ps_txt = shell_exec($ps_cmd);
  $ps_a = explode("\n", $ps_txt);

  $result = array();
  $result_cnt = array();
  foreach($ps_filter as $process){
    if(!isset($result[$process])) $result[$process] = 0;
    if(!isset($result_cnt[$process])) $result_cnt[$process] = 0;
    foreach($ps_a as $line){
      $line_a = explode(',', $line, 2);
      if(count($line_a) == 2){
        if(stripos($line_a[1], $process) !== false){
          $result[$process] += floatval($line_a[0]);
          ++$result_cnt[$process];
        }
      }
    }
  }

  echo "multigraph multicpu_pcpu\n";
  foreach($result as $process => $value) echo "$process.value $value\n";
  echo "multigraph multicpu_cnt\n";
  foreach($result_cnt as $process => $value) echo "$process.value $value\n";