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

ByDmitriy Altuhov

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 https://github.com/benhoyt/graphyte and simple Python script to export to Graphite (Carbon-cache)

import graphyte import time import logging import subprocess try: graphyte.init('graphite.host', 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 '12.123.26.128' | 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 '12.123.26.128' | 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 ‘12.123.26.128’

About the author

Dmitriy Altuhov administrator