Skip to content
Snippets Groups Projects
get_host_from_osd.py 1.12 KiB
#!/usr/bin/python
import os,sys,getopt
#systemctl list-units --type=service --state=active

SCRIPT='get_host_from_osd.py'

def help():
  print '%s -i <id>'%SCRIPT
  print '%s -h'%SCRIPT

def get_osd_hosts():
  cmd = os.popen("ceph osd tree | grep host | awk '{print $4}'").read()
  hosts = cmd.split("\n")
  hosts = hosts[0:-1]
  osds_dict = {}
  for h in hosts:
    cmd = os.popen("ssh %s systemctl list-units --type=service --state=active | grep ceph-osd | awk '{print $1}'"%h).read()
    services = cmd.split("\n")
    services = services[0:-1]
    for i in services:
      osd = (i.split(".")[0]).split("@")[-1]
      osds_dict[osd] = h
  return osds_dict


def main(argv):
   print argv
   try:
      opts, args = getopt.getopt(argv,"hi:",["machine="])
   except getopt.GetoptError:
      help()
      sys.exit(2)
   for opt, arg in opts:
      if opt == '-h':
         help()
         sys.exit()
      elif opt in ("-i", "--id"):
         osd_id = arg
         osds_dict = get_osd_hosts()
         print "Host is : ",osds_dict[osd_id]
         
      else:
        help()
        sys.exit()

if __name__ == "__main__":
   main(sys.argv[1:])