Skip to content

Commit

Permalink
Merge pull request #487 from Caascad/print_zone_infos
Browse files Browse the repository at this point in the history
print-client-zones-infos: init at 1.0.0
  • Loading branch information
ngc104 authored Nov 22, 2023
2 parents 05a13b1 + 82b1c3c commit 51228d5
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
2 changes: 2 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down
32 changes: 32 additions & 0 deletions pkgs/print-client-zones-infos/default.nix
Original file line number Diff line number Diff line change
@@ -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" ];
};

}
112 changes: 112 additions & 0 deletions pkgs/print-client-zones-infos/print-client-zones-infos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#! /bin/bash

CLIENT=$1

APP=print-client-zones-infos

if [ "${CLIENT}" = "" ]; then
echo "Usage: $0 <client>"
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

0 comments on commit 51228d5

Please sign in to comment.