-
Notifications
You must be signed in to change notification settings - Fork 13
/
snmptrap-parser.py
61 lines (52 loc) · 1.77 KB
/
snmptrap-parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2021 Opensource ICT Solutions B.V.
# https://oicts.com
#
#version: 2.0.0
#date: 11-02-2021
#
#All rights reserved Opensource ICT Solutions B.V.
#Free to redistribute with mention to Opensource ICT Solutions
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <https://www.gnu.org/licenses/>.
# For use with Zabbix
# Place in directory
# Update snmptrapd.conf to use this script:
# disableAuthorization yes
# traphandle default /usr/bin/python3.6 <location of this script>
# And start Zabbix stuffs
import re
import sys
import time
import subprocess
# Trap write location:
destination = "/tmp/snmptrap.log" # File destination
# Getting Trap
trap = sys.stdin.readlines() # Read from stdin
trap.pop(0)
result = "" #Declare empty variable
r = "".join(trap) # Convert LIST to STRING
# Format the time string
time = time.strftime("%H:%M:%S %Y/%m/%d")
time = str(time)
# Matching on IPaddress
source = re.findall('UDP:.\[([\d.+]+)\]' ,r)
# Building the HEADER so that Zabbix understands it
HEADER = ''.join(source)
IP = str(source).strip("[]'")
HEADER = time + " ZBXTRAP " + str(IP)
# Appending it to the log file for Zabbix to pickup
with open(destination, "a") as file:
file.write (HEADER + "\n" + r)