Last active
November 13, 2023 23:46
-
-
Save ParadoxGuitarist/e92980a2e4d66a3d8a900c2c7bf15655 to your computer and use it in GitHub Desktop.
mk-alertmanagerconfigs.sh - Generates yaml for alertmanager namespaces in a cluster with provided kubeconfig (or default if none given)
This file contains hidden or 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 | |
# Replace Line 134 with the encoded webhook secret! and Lines 80 and 108 with your slack channel. | |
usage() { | |
echo "Usage: $0 [OPTIONS]" | |
echo "Options:" | |
echo " -h, Display this help message" | |
echo " -a, Combine all the configs into a single yaml | |
instead of breaking them up." | |
echo " -v, Enable verbose mode" | |
echo " -c, Insert the Clustername into the alertconfig" | |
echo " -k, Specify path to the kubeconfig" | |
} | |
while getopts "hvac:k:" flag; do | |
case $flag in | |
h) # Handle the -h flag | |
# Display script help information | |
usage | |
exit 0 | |
;; | |
v) # Handle the -v flag | |
# Enable verbose mode | |
;; | |
a) # Handle the -a flag | |
#combine all the configs into a single yaml instead of breaking them up. | |
addative="true" | |
;; | |
c) # Handle the -c flag with an argument | |
clustername=$OPTARG | |
# Specify the human readable clustername in the slack message. | |
;; | |
k) # Handle the -k with an argument | |
kubeconfig=$OPTARG | |
;; | |
\?) | |
# Handle invalid options | |
usage | |
exit 1 | |
;; | |
esac | |
done | |
# Get all the namespaces | |
if [ -n "$kubeconfig" ]; then | |
nsarray=$(kubectl get ns --no-headers --kubeconfig $kubeconfig | awk '{print $1}') | |
else | |
nsarray=$(kubectl get ns --no-headers | awk '{print $1}') | |
fi | |
# Adjust Clustername as the alertname or default to the upstream. | |
# | |
if [ -n "$clustername" ]; then | |
username="$clustername Alert" | |
else | |
username='{{ template "slack.default.username" . }}' | |
fi | |
if [ -n "$addative" ]; then | |
echo "" > alertmanagerConfigs.yaml | |
fi | |
for namespace in $nsarray | |
do | |
config=$(cat <<-EOM | |
--- | |
apiVersion: monitoring.coreos.com/v1alpha1 | |
kind: AlertmanagerConfig | |
metadata: | |
name: critical-alerts | |
namespace: $namespace | |
spec: | |
receivers: | |
- name: slack | |
slackConfigs: | |
- apiURL: | |
key: slack | |
name: slack | |
channel: '#YourChannelHere' | |
username: $username | |
sendResolved: true | |
text: '{{ template "slack.rancher.text" . }}' | |
route: | |
groupBy: | |
- job | |
groupInterval: 2m | |
groupWait: 30s | |
matchers: | |
- matchType: '=~' | |
name: severity | |
value: critical | |
receiver: slack | |
repeatInterval: 3h | |
--- | |
apiVersion: monitoring.coreos.com/v1alpha1 | |
kind: AlertmanagerConfig | |
metadata: | |
name: general-alerts | |
namespace: $namespace | |
spec: | |
receivers: | |
- name: slack | |
slackConfigs: | |
- apiURL: | |
key: slack | |
name: slack | |
channel: '#YourChannelHere' | |
color: '{{ if eq .Status "firing" }}warning{{ else }}good{{ end }}' | |
username: $username | |
sendResolved: true | |
text: '{{ template "slack.rancher.text" . }}' | |
route: | |
groupBy: | |
- job | |
groupInterval: 2m | |
groupWait: 30s | |
matchers: | |
- matchType: '=~' | |
name: severity | |
value: warning|critical | |
receiver: slack | |
repeatInterval: 3d | |
--- | |
apiVersion: v1 | |
kind: Secret | |
metadata: | |
annotations: | |
field.cattle.io/description: Slack URL to Slack instance | |
name: slack | |
namespace: $namespace | |
data: | |
slack: >- | |
<replace with encoded webhook> #FIXME Replace this line! | |
EOM | |
) | |
if [ -n "$addative" ]; then | |
echo "$config" >> alertmanagerConfigs.yaml | |
else | |
echo "$config" > $namespace-alertmanagerConfig.yaml | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment