Chapter 09/circuit-oneops-1-master/components/cookbooks/diagnostic_cache/files/default/couchbase_monitor.rb
#Gather cluster statistics and stream it to the graphite servers
Gather cluster statistics and stream it to the graphite servers
Gather cluster statistics and stream it to the graphite servers
def cluster_stats(url, time)
    logger.info "cluster_stats:start: url = #{url}, time = #{time.to_i}"
    jsonResponse = rest_call(url)
    response_hash = JSON.parse(jsonResponse)
    node_count = response_hash['nodes'].count
    nodes = response_hash['nodes']
    rebalanceStatus = response_hash['rebalanceStatus']
    rebalance = rebalanceStatus.eql?('none') ? 0 : 1
    ram_total = response_hash['storageTotals']['ram']['total']
    ram_quota_total = response_hash['storageTotals']['ram']['quotaTotal']
    ram_quota_used = response_hash['storageTotals']['ram']['quotaUsed']
    ram_used = response_hash['storageTotals']['ram']['used']
    ram_used_by_data = response_hash['storageTotals']['ram']['usedByData']
    ram_quota_used_per_node = response_hash['storageTotals']['ram']['quotaUsedPerNode']
    ram_quota_total_per_node = response_hash['storageTotals']['ram']['quotaTotalPerNode']
    write_to_graphite( construct_metric_name('rebalance', CONST_CLUSTER_LEVEL), rebalance, time)
    write_to_graphite( construct_metric_name('ram_total', CONST_CLUSTER_LEVEL), ram_total.to_s, time)
    write_to_graphite( construct_metric_name('ram_quota_total', CONST_CLUSTER_LEVEL), ram_quota_total.to_s, time)
    write_to_graphite( construct_metric_name('ram_quota_used', CONST_CLUSTER_LEVEL), ram_quota_used.to_s, time)
    write_to_graphite( construct_metric_name('ram_used', CONST_CLUSTER_LEVEL), ram_used.to_s, time)
    write_to_graphite( construct_metric_name('ram_used_by_data', CONST_CLUSTER_LEVEL), ram_used_by_data.to_s, time)
    write_to_graphite( construct_metric_name('ram_quota_used_per_node', CONST_CLUSTER_LEVEL), ram_quota_used_per_node.to_s, time)
    write_to_graphite( construct_metric_name('ram_quota_total_per_node', CONST_CLUSTER_LEVEL), ram_quota_total_per_node.to_s, time)
    hdd_total = response_hash['storageTotals']['hdd']['total']
    hdd_quota_total = response_hash['storageTotals']['hdd']['quotaTotal']
    hdd_used = response_hash['storageTotals']['hdd']['used']
    hdd_used_by_data = response_hash['storageTotals']['hdd']['usedByData']
    hdd_free = response_hash['storageTotals']['hdd']['free']
    write_to_graphite( construct_metric_name('hdd_total', CONST_CLUSTER_LEVEL), hdd_total.to_s, time)
    write_to_graphite( construct_metric_name('hdd_quota_total', CONST_CLUSTER_LEVEL), hdd_quota_total.to_s, time)
    write_to_graphite( construct_metric_name('hdd_used', CONST_CLUSTER_LEVEL), hdd_used.to_s, time)
    write_to_graphite( construct_metric_name('hdd_used_by_data', CONST_CLUSTER_LEVEL), hdd_used_by_data.to_s, time)
    write_to_graphite( construct_metric_name('hdd_free', CONST_CLUSTER_LEVEL), hdd_free.to_s, time)
    nodes.each do |node|
      swap_total = node['systemStats']['swap_total']
      swap_used = node['systemStats']['swap_used']
      mem_total = node['systemStats']['mem_total']
      mem_free = node['systemStats']['mem_free']
      cmd_get = node['interestingStats']['cmd_get']
      couch_docs_actual_disk_size = node['interestingStats']['couch_docs_actual_disk_size']
      couch_docs_data_size = node['interestingStats']['couch_docs_data_size']
      curr_items = node['interestingStats']['curr_items']
      curr_items_tot = node['interestingStats']['curr_items_tot']
      ep_bg_fetched = node['interestingStats']['ep_bg_fetched']
      get_hits = node['interestingStats']['get_hits']
      mem_used = node['interestingStats']['mem_used']
      ops = node['interestingStats']['ops']
      vb_replica_curr_items = node['interestingStats']['vb_replica_curr_items']
      clusterMembership = node['clusterMembership']
      status = node['status']
      otpNode = node['otpNode']
      otpNode = otpNode.gsub('.','-')
      if (status =='healthy' && clusterMembership == 'active' )
        @healthy_nodes += 1
      end
      if (clusterMembership == 'inactiveFailed' )
        @failover_nodes += 1
      end
      write_to_graphite( construct_metric_name("#{otpNode}.swap_total", CONST_NODE_LEVEL), swap_total.to_s, time)
      write_to_graphite( construct_metric_name("#{otpNode}.swap_used", CONST_NODE_LEVEL), swap_used.to_s, time)
      write_to_graphite( construct_metric_name("#{otpNode}.mem_total", CONST_NODE_LEVEL), mem_total.to_s, time)
      write_to_graphite( construct_metric_name("#{otpNode}.mem_free", CONST_NODE_LEVEL), mem_free.to_s, time)
      write_to_graphite( construct_metric_name("#{otpNode}.cmd_get", CONST_NODE_LEVEL), cmd_get, time)
      write_to_graphite( construct_metric_name("#{otpNode}.couch_docs_actual_disk_size", CONST_NODE_LEVEL), couch_docs_actual_disk_size, time)
      write_to_graphite( construct_metric_name("#{otpNode}.couch_docs_data_size", CONST_NODE_LEVEL), couch_docs_data_size, time)
      write_to_graphite( construct_metric_name("#{otpNode}.curr_items", CONST_NODE_LEVEL), curr_items)
      write_to_graphite( construct_metric_name("#{otpNode}.curr_items_tot", CONST_NODE_LEVEL), curr_items_tot, time)
      write_to_graphite( construct_metric_name("#{otpNode}.ep_bg_fetched", CONST_NODE_LEVEL), ep_bg_fetched, time)
      write_to_graphite( construct_metric_name("#{otpNode}.get_hits", CONST_NODE_LEVEL), get_hits, time)
      write_to_graphite( construct_metric_name("#{otpNode}.mem_used", CONST_NODE_LEVEL), mem_used, time)
      write_to_graphite( construct_metric_name("#{otpNode}.ops", CONST_NODE_LEVEL), ops, time)
      write_to_graphite( construct_metric_name("#{otpNode}.vb_replica_curr_items", CONST_NODE_LEVEL), vb_replica_curr_items, time)
    end
    healthy = nodes.length == @healthy_nodes ? 1 : 0
    write_to_graphite( construct_metric_name('healthy_node_num', CONST_CLUSTER_LEVEL), @healthy_nodes.to_s, time)
    write_to_graphite( construct_metric_name('node_num', CONST_CLUSTER_LEVEL), nodes.length.to_s, time)
    write_to_graphite( construct_metric_name('healthy', CONST_CLUSTER_LEVEL), healthy.to_s, time)
    logger.info 'cluster_stats:end: Completed'
  end
def cluster_stats ( url , time ) logger . info "cluster_stats:start: url = #{url}, time = #{time.to_i}" jsonResponse = rest_call ( url ) response_hash = JSON . parse ( jsonResponse ) node_count = response_hash [ 'nodes' ] . count nodes = response_hash [ 'nodes' ] rebalanceStatus = response_hash [ 'rebalanceStatus' ] rebalance = rebalanceStatus . eql? ( 'none' ) ? 0 : 1 ram_total = response_hash [ 'storageTotals' ] [ 'ram' ] [ 'total' ] ram_quota_total = response_hash [ 'storageTotals' ] [ 'ram' ] [ 'quotaTotal' ] ram_quota_used = response_hash [ 'storageTotals' ] [ 'ram' ] [ 'quotaUsed' ] ram_used = response_hash [ 'storageTotals' ] [ 'ram' ] [ 'used' ] ram_used_by_data = response_hash [ 'storageTotals' ] [ 'ram' ] [ 'usedByData' ] ram_quota_used_per_node = response_hash [ 'storageTotals' ] [ 'ram' ] [ 'quotaUsedPerNode' ] ram_quota_total_per_node = response_hash [ 'storageTotals' ] [ 'ram' ] [ 'quotaTotalPerNode' ] write_to_graphite ( construct_metric_name ( 'rebalance' , CONST_CLUSTER_LEVEL ) , rebalance , time ) write_to_graphite ( construct_metric_name ( 'ram_total' , CONST_CLUSTER_LEVEL ) , ram_total . to_s , time ) write_to_graphite ( construct_metric_name ( 'ram_quota_total' , CONST_CLUSTER_LEVEL ) , ram_quota_total . to_s , time ) write_to_graphite ( construct_metric_name ( 'ram_quota_used' , CONST_CLUSTER_LEVEL ) , ram_quota_used . to_s , time ) write_to_graphite ( construct_metric_name ( 'ram_used' , CONST_CLUSTER_LEVEL ) , ram_used . to_s , time ) write_to_graphite ( construct_metric_name ( 'ram_used_by_data' , CONST_CLUSTER_LEVEL ) , ram_used_by_data . to_s , time ) write_to_graphite ( construct_metric_name ( 'ram_quota_used_per_node' , CONST_CLUSTER_LEVEL ) , ram_quota_used_per_node . to_s , time ) write_to_graphite ( construct_metric_name ( 'ram_quota_total_per_node' , CONST_CLUSTER_LEVEL ) , ram_quota_total_per_node . to_s , time ) hdd_total = response_hash [ 'storageTotals' ] [ 'hdd' ] [ 'total' ] hdd_quota_total = response_hash [ 'storageTotals' ] [ 'hdd' ] [ 'quotaTotal' ] hdd_used = response_hash [ 'storageTotals' ] [ 'hdd' ] [ 'used' ] hdd_used_by_data = response_hash [ 'storageTotals' ] [ 'hdd' ] [ 'usedByData' ] hdd_free = response_hash [ 'storageTotals' ] [ 'hdd' ] [ 'free' ] write_to_graphite ( construct_metric_name ( 'hdd_total' , CONST_CLUSTER_LEVEL ) , hdd_total . to_s , time ) write_to_graphite ( construct_metric_name ( 'hdd_quota_total' , CONST_CLUSTER_LEVEL ) , hdd_quota_total . to_s , time ) write_to_graphite ( construct_metric_name ( 'hdd_used' , CONST_CLUSTER_LEVEL ) , hdd_used . to_s , time ) write_to_graphite ( construct_metric_name ( 'hdd_used_by_data' , CONST_CLUSTER_LEVEL ) , hdd_used_by_data . to_s , time ) write_to_graphite ( construct_metric_name ( 'hdd_free' , CONST_CLUSTER_LEVEL ) , hdd_free . to_s , time ) nodes . each do | node | swap_total = node [ 'systemStats' ] [ 'swap_total' ] swap_used = node [ 'systemStats' ] [ 'swap_used' ] mem_total = node [ 'systemStats' ] [ 'mem_total' ] mem_free = node [ 'systemStats' ] [ 'mem_free' ] cmd_get = node [ 'interestingStats' ] [ 'cmd_get' ] couch_docs_actual_disk_size = node [ 'interestingStats' ] [ 'couch_docs_actual_disk_size' ] couch_docs_data_size = node [ 'interestingStats' ] [ 'couch_docs_data_size' ] curr_items = node [ 'interestingStats' ] [ 'curr_items' ] curr_items_tot = node [ 'interestingStats' ] [ 'curr_items_tot' ] ep_bg_fetched = node [ 'interestingStats' ] [ 'ep_bg_fetched' ] get_hits = node [ 'interestingStats' ] [ 'get_hits' ] mem_used = node [ 'interestingStats' ] [ 'mem_used' ] ops = node [ 'interestingStats' ] [ 'ops' ] vb_replica_curr_items = node [ 'interestingStats' ] [ 'vb_replica_curr_items' ] clusterMembership = node [ 'clusterMembership' ] status = node [ 'status' ] otpNode = node [ 'otpNode' ] otpNode = otpNode . gsub ( '.' , '-' ) if ( status == 'healthy' && clusterMembership == 'active' ) @healthy_nodes += 1 end if ( clusterMembership == 'inactiveFailed' ) @failover_nodes += 1 end write_to_graphite ( construct_metric_name ( "#{otpNode}.swap_total" , CONST_NODE_LEVEL ) , swap_total . to_s , time ) write_to_graphite ( construct_metric_name ( "#{otpNode}.swap_used" , CONST_NODE_LEVEL ) , swap_used . to_s , time ) write_to_graphite ( construct_metric_name ( "#{otpNode}.mem_total" , CONST_NODE_LEVEL ) , mem_total . to_s , time ) write_to_graphite ( construct_metric_name ( "#{otpNode}.mem_free" , CONST_NODE_LEVEL ) , mem_free . to_s , time ) write_to_graphite ( construct_metric_name ( "#{otpNode}.cmd_get" , CONST_NODE_LEVEL ) , cmd_get , time ) write_to_graphite ( construct_metric_name ( "#{otpNode}.couch_docs_actual_disk_size" , CONST_NODE_LEVEL ) , couch_docs_actual_disk_size , time ) write_to_graphite ( construct_metric_name ( "#{otpNode}.couch_docs_data_size" , CONST_NODE_LEVEL ) , couch_docs_data_size , time ) write_to_graphite ( construct_metric_name ( "#{otpNode}.curr_items" , CONST_NODE_LEVEL ) , curr_items ) write_to_graphite ( construct_metric_name ( "#{otpNode}.curr_items_tot" , CONST_NODE_LEVEL ) , curr_items_tot , time ) write_to_graphite ( construct_metric_name ( "#{otpNode}.ep_bg_fetched" , CONST_NODE_LEVEL ) , ep_bg_fetched , time ) write_to_graphite ( construct_metric_name ( "#{otpNode}.get_hits" , CONST_NODE_LEVEL ) , get_hits , time ) write_to_graphite ( construct_metric_name ( "#{otpNode}.mem_used" , CONST_NODE_LEVEL ) , mem_used , time ) write_to_graphite ( construct_metric_name ( "#{otpNode}.ops" , CONST_NODE_LEVEL ) , ops , time ) write_to_graphite ( construct_metric_name ( "#{otpNode}.vb_replica_curr_items" , CONST_NODE_LEVEL ) , vb_replica_curr_items , time ) end healthy = nodes . length == @healthy_nodes ? 1 : 0 write_to_graphite ( construct_metric_name ( 'healthy_node_num' , CONST_CLUSTER_LEVEL ) , @healthy_nodes . to_s , time ) write_to_graphite ( construct_metric_name ( 'node_num' , CONST_CLUSTER_LEVEL ) , nodes . length . to_s , time ) write_to_graphite ( construct_metric_name ( 'healthy' , CONST_CLUSTER_LEVEL ) , healthy . to_s , time ) logger . info 'cluster_stats:end: Completed' end
Gather cluster statistics and stream it to the graphite servers
Gather cluster statistics and stream it to the graphite servers
#puts response_hash #puts "Total number of nodes in the cluster = #{node_count.to_s}" #rebalance status in the cluster #ram metrics in the cluster #cluster RAM level stats #HDD metrics in the cluster #cluster HDD level stats #systemStats #interestingStats #node level systemStats #node level interestingStats
url
<not_specific>
time
<not_specific>