~ko-de-r-de/zabbix/precise

« back to all changes in this revision

Viewing changes to frontends/php/include/func.inc.php

  • Committer: guntis
  • Date: 2013-08-01 06:46:25 UTC
  • Revision ID: svn-v4:f4e7fd02-d210-4e34-a1e0-d4093fb66cc2:trunk:37472
..F....... [ZBX-4986] absolute urls replaced with relative and fixed commit bugs

Show diffs side-by-side

added added

removed removed

Lines of Context:
544
544
 * Example:
545
545
 *      6442450944 B convert to 6 GB
546
546
 *
547
 
 * @param array  $options
548
 
 * @param string $options['value']
549
 
 * @param string $options['units']
550
 
 * @param string $options['convert']
551
 
 * @param string $options['byteStep']
552
 
 * @param string $options['pow']
553
 
 * @param bool   $options['ignoreMillisec']
554
 
 * @param string $options['length']
 
547
 * @param string $value
 
548
 * @param string $units
 
549
 * @param string $convert
 
550
 * @param string $byteStep
 
551
 * @param string $pow
 
552
 * @param bool $ignoreMillisec
555
553
 *
556
554
 * @return string
557
555
 */
558
 
function convert_units($options = array()) {
559
 
        $defOptions = array(
560
 
                'value' => null,
561
 
                'units' => null,
562
 
                'convert' => ITEM_CONVERT_WITH_UNITS,
563
 
                'byteStep' => false,
564
 
                'pow' => false,
565
 
                'ignoreMillisec' => false,
566
 
                'length' => false
567
 
        );
568
 
 
569
 
        $options = zbx_array_merge($defOptions, $options);
570
 
 
 
556
function convert_units($value, $units, $convert = ITEM_CONVERT_WITH_UNITS, $byteStep = false, $pow = false, $ignoreMillisec = false) {
571
557
        // special processing for unix timestamps
572
 
        if ($options['units'] == 'unixtime') {
573
 
                return zbx_date2str(_('Y.m.d H:i:s'), $options['value']);
 
558
        if ($units == 'unixtime') {
 
559
                return zbx_date2str(_('Y.m.d H:i:s'), $value);
574
560
        }
575
561
 
576
562
        // special processing of uptime
577
 
        if ($options['units'] == 'uptime') {
578
 
                return convertUnitsUptime($options['value']);
 
563
        if ($units == 'uptime') {
 
564
                return convertUnitsUptime($value);
579
565
        }
580
566
 
581
567
        // special processing for seconds
582
 
        if ($options['units'] == 's') {
583
 
                return convertUnitsS($options['value'], $options['ignoreMillisec']);
 
568
        if ($units == 's') {
 
569
                return convertUnitsS($value, $ignoreMillisec);
584
570
        }
585
571
 
586
572
        // any other unit
587
573
        // black list wich do not require units metrics..
588
574
        $blackList = array('%', 'ms', 'rpm', 'RPM');
589
575
 
590
 
        if (in_array($options['units'], $blackList) || (zbx_empty($options['units'])
591
 
                        && ($options['convert'] == ITEM_CONVERT_WITH_UNITS))) {
592
 
                if (abs($options['value']) >= ZBX_UNITS_ROUNDOFF_THRESHOLD) {
593
 
                        $options['value'] = round($options['value'], ZBX_UNITS_ROUNDOFF_UPPER_LIMIT);
 
576
        if (in_array($units, $blackList) || (zbx_empty($units) && ($convert == ITEM_CONVERT_WITH_UNITS))) {
 
577
                if (abs($value) >= ZBX_UNITS_ROUNDOFF_THRESHOLD) {
 
578
                        $value = round($value, ZBX_UNITS_ROUNDOFF_UPPER_LIMIT);
594
579
                }
595
 
                $options['value'] = sprintf('%.'.ZBX_UNITS_ROUNDOFF_LOWER_LIMIT.'f', $options['value']);
596
 
                $options['value'] = preg_replace('/^([\-0-9]+)(\.)([0-9]*)[0]+$/U', '$1$2$3', $options['value']);
597
 
                $options['value'] = rtrim($options['value'], '.');
 
580
                $value = sprintf('%.'.ZBX_UNITS_ROUNDOFF_LOWER_LIMIT.'f', $value);
 
581
                $value = preg_replace('/^([\-0-9]+)(\.)([0-9]*)[0]+$/U', '$1$2$3', $value);
 
582
                $value = rtrim($value, '.');
598
583
 
599
 
                return trim($options['value'].' '.$options['units']);
 
584
                if (zbx_empty($units)) {
 
585
                        return $value;
 
586
                }
 
587
                else {
 
588
                        return $value.' '.$units;
 
589
                }
600
590
        }
601
591
 
602
592
        // if one or more items is B or Bps, then Y-scale use base 8 and calculated in bytes
603
 
        if ($options['byteStep']) {
 
593
        if ($byteStep) {
604
594
                $step = 1024;
605
595
        }
606
596
        else {
607
 
                switch ($options['units']) {
 
597
                switch ($units) {
608
598
                        case 'Bps':
609
599
                        case 'B':
610
600
                                $step = 1024;
611
 
                                $options['convert'] = $options['convert'] ? $options['convert'] : ITEM_CONVERT_NO_UNITS;
 
601
                                $convert = $convert ? $convert : ITEM_CONVERT_NO_UNITS;
612
602
                                break;
613
603
                        case 'b':
614
604
                        case 'bps':
615
 
                                $options['convert'] = $options['convert'] ? $options['convert'] : ITEM_CONVERT_NO_UNITS;
 
605
                                $convert = $convert ? $convert : ITEM_CONVERT_NO_UNITS;
616
606
                        default:
617
607
                                $step = 1000;
618
608
                }
619
609
        }
620
610
 
621
 
        if ($options['value'] < 0) {
622
 
                $abs = bcmul($options['value'], '-1');
623
 
        }
624
 
        else {
625
 
                $abs = $options['value'];
626
 
        }
627
 
 
628
 
        if (bccomp($abs, 1) == -1) {
629
 
                $options['value'] = round($options['value'], ZBX_UNITS_ROUNDOFF_MIDDLE_LIMIT);
630
 
                $options['value'] = ($options['length'] && $options['value'] != 0)
631
 
                        ? sprintf('%.'.$options['length'].'f',$options['value']) : $options['value'];
632
 
 
633
 
                return trim($options['value'].' '.$options['units']);
634
 
        }
635
 
 
636
611
        // init intervals
637
612
        static $digitUnits;
638
613
        if (is_null($digitUnits)) {
641
616
 
642
617
        if (!isset($digitUnits[$step])) {
643
618
                $digitUnits[$step] = array(
 
619
                        array('pow' => -2, 'short' => _x('µ', 'Micro short'), 'long' => _('Micro')),
 
620
                        array('pow' => -1, 'short' => _x('m', 'Milli short'), 'long' => _('Milli')),
644
621
                        array('pow' => 0, 'short' => '', 'long' => ''),
645
622
                        array('pow' => 1, 'short' => _x('K', 'Kilo short'), 'long' => _('Kilo')),
646
623
                        array('pow' => 2, 'short' => _x('M', 'Mega short'), 'long' => _('Mega')),
658
635
                }
659
636
        }
660
637
 
661
 
 
662
 
        $valUnit = array('pow' => 0, 'short' => '', 'long' => '', 'value' => $options['value']);
663
 
 
664
 
        if ($options['pow'] === false || $options['value'] == 0) {
 
638
        if ($value < 0) {
 
639
                $abs = bcmul($value, '-1');
 
640
        }
 
641
        else {
 
642
                $abs = $value;
 
643
        }
 
644
 
 
645
        $valUnit = array('pow' => 0, 'short' => '', 'long' => '', 'value' => $value);
 
646
 
 
647
        if ($pow === false || $value == 0) {
665
648
                foreach ($digitUnits[$step] as $dnum => $data) {
666
649
                        if (bccomp($abs, $data['value']) > -1) {
667
650
                                $valUnit = $data;
673
656
        }
674
657
        else {
675
658
                foreach ($digitUnits[$step] as $data) {
676
 
                        if ($options['pow'] == $data['pow']) {
 
659
                        if ($pow == $data['pow']) {
677
660
                                $valUnit = $data;
678
661
                                break;
679
662
                        }
680
663
                }
681
664
        }
682
665
 
683
 
        if (round($valUnit['value'], ZBX_UNITS_ROUNDOFF_MIDDLE_LIMIT) > 0) {
684
 
                $valUnit['value'] = bcdiv(sprintf('%.10f',$options['value']), sprintf('%.10f', $valUnit['value'])
685
 
                        , ZBX_PRECISION_10);
 
666
        if (round($valUnit['value'], ZBX_UNITS_ROUNDOFF_LOWER_LIMIT) > 0) {
 
667
                if ($valUnit['pow'] >= 0) {
 
668
                        $valUnit['value'] = bcdiv(sprintf('%.6f',$value), sprintf('%.6f', $valUnit['value']),
 
669
                                ZBX_UNITS_ROUNDOFF_LOWER_LIMIT);
 
670
                }
 
671
                else {
 
672
                        $valUnit['value'] = bcdiv(sprintf('%.10f',$value), sprintf('%.10f', $valUnit['value']), ZBX_PRECISION_10);
 
673
                }
686
674
        }
687
675
        else {
688
676
                $valUnit['value'] = 0;
689
677
        }
690
678
 
691
 
        switch ($options['convert']) {
692
 
                case 0: $options['units'] = trim($options['units']);
 
679
        switch ($convert) {
 
680
                case 0: $units = trim($units);
693
681
                case 1: $desc = $valUnit['short']; break;
694
682
                case 2: $desc = $valUnit['long']; break;
695
683
        }
696
684
 
697
 
        $options['value'] = preg_replace('/^([\-0-9]+)(\.)([0-9]*)[0]+$/U','$1$2$3', round($valUnit['value'],
698
 
                ZBX_UNITS_ROUNDOFF_UPPER_LIMIT));
699
 
 
700
 
        $options['value'] = rtrim($options['value'], '.');
 
685
        $value = preg_replace('/^([\-0-9]+)(\.)([0-9]*)[0]+$/U','$1$2$3', round($valUnit['value'], ZBX_UNITS_ROUNDOFF_UPPER_LIMIT));
 
686
        $value = rtrim($value, '.');
701
687
 
702
688
        // fix negative zero
703
 
        if (bccomp($options['value'], 0) == 0) {
704
 
                $options['value'] = 0;
 
689
        if (bccomp($value, 0) == 0) {
 
690
                $value = 0;
705
691
        }
706
692
 
707
 
        return trim(sprintf('%s %s%s', $options['length']
708
 
                ? sprintf('%.'.$options['length'].'f',$options['value'])
709
 
                : $options['value'], $desc, $options['units']));
 
693
        return rtrim(sprintf('%s %s%s', $value, $desc, $units));
710
694
}
711
695
 
712
696
/**