Subversion Repositories vnag

Rev

Rev 4 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php /* <ViaThinkSoftSignature>
  2. AHwkMePLHKOt4R/D3Ku9ARyOKU1u6VjTtn8dbcody41d7es9OiXW/ta/C9PzlVVDt
  3. nTya6pH1y0qL7XSbffsFYCKpS9zqJ2akCtZzHJ8ePYp+z/HjuEzAzb2y712EobG/A
  4. rQSQo+f57P4c0nr5RMfg6b5QTIruKhm3gVwv81QuRm1xI9SApETYOeiBwLowDN3N4
  5. 4x4AVcVXf6GenSCVkSD7XrwjDVI3U4h9m+DLuwuWP6Ti8W3eRRqP5Fp3wxgdbnWsf
  6. hSgkAqoO0E3sHhcKDRinA5mDbynaRmNrauxSPL11S1WTnnFVDVgPJSIYSKUuxrdpm
  7. 4UKBGHC18CSRlx7Zc7+DNEq+zV8PHgRkmN9lHUEnMx4+Kxkptz1TJmClk8IE2WMS0
  8. JYdFPd+aW4W/RUzXMmry5Ls6WlMM4wMKv/u4mtsJLKCz+D2ODwdVhDpj5yzdYG1Ox
  9. UN4HhgKzgeKkCgeZOPUDAxU0LJYhTeC4Lu2gE0XujXTOkko7sPNhK7lEkpXK4dfFf
  10. 2FQgiOwZNho2WhhvU3TWy2xyNMRd9vh0iVbJgE22lx+J40bNBUa44adX1wApq8oRP
  11. kFiZdV5Xb4xrLCRY8EekO7iiW6vcAM5vzLnzGZ4BhDOLapZrs/TcxpPnbvsH0WXth
  12. yDduBWJytYpKJ3fTpAUlwJzwKCjGEHwq+SSqCxd2KDCG6+eaARwkcs4wPHYcR0cky
  13. +uqDDmOJT8r62F6tILtfK33SxSDPVTo1CgYmrl46l+Rbm5tqWxdFRQ/1htGJLkD1L
  14. 7DC5pyOrd53GnLvQ6+LCbcFx9D6xT27QjcXEAcGPQ5WTHQbJ2FBBJRJ/giESmEhoB
  15. nmUXxfrdaSqQ4rrImNckMRpPP01me1XnZSA9l23UTT77UVCktGGW6HXZxvJMi1D/J
  16. qLb8nrIJ62mhVq1eHboGnR1BJVNXjh2GDShBRcDoadvCLNayp9PkpQJgShkxENzeq
  17. +7xCCWqaVUPprvPwlG/z1kwEnX7BuTLrFmwrsYDdn1zKQMK8o3KckwgWO0ipMcTD+
  18. hr+OC0LFCn4M4RIo2R+8E3243yCmclkLPaQax+f7I9DKMrKtvVtUpNs9srHmUBh1M
  19. EZcFE4jFVjOVz3tw1e+u5nugx3Bj9RvPif4hJgkTXIExWAw8CjOJoqxKez46opHXm
  20. 7c3mi1z9L9EbRBBzWPexTUPiWzcna/r1lVVEoAzVGt9SXWRjsbPZr3+rJSxwFdEkf
  21. JrZL14/vcdh7UZ4QWRcVpWjXPuA7vlWoNb5EBm3acm/mxAVM7kj56HtWOabM9zyFi
  22. m45gDhw/1XOophKpq824vBfkhFPhLdvWXDYeR+JrFZbSO1i+UwqitRkIarLqHMT8P
  23. A==
  24. </ViaThinkSoftSignature> */ ?>
  25. <?php
  26.  
  27. /*
  28.  * VNag - Nagios Framework for PHP
  29.  * Developed by Daniel Marschall, ViaThinkSoft <www.viathinksoft.com>
  30.  * Licensed under the terms of the Apache 2.0 license
  31.  *
  32.  * Revision 2018-07-19
  33.  */
  34.  
  35. # IMPORTANT
  36. # The log files have to be named YYYY_MM_DD_HH.log and need to be cleared (zeroed) every hour and dumped at least every hour (every minute is also possible)
  37. # Please see the file INSTALL
  38.  
  39. # ---
  40.  
  41. # TODO: trennen zwischen server (ip1, ip2) ermöglichen...
  42. # TODO: prüfen lassen ob die config von ipfm korrekt ist (dumped every 1 hour)
  43. # TODO: monat vor/zurückscrollen? (mon/year)
  44. # TODO: anstelle "year .... overview" lieber "last 12 months"
  45. # TODO: Some hosts might reset the traffic counter at any other day of the month. Should we also support this?
  46.  
  47. # ---
  48.  
  49. declare(ticks=1);
  50.  
  51. # Attention: This constant must be a valid Nagios UOM!
  52. # TODO: Make it configurable via command line? Should we use the UOM from the w/c/l arguments?
  53. define('OUTPUT_UOM', 'GB');
  54.  
  55. # ---
  56.  
  57. class IpFlowMonitorCheck extends VNag {
  58.         protected $argLogDir = null;
  59.         protected $argLimit = null;
  60.  
  61.         public function __construct() {
  62.                 parent::__construct();
  63.  
  64.                 $this->registerExpectedStandardArguments('Vhtwc');
  65.  
  66.                 $this->getHelpManager()->setPluginName('check_ipfm');
  67.                 $this->getHelpManager()->setVersion('1.0');
  68.                 $this->getHelpManager()->setShortDescription('This plugin checks the network traffic monitored by ipfm.');
  69.                 $this->getHelpManager()->setCopyright('Copyright (C) 2011-$CURYEAR$ Daniel Marschall, ViaThinkSoft.');
  70.                 $this->getHelpManager()->setSyntax('$SCRIPTNAME$ [-w currentGB[,expectedGB]] [-c currentGB[,expectedGB]] [-l limitGB] [-L path]');
  71.                 $this->getHelpManager()->setFootNotes('If you encounter bugs, please contact ViaThinkSoft at www.viathinksoft.com');
  72.  
  73.                 // This is the amount of traffic which is free. Every byte above this limit will cost additional money.
  74.                 // You should therefore set your warning and critical limit below this limit.
  75.                 // Don't set this argument if you have an unlimited connection.
  76.                 // This value is only for visual output. It does not influence the OK/Warning/Critial result.
  77.                 $this->addExpectedArgument($this->argLimit = new VNagArgument('l', 'limit', VNagArgument::VALUE_REQUIRED, 'limit', '"Traffic inclusive" limit (e.g. 10TB)', null));
  78.  
  79.                 $this->addExpectedArgument($this->argLogDir = new VNagArgument('L', 'logdir', VNagArgument::VALUE_REQUIRED, 'path', 'Location of the ipfm log dir (Default: /var/log/ipfm)', '/var/log/ipfm'));
  80.  
  81.                 // When the user writes "-w 10GB" then he actually means "-w @10GB:~" or "-w ~:10GB", so these commands allow this notation:
  82.                 $this->warningSingleValueRangeBehaviors[0]  = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD;
  83.                 $this->warningSingleValueRangeBehaviors[1]  = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD;
  84.                 $this->criticalSingleValueRangeBehaviors[0] = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD;
  85.                 $this->criticalSingleValueRangeBehaviors[1] = self::SINGLEVALUE_RANGE_VAL_GT_X_BAD;
  86.         }
  87.  
  88.         protected function interprete_ipfm_logfile($logfile) {
  89.                 $res = array();
  90.  
  91.                 $ary = file($logfile);
  92.                 foreach ($ary as $a) {
  93.                         $a = trim($a);
  94.                         if ($a == '') continue;
  95.                         if (substr($a, 0, 1) == '#') continue;
  96.                         $a = preg_replace("|\s+|ism", ' ', $a);
  97.                         $bry = explode(' ', $a);
  98.                         $res[$bry[0]] = array($bry[1], $bry[2], $bry[3]);
  99.                 }
  100.  
  101.                 return $res;
  102.         }
  103.  
  104.         protected function cbRun($optional_args=array()) {
  105.                 if (!defined('USE_DYGRAPH')) define('USE_DYGRAPH', false);
  106.  
  107.                 $logDir = $this->argLogDir->getValue();
  108.                 if (!is_dir($logDir)) throw new Exception("Log dir $logDir not found");
  109.  
  110.                 $monthLimit = $this->argLimit->getValue();
  111.                 if (!is_null($monthLimit)) {
  112.                         $p = (new VNagValueUomPair($monthLimit))->normalize(OUTPUT_UOM);
  113.                         $monthLimitValue = $p->getValue();
  114.                         $monthLimitUom = $p->getUom();
  115.                 }
  116.  
  117.                 ob_start();
  118.  
  119.                 ?>
  120.  
  121.                 <script type="text/javascript">
  122.                         google.load("visualization", "1", {packages:['imageareachart', 'corechart']});
  123.                         google.setOnLoadCallback(drawChart);
  124.                         function drawChart() {
  125.                                 var data = new google.visualization.DataTable();
  126.                                 data.addColumn('string', 'Year');
  127.                                 data.addColumn('number', 'Total');
  128.                                 data.addColumn('number', 'In');
  129.                                 data.addRows([
  130.  
  131.                 <?php
  132.  
  133.                 $day = date('d');
  134.                 $mon = date('m');
  135.                 $year = date('Y');
  136.  
  137.                 $didata = '';
  138.                 $logfiles = glob($logDir.'/*.log');
  139.                 sort($logfiles);
  140.  
  141.                 $monthtotal[$year][$mon] = 0;
  142.                 $daystotal = array();
  143.                 $max = 0;
  144.                 $first = true;
  145.                 foreach ($logfiles as $logfile) {
  146.                         $stat = $this->interprete_ipfm_logfile($logfile);
  147.  
  148.                         $date = '';
  149.                         if (preg_match('/^.*_00\.log$/', $logfile)) {
  150.                                 preg_match('@(\d{4})_(\d{2})_(\d{2})@ismU', $logfile, $m);
  151.                                 $t_day = $m[3];
  152.                                 $t_mon = $m[2];
  153.                                 $t_year = $m[1];
  154.                                 // $date = "$t_day.$t_mon.$t_year";
  155.                                 $date = $t_day;
  156.                         //} else if (preg_match('/^.*_(02|04|06|08|10|12|14|16|18|20|22)\.log$/', $logfile, $m)) {
  157.                         //      $date = $m[1].':00';
  158.                         }
  159.  
  160.                         $in = $out = $total = 0;
  161.                         foreach ($stat as $s) {
  162.                                 $in += $s[0];
  163.                                 $out += $s[1];
  164.                                 $total += $s[2];
  165.                         }
  166.  
  167.                         $in    = (new VNagValueUomPair($in.'B'))->normalize(OUTPUT_UOM)->getValue();
  168.                         $out   = (new VNagValueUomPair($out.'B'))->normalize(OUTPUT_UOM)->getValue();
  169.                         $total = (new VNagValueUomPair($total.'B'))->normalize(OUTPUT_UOM)->getValue();
  170.  
  171.                         if (preg_match('/^.*(\d{4})_(\d{2})_(\d{2})_(\d{2})\.log$/', $logfile, $m)) {
  172.                                 $t_year = $m[1];
  173.                                 $t_mon = $m[2];
  174.                                 $t_day = $m[3];
  175.                                 $t_hour = $m[4];
  176.                                 if (isset($monthtotal[$t_year][$t_mon])) {
  177.                                         $monthtotal[$t_year][$t_mon] += $total;
  178.                                 } else {
  179.                                         $monthtotal[$t_year][$t_mon] = $total;
  180.                                 }
  181.  
  182.                                 if (($t_year == $year) && ($t_mon == $mon)) {
  183.                                         if (isset($daystotal[$t_day])) {
  184.                                                 $daystotal[$t_day] += $total;
  185.                                         } else {
  186.                                                 $daystotal[$t_day] = $total;
  187.                                         }
  188.                                         if (!$first) echo ","; else $first = false;
  189.                                         echo "['$date', ".round($total,2).", ".round($in,2)."]\n";
  190.                                         $didata .= '"'.$t_year.'-'.$t_mon.'-'.$t_day.' '.$t_hour.':00:00,'.round($in,2).','.round($out,2).','.round($total,2).'\n" +'."\n";
  191.                                 }
  192.                         }
  193.  
  194.                         if ($total > $max) $max = $total;
  195.                 }
  196.  
  197.                 $num_days_in_month = date('t', mktime(0, 0, 0, intval($mon), 1, intval($year)));
  198.                 $expected = $monthtotal[$year][$mon]/$day * $num_days_in_month;
  199.  
  200.                 ?>
  201.                         ]);
  202.  
  203.                         var chart = new google.visualization.ImageAreaChart(document.getElementById('chart_div'));
  204.                 <?php if (!USE_DYGRAPH) { ?>
  205.                         chart.draw(data, {width: 800, height: 440, min: 0, max: <?php echo $max; ?>, title: 'Traffic in <?php echo $mon.'/'.$year; ?> [in <?php echo OUTPUT_UOM; ?>/h]'});
  206.                 <?php } ?>
  207.  
  208.                         // ---
  209.  
  210.                         var data_month = new google.visualization.DataTable();
  211.                         data_month.addColumn('string', 'Day');
  212.                         data_month.addColumn('number', 'Total');
  213.  
  214.                         data_month.addRows([
  215.                 <?php
  216.  
  217.                 $first = true;
  218.                 foreach ($daystotal as $t_day => $traffic) {
  219.                         if (!$first) echo ","; else $first = false;
  220.                         echo "['$t_day.$mon.$year', ".round($traffic,2)."]\n";
  221.                 }
  222.  
  223.                 ?>
  224.                         ]);
  225.  
  226.                         var options_month = {
  227.                                 width: 800, height: 440,
  228.                                 vAxis: { viewWindow: { min: 0 } },
  229.                                 title: 'Month overview for <?php echo $mon.'/'.$year; ?> [GiB]'
  230.                         };
  231.  
  232.                         var chart_month = new google.visualization.ColumnChart(document.getElementById('chart_div3'));
  233.                         chart_month.draw(data_month, options_month);
  234.  
  235.                         // ---
  236.  
  237.                         var data_year = new google.visualization.DataTable();
  238.                         data_year.addColumn('string', 'Month/Year');
  239.                         data_year.addColumn('number', 'Total');
  240.  
  241.                         data_year.addRows([
  242.                 <?php
  243.  
  244.                 ksort($monthtotal); // First sort by year
  245.  
  246.                 $first = true;
  247.                 foreach ($monthtotal as $t_year => $x) {
  248.                         ksort($x); // Then sort by month
  249.                         foreach ($x as $t_mon => $traffic) {
  250.                                 if ($t_year != $year) continue; // Only this year
  251.  
  252.                                 $date = "$t_mon/$t_year";
  253.  
  254.                                 if (!$first) echo ","; else $first = false;
  255.                                 echo "['$date', ".round($traffic,2)."]\n";
  256.                         }
  257.                         echo ",['Expected in $mon/$year', $expected]\n";
  258.                 }
  259.  
  260.                 ?>
  261.                         ]);
  262.  
  263.                         var options_year = {
  264.                                 width: 800, height: 440,
  265.                                 vAxis: { viewWindow: { min: 0 } },
  266.                                 title: 'Year <?php echo $year; ?> overview [GiB]'
  267.                         };
  268.  
  269.                         var chart_year = new google.visualization.ColumnChart(document.getElementById('chart_div2'));
  270.                         chart_year.draw(data_year, options_year);
  271.                 }
  272.  
  273.                 </script>
  274.  
  275.                 <?php if (USE_DYGRAPH) { ?>
  276.                         <script type="text/javascript">
  277.                                 g = new Dygraph(
  278.                                         document.getElementById("graphdiv"),
  279.                                         "Date,In,Out,Total\n" +
  280.                                         <?php echo $didata; ?>""
  281.                                 );
  282.                         </script>
  283.                 <?php } ?>
  284.  
  285.                 <?php
  286.  
  287.                 if (!is_null($monthLimit)) {
  288.                         echo "<p>Constraint: Max ".$monthLimitValue.' '.OUTPUT_UOM."/Month</p>";
  289.                 }
  290.  
  291.                 $current = $monthtotal[$year][$mon];
  292.  
  293.                 $fontcolor = 'green';
  294.                 if ($this->checkAgainstCriticalRange($current.OUTPUT_UOM, false, true, 0)) $fontcolor = 'red';
  295.                 if ($this->checkAgainstWarningRange($current.OUTPUT_UOM, false, true, 0))  $fontcolor = '#FF8000';
  296.  
  297.                 if (!is_null($monthLimit)) {
  298.                         echo "<p>This month ($mon/$year): <font color=\"$fontcolor\">".round($current,0)." ".OUTPUT_UOM." (".round($current/$monthLimitValue*100,2)."%)</font></p>";
  299.                 } else {
  300.                         echo "<p>This month ($mon/$year): <font color=\"$fontcolor\">".round($current,0)." ".OUTPUT_UOM."</font></p>";
  301.                 }
  302.  
  303.                 $fontcolor = 'green';
  304.                 if ($this->checkAgainstCriticalRange($expected.OUTPUT_UOM, false, true, 1)) $fontcolor = 'red';
  305.                 if ($this->checkAgainstWarningRange($expected.OUTPUT_UOM, false, true, 1))  $fontcolor = '#FF8000';
  306.  
  307.                 if (!is_null($monthLimit)) {
  308.                         echo "<p>Expected for this month: <font color=\"$fontcolor\">".round($expected,0)." ".OUTPUT_UOM." (".round($expected/$monthLimitValue*100,2)."%)</font></p>";
  309.                 } else {
  310.                         echo "<p>Expected for this month: <font color=\"$fontcolor\">".round($expected,0)." ".OUTPUT_UOM."</font></p>";
  311.                 }
  312.  
  313.                 ?>
  314.  
  315.                 <div id="chart_div"></div>
  316.                 <div id="graphdiv"></div>
  317.                 <div id="chart_div3"></div>
  318.                 <div id="chart_div2"></div>
  319.  
  320.                 <?php
  321.  
  322.                 $html = ob_get_contents();
  323.                 ob_end_clean();
  324.  
  325.                 $this->outputHTML($html, true);
  326.  
  327.                 if (!is_null($monthLimit)) {
  328.                         // TODO: should we put a percentage at "expected"? or if it exceeds, should we show how much it would exceed?
  329.                         $this->setHeadline(round($current,0).' '.OUTPUT_UOM." / ".$monthLimitValue.' '.OUTPUT_UOM." (".round($current/$monthLimitValue*100,2)."%, expected ".round($expected,0).' '.OUTPUT_UOM.") traffic used this month ($mon/$year)", true);
  330.                 } else {
  331.                         $this->setHeadline(round($current,0).' '.OUTPUT_UOM." (expected ".round($expected,0).' '.OUTPUT_UOM.") traffic used this month ($mon/$year)", true);
  332.                 }
  333.  
  334.                 $warn = is_null($this->getWarningRange(0)) ? null : $this->getWarningRange(0)->end->normalize(OUTPUT_UOM)->getValue();
  335.                 $crit = is_null($this->getCriticalRange(0)) ? null : $this->getCriticalRange(0)->end->normalize(OUTPUT_UOM)->getValue();
  336.                 $this->addPerformanceData(new VNagPerformanceData('Current', $current.OUTPUT_UOM, $warn, $crit, 0, is_null($monthLimit) ? null : $monthLimitValue));
  337.  
  338.                 $warn = is_null($this->getWarningRange(1)) ? null : $this->getWarningRange(1)->end->normalize(OUTPUT_UOM)->getValue();
  339.                 $crit = is_null($this->getCriticalRange(1)) ? null : $this->getCriticalRange(1)->end->normalize(OUTPUT_UOM)->getValue();
  340.                 $this->addPerformanceData(new VNagPerformanceData('Expected', $expected.OUTPUT_UOM, $warn, $crit, 0, is_null($monthLimit) ? null : $monthLimitValue));
  341.         }
  342. }
  343.