# Path: cephinfo/cephinfo.py
# def jsoncmd(command):
# def init_df():
# def init_mon():
# def init_osd():
# def init_pg():
# def init_auth():
# def init_stat():
# def init_crush():
# def get_json():
# def get_pools_data():
# def get_n_pools():
# def get_osds_data():
# def get_n_osds():
# def get_osd_states():
# def get_pg_data():
# def get_pg_stats():
# def get_pg_stats_sum():
# def get_osd_stats_sum():
# def get_pg_stats_delta():
# def get_n_pgs():
# def get_pg_states():
# def get_n_mons():
# def get_n_mons_quorum():
# def get_write_latency():
# def get_read_latency():
# def rados_cleanup(prefix):
# def get_n_openstack_volumes():
# def get_n_openstack_images():
# def get_smooth_activity(n):


from cephinfo import cephinfo
from optparse import OptionParser
from collections import defaultdict
import ceph_osds_in_bucket
import os
  import sys

  # filter out the empty osds
  nonempty_osds = [ osd for osd in pgm['osd_stats'] if float(osd['kb']) > 0 and get_weight(osd['osd'],type='crush_weight') > 0 ]

  if VERBOSE: print "Found %d non-empty OSDs" % len(nonempty_osds)

  # optionally filter out osds not in the requested bucket
  # and recalculate average_util
  if options.bucket:
    bucket_osds = []
    for bucket in options.bucket:
      bucket_osds.extend(ceph_osds_in_bucket.list(bucket))
    sum_kb = 0
    sum_weight = 0
    sum_kb_used = 0
    filtered_osds = []
    for osd in nonempty_osds:
      name = 'osd.%d' % osd['osd']
      if name in bucket_osds:
        sum_weight += get_weight(osd['osd'], 'crush_weight') * 1024*1024*1024
        sum_kb_used += osd['kb_used']
        filtered_osds.insert(0, osd)
    average_util = float(sum_kb_used) / float(sum_weight)
    if VERBOSE: print "Found %d OSDs after filtering by bucket" % len(filtered_osds)
  else:
    filtered_osds = nonempty_osds


  # sort osds from most to least deviant from the average_util
  if options.by_pg:

# Path: cephinfo/cephinfo.py
# def jsoncmd(command):
# def init_df():
# def init_mon():
# def init_osd():
# def init_pg():
# def init_auth():
# def init_stat():
# def init_crush():
# def get_json():
# def get_pools_data():
# def get_n_pools():
# def get_osds_data():
# def get_n_osds():
# def get_osd_states():
# def get_pg_data():
# def get_pg_stats():
# def get_pg_stats_sum():
# def get_osd_stats_sum():
# def get_pg_stats_delta():
# def get_n_pgs():
# def get_pg_states():
# def get_n_mons():
# def get_n_mons_quorum():
# def get_write_latency():
# def get_read_latency():
# def rados_cleanup(prefix):
# def get_n_openstack_volumes():
# def get_n_openstack_images():
# def get_smooth_activity(n):


# Path: tools/crush-reweight-by-utilization.py
from cephinfo import cephinfo
from optparse import OptionParser
from collections import defaultdict
import ceph_osds_in_bucket
import os
  import sys


  # filter out the empty osds
  nonempty_osds = [ osd for osd in pgm['osd_stats'] if float(osd['kb']) > 0 and get_weight(osd['osd'],type='crush_weight') > 0 ]

  if VERBOSE: print "Found %d non-empty OSDs" % len(nonempty_osds)

  # optionally filter out osds not in the requested bucket
  # and recalculate average_util
  if options.bucket:
    bucket_osds = []
    for bucket in options.bucket:
      bucket_osds.extend(ceph_osds_in_bucket.list(bucket))
    sum_kb = 0
    sum_weight = 0
    sum_kb_used = 0
    filtered_osds = []
    for osd in nonempty_osds:
      name = 'osd.%d' % osd['osd']
      if name in bucket_osds:
        sum_weight += get_weight(osd['osd'], 'crush_weight') * 1024*1024*1024
        sum_kb_used += osd['kb_used']
        filtered_osds.insert(0, osd)
    average_util = float(sum_kb_used) / float(sum_weight)
    if VERBOSE: print "Found %d OSDs after filtering by bucket" % len(filtered_osds)
  else:
    filtered_osds = nonempty_osds


  # sort osds from most to least deviant from the average_util
  if options.by_pg:

    osds = sorted(filtered_osds, key=lambda osd: -abs(average_util - pgs_by_osd[osd['osd']] / get_weight(osd['osd'],type='crush_weight')))