Skip to content

Instantly share code, notes, and snippets.

@toubib
Last active June 10, 2016 22:22
Show Gist options
  • Save toubib/e1004ace58c3b4a3446b1e4e4cc59ada to your computer and use it in GitHub Desktop.
Save toubib/e1004ace58c3b4a3446b1e4e4cc59ada to your computer and use it in GitHub Desktop.
shorewall-graphite-grafana
#!/usr/bin/python
# made from https://github.com/graphite-project/carbon/blob/master/examples/example-pickle-client.py
import re
import sys
import time
import socket
import platform
import subprocess
import pickle
import struct
from daemonize import Daemonize
ACCOUNTING_LABEL = 'loc-net'
CARBON_SERVER = '192.168.0.x'
CARBON_PICKLE_PORT = 2004
DELAY = 60
#get accounting data from shell
def get_accounting():
accounting_data = []
#Regex to parse the iptaccount output
target = re.compile('^(?P<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3});(?P<src_packets>\d+);(?P<src_bytes>\d+);(?P<dst_packets>\d+);(?P<dst_bytes>\d+)$')
#Shell command
command = "iptaccount -s -l %s" % ACCOUNTING_LABEL
#Run the iptaccount command
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
out, err = process.communicate()
#Add metrics data to accounting_data
for line in out.splitlines():
match = target.match(line)
if match:
data = match.groupdict()
accounting_data.append(data)
return accounting_data
def run(sock, delay):
"""Make the client go go go"""
hostname = socket.gethostname()
while True:
now = int(time.time())
tuples = ([])
lines = []
#Get accounting_data
accounting_data = get_accounting()
#Fill the graphite payload
for data in accounting_data:
ip = data['IP'].replace('.','_')
tuples.append(("firewall.%s.accounting.perip.%s.tx_packets" % (hostname, ip),(now, data['src_packets'])))
tuples.append(("firewall.%s.accounting.perip.%s.rx_packets" % (hostname, ip),(now, data['dst_packets'])))
tuples.append(("firewall.%s.accounting.perip.%s.tx_bytes" % (hostname, ip),(now, data['src_bytes'])))
tuples.append(("firewall.%s.accounting.perip.%s.rx_bytes" % (hostname, ip),(now, data['dst_bytes'])))
lines.append("firewall.%s.accounting.perip.%s.tx_packets %s %d" % (hostname, ip, data['src_packets'], now))
lines.append("firewall.%s.accounting.perip.%s.rx_packets %s %d" % (hostname, ip, data['dst_packets'], now))
lines.append("firewall.%s.accounting.perip.%s.tx_bytes %s %d" % (hostname, ip, data['src_bytes'], now))
lines.append("firewall.%s.accounting.perip.%s.rx_bytes %s %d" % (hostname, ip, data['dst_bytes'], now))
message = '\n'.join(lines) + '\n' #all lines must end in a newline
print "sending message"
print '-' * 80
print message
#send to graphite
package = pickle.dumps(tuples, 1)
size = struct.pack('!L', len(package))
sock.sendall(size)
sock.sendall(package)
#wait for next run
time.sleep(delay)
def main():
"""Wrap it all up together"""
delay = DELAY
if len(sys.argv) > 1:
arg = sys.argv[1]
if arg.isdigit():
delay = int(arg)
else:
sys.stderr.write("Ignoring non-integer argument. Using default: %ss\n" % delay)
sock = socket.socket()
try:
sock.connect( (CARBON_SERVER, CARBON_PICKLE_PORT) )
except socket.error:
raise SystemExit("Couldn't connect to %(server)s on port %(port)d, is carbon-cache.py running?" % { 'server':CARBON_SERVER, 'port':CARBON_PICKLE_PORT })
try:
run(sock, delay)
except KeyboardInterrupt:
sys.stderr.write("\nExiting on CTRL-c\n")
sys.exit(0)
# Run the daemon
if __name__ == "__main__":
daemon = Daemonize(app="shorewall-ipa-graphite", pid='/var/run/shorewall-ipa-graphite.pid', action=main)
daemon.start()
[Unit]
Description=shorewall-ipa-graphite
After=network.target
[Service]
Type=forking
WorkingDirectory=/var/tmp
User=root
PIDFile=/var/run/shorewall-ipa-graphite.pid
ExecStart=/usr/local/bin/shorewall-ipa-graphite.py
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment