#
# MIT License
#
# (C) Copyright 2021-2022 Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
apiVersion: v1
kind: ConfigMap
metadata:
  name: patch-nodeexporter-alerts
data:
  fix-NodeClockSkewDetected-alert.sh: |-
    #!/bin/sh

    echo "Reconfiguring cray-sysmgmt-health-promet-node-exporter prometheus NodeClockSkewDetected alert rule"
    mfile=/tmp/cray-sysmgmt-health-NodeClockSkewDetected.yaml
    kubectl get  -n sysmgmt-health   prometheusrules cray-sysmgmt-health-promet-node-exporter -o yaml > $mfile
    sed -i 's/node_timex_offset_seconds\s>\s0.05/node_timex_offset_seconds\ >\ 0.000166/' $mfile
    sed -i 's/node_timex_offset_seconds\s<\s-0.05/node_timex_offset_seconds\ <\ -0.000166/' $mfile
    sed -i 's/300s/1s/' $mfile
    kubectl -n sysmgmt-health apply -f $mfile
  
  fix-CephNetworkPacketsDropped-alert.sh: |-
    #!/bin/sh
  
    echo "Renaming cray-sysmgmt-health-ceph-prometheus-alert.rules prometheus CephNetworkPacketsDropped to NetworkPacketsDropped alert rule"
    mfile=/tmp/cray-sysmgmt-health-NetworkPacketsDropped.yaml
    kubectl get -n sysmgmt-health prometheusrules cray-sysmgmt-health-ceph-prometheus-alert.rules -o yaml > $mfile
    sed -i 's/CephNetworkPacketsDropped/NetworkPacketsDropped/' $mfile
    kubectl -n sysmgmt-health apply -f $mfile

  fix-NodeFilesystemAlmostOutOfSpace-alert.sh: |-
    #!/bin/sh

    echo "Reconfiguring cray-sysmgmt-health-promet-node-exporter prometheus NodeFilesystemAlmostOutOfSpace alert rule"
    mfile=/tmp/cray-sysmgmt-health-NodeFilesystemAlmostOutOfSpace.yaml
    kubectl get  -n sysmgmt-health   prometheusrules cray-sysmgmt-health-promet-node-exporter -o yaml > $mfile
    sed -i '/alert: NodeFilesystemAlmostOutOfSpace/{N;N;N;N;N;N;s/Filesystem has less than 5% space left/Filesystem has less than 17% space left/}' $mfile
    sed -i '/alert: NodeFilesystemAlmostOutOfSpace/{N;N;N;N;N;N;N;N;N;s/100 < 5/100 < 17/}' $mfile
    sed -i '/alert: NodeFilesystemAlmostOutOfSpace/{N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;s/Filesystem has less than 3% space left/Filesystem has less than 15% space left/}' $mfile
    sed -i '/alert: NodeFilesystemAlmostOutOfSpace/{N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;s/100 < 3/100 < 15/}' $mfile
    kubectl -n sysmgmt-health apply -f $mfile
  
  fix-Nodenumcpu-alert.sh: |-
    #!/bin/sh

    echo "Reconfiguring cray-sysmgmt-health-promet-node-exporter prometheus NodeFilesystemAlmostOutOfSpace alert rule"
    mfile=/tmp/cray-sysmgmt-health-Nodenumcpu-delete.yaml
    mfile1=/tmp/cray-sysmgmt-health-Nodenumcpu.yaml
    kubectl get  prometheusRules  -n sysmgmt-health cray-sysmgmt-health-promet-node.rules -o yaml > $mfile
    sed -ine '/count by (cluster, node) (sum by (node, cpu) (/{N;N;N;N;d}' $mfile
    sed -n '/ record: node:node_num_cpu:sum/{x;s/|-/count without (cpu) (  count without (mode) (node_cpu_seconds_total{job="node-exporter"}))/;x};x;1!p;${x;p}' $mfile > $mfile1
    kubectl -n sysmgmt-health apply -f $mfile1
