diff --git a/default.nix b/default.nix index b1b05697..b9f1c37f 100644 --- a/default.nix +++ b/default.nix @@ -193,6 +193,8 @@ inherit (pkgs) nix kapp kubectl stern vault docker-compose cfssl kompose buildGoModule = pkgs.buildGo120Module; }; + print-client-zones-infos = pkgs.callPackage ./pkgs/print-client-zones-infos { }; + promtool = pkgs.callPackage ./pkgs/promtool.nix {}; sd = import sources.sd.outPath { inherit pkgs; }; diff --git a/pkgs/print-client-zones-infos/default.nix b/pkgs/print-client-zones-infos/default.nix new file mode 100644 index 00000000..ca9088b5 --- /dev/null +++ b/pkgs/print-client-zones-infos/default.nix @@ -0,0 +1,32 @@ +{ stdenv +, lib +, runCommand +, makeWrapper +}: + +stdenv.mkDerivation rec { + pname = "print-client-zones-infos"; + version = "1.0.0"; + + buildInputs = [ makeWrapper ]; + + dontBuild = true; + dontConfigure = true; + unpackPhase = ":"; + installPhase = '' + mkdir -p $out/bin + cp ${./print-client-zones-infos.sh} $out/bin/print-client-zones-infos + chmod +x $out/bin/print-client-zones-infos + + substituteInPlace $out/bin/print-client-zones-infos --replace PRINT_CLIENT_ZONES_INFOS_VERSION ${version} + wrapProgram $out/bin/print-client-zones-infos --prefix PATH ":" ${lib.makeBinPath [ ]} + ''; + + meta = with lib; { + description = "Print NGOT zones infos for a given client/contract"; + homepage = "https://github.com/Caascad/toolbox"; + license = licenses.mit; + maintainers = with maintainers; [ "ngc104" ]; + }; + +} diff --git a/pkgs/print-client-zones-infos/print-client-zones-infos.sh b/pkgs/print-client-zones-infos/print-client-zones-infos.sh new file mode 100755 index 00000000..b3c79abb --- /dev/null +++ b/pkgs/print-client-zones-infos/print-client-zones-infos.sh @@ -0,0 +1,112 @@ +#! /bin/bash + +CLIENT=$1 + +APP=print-client-zones-infos + +if [ "${CLIENT}" = "" ]; then + echo "Usage: $0 " + echo "Exemple : $0 demo" + exit 1 +fi + +if [ "${CLIENT}" = "version" ] || [ "${CLIENT}" = "--version" ]; then + echo "${APP} version : PRINT_CLIENT_ZONES_INFOS_VERSION" + exit 0 +fi + +CAASCAD_ZONES_URL=${CAASCAD_ZONES_URL:-https://git.corp.caascad.com/caascad/terraform/envs-ng/-/raw/master/gen/zones_static/zones.json} + +ALLZONES=$(curl -sL --connect-timeout 2 "${CAASCAD_ZONES_URL}" 2>/dev/null) + +#CONTRACT below is unused. Leaving it here for the example. +#CONTRACT=$(echo "${ALLZONES}" | jq -r --arg c "${CLIENT}" '.["obs-"+$c]') + +print_header() { + header="$1" + line="=====================================================" + printf "\n=== %s %s\n" "${header}" "${line:${#header}}" +} + +print_kv() { + k="$1" + v="$2" + printf "%-40s : %s\n" "$k" "$v" +} + +print_dict() { + k="$1" + v="$2" + + while read -r l; do + eval "${l}" + echo "" + done <<< "$(echo "${v}" | jq -r --arg title "${k}" '. | to_entries | .[]| ["printf", "%-40s : %s", $title+"("+.key+")", .value]|@sh')" +} + +get_zone() { + subtype="$1" + echo "${ALLZONES}" | jq -r --arg c "${CLIENT}" --arg s "${subtype}" ' + .[] + | select(.contract_zone_name == "obs-"+$c) + | select(.subtype == $s) + ' +} + +print_grafana_client() { + z=$(get_zone "grafana-client") + [ -z "$z" ] && return + zone_name=$(echo "$z" | jq -r '.name') + dns_domain=$(echo "$z" | jq -r '.dns_domain') + cluster_name=$(echo "$z" | jq -r '.cluster_zone_name') + mapfile -t thanos_query_connected_services < <(echo "$z" | jq -r '.parameters.thanos_query_connected_services[].name' |sed -e 's/svc-monitoring-stack-client-\([a-z]*\)/\1/') + + print_header "Grafana" + print_kv "name" "${zone_name}" + print_kv "Cluster" "${cluster_name}" + print_dict "Namespace" '{"main": "grafana-client-obs-'"${CLIENT}"'", "dashboards": "grafana-dashboards-obs-'"${CLIENT}"'"}' + print_kv "Grafana URL" "https://grafana.${dns_domain}" + print_kv "Grafana DS Thanos" "$(printf ",%s" "${thanos_query_connected_services[@]}" | sed -e 's/^,//g')" +} + +print_monitoring_stack_client() { + z=$(get_zone "monitoring-stack-client") + [ -z "$z" ] && return + zone_name=$(echo "$z" | jq -r '.name') + cluster_name=$(echo "$z" | jq -r '.cluster_zone_name') + namespace=$(echo "$z" | jq -r '.parameters["monitoring-stack"].namespace') + notifications_targets=$(echo "$z" | jq -r '[.parameters["monitoring-stack"].alertmanager.notifications_targets[].name]|@csv' | sed -e 's/"//g' -e 's/,/, /g') + [ -z "${notifications_targets}" ] && notifications_targets="(aucune)" + exporters_nb=$(echo "$z" | jq -r '.parameters["monitoring-stack"].prometheus.exporters | to_entries | map(.value = (.value | length)) | from_entries') + retention_raw=$(echo "$z" | jq -r '.parameters.thanos.retention.raw') + retention_5m=$(echo "$z" | jq -r '.parameters.thanos.retention.downsampling_5m') + retention_1h=$(echo "$z" | jq -r '.parameters.thanos.retention.downsampling_1h') + + print_header "Monitoring stack" + print_kv "name" "${zone_name}" + print_kv "Cluster" "${cluster_name}" + print_dict "Namespace" '{"main": "'"${namespace}"'", "probes": "probes-obs-'"${CLIENT}"'", "rules": "rules-obs-'"${CLIENT}"'"}' + print_kv "Alertmanager notifications targets" "${notifications_targets}" + + print_dict "NB Exporters" "${exporters_nb}" + print_kv "Retentions (raw / 5m / 1h)" "${retention_raw} / ${retention_5m} / ${retention_1h}" +} + +print_loki_client() { + z=$(get_zone "loki-client") + [ -z "$z" ] && return + zone_name=$(echo "$z" | jq -r '.name') + cluster_name=$(echo "$z" | jq -r '.cluster_zone_name') + retention=$(echo "$z" | jq -r '.parameters.loki.retention') + + print_header "Loki" + print_kv "name" "${zone_name}" + print_kv "Cluster" "${cluster_name}" + print_kv "Namespace" "loki-client-obs-${CLIENT}" + + print_kv "Retention" "${retention}" +} + +print_grafana_client +print_monitoring_stack_client +print_loki_client