├── README.md └── php-fpm.php /README.md: -------------------------------------------------------------------------------- 1 | This is a revised version of [Devlopnet / munin-php-fpm](https://github.com/Devlopnet/munin-php-fpm) with the difference that processes are combined based on their pools. Also there is an average process age graph and a process count graph. 2 | 3 | Setup PHP-FPM 4 | ------------- 5 | 6 | This plugin requires PHP CLI. 7 | 8 | ### Install Plugin 9 | `$ cd /usr/share/munin/plugins/` 10 | `$ [sudo] wget -O php-fpm https://raw.github.com/MorbZ/munin-php-fpm/master/php-fpm.php` 11 | `$ [sudo] chmod +x php-fpm` 12 | 13 | ### Setup Graphs 14 | Average process memory per pool: 15 | `$ [sudo] ln -s /usr/share/munin/plugins/php-fpm /etc/munin/plugins/php-fpm-memory` 16 | 17 | CPU per pool: 18 | `$ [sudo] ln -s /usr/share/munin/plugins/php-fpm /etc/munin/plugins/php-fpm-cpu` 19 | 20 | Number of processes per pool: 21 | `$ [sudo] ln -s /usr/share/munin/plugins/php-fpm /etc/munin/plugins/php-fpm-count` 22 | 23 | Average process age per pool: 24 | `$ [sudo] ln -s /usr/share/munin/plugins/php-fpm /etc/munin/plugins/php-fpm-time` 25 | 26 | Don't forget to restart Munin after changing plugins: 27 | `$ [sudo] service munin-node restart` -------------------------------------------------------------------------------- /php-fpm.php: -------------------------------------------------------------------------------- 1 | #!/usr/bin/php 2 | 3 | 0, 36 | 'memory' => 0, 37 | 'cpu' => 0, 38 | 'time' => 0 39 | ); 40 | } 41 | 42 | //add values 43 | $groups[$groupName]['count']++; 44 | $groups[$groupName]['cpu'] += $cpu; 45 | $groups[$groupName]['time'] += timeToSeconds($time); 46 | $groups[$groupName]['memory'] += $ram / 1024; 47 | } 48 | 49 | //check args 50 | if(!isset($argv) || !isset($argv[0])) { 51 | die("Error: No Plugin name provided\n"); 52 | } 53 | $fileCalled = basename($argv[0]); 54 | $isConfig = isset($argv[1]) && $argv[1] == 'config'; 55 | 56 | //which plugin? 57 | switch ($fileCalled) { 58 | // ------------------------------------------------------ 59 | case 'php-fpm-memory': 60 | // ------------------------------------------------------ 61 | $elements = array(); 62 | foreach ($groups as $name=>$array) { 63 | $ramMb = $array['memory'] / $array['count']; 64 | $label = 'Pool ' . $name; 65 | $elements[$name] = array( 66 | 'label' => $label, 67 | 'type' => 'GAUGE', 68 | 'value' => $ramMb 69 | ); 70 | } 71 | $config = array( 72 | 'params' => array( 73 | 'graph_title' => 'PHP-FPM Average Process Memory', 74 | 'graph_vlabel' => 'MB' 75 | ), 76 | 'elements' => $elements 77 | ); 78 | break; 79 | // ------------------------------------------------------ 80 | case 'php-fpm-cpu': 81 | // ------------------------------------------------------ 82 | $elements = array(); 83 | foreach ($groups as $name=>$array) { 84 | $cpu = $array['cpu']; 85 | $label = 'Pool ' . $name; 86 | $elements[$name] = array( 87 | 'label' => $label, 88 | 'type' => 'GAUGE', 89 | 'value' => $cpu 90 | ); 91 | } 92 | $config = array( 93 | 'params' => array( 94 | 'graph_title' => 'PHP-FPM CPU', 95 | 'graph_vlabel' => '%', 96 | 'graph_scale' => 'no' 97 | ), 98 | 'elements' => $elements 99 | ); 100 | break; 101 | // ------------------------------------------------------ 102 | case 'php-fpm-count': 103 | // ------------------------------------------------------ 104 | $elements = array(); 105 | foreach ($groups as $name=>$array) { 106 | $label = 'Pool ' . $name; 107 | $elements[$name] = array( 108 | 'label' => $label, 109 | 'type' => 'GAUGE', 110 | 'value' => $array['count'] 111 | ); 112 | } 113 | $config = array( 114 | 'params' => array( 115 | 'graph_title' => 'PHP-FPM Processes', 116 | 'graph_vlabel' => 'processes' 117 | ), 118 | 'elements' => $elements 119 | ); 120 | break; 121 | // ------------------------------------------------------ 122 | case 'php-fpm-time': 123 | // ------------------------------------------------------ 124 | $elements = array(); 125 | foreach ($groups as $name=>$array) { 126 | $time = round($array['time'] / $array['count']); 127 | $label = 'Pool ' . $name; 128 | $elements[$name] = array( 129 | 'label' => $label, 130 | 'type' => 'GAUGE', 131 | 'value' => $time 132 | ); 133 | } 134 | $config = array( 135 | 'params' => array( 136 | 'graph_title' => 'PHP-FPM Average Process Age', 137 | 'graph_vlabel' => 'seconds', 138 | 'graph_scale' => 'no' 139 | ), 140 | 'elements' => $elements 141 | ); 142 | break; 143 | // ------------------------------------------------------ 144 | default: 145 | die("Error: Unrecognized Plugin name $fileCalled\n"); 146 | } 147 | 148 | //output 149 | ksort($config['elements']); 150 | if ($isConfig) { 151 | //graph params 152 | echo "graph_category PHP-FPM\n"; 153 | foreach($config['params'] as $key=>$value) { 154 | echo $key . ' ' . $value . "\n"; 155 | } 156 | 157 | //element params 158 | foreach($config['elements'] as $element=>$data) { 159 | foreach ($data as $key=>$value) { 160 | if ($key == 'value') continue; 161 | echo $element . '.' . $key . ' ' . $value . "\n"; 162 | } 163 | } 164 | } else { 165 | //element values 166 | foreach ($config['elements'] as $pool=>$element) { 167 | echo $pool . '.value ' . $element['value'] . "\n"; 168 | } 169 | } 170 | 171 | //functions 172 | function timeToSeconds ($time) { 173 | $seconds = 0; 174 | 175 | //days 176 | $parts = explode('-', $time); 177 | if(count($parts) == 2) { 178 | $seconds += $parts[0] * 86400; 179 | $time = $parts[1]; 180 | } 181 | 182 | //hours 183 | $parts = explode(':', $time); 184 | if(count($parts) == 3) { 185 | $seconds += array_shift($parts) * 3600; 186 | } 187 | 188 | //minutes/seconds 189 | $seconds += $parts[0] * 60 + $parts[1]; 190 | return $seconds; 191 | } --------------------------------------------------------------------------------