alerts.sh 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/bin/bash
  2. requiresJsonnet() {
  3. if ! type "jsonnet" > /dev/null; then
  4. echo "you need you install jsonnet to run this script"
  5. echo "follow the instructions on https://github.com/google/jsonnet"
  6. exit 1
  7. fi
  8. }
  9. setup() {
  10. STATUS=$(curl -s -o /dev/null -w '%{http_code}' http://admin:admin@grafana.loc/api/alert-notifications/1)
  11. if [ $STATUS -eq 200 ]; then
  12. echo "Email already exists, skipping..."
  13. else
  14. curl -H "Content-Type: application/json" \
  15. -d '{
  16. "name": "Email",
  17. "type": "email",
  18. "isDefault": false,
  19. "sendReminder": false,
  20. "uploadImage": true,
  21. "settings": {
  22. "addresses": "user@test.com"
  23. }
  24. }' \
  25. http://admin:admin@grafana.loc/api/alert-notifications
  26. fi
  27. STATUS=$(curl -s -o /dev/null -w '%{http_code}' http://admin:admin@grafana.loc/api/alert-notifications/2)
  28. if [ $STATUS -eq 200 ]; then
  29. echo "Slack already exists, skipping..."
  30. else
  31. curl -H "Content-Type: application/json" \
  32. -d '{
  33. "name": "Slack",
  34. "type": "slack",
  35. "isDefault": false,
  36. "sendReminder": false,
  37. "uploadImage": true
  38. }' \
  39. http://admin:admin@grafana.loc/api/alert-notifications
  40. fi
  41. }
  42. slack() {
  43. enabled=true
  44. url=''
  45. remind=false
  46. remindEvery='10m'
  47. while getopts ":e:u:dr" o; do
  48. case "${o}" in
  49. e)
  50. remindEvery=${OPTARG}
  51. ;;
  52. u)
  53. url=${OPTARG}
  54. ;;
  55. d)
  56. enabled=false
  57. ;;
  58. r)
  59. remind=true
  60. ;;
  61. esac
  62. done
  63. shift $((OPTIND-1))
  64. curl -X PUT \
  65. -H "Content-Type: application/json" \
  66. -d '{
  67. "id": 2,
  68. "name": "Slack",
  69. "type": "slack",
  70. "isDefault": '$enabled',
  71. "sendReminder": '$remind',
  72. "frequency": "'$remindEvery'",
  73. "uploadImage": true,
  74. "settings": {
  75. "url": "'$url'"
  76. }
  77. }' \
  78. http://admin:admin@grafana.loc/api/alert-notifications/2
  79. }
  80. provision() {
  81. alerts=1
  82. condition=65
  83. while getopts ":a:c:" o; do
  84. case "${o}" in
  85. a)
  86. alerts=${OPTARG}
  87. ;;
  88. c)
  89. condition=${OPTARG}
  90. ;;
  91. esac
  92. done
  93. shift $((OPTIND-1))
  94. requiresJsonnet
  95. rm -rf grafana/provisioning/dashboards/alerts/alert-*.json
  96. jsonnet -m grafana/provisioning/dashboards/alerts grafana/provisioning/alerts.jsonnet --ext-code alerts=$alerts --ext-code condition=$condition
  97. }
  98. pause() {
  99. curl -H "Content-Type: application/json" \
  100. -d '{"paused":true}' \
  101. http://admin:admin@grafana.loc/api/admin/pause-all-alerts
  102. }
  103. unpause() {
  104. curl -H "Content-Type: application/json" \
  105. -d '{"paused":false}' \
  106. http://admin:admin@grafana.loc/api/admin/pause-all-alerts
  107. }
  108. usage() {
  109. echo -e "Usage: ./alerts.sh COMMAND [OPTIONS]\n"
  110. echo -e "Commands"
  111. echo -e " setup\t\t creates default alert notification channels"
  112. echo -e " slack\t\t configure slack notification channel"
  113. echo -e " [-d]\t\t\t disable notifier, default enabled"
  114. echo -e " [-u]\t\t\t url"
  115. echo -e " [-r]\t\t\t send reminders"
  116. echo -e " [-e <remind every>]\t\t default 10m\n"
  117. echo -e " provision\t provision alerts"
  118. echo -e " [-a <alert rule count>]\t default 1"
  119. echo -e " [-c <condition value>]\t default 65\n"
  120. echo -e " pause\t\t pause all alerts"
  121. echo -e " unpause\t unpause all alerts"
  122. }
  123. main() {
  124. local cmd=$1
  125. if [[ $cmd == "setup" ]]; then
  126. setup
  127. elif [[ $cmd == "slack" ]]; then
  128. slack "${@:2}"
  129. elif [[ $cmd == "provision" ]]; then
  130. provision "${@:2}"
  131. elif [[ $cmd == "pause" ]]; then
  132. pause
  133. elif [[ $cmd == "unpause" ]]; then
  134. unpause
  135. fi
  136. if [[ -z "$cmd" ]]; then
  137. usage
  138. fi
  139. }
  140. main "$@"