]> git.ipfire.org Git - people/ms/suricata.git/blob - contrib/suri-graphite
Add one shot run option to suri-graphite.
[people/ms/suricata.git] / contrib / suri-graphite
1 #!/usr/bin/env python
2 # Copyright (C) 2013 Eric Leblond <eric@regit.org>
3 #
4 # You can copy, redistribute or modify this Program under the terms of
5 # the GNU General Public License version 3 as published by the Free
6 # Software Foundation.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # version 3 along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 # 02110-1301, USA.
17
18 import suricatasc
19 import socket
20 import time
21 import argparse
22
23 parser = argparse.ArgumentParser(prog='suri-graphite', description='Export suricata stats to Graphite')
24 parser.add_argument('-H', '--host', default='localhost', help='Host running Graphite')
25 parser.add_argument('-P', '--port', default=2003, help='Port of Graphite data socket')
26 parser.add_argument('-O', '--oneshot', action='store_const', const=True, help='Send one update and exit', default=False)
27 parser.add_argument('-D', '--delay', default=10, help='Delay between data dump')
28 parser.add_argument('-r', '--root', default='suricata.perf', help='Prefix of data name in Graphite')
29 parser.add_argument('socket', help='suricata socket file to connect to',
30 default="/usr/local/var/run/suricata/suricata-command.socket", nargs='?')
31 parser.add_argument('-v', '--verbose', action='store_const', const=True, help='verbose output', default=False)
32
33 args = parser.parse_args()
34 sc = suricatasc.SuricataSC(args.socket)
35 sc.connect()
36
37 sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
38 sck.connect((args.host, args.port))
39
40 while 1:
41 res = sc.send_command("dump-counters")
42 res = res['message']
43 tnow = int(time.time())
44 for thread in res:
45 for counter in res[thread]:
46 sck.send("%s.%s.%s %s %d\n" % (args.root, thread , counter, res[thread][counter], tnow))
47 if args.verbose:
48 print "%s.%s.%s %s %d\n" % (args.root, thread , counter, res[thread][counter], tnow)
49 if args.oneshot:
50 break
51 time.sleep(float(args.delay))