#!/bin/bash
source /usr/share/ceph-tools/_ct-common

help_msg () {
    printf """
usage: $0 [-h] KEY

Description
  -h, --help              show this help message and exit
  -g, --get               get the configuration
  -s, --set               set the configuration
  KEY                     config key to show
  [VALUE]                 value for the key (optionnal)

Example
    $0 -g osd_recovery_sleep
    $0 -s osd_recovery_sleep 0.1
"""
}

get_config() {
    osd_list=$(ceph osd df -f json | jq '.nodes[]["id"]' |sort -n -u)
    search=$'\'.[]|select(.name|test(\"'${1}$'\")).value\''
    for id in ${osd_list}
    do
    echo -n "osd.${id} : "
    eval "ceph config show osd.${id} -f json | jq -e ${search}|| echo -n -e '\r'"
    done
}
set_config(){
    ceph tell "osd.*" injectargs "--${1}=${2}"
}

ct_help_min $# 1

while [ $# -gt 0 ] ; do
    arg="$1"
    case $arg in
        -h|--help)
            help_msg
            ct_on_exit 0
            ;;
        -g|--get)
            key="^${2}$"$
            get_config $key
            ct_on_exit 0
            ;;
        -s|--set)
            key="${2}"
            value=$3
            set_config $key $value
            ct_on_exit 0
            ;;
    esac
done

ct_on_exit 0