executable_opensearch 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env bash
  2. # Run example:
  3. # OPENSEARCH_ENDPOINT="https://opensearch.corp1.majordomo.ru" OPENSEARCH_PASSWORD="$(pass show majordomo/public/opensearch-dashboards/admin)" OPENSEARCH_QUERY_SIZE=5 OPENSEARCH_QUERY_NAMESPACE=opensearch OPENSEARCH_QUERY_MINUTES=5 /home/oleg/.local/share/chezmoi/dot_local/bin/executable_opensearch search
  4. OPENSEARCH_ARGS=(
  5. --user "admin:${OPENSEARCH_PASSWORD}"
  6. )
  7. case "$1" in
  8. search)
  9. opensearch_query()
  10. {
  11. cat <<EOF
  12. {
  13. "version": true,
  14. "size": ${OPENSEARCH_QUERY_SIZE:-1000},
  15. "sort": [
  16. {
  17. "@timestamp": {
  18. "order": "desc",
  19. "unmapped_type": "boolean"
  20. }
  21. }
  22. ],
  23. "aggs": {
  24. "2": {
  25. "date_histogram": {
  26. "field": "@timestamp",
  27. "calendar_interval": "1m",
  28. "time_zone": "Europe/Moscow",
  29. "min_doc_count": 1
  30. }
  31. }
  32. },
  33. "stored_fields": [
  34. "*"
  35. ],
  36. "script_fields": {},
  37. "docvalue_fields": [
  38. {
  39. "field": "@timestamp",
  40. "format": "date_time"
  41. },
  42. {
  43. "field": "time",
  44. "format": "date_time"
  45. }
  46. ],
  47. "_source": {
  48. "excludes": []
  49. },
  50. "query": {
  51. "bool": {
  52. "must": [],
  53. "filter": [
  54. {
  55. "match_all": {}
  56. },
  57. {
  58. "match_phrase": {
  59. "kubernetes.namespace_name.keyword": "${OPENSEARCH_QUERY_NAMESPACE}"
  60. }
  61. },
  62. {
  63. "range": {
  64. "@timestamp": {
  65. "gte": "$(date -u -d "-${OPENSEARCH_QUERY_MINUTES:-60} minutes" +"%Y-%m-%dT%H:%M:%S.%3NZ")",
  66. "lte": "$(date -u +"%Y-%m-%dT%H:%M:%S.%3NZ")",
  67. "format": "strict_date_optional_time"
  68. }
  69. }
  70. }
  71. ],
  72. "should": [],
  73. "must_not": []
  74. }
  75. },
  76. "highlight": {
  77. "pre_tags": [
  78. "@opensearch-dashboards-highlighted-field@"
  79. ],
  80. "post_tags": [
  81. "@/opensearch-dashboards-highlighted-field@"
  82. ],
  83. "fields": {
  84. "*": {}
  85. },
  86. "fragment_size": 2147483647
  87. }
  88. }
  89. EOF
  90. }
  91. echo "$(opensearch_query)" \
  92. | curl --max-time 5 \
  93. --insecure \
  94. --silent "${OPENSEARCH_ARGS[@]}" \
  95. --header 'Content-Type: application/json' \
  96. --data @- \
  97. "${OPENSEARCH_ENDPOINT}/logstash-*/_search" \
  98. | jq --raw-output '.hits.hits | group_by(._source.kubernetes.pod_name)[][] | [._source.kubernetes.pod_name, ._source.log] | @tsv' \
  99. | sort --version-sort
  100. ;;
  101. esac