scripts/munin/iptables_

51 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Compteur des règles iptables
import sys,commands,string
# On prend l'argument pour définir la table à analyser
TABLE = sys.argv[0].split('_')[1]
if TABLE :
IPTABLES = "iptables -n -t %s -L " % TABLE
else :
IPTABLES = "iptables -n -L "
try :
arg = sys.argv[1]
except :
arg = ''
CHAINS = commands.getoutput('%s | grep Chain | awk \'{print $2}\'' % IPTABLES).split('\n')
def name_of_chain(chain):
"""Identifiant munin de la chaine"""
return string.lower(chain.replace('_', '').replace('-', '').replace('.','').replace('/', ''))
def label_of_chain(chain):
"""Label affiché sur le graphe"""
return chain.replace('_', '-').replace('.','-').replace('/','-')
if arg == "config" :
print 'graph_title Firewall %s' % string.lower(TABLE)
print 'graph_args --base 1000 --lower-limit 0'
print 'graph_category network'
print "graph_vlabel nb de regles"
for chain in CHAINS :
nom = name_of_chain(chain)
label = label_of_chain(chain)
print "%s.label %s" % (nom, label)
print "%s.draw AREASTACK" % nom
if (label,TABLE) == ("TEST-MAC-IP",'filter'):
print "%s.warning 8:12" % nom
print "%s.critical 1:20" % nom
else :
for chain in CHAINS :
nom = name_of_chain(chain)
label = label_of_chain(chain)
# On retire 2 pour les entêtes
value = int(commands.getoutput('%s %s | wc -l' % (IPTABLES, chain))) - 2
print "%s.value %d" % (nom, value)