PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ganglia-3.3.7/web/nagios/check_metric.php

#
PHP | 107 lines | 72 code | 15 blank | 20 comment | 38 complexity | c06b8dc54482ed6cb84087ff514189f7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. ##########################################################################################
  3. # Author; Vladimir Vuksan
  4. # This is a Ganglia Nagios plugins that alerts based on values extracted from Ganglia
  5. #
  6. # You need to supply following GET values
  7. #
  8. # host = "Hostname"
  9. # metric_name e.g. load_one, bytes_out
  10. # operator for critical condition e.g. less, more, equal, notequal
  11. # critical_value e.g. value for critical
  12. ##########################################################################################
  13. $conf['gweb_root'] = dirname(dirname(__FILE__));
  14. include_once $conf['gweb_root'] . "/eval_conf.php";
  15. # To turn on debug set to 1
  16. $debug = 0;
  17. if ( isset($_GET['host']) && isset($_GET['metric_name']) && isset($_GET['operator']) && isset($_GET['critical_value']) ) {
  18. $host = $_GET['host'];
  19. $metric_name = $_GET['metric_name'];
  20. $operator = $_GET['operator'];
  21. $critical_value = $_GET['critical_value'];
  22. } else {
  23. die("You need to supply host, metric_name, operator and critical_value");
  24. }
  25. global $metrics;
  26. if($conf['nagios_cache_enabled'] && file_exists($conf['nagios_cache_file'])){
  27. // check for the cached file
  28. // snag it and return it if it is still fresh
  29. $time_diff = time() - filemtime($conf['nagios_cache_file']);
  30. $expires_in = $conf['nagios_cache_time'] - $time_diff;
  31. if( $time_diff < $conf['nagios_cache_time']){
  32. if ( $debug == 1 ) {
  33. error_log("DEBUG: Fetching data from cache. Expires in " . $expires_in . " seconds.\n");
  34. }
  35. $metrics = unserialize(file_get_contents($conf['nagios_cache_file']));
  36. }
  37. }
  38. if ( ! is_array( $metrics ) ) {
  39. if ( $debug == 1 ) {
  40. error_log("DEBUG: Querying GMond for new data\n");
  41. }
  42. $context = "cluster";
  43. include_once $conf['gweb_root'] . "/functions.php";
  44. include_once $conf['gweb_root'] . "/ganglia.php";
  45. include_once $conf['gweb_root'] . "/get_ganglia.php";
  46. # Massage the metrics to minimize the cache file by caching only attributes
  47. # we care about
  48. foreach ( $metrics as $mhost => $host_metrics ) {
  49. foreach ( $host_metrics as $name => $attributes ) {
  50. $new_metrics[$mhost][$name]['VAL'] = $metrics[$mhost][$name]['VAL'];
  51. if ( isset($metrics[$mhost][$name]['UNITS']) )
  52. $new_metrics[$mhost][$name]['UNITS'] = $metrics[$mhost][$name]['UNITS'];
  53. }
  54. }
  55. file_put_contents($conf['nagios_cache_file'], serialize($new_metrics));
  56. unset($metrics);
  57. $metrics = $new_metrics;
  58. unset($new_metrics);
  59. }
  60. # Get a list of all hosts
  61. $ganglia_hosts_array = array_keys($metrics);
  62. $host_found = 0;
  63. # Find a FQDN of a supplied server name.
  64. for ( $i = 0 ; $i < sizeof($ganglia_hosts_array) ; $i++ ) {
  65. if ( !strcasecmp( $ganglia_hosts_array[$i], $host ) ) {
  66. $fqdn = $ganglia_hosts_array[$i];
  67. $host_found = 1;
  68. break;
  69. }
  70. }
  71. # Host has been found in the Ganglia tree
  72. if ( $host_found == 1 ) {
  73. # Check for the existence of a metric
  74. if ( isset($metrics[$fqdn][$metric_name]['VAL']) ) {
  75. $metric_value = $metrics[$fqdn][$metric_name]['VAL'];
  76. } else {
  77. echo("UNKNOWN|" . $metric_name . " - Invalid metric request for this host. Please check metric exists.");
  78. exit(3);
  79. }
  80. $ganglia_units = $metrics[$fqdn][$metric_name]['UNITS'];
  81. if ( ($operator == "less" && $metric_value > $critical_value) || ( $operator == "more" && $metric_value < $critical_value ) || ( $operator == "equal" && trim($metric_value) != trim($critical_value) ) || ( $operator == "notequal" && trim($metric_value) == trim($critical_value) ) ) {
  82. print "OK|" . $metric_name . " = " . $metric_value . " " . $ganglia_units;
  83. exit (0);
  84. } else {
  85. print "CRITICAL|" . $metric_name . " = ". $metric_value . " " . $ganglia_units;
  86. exit (2);
  87. }
  88. } else {
  89. echo("UNKNOWN|" . $metric_name . " - Hostname info not available. Likely invalid hostname");
  90. exit(3);
  91. }
  92. ?>