#!/bin/bash

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)
"""
}

get_config() {
    osd_list=$(ceph osd df -f json | jq '.nodes[]["id"]' |sort -n |uniq)
    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}"
}

if [ $# -eq 0 ]
then
    help_msg
    exit 1
fi

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