Skip to content

Instantly share code, notes, and snippets.

@bluven
Created November 6, 2019 09:04
Show Gist options
  • Save bluven/ec6371c3b77f7efe5814cd0e0d554799 to your computer and use it in GitHub Desktop.
Save bluven/ec6371c3b77f7efe5814cd0e0d554799 to your computer and use it in GitHub Desktop.
filebeat metrics
import requests
import argparse
import time
import re
def main():
parser = argparse.ArgumentParser(
description="Print per second stats from expvars")
parser.add_argument("--url", default="http://localhost:6060",
help="The URL from where to read the values")
parser.add_argument("--filter", default=None,
help="Filter metrics by this search regexp")
args = parser.parse_args()
last_vals = {}
# running average for last 30 measurements
N = 30
avg_vals = {}
now = time.time()
while True:
try:
time.sleep(1.0)
r = requests.get(args.url + "/debug/vars")
json = r.json()
last = now
now = time.time()
dt = now - last
for key, total in json.items():
if args.filter is not None:
if re.search(args.filter, key) is None:
continue
if isinstance(total, (int, long, float)):
if key in last_vals:
per_sec = (total - last_vals[key])/dt
if key not in avg_vals:
avg_vals[key] = []
avg_vals[key].append(per_sec)
if len(avg_vals[key]) > N:
avg_vals[key] = avg_vals[key][1:]
avg_sec = sum(avg_vals[key])/len(avg_vals[key])
else:
per_sec = "na"
avg_sec = "na"
last_vals[key] = total
print("{}: {}/s (avg: {}/s) (total: {})\n"
.format(key, per_sec, avg_sec, total))
except requests.ConnectionError:
print("Waiting for connection...\n")
last_vals = {}
avg_vals = {}
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment