show_dhcp_stats.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. #
  3. # Copyright (c) 2017-2018 Joe Clarke <jclarke@cisco.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. $contents = file_get_contents("/home/jclarke/dhcp_scope_stats.dat");
  27. $sarr = json_decode($contents, true);
  28. $scopes = array();
  29. foreach ($sarr as $k => $v) {
  30. $scopes[$k] = $v['perc'];
  31. }
  32. arsort($scopes, SORT_NUMERIC);
  33. $mtime = date ("F d Y H:i:s", filemtime("/home/jclarke/dhcp_scope_stats.dat"));
  34. ?>
  35. <html>
  36. <head>
  37. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.18/c3.min.css" integrity="sha256-rp5Udclt95vV/qBRPHck2jUp/0xytxZgbHCCVRqV9vc=" crossorigin="anonymous" />
  38. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  39. <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js" integrity="sha256-dsOXGNHAo/syFnazt+KTBsCQeRmlcW1XKL0bCK4Baec=" crossorigin="anonymous"></script>
  40. <script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.18/c3.min.js" integrity="sha256-rx6BHKxiLgSA2BslVk0Gq+bclIxvxBm4eDKxvpS+7wI=" crossorigin="anonymous"></script>
  41. <script>
  42. setTimeout("location.reload(true);", 600000);
  43. </script>
  44. </head>
  45. <body>
  46. <h1 style="text-align: center">DHCP Scope Statistics</h1>
  47. <div style="text-align: right; font-size: 10pt;"><b>Last Updated: <?=$mtime?></b></div>
  48. <table border="0" style="/*table-layout: fixed*/" align="center" width="100%">
  49. <tbody>
  50. <?php
  51. $max = 3;
  52. $i = 0;
  53. $topn = 9;
  54. foreach ($scopes as $scope => $perc) {
  55. if ($i % $max == 0) {
  56. echo "<tr>\n";
  57. }
  58. echo "<td style=\"text-align: center; width: 33%; font-size: 14pt;\">\n";
  59. echo "<div id=\"chart_div_$i\"></div>\n";
  60. echo "$scope\n";
  61. echo "</td>\n";
  62. $i++;
  63. if ($i % $max == 0) {
  64. echo "</tr>\n";
  65. }
  66. if (!isset($_GET['show_all']) && $i == $topn) {
  67. break;
  68. }
  69. }
  70. ?>
  71. </tbody>
  72. </table>
  73. <script language="javascript">
  74. <?php
  75. $i = 0;
  76. foreach ($scopes as $scope => $perc) {
  77. ?>
  78. var chart<?=$i?> = c3.generate({
  79. bindto: '#chart_div_<?=$i?>',
  80. data: {
  81. columns: [
  82. ['<?=$scope?>', <?=$perc?>]
  83. ],
  84. type: 'gauge'
  85. },
  86. gauge: { },
  87. color: {
  88. pattern: ['#60B044', '#F6C600', '#FF0000'],
  89. threshold: {
  90. values: [75, 90]
  91. }
  92. },
  93. size: {
  94. height: 180
  95. }
  96. });
  97. <?php
  98. $i++;
  99. }
  100. ?>
  101. </script>
  102. <?php
  103. if (!isset($_GET['show_all'])) {
  104. ?>
  105. <p style="text-align: left; font-size: 9pt;"><a href="<?=$_SERVER['PHP_SELF']?>?show_all=1">Show All</a></p>
  106. <?php
  107. } else {
  108. ?>
  109. <p style="text-align: left; font-size: 9pt;"><a href="<?=$_SERVER['PHP_SELF']?>">Show Top 9</a></p>
  110. <?php
  111. }
  112. ?>
  113. </body>
  114. </html>