Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / network / shorewall_acc @ a7139bca

Historique | Voir | Annoter | Télécharger (1,93 ko)

1
#!/usr/bin/env python
2
# shorewall_accounting
3
# A munin plugin for tracking traffic as recorded by shorewall accounting rules
4
# Written by Chris AtLee <chris@atlee.ca>
5
#  Modified by Tanguy Pruvot for KMGT <tanguy.pruvot@gmail.com>
6
# Released under the GPL v2
7
import sys, commands, re
8
accountingLineExp = re.compile(r"^\s*\d+[KMG]*\s+(\d+)([KMGT]*)\s+(\w+).*$")
9

    
10
def getBytesByChain():
11
    trafficCmd = "shorewall"
12
    status, output = commands.getstatusoutput("/sbin/shorewall show accounting 2>/dev/null")
13
    if status != 0:
14
        raise OSError("Error running command (%s)[%i]: %s" % (trafficCmd, status, output))
15
    chains = {}
16
    for line in output.split("\n"):
17
        m = accountingLineExp.match(line)
18
        if m is not None:
19
            target = m.group(3)
20
            bytes = int(m.group(1))
21

    
22
	    if m.group(2) == "K":
23
	    	bytes *= 1024
24
            elif m.group(2) == "M":
25
		bytes *= 1024 * 1024
26
	    elif m.group(2) == "G":
27
		bytes *= 1024 * 1024 * 1024
28
	    elif m.group(2) == "T":
29
		bytes *= 1024 * 1024 * 1024 * 1024
30

    
31
            if target in chains:
32
                chains[target] += bytes
33
            else:
34
                chains[target] = bytes
35
#	else:
36
#	    print "IGNORED LINE %s" % ( line)
37

    
38
    retval = []
39
    chainNames = chains.keys()
40
    chainNames.sort()
41
    for name in chainNames:
42
        retval.append((name, chains[name]))
43
    return retval
44

    
45
if len(sys.argv) > 1:
46
    if sys.argv[1] == "autoconf":
47
        print "yes"
48
        sys.exit(0)
49
    elif sys.argv[1] == "config":
50
        print "graph_title Shorewall accounting"
51
        print "graph_category network"
52
        print "graph_vlabel bits per ${graph_period}"
53
        for chain,bytes in getBytesByChain():
54
            print "%s.min 0" % chain
55
            print "%s.type DERIVE" % chain
56
            print "%s.label %s" % (chain, chain)
57
            print "%s.cdef %s,8,*" % (chain, chain)
58
        sys.exit(0)
59

    
60
for chain, bytes in getBytesByChain():
61
    print "%s.value %i" % (chain, bytes)