Advertisement
Guest User

Untitled

a guest
Oct 27th, 2018
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.10 KB | None | 0 0
  1. <?php
  2.  
  3. ##|+PRIV
  4. ##|*IDENT=page-status-trafficshaper-queues
  5. ##|*NAME=Status: Traffic Shaper: Queues
  6. ##|*DESCR=Allow access to the 'Status: Traffic Shaper: Queues' page.
  7. ##|*MATCH=status_queues.php*
  8. ##|-PRIV
  9. /*
  10. header("Last-Modified: " . gmdate("D, j M Y H:i:s") . " GMT");
  11. header("Expires: " . gmdate("D, j M Y H:i:s", time()) . " GMT");
  12. header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP/1.1
  13. header("Pragma: no-cache"); // HTTP/1.0
  14. */
  15.  
  16. require_once("guiconfig.inc");
  17. class QueueStats {
  18.     public $queuename;
  19.     public $queuelength;
  20.     public $pps;
  21.     public $bandwidth;
  22.     public $borrows;
  23.     public $suspends;
  24.     public $drops;
  25. }
  26.  
  27. include_once("shaper.inc");
  28.  
  29. $stats = get_queue_stats();
  30.  
  31. $pgtitle = array(gettext("Status"), gettext("Queues"));
  32. $shortcut_section = "trafficshaper";
  33.  
  34. if (!$error): ?>
  35. <form action="status_queues.php" method="post">
  36. <script type="text/javascript">
  37. //<![CDATA[
  38. var refreshrate = 1000;
  39. var queuestathistorylength = 0;
  40. var queuestathistory = [];
  41. var queuestatprevious = [];
  42. var timestampprevious;
  43. var graphstatmax = 0;
  44. events.push(function() {
  45.     $('#updatespeed').on('change', function() {
  46.         refreshrate = $("#updatespeed").val();
  47.     });
  48.  
  49.     function getqueueactivity() {
  50.         var url = "/getqueuestats.php";
  51.         var pars = "format=json";
  52.         $.ajax(
  53.             url,
  54.             {
  55.                 type: 'post',
  56.                 data: pars,
  57.                 complete: activitycallback
  58.             });
  59.     }
  60.  
  61.     function escapeStr(str)
  62.     {
  63.         if (str)
  64.             return str.replace(/([ #;?%&,.+*~\':"!^$[\]()=>|\/@])/g,'\\$1');
  65.         return str;
  66.     }
  67.  
  68.     function activitycallback(transport) {
  69.         setTimeout(getqueueactivity, refreshrate);
  70.         json = transport.responseJSON;
  71.         if (!json) {
  72.             return;
  73.         }
  74.         timestamp = json.timestamp;
  75.         timestampdiff = timestamp - timestampprevious;
  76.         $stattype = $('#selStatistic').val();
  77.  
  78.         interfacename_stats = [];
  79.         for (interfacename in json.interfacestats) {
  80.             var queueparents = [];
  81.             interface = json.interfacestats[interfacename];
  82.             interfacename_stats[interfacename] = [];
  83.             for (queuename in interface) {
  84.                 queue = interface[queuename];
  85.                 statqname = queue['name'] + queue['interface'];
  86.  
  87.                 for(childnr in queue['contains']) {
  88.                     child = queue['contains'][childnr];
  89.                     if (!queueparents[child]) {
  90.                         queueparents[child] = [];
  91.                     }
  92.                     queueparents[child] = queuename;
  93.                 }
  94.  
  95.                 if (queuestatprevious[statqname]) {
  96.                     interfacename_stats[interfacename][statqname] = [];
  97.                     pkts_ps = (queue['pkts'] - queuestatprevious[statqname]['pkts']) / timestampdiff
  98.                     bytes_ps = (queue['bytes'] - queuestatprevious[statqname]['bytes']) / timestampdiff
  99.                     droppedpkts = parseFloat(queue['droppedpkts']);
  100.                     borrows = parseFloat(queue['borrows']);
  101.                     suspends = parseFloat(queue['suspends']);
  102.                     interfacename_stats[interfacename][statqname]['pkts_ps'] = pkts_ps;
  103.                     interfacename_stats[interfacename][statqname]['bytes_ps'] = bytes_ps;
  104.                     interfacename_stats[interfacename][statqname]['borrows'] = borrows;
  105.                     interfacename_stats[interfacename][statqname]['suspends'] = suspends;
  106.                     interfacename_stats[interfacename][statqname]['droppedpkts'] = droppedpkts;
  107.                     interfacename_stats[interfacename][statqname]['qlengthitems'] = queue['qlengthitems'];
  108.                     interfacename_stats[interfacename][statqname]['qlengthsize'] = queue['qlengthsize'];
  109.                     find = queuename;
  110.                     while(queueparents[find]) {
  111.                         // add diff values also to parent queues
  112.                         parentname = queueparents[find];
  113.                         parentqueuename = parentname+interfacename;
  114.                         interfacename_stats[interfacename][parentqueuename]['pkts_ps'] += pkts_ps;
  115.                         interfacename_stats[interfacename][parentqueuename]['bytes_ps'] += bytes_ps;
  116.                         interfacename_stats[interfacename][parentqueuename]['borrows'] += borrows;
  117.                         interfacename_stats[interfacename][parentqueuename]['suspends'] += suspends;
  118.                         interfacename_stats[interfacename][parentqueuename]['droppedpkts'] += droppedpkts;
  119.                         find = parentname;
  120.                     }
  121.                 }
  122.                 queuestatprevious[statqname] = queue;
  123.             }
  124.         }
  125.         // Find max pps/bps needed for any scale bar
  126.         statmax = 0;
  127.         for (interfacename in interfacename_stats) {
  128.             interface = interfacename_stats[interfacename];
  129.             for (queuename in interface) {
  130.                 queue = interface[queuename];
  131.                 if ($stattype == "0") {
  132.                     if (statmax < queue['pkts_ps']) {
  133.                         statmax = queue['pkts_ps'];
  134.                     }
  135.                 } else {
  136.                     if (statmax < queue['bytes_ps']) {
  137.                         statmax = queue['bytes_ps'];
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.         // use a slowly sliding max scale value but do make sure its always large enough to accomodate the largest value..
  143.         if (graphstatmax < statmax) {
  144.             // peek value + 10% keeps a little room for it to increase
  145.             graphstatmax = statmax * 1.1;
  146.         } else {
  147.             // in general make largest bar fill +- 2/3 of the scale
  148.             graphstatmax = (graphstatmax * 20 + statmax * 1.5) / 21;
  149.         }
  150.         // set values on the objects
  151.         for (interfacename in interfacename_stats) {
  152.             interface = interfacename_stats[interfacename];
  153.             for (queuename in interface) {
  154.                 queue = interface[queuename];
  155.                 statqname = escapeStr(queuename);
  156.                 if ($stattype == "0") {
  157.                     $('#queue'+statqname+'width').css('width', (queue['pkts_ps']*100/graphstatmax).toFixed(0) + '%');
  158.                 } else {
  159.                     $('#queue'+statqname+'width').css('width', (queue['bytes_ps']*100/graphstatmax).toFixed(0) + '%');
  160.                 }
  161.                 $('#queue'+statqname+'pps').val(queue['pkts_ps'].toFixed(1));
  162.                 $('#queue'+statqname+'bps').val(formatSpeedBits(queue['bytes_ps']));
  163.                 $('#queue'+statqname+'borrows').val(queue['borrows']);
  164.                 $('#queue'+statqname+'suspends').val(queue['suspends']);
  165.                 $('#queue'+statqname+'drops').val(queue['droppedpkts']);
  166.                 $('#queue'+statqname+'length').val(queue['qlengthitems']+'/'+queue['qlengthsize']);
  167.             }
  168.         }
  169.         timestampprevious = timestamp;
  170.     }
  171.  
  172.     $(document).ready(function() {
  173.         setTimeout(getqueueactivity, 150);
  174.     });
  175. });
  176.  
  177. function formatSpeedBits(speed) {
  178.     // format speed in bits/sec, input: bytes/sec
  179.     if (speed < 125000) {
  180.         return Math.round(speed / 125) + " <?=gettext("Kbps"); ?>";
  181.     }
  182.     if (speed < 125000000) {
  183.         return Math.round(speed / 1250)/100 + " <?=gettext("Mbps"); ?>";
  184.     }
  185.     // else
  186.     return Math.round(speed / 1250000)/100 + " <?=gettext("Gbps"); ?>";  /* wow! */
  187. }
  188. //]]>
  189. </script>
  190. <?php endif;
  191.  
  192. if ($error):
  193. else: ?>
  194.     <div class="panel panel-default">
  195.         <div class="panel-body table-responsive">
  196.             <label class="col-sm-2 control-label">
  197.                 <span>Refresh rate</span>
  198.             </label>
  199.             <div class="col-sm-10">
  200.                 <select id="updatespeed" class="form-control">
  201.                     <option value="500">0.5 <?=gettext("seconds");?></option>
  202.                     <option value="1000" selected>1 <?=gettext("seconds");?></option>
  203.                     <option value="2000">2 <?=gettext("seconds");?></option>
  204.                     <option value="5000">5 <?=gettext("seconds");?></option>
  205.                 </select>
  206.             </div>
  207.         </div>
  208.         <div class="panel-body table-responsive">
  209.             <table class="table table-striped table-hover">
  210.                 <thead>
  211.                     <tr>
  212.                         <th><?=gettext("Queue"); ?></th>
  213.                         <th><?=gettext("Statistics"); ?>
  214.                         </th>
  215.                         <th><?=gettext("PPS"); ?></th>
  216.                         <th><?=gettext("Bandwidth"); ?></th>
  217.                     </tr>
  218.                 </thead>
  219.                 <tbody>
  220. <?php
  221.     $if_queue_list = get_configured_interface_list_by_realif(true);
  222.     processInterfaceQueues($stats, 0, "");
  223. ?>
  224. <?php endif; ?>
  225.                 </tbody>
  226.             </table>
  227.         </div>
  228.     </div>
  229. <br/>
  230.  
  231. <script type="text/javascript">
  232. //<![CDATA[
  233.     function StatsShowHide(classname) {
  234.         var firstrow = $("." + classname).first();
  235.         if (firstrow.is(':visible')) {
  236.             $("." + classname).hide();
  237.         } else {
  238.             $("." + classname).show();
  239.         }
  240.     }
  241. //]]>
  242. </script>
  243. </form>
  244. <?php
  245.  
  246. function processInterfaceQueues($altqstats, $parent_name) {
  247.     global $g;
  248.     global $if_queue_list;
  249.  
  250.     $parent_name = $parent_name . " queuerow" . $altqstats['name'] . convert_real_interface_to_friendly_interface_name($altqstats['interface']);
  251.     $prev_if = $altqstats['interface'];
  252.     if (!is_array($altqstats['interfacestats'])) {
  253.         print("<tr><td>");
  254.         print("No Queue data available");
  255.         print("</td></tr>");
  256.         return;
  257.     }
  258.     foreach ($altqstats['interfacestats'] as $if => $ifq) {
  259.         $parents = array();
  260.         echo "<tr><td colspan=\"8\"><b>Interface " . htmlspecialchars(convert_real_interface_to_friendly_descr($if)) . "</b></td></tr>\n";
  261.         $if_name = "";
  262.         foreach ($if_queue_list as $oif => $real_name) {
  263.             if ($oif == $if) {
  264.                 $if_name = $real_name;
  265.                 break;
  266.             }
  267.         }
  268.         if ($prev_if != $q['interface']) {
  269.             $prev_if = $q['interface'];
  270.         }
  271.         foreach($ifq as $qkey => $q) {
  272.             $parent_name = $if . " queuerow" . $altqstats['name'] . convert_real_interface_to_friendly_interface_name($altqstats['interface']);
  273.             if (isset($q['contains'])) {
  274.                 foreach($q['contains'] as $child) {
  275.                     $parents[$child] = $qkey;
  276.                 }
  277.             }
  278.             $find = $qkey;
  279.             $level = 0;
  280.             while(isset($parents[$find])) {
  281.                 $find = $parents[$find];
  282.                 $level++;
  283.                 $parent_name = $parent_name . " queuerow" . $find . $q['interface'];
  284.             }
  285.             $qfinterface = convert_real_interface_to_friendly_interface_name($q['interface']);
  286.             $qname = str_replace($q['interface'], $qfinterface, $q['name']);
  287. ?>
  288.             <tr class="<?=$parent_name;?>">
  289.                 <td class="<?=$row_class?>" style="padding-left:<?=$level * 20?>px;">
  290.                     <?php
  291.                     if (is_array($q['contains'])) {
  292.                         echo "<a href=\"#\" onclick=\"StatsShowHide('queuerow{$qname}{$qfinterface}');return false\">+/-</a>";
  293.                     }
  294.                     if (strstr($qname, "root_")) {
  295.                         echo "<a href=\"firewall_shaper.php?interface={$if_name}&amp;queue={$if_name}&amp;action=show\">Root queue</a>";
  296.                     } else {
  297.                         echo "<a href=\"firewall_shaper.php?interface={$if_name}&amp;queue={$qname}&amp;action=show\">" . htmlspecialchars($qname) . "</a>";
  298.                     }
  299.                     ?>
  300.                 </td>
  301. <?php
  302.             $cpuUsage = 0;
  303.             $stat_prefix = "queue" . $q['name'] . $q['interface'];
  304.             print('<td>');
  305.             print('<div class="progress" style="height: 7px;width: 170px;">');
  306.             print('     <div class="progress-bar" role="progressbar" id="' . $stat_prefix . 'width" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" style="width: ' . $cpuUsage*100 . '%;"></div>');
  307.             print('   </div>');
  308.             print('</td>');
  309.             print('<td><input readonly style="border:0;width:70px;text-align:right;" name="' . $stat_prefix . 'pps"      id="' . $stat_prefix . 'pps"      value="(' . gettext("Loading") . ')" /></td>');
  310.             print('<td><input readonly style="border:0;width:80px;text-align:right;" name="' . $stat_prefix . 'bps"      id="' . $stat_prefix . 'bps"      value="" /></td>');
  311. ?>
  312.             </tr>
  313. <?php
  314.             if (is_array($q['queue'])) {
  315.                 processInterfaceQueues($q, $level + 1, $parent_name);
  316.             }
  317.         }
  318.     };
  319. }
  320.  
  321. function statsQueues($xml) {
  322.     global $statistics;
  323.  
  324.     $fname = convert_real_interface_to_friendly_interface_name($xml['interface']);
  325.     $qname = str_replace($xml['interface'], $fname, $xml['name']);
  326.  
  327.     $current = new QueueStats();
  328.     $child = new QueueStats();
  329.     $current->queuename = $qname . $fname;
  330.     $current->queuelength = $xml['qlength'];
  331.     $current->pps = $xml['measured'];
  332.     $current->bandwidth = $xml['measuredspeedint'];
  333.     $current->borrows = intval($xml['borrows']);
  334.     $current->suspends = intval($xml['suspends']);
  335.     $current->drops = intval($xml['droppedpkts']);
  336.     if (is_array($xml['queue'])) {
  337.         foreach ($xml['queue'] as $q) {
  338.             $child = statsQueues($q);
  339.             $current->pps += $child->pps;
  340.             $current->bandwidth += $child->bandwidth;
  341.         }
  342.     }
  343.     unset($child);
  344.     $statistics[] = $current;
  345.     return $current;
  346. }
  347. function format_bits($bits) {
  348.     if ($bits >= 1000000000) {
  349.         return sprintf("%.2f Gbps", $bits/1000000000);
  350.     } else if ($bits >= 1000000) {
  351.         return sprintf("%.2f Mbps", $bits/1000000);
  352.     } else if ($bits >= 1000) {
  353.         return sprintf("%.2f Kbps", $bits/1000);
  354.     } else {
  355.         return sprintf("%d bps", $bits);
  356.     }
  357. }
  358. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement