Created
June 10, 2025 18:57
-
-
Save paulreece42/4d8609b11078313a6513fd6ae22cde83 to your computer and use it in GitHub Desktop.
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/env python3 | |
| # | |
| # Gets the estimated space usage of prometheus | |
| # | |
| # usage: /opt/prom_calc.py 1y [optional number of samples per sec] | |
| # y = years | |
| # m = months | |
| # d = days | |
| # | |
| # If number of samples not provided, queries from prometheus on prometheus.mydomain.com:9090 | |
| # | |
| # Author: Paul Reece | |
| # 2023-07-12 | |
| from datetime import timedelta | |
| import sys | |
| import re | |
| import requests | |
| PROMETHEUS='http://prometheus.mydomain.com:9090' | |
| if re.search('[0-9]{1,4}y', sys.argv[1]): | |
| days = int(sys.argv[1].split('y')[0]) * 365 | |
| retention_time_seconds = int(timedelta(days).total_seconds()) | |
| elif re.search('[0-9]{1,4}m', sys.argv[1]): | |
| days = int(sys.argv[1].split('m')[0]) * 30 | |
| retention_time_seconds = int(timedelta(days).total_seconds()) | |
| if re.search('[0-9]{1,4}d', sys.argv[1]): | |
| days = int(sys.argv[1].split('d')[0]) | |
| retention_time_seconds = int(timedelta(days).total_seconds()) | |
| else: | |
| retention_time_seconds = int(timedelta(days).total_seconds()) | |
| if len(sys.argv) > 2: | |
| ingested_samples_per_second = sys.argv[2] | |
| else: | |
| r = requests.get(PROMETHEUS + '/api/v1/query?query=rate(prometheus_tsdb_head_samples_appended_total[24h])') | |
| r = r.json() | |
| if r['status'] != 'success': | |
| print("failed to query prometheus at " + PROMETHEUS) | |
| print("provide samples per second manually or change endpoint or run the script from another place that has access") | |
| os.exit(2) | |
| else: | |
| ingested_samples_per_second = int(r['data']['result'][0]['value'][1].split('.')[0]) | |
| bytes_per_sample = int(2) # from prometheus docs https://prometheus.io/docs/prometheus/latest/storage/ | |
| def sizeof_fmt(num, suffix="B"): | |
| for unit in ("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"): | |
| if abs(num) < 1024.0: | |
| return f"{num:3.1f}{unit}{suffix}" | |
| num /= 1024.0 | |
| return f"{num:.1f}Yi{suffix}" | |
| needed_disk_space = int(retention_time_seconds) * int(ingested_samples_per_second) * int(bytes_per_sample) # also from prom docs | |
| print("Estimated disk space " + sizeof_fmt(needed_disk_space)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment