Skip to content

Instantly share code, notes, and snippets.

@richcollier
Created January 11, 2024 13:52
Show Gist options
  • Save richcollier/7c43c5847044227688aeef19498aac9e to your computer and use it in GitHub Desktop.
Save richcollier/7c43c5847044227688aeef19498aac9e to your computer and use it in GitHub Desktop.
Capture index sizes daily
POST _watcher/watch/_execute
{
"watch": {
"trigger": {
"schedule": {
"interval": "1d"
}
},
"input": {
"http": {
"request": {
"host": "e1964ea9b74a4168a7e26c3be7bb330f.europe-west1.gcp.cloud.es.io",
"port": 9243,
"scheme": "https",
"path": "/_cat/indices/*,-.*",
"params": {
"format": "json",
"h": "index,store.size",
"bytes": "b"
},
"headers": {},
"auth": {
"basic": {
"username": "elastic",
"password": "xxx"
}
}
}
}
},
"condition": {
"always": {}
},
"actions" : {
"index_payload": {
"transform": {
"script": """
def documents = ctx.payload.data.stream()
.map(hit -> [
"_index": "index_sizes",
"index": hit.index,
"store.size": hit['store.size']
])
.collect(Collectors.toList());
return [ "_doc" : documents];
"""
},
"index": {
"execution_time_field": "@timestamp"
}
}
}
}
}
@richcollier
Copy link
Author

GET index_sizes/_search will yield something like:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 8,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "index_sizes",
        "_id": "y1jD-IwBRZWNq0rMQ4W9",
        "_score": 1,
        "_source": {
          "@timestamp": "2024-01-11T13:43:15.438Z",
          "index": "test-data",
          "store.size": "907197"
        }
      },
      {
        "_index": "index_sizes",
        "_id": "zFjD-IwBRZWNq0rMQ4W9",
        "_score": 1,
        "_source": {
          "@timestamp": "2024-01-11T13:43:15.438Z",
          "index": "my-index",
          "store.size": "284852"
        }
      },
      {
        "_index": "index_sizes",
        "_id": "zVjD-IwBRZWNq0rMQ4W9",
        "_score": 1,
        "_source": {
          "@timestamp": "2024-01-11T13:43:15.438Z",
          "index": "firmware_info",
          "store.size": "17610"
        }
      },
      {
        "_index": "index_sizes",
        "_id": "zljD-IwBRZWNq0rMQ4W9",
        "_score": 1,
        "_source": {
          "@timestamp": "2024-01-11T13:43:15.438Z",
          "index": "end_firmware_mismatch",
          "store.size": "14622"
        }
      },
      {
        "_index": "index_sizes",
        "_id": "z1jD-IwBRZWNq0rMQ4W9",
        "_score": 1,
        "_source": {
          "@timestamp": "2024-01-11T13:43:15.438Z",
          "index": "vbtime",
          "store.size": "11148"
        }
      },
      {
        "_index": "index_sizes",
        "_id": "0FjD-IwBRZWNq0rMQ4W9",
        "_score": 1,
        "_source": {
          "@timestamp": "2024-01-11T13:43:15.438Z",
          "index": "firmware_records",
          "store.size": "31034"
        }
      },
      {
        "_index": "index_sizes",
        "_id": "0VjD-IwBRZWNq0rMQ4W9",
        "_score": 1,
        "_source": {
          "@timestamp": "2024-01-11T13:43:15.438Z",
          "index": "kibana_sample_data_ecommerce",
          "store.size": "8783478"
        }
      },
      {
        "_index": "index_sizes",
        "_id": "0ljD-IwBRZWNq0rMQ4W9",
        "_score": 1,
        "_source": {
          "@timestamp": "2024-01-11T13:43:15.438Z",
          "index": "start_firmware_mismatch",
          "store.size": "15344"
        }
      }
    ]
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment