#!/bin/bash
cluster_name=$(oc get console cluster -o json | jq '.status' | tr -d '"{}: // .\n' | sed 's/consoleURLhttpsconsole-openshift-consoleapps//g' | sed 's/sbrfru//g' | sed 's/sigma/-sigma/g' | sed 's/ca/-alpha/g')
total_cpu_master=$(curl -sk -H "Authorization: Bearer $(oc whoami -t)" https://"$(oc get route prometheus-k8s -n openshift-monitoring -o jsonpath='{.
spec.host}')/api/v1/query" --data-urlencode 'query=sum(cluster:capacity_cpu_cores:sum{l$
total_mem_master=$(curl -sk -H "Authorization: Bearer $(oc whoami -t)" https://"$(oc get route prometheus-k8s -n openshift-monitoring -o jsonpath='{.
spec.host}')/api/v1/query" --data-urlencode 'query=sum(cluster:capacity_memory_bytes:su$
total_cpu_router=$(for i in $(oc get no -L type | grep router | awk 'NR>2 {print $1}'); do oc get no $i -o json | jq '.status.capacity.cpu'; done | paste -sd+ | tr -d '"' | bc)
total_mem_router=$(for i in $(oc get no -L type | grep router | awk 'NR>2 {print $1}'); do oc get no $i -o json | jq '.status.capacity.memory'; done | paste -sd+ | tr -d '"Ki' | bc)
total_cpu_monitoring=$(for i in $(oc get no -L type | grep monitoring | awk 'NR>2 {print $1}'); do oc get no $i -o json | jq '.status.capacity.cpu'; done | paste -sd+ | tr -d '"' | bc)
total_mem_monitoring=$(for i in $(oc get no -L type | grep monitoring | awk 'NR>2 {print $1}'); do oc get no $i -o json | jq '.status.capacity.memory'; done | paste -sd+ | tr -d '"Ki' | bc)
total_cpu_logging=$(for i in $(oc get no -L type | grep logging | awk 'NR>2 {print $1}'); do oc get no $i -o json | jq '.status.capacity.cpu'; done | paste -sd+ | tr -d '"' | bc)
total_mem_logging=$(for i in $(oc get no -L type | grep logging | awk 'NR>2 {print $1}'); do oc get no $i -o json | jq '.status.capacity.memory'; done | paste -sd+ | tr -d '"ki' | bc)
json_string='{"cluster_name": "'$cluster_name'",
"total_mem_master": "'$total_mem_master'","total_cpu_master": "'$total_cpu_master'",
"total_mem_router": "'$total_mem_router'","total_cpu_router": "'$total_cpu_router'",
"total_mem_monitoring": "'$total_mem_monitoring'","total_cpu_monitoring": "'$total_cpu_monitoring'"},
"total_mem_logging": "'$total_mem_logging'","total_cpu_logging": "'$total_cpu_logging'"},'
echo $json_string >> cluster.json