Created
November 24, 2017 14:44
-
-
Save bodsch/1b78bfa81512694ad63a23e8c2b15386 to your computer and use it in GitHub Desktop.
icinga2 extract api calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
ICINGA_HOST=${ICINGA_HOST:-localhost} | |
ICINGA_API_PORT=${ICINGA_API_PORT:-5665} | |
ICINGA_API_USER=${ICINGA_API_USER:-root} | |
ICINGA_API_PASSWORD=${ICINGA_API_PASSWORD:-icinga} | |
types= | |
ICINGA_TYPES=(Application Object Command ConfigObject CustomVarObject Checkable DbConnection Type | |
Host Service | |
Downtime Notification NotificationCommand NotificationComponent HostGroup ApiListener ApiUser Application CheckCommand CheckResult CheckResultReader | |
CheckerComponent Comment CompatLogger Dependency Endpoint EventCommand ExternalCommandListener | |
FileLogger Function IcingaApplication Logger PerfdataValue ScheduledDowntime ServiceGroup StatusDataWriter | |
StreamLogger SyslogLogger TimePeriod | |
User UserGroup Zone | |
) | |
get_values() { | |
local type="${1}" | |
# echo $type | |
data=$(curl -k -s -u ${ICINGA_API_USER}:${ICINGA_API_PASSWORD} "https://${ICINGA_HOST}:${ICINGA_API_PORT}/v1/types/${type}") | |
error=$(echo "${data}" | jq '.error') | |
if [ ${error} != null ] | |
then | |
# skip this | |
echo ${data} | |
continue | |
fi | |
echo "${data}" | jq -c '.[]' | jq -c '.[]' | jq --compact-output --raw-output '.' | while IFS='' read u | |
do | |
base=$(echo "${u}" | jq --raw-output '.base') | |
fields=$(echo "${u}" | jq --compact-output --raw-output '.fields') | |
[ ${base} == null ] && continue | |
destination_file="${type}_${base}.api" | |
[ -f ${destination_file} ] && rm -f ${destination_file} | |
base_file=$(find . -name "${base}_*.api") | |
if ( [ ! -z ${base_file} ] && [ -f ${base_file} ] ) | |
then | |
cp -a "${base_file}" "${destination_file}" | |
fi | |
echo "${fields}" | jq -r 'to_entries[] | [.key, .value.type, .value.attributes.config, .value.attributes.no_user_modify] | @tsv' | while read t | |
do | |
api_call=$(echo "${t}" | awk -F ' ' '{print $1}') | |
api_type=$(echo "${t}" | awk -F ' ' '{print $2}') | |
config=$(echo "${t}" | awk -F ' ' '{print $3}') | |
modify=$(echo "${t}" | awk -F ' ' '{print $4}') | |
[ "${config}" == "false" ] && continue | |
[ "${modify}" == "true" ] && continue | |
echo "${api_call} (${api_type})" >> ${destination_file} | |
done | |
done | |
} | |
for it in "${ICINGA_TYPES[@]}" | |
do | |
get_values ${it} | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment