/
wallarm_snmp_notify.py
119 lines (99 loc) · 3.79 KB
/
wallarm_snmp_notify.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
# Copyright 2014 Andrey Danin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import collectd
from pysnmp.entity.rfc3413.oneliner import ntforg
from threading import Thread
version = "0.0.1"
plugin_name = 'WallarmSNMPNotify'
class WallarmSNMPNotify(object):
def __init__(self, plugin_name):
self.plugin_name = plugin_name
self.config = {
'logging': {
'enabled': False,
'filename': '/tmp/wallarm_api_writer.log',
'level': 'debug',
},
'configured': False,
}
self.dest_hosts = {
'*/*': [
{
'version': 'v2c',
'community': 'wallarm',
'secret': None,
'hostname': '127.0.0.1',
'port': 162,
'oid': '1.3.6.1.4.44332.1.1',
},
],
}
self.logger = None
def get_time(self):
"""
Return the current time as epoch seconds.
"""
return int(time.mktime(time.localtime()))
def setup_logging(self):
if ('logging' not in self.config or
not self.config['logging'].get('enabled')):
return
logconfig = self.config['logging']
logging.basicConfig(
filename=logconfig['filename'],
level=logging.getLevelName(logconfig['level'].upper()))
self.logger = logging.getLogger()
def log(self, level, msg):
if level not in ('debug', 'info', 'notice', 'warning', 'error'):
err_msg = 'Unknown log level {}. Change it to "info"'.format(level)
self.logger and self.logger.info(err_msg)
collectd.info(err_msg)
level = 'info'
if self.logger:
getattr(self.logger, level)(msg)
getattr(collectd, level)(msg)
def get_dest_hosts(self, notification):
# TODO(adanin) Implement a real filtering algorithm.
return self.dest_hosts['*/*']
def send_trap(self, host, notification):
ntfOrg = ntforg.NotificationOriginator()
errorIndication = ntfOrg.sendNotification(
ntforg.CommunityData(host['community']),
ntforg.UdpTransportTarget((host['hostname'], host['port'])),
'trap',
ntforg.MibVariable('SNMPv2-MIB', host['oid']),
(ntforg.MibVariable('SNMPv2-MIB', 'sysName', 0), 'new name')
)
if errorIndication:
self.log('info', 'Notification did not sent: %s' % errorIndication)
def wallarm_snmp_notify(self, notification):
hosts = self.get_dest_hosts()
for host in hosts:
thr = Thread(target=send_trap, args=(host, notification))
thr.start()
def wallarm_snmp_notify_config(self, cfg):
# TODO(adanin) Add a real config parsing
self.config['configured'] = True
def wallarm_snmp_notify_init(self):
if self.config['configured']:
collectd.register_notification(self.wallarm_snmp_notify)
else:
self.log(
'warning',
'A configuration error occured. Abort initializing'
)
plugin = WallarmSNMPNotify(plugin_name)
collectd.register_config(plugin.wallarm_snmp_notify_config)
collectd.register_init(plugin.wallarm_snmp_notify_init)