forked from etingof/snmpfwd
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogger.py
253 lines (182 loc) · 6.37 KB
/
logger.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#
# This file is part of snmpfwd software.
#
# Copyright (c) 2014-2019, Ilya Etingof <[email protected]>
# License: https://www.pysnmp.com/snmpfwd/license.html
#
# SNMP Proxy Forwarder plugin module
#
import logging
from logging import handlers
import time
import os
import socket
try:
from ConfigParser import RawConfigParser, Error
except ImportError:
from configparser import RawConfigParser, Error
from snmpfwd.plugins import status
from snmpfwd.error import SnmpfwdError
from snmpfwd import log
from pysnmp.proto.api import v2c
hostProgs = 'snmpfwd-server', 'snmpfwd-client'
apiVersions = 0, 2
PLUGIN_NAME = 'logger'
DEFAULTS = {
# general
'method': 'snmpfwd',
'level': 'INFO',
# file
'rotation': 'timed',
'backupcount': 30,
'timescale': 'D',
'interval': 1,
# syslog
'transport': 'socket',
'facility': 'daemon',
'host': 'localhost',
'port': 514,
# content
'pdus': ('GetRequest GetNextRequest SetRequest '
'GetBulkRequest InformRequest SNMPv2Trap Response'),
'template': '${isotime} ${callflow-id} ${snmp-peer-address} '
'${snmp-pdu-type} ${snmp-var-binds}',
'parentheses': '" "'
}
SYSLOG_TRANSPORTS = {
'udp': socket.SOCK_DGRAM,
'tcp': socket.SOCK_STREAM
}
SYSLOG_SOCKET_PATHS = (
'/dev/log',
'/var/run/syslog'
)
PDU_MAP = {}
# NOTE(etingof): this is a root logger
logger = logging.getLogger('snmpfwd-logger')
config = RawConfigParser(defaults=DEFAULTS)
for moduleOption in moduleOptions:
optionName, optionValue = moduleOption.split('=', 1)
if optionName == 'config':
configFile = optionValue
config.read(configFile)
method = config.get('general', 'method')
if method == 'file':
rotation = config.get('file', 'rotation')
if rotation == 'timed':
filename = config.get('file', 'destination')
handler = log.FileLogger.TimedRotatingFileHandler(
filename,
config.get('file', 'timescale'),
int(config.get('file', 'interval')),
int(config.get('file', 'backupcount'))
)
else:
raise SnmpfwdError('%s: unknown rotation method %s' % (PLUGIN_NAME, rotation))
elif method == 'syslog':
transport = config.get('syslog', 'transport')
if transport in SYSLOG_TRANSPORTS:
address = (
config.get('syslog', 'host'),
int(config.get('syslog', 'port'))
)
else:
address = None
for dev in SYSLOG_SOCKET_PATHS:
if os.path.exists(dev):
address = dev
transport = None
break
if transport and transport.startswith(os.path.sep):
address = transport
transport = None
if not address:
raise SnmpfwdError('Unknown syslog transport configured')
facility = config.get('syslog', 'facility').lower()
handler = handlers.SysLogHandler(
address=address,
facility=facility,
socktype=transport)
elif method == 'snmpfwd':
class ProxyLogger(object):
"""Just a mock to convey logging calls to snmpfwd log"""
error = log.error
info = log.info
debug = log.debug
def __str__(self):
return PLUGIN_NAME
handler = None
logger = ProxyLogger()
elif method == 'null':
handler = logging.NullHandler()
else:
raise SnmpfwdError('%s: unknown logging method %s' % (PLUGIN_NAME, method))
if handler:
level = config.get('general', 'level').upper()
try:
level = getattr(logging, level)
except AttributeError:
raise SnmpfwdError('%s: unknown log level %s' % (PLUGIN_NAME, level))
handler.setLevel(level)
# set logger level if this is a root logger (i.e. separate from snmpfwd)
logger.setLevel(level)
logger.addHandler(handler)
template = config.get('content', 'template')
logger.debug('%s: using %s logging method' % (PLUGIN_NAME, method))
pdus = config.get('content', 'pdus')
for pdu in pdus.split():
try:
PDU_MAP[getattr(v2c, pdu + 'PDU').tagSet] = pdu
except AttributeError:
raise SnmpfwdError('%s: unknown PDU %s' % (PLUGIN_NAME, pdu))
else:
logger.debug('%s: PDU ACL includes %s' % (PLUGIN_NAME, pdu))
try:
leftParen, rightParen = config.get('content', 'parentheses').split()
except Exception:
raise SnmpfwdError('%s: malformed "parentheses" values' % PLUGIN_NAME)
logger.debug('%s: using var-bind value parentheses %s %s' % (PLUGIN_NAME, leftParen, rightParen))
started = time.time()
logger.info('%s: plugin initialization complete' % PLUGIN_NAME)
def _format(template, pdu, context):
for key, value in context.items():
token = '${%s}' % key
if token in template:
template = template.replace(token, str(value))
token = '${snmp-var-binds}'
if token in template:
varBinds = ['%s %s%s%s' % (vb[0].prettyPrint(),
leftParen,
vb[1].prettyPrint().replace(leftParen, leftParen + leftParen).replace(rightParen, rightParen + rightParen),
rightParen)
for vb in v2c.apiPDU.getVarBinds(pdu)]
template = template.replace(token, ' '.join(varBinds))
token = '${snmp-pdu-type}'
if token in template:
template = template.replace(token, PDU_MAP[pdu.tagSet])
now = time.time()
token = '${asctime}'
if token in template:
timestamp = time.asctime(time.localtime(now))
template = template.replace(token, timestamp)
token = '${isotime}'
if token in template:
timestamp = time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(now))
timestamp += '.%02d' % (now % 1 * 100)
template = template.replace(token, timestamp)
token = '${timestamp}'
if token in template:
timestamp = '%.2f' % now
template = template.replace(token, timestamp)
token = '${uptime}'
if token in template:
timestamp = '%012.2f' % (time.time() - started)
template = template.replace(token, timestamp)
return template
def processCommandRequest(pluginId, snmpEngine, pdu, trunkMsg, reqCtx):
if pdu.tagSet in PDU_MAP:
logger.info(_format(template, pdu, trunkMsg))
return status.NEXT, pdu
processCommandResponse = processCommandRequest
processNotificationRequest = processCommandRequest
processNotificationResponse = processCommandRequest