Skip to content

Instantly share code, notes, and snippets.

@Rand01ph
Last active October 10, 2019 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rand01ph/eb721fb7a4767e12414a92db0bd6756f to your computer and use it in GitHub Desktop.
Save Rand01ph/eb721fb7a4767e12414a92db0bd6756f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by Rand01ph on 2019/10/9
import requests
import argparse
import time
import curses
stdscr = curses.initscr()
def main():
global stdscr
parser = argparse.ArgumentParser(
description="Print per second stats from expvars")
parser.add_argument("--url", default="http://172.16.1.156:5066",
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()
curses.noecho()
curses.cbreak()
last_vals = {}
# 采样保留长度
N = 30
avg_vals = {}
now = time.time()
while True:
try:
time.sleep(1.0)
stdscr.erase()
r = requests.get(args.url + "/stats")
r_json = r.json()
last = now
now = time.time()
dt = now - last
filebeat_events = r_json.get('filebeat').get('events')
libbeat_output_events = r_json.get('libbeat').get('output').get('events')
libbeat_pipeline_events = r_json.get('libbeat').get('pipeline').get('events')
registrar_writes = r_json.get('registrar').get("writes")
line_display("filebeat.events", filebeat_events, last_vals, avg_vals, dt, N)
line_display("libbeat.output.events", libbeat_output_events, last_vals, avg_vals, dt, N)
line_display("libbeat.pipeline.events", libbeat_pipeline_events, last_vals, avg_vals, dt, N)
line_display("registrar.writes", registrar_writes, last_vals, avg_vals, dt, N)
stdscr.refresh()
except requests.ConnectionError:
stdscr.addstr("Waiting for connection...\n")
stdscr.refresh()
last_vals = {}
avg_vals = {}
def line_display(line_name, line_json, last_vals, avg_vals, dt, N):
global stdscr
for key, total in line_json.items():
key = line_name + "." + key
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
try:
stdscr.addstr("{}.{}: {}/s (avg: {}/s) (total: {})\n"
.format(line_name, key, per_sec, avg_sec, total))
except Exception as e:
raise Exception("curses.addstr fail. Resize the " +
"terminal window or use the filter" +
"option: {}".format(e))
def unset_win():
global stdstr
# from curses.wrapper()
stdscr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
if __name__ == "__main__":
try:
main()
except Exception as e:
print e
finally:
unset_win()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment