Python. Export to Graphite (carbon) simultaneous connections (and other).

Автор:Дмитрий Алтухов

Python. Export to Graphite (carbon) simultaneous connections (and other).

We have a small task: export number of a simultaneous connections to Graphite (carbon) and visualize it in the Grafana

We used and simple Python script to export to Graphite (Carbon-cache)

import graphyte import time import logging import subprocess try: graphyte.init('', prefix='metric.prefix', port=2003, interval=10) except Exception as e: logging.error(str(e)) raise SystemExit try: while True: command_value = subprocess.check_output("netstat -n | grep -e '11.169' -e '165.172' | grep -v -e '' | grep -e 'ESTABLISHED' | wc -l", shell=True) command_int = int(command_value.decode()) graphyte.send('metricname', command_int) #print(command_int) time.sleep(1) except (KeyboardInterrupt, SystemExit): print(' - Ctrl-C pressed')
Code language: Python (python)
netstat -n | grep -e '11.169' -e '165.172' | grep -v -e '' | grep -e 'ESTABLISHED' | wc -l
Code language: Bash (bash)

This command returns number of the current ESTABLISHED simultaneous connections to the IPs starting from ‘11.169’ and ‘165.172’ (like «mask») and exept IP ‘’

Об авторе

Дмитрий Алтухов administrator