% :) Example: (:calc-avail 99.99% :) * Form 2: (:calc-avail mtbf= mttr= :) Example: (:calc-avail mtbf=5.5y mttr=12h :) * Form 3: (:calc-avail :) Example: (:calc-avail :) The arguments are used as follows A number 0-100 of availability in percent, e.g. 99.9. % The percent sign after the number is mandatory. A number (e.g. 12.5) indicating mean time between failure. A mandatory character indicating the unit, where 'y' = Year (365 days per week) 'm' = Month (30 days per week) 'w' = Week (7 days/per week) 'd' = Day 'h' = Hour (24 hours per day) A number (e.g. .5) indicating mean time to repeair. A character indicating the unit, same as described above. */ $RecipeInfo['CalculateAvailability']['Version'] = '2006-12-02'; // Define the markup for (:calc-avail ... :) Markup('calculate_availability', 'directives', '/\\(:calc-avail\\s+(.*?)\\s*:\\)/ei', "CalculateAvailability(\$pagename, PSS('$1'))"); // The array $CalculateAvailabilityD stores all global data // used by this recipe. The memeber 'current' contains the // currently estimated availability. SDVA($CalculateAvailabilityD, array('current'=>1.0)); // // Function: Calculate availability given arguments from the // the directive (:calc-avail ... :). See the top of this // script and the recipe page for details about the syntax and use. // // Note that actual calculations when using MTBF/MTTR are // done in units as specified for the MTBF. This is to avoid // numerical problems although it could probably be achieved // using some other way. // function CalculateAvailability($pagename, $arguments) { global $CalculateAvailabilityD; $defaults = array('fmt'=>'default'); $args = array_change_key_case( array_merge($defaults, ParseArgs($arguments))); if(array_key_exists('mttr', $args)) { // Use MTTR and MTBF $mttr = CalculateAvailabilityGetNumber($args['mttr']); $mtbf = CalculateAvailabilityGetNumber($args['mtbf']); if(!$mttr['unit'] || !$mtbf['unit']) return "Both mttr and mtbf must have units!"; $u = $mtbf['unit']; $A = $mtbf[$u] / ($mtbf[$u] + $mttr[$u]); $CalculateAvailabilityD['current'] *= $A; $s = sprintf("(%-10s MTTR=%s)", sprintf('MTBF=%s,', $args['mtbf']), $args['mttr']); $s .= sprintf(' %.3f%% ', $A*100); } elseif(count($args['']) == 0) { $s = sprintf('%.2f%% ', $CalculateAvailabilityD['current'] * 100); $CalculateAvailabilityD['current'] = 1.0; } else { $x = implode('', $args['']); if(substr($x, -1) != '%') return "A separate availability argument must end in '%'"; $A = $x * 0.01; $CalculateAvailabilityD['current'] *= $A; $s = sprintf('%.3f%% ', $A*100); } return "$s"; } function CalculateAvailabilityGetNumber($x) { $toDays = array('h'=>1/24, 'd'=>1, 'w'=>7, 'm'=>30, 'y'=>365); if(preg_match('/([0-9.]+)([hHdDwWmMyY])/', $x, $matches)) { $d = $matches[1] * $toDays[strtolower($matches[2])]; $res = array('h'=>$d*24, 'd'=>$d, 'w'=>$d/7, 'm'=>$d/30, 'y'=>$d/365, 'unit'=>strtolower($matches[2])); } else { $res = array('h'=>$x, 'unit' => false); } return $res; } ?>