"Fossies" - the Fresh Open Source Software Archive

Member "ganglia-web-3.7.2/api/metrics.php" (8 Jun 2016, 2945 Bytes) of package /linux/www/ganglia-web-3.7.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "metrics.php" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.7.1_vs_3.7.2.

    1 <?php
    2 
    3 // vim: tabstop=2:softtabstop=2:shiftwidth=2:expandtab
    4 
    5 if ( !isset($_GET['debug']) ) {
    6   header("Content-Type: text/json");
    7 }
    8 
    9 $conf['gweb_root'] = dirname(dirname(__FILE__));
   10 
   11 include_once $conf['gweb_root'] . "/eval_conf.php";
   12 include_once $conf['gweb_root'] . "/functions.php";
   13 include_once $conf['gweb_root'] . "/lib/common_api.php";
   14 
   15 if ( isset($_GET['host']) && isset($_GET['metric_name']) ) {
   16    $host = $_GET['host'];
   17    $metric_name = $_GET['metric_name'];
   18 } else {
   19    api_return_error("You need to supply host and metric_name.");
   20 }
   21 
   22 global $metrics;
   23 
   24 if($conf['nagios_cache_enabled'] && file_exists($conf['nagios_cache_file'])){
   25         // check for the cached file
   26         // snag it and return it if it is still fresh
   27     $time_diff = time() - filemtime($conf['nagios_cache_file']);
   28     $expires_in = $conf['nagios_cache_time'] - $time_diff;
   29         if( $time_diff < $conf['nagios_cache_time']){
   30         if ( $debug == 1 ) {
   31           error_log("DEBUG: Fetching data from cache. Expires in " . $expires_in . " seconds.\n");
   32         }
   33             $metrics = unserialize(file_get_contents($conf['nagios_cache_file']));
   34         }
   35 }
   36 
   37 if ( ! is_array( $metrics ) ) {
   38 
   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 }
   61 
   62 # Get a list of all hosts
   63 $ganglia_hosts_array = array_keys($metrics);
   64 $host_found = 0;
   65 
   66 # Find a FQDN of a supplied server name.
   67 for ( $i = 0 ; $i < count($ganglia_hosts_array) ; $i++ ) {
   68  if ( !strcasecmp( $ganglia_hosts_array[$i], $host )   ) {
   69   $fqdn = $ganglia_hosts_array[$i];
   70   $host_found = 1;
   71   break;
   72  }
   73 }
   74 
   75 # Host has been found in the Ganglia tree
   76 if ( $host_found == 1 ) {
   77   # Check for the existence of a metric
   78   if ( isset($metrics[$fqdn][$metric_name]['VAL']) ) {
   79     $metric_value = $metrics[$fqdn][$metric_name]['VAL'];
   80   } else {
   81     api_return_error(htmlentities($metric_name) . " - Invalid metric request for this host. Please check metric exists.");
   82     exit(3);
   83   }
   84 
   85   $ganglia_units = $metrics[$fqdn][$metric_name]['UNITS'];
   86 
   87   api_return_ok(array(
   88       'metric_value' => $metric_value
   89     , 'units' => $ganglia_units
   90   ));
   91 } else {
   92    api_return_error(htmlentities($metric_name) . " - Hostname info not available. Likely invalid hostname");
   93 }
   94