script.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/sh
  2. set -o nounset -o errexit -o pipefail -o xtrace
  3. username="${SCRIPT_OPENSEARCH_DASHBOARDS_USERNAME:-admin}"
  4. password="${SCRIPT_OPENSEARCH_DASHBOARDS_PASSWORD:-admin}"
  5. hostname="${SCRIPT_OPENSEARCH_DASHBOARDS_HOSTNAME:-opensearch-dashboards}"
  6. port="${SCRIPT_OPENSEARCH_DASHBOARDS_PORT:-5601}"
  7. users_and_passwors=(
  8. "${username}:${password}"
  9. "readall:readall"
  10. )
  11. # Return 0 if index pattern exists, other number otherwise.
  12. index_pattern()
  13. {
  14. curl --fail \
  15. --user "${username}:${password}" \
  16. --request GET \
  17. --silent \
  18. --verbose \
  19. "http://${hostname}:${port}/api/saved_objects/index-pattern/${1}"
  20. }
  21. for user_and_password in "${users_and_passwors[@]}"
  22. do
  23. if index_pattern 'logstash*'
  24. then
  25. echo "index-pattern 'logstash*' exists, skipping creation." >&2
  26. else
  27. curl --user "$user_and_password" \
  28. --silent \
  29. --verbose \
  30. --request POST \
  31. "http://${hostname}:${port}/api/saved_objects/index-pattern/logstash*" \
  32. --header "osd-xsrf:true" \
  33. --header "content-type:application/json" \
  34. --data '{"attributes": {"title": "logstash*", "timeFieldName": "@timestamp"}}'
  35. fi
  36. # sampleSize
  37. # The number of rows to show in the table
  38. # Default: 500
  39. #
  40. # truncate:maxHeight
  41. # The maximum height that a cell in a table should occupy. Set to 0 to disable truncation
  42. # Default: 115
  43. curl --user "$user_and_password" \
  44. --silent \
  45. --verbose \
  46. --request POST \
  47. --header 'Content-Type: application/json' \
  48. --header "osd-xsrf:true" \
  49. --data-raw '{"changes":{"discover:sampleSize":10000,"truncate:maxHeight":0}}' \
  50. "http://${hostname}:${port}/api/opensearch-dashboards/settings"
  51. done
  52. # Return 0 if the policy exists, other number otherwise.
  53. delete_old_indexes_policy()
  54. {
  55. curl --fail \
  56. --user "${username}:${password}" \
  57. --request GET \
  58. --silent \
  59. --verbose \
  60. "http://${hostname}:${port}/api/ism/policies/delete_old_indexes"
  61. }
  62. if [[ $(delete_old_indexes_policy) == *'{"ok":true,"response":{"id":"delete_old_indexes"'* ]]
  63. then
  64. echo "delete_old_indexes policy exists, skipping creation." >&2
  65. else
  66. curl --user "${username}:${password}" \
  67. --silent \
  68. --verbose \
  69. --header "osd-version: 1.2.0" \
  70. "http://${hostname}:${port}/api/ism/policies/delete_old_indexes" \
  71. --request PUT \
  72. --header 'Content-Type: application/json' \
  73. --data-raw '
  74. {
  75. "policy": {
  76. "description": "A simple default policy that deletes old indexes between hot and cold states.",
  77. "default_state": "hot",
  78. "states": [
  79. {
  80. "name": "hot",
  81. "actions": [],
  82. "transitions": [
  83. {
  84. "state_name": "cold",
  85. "conditions": {
  86. "min_index_age": "1d"
  87. }
  88. }
  89. ]
  90. },
  91. {
  92. "name": "cold",
  93. "actions": [
  94. {
  95. "delete": {}
  96. }
  97. ],
  98. "transitions": []
  99. }
  100. ],
  101. "ism_template": [
  102. {
  103. "index_patterns": [
  104. "filebeat-*",
  105. "logstash-*",
  106. "security-auditlog-*",
  107. "winlogbeat-*"
  108. ],
  109. "priority": 1
  110. }
  111. ]
  112. }
  113. }
  114. '
  115. fi