forked from yantisj/netgrph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerts.py
177 lines (133 loc) · 5.54 KB
/
alerts.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
#!/usr/bin/env python
#
# Copyright (c) 2016 "Jonathan Yantis"
#
# This file is a part of NetGrph.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3,
# as published by the Free Software Foundation.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# As a special exception, the copyright holders give permission to link the
# code of portions of this program with the OpenSSL library under certain
# conditions as described in each individual source file and distribute
# linked combinations including the program with the OpenSSL library. You
# must comply with the GNU Affero General Public License in all respects
# for all of the code used other than as permitted herein. If you modify
# file(s) with this exception, you may extend this exception to your
# version of the file(s), but you are not obligated to do so. If you do not
# wish to do so, delete this exception statement from your version. If you
# delete this exception statement from all source files in the program,
# then also delete it in the license file.
"""Process Alerts for different network events"""
import configparser
import smtplib
import logging
from email.mime.text import MIMEText
#from email.mime.multipart import MIMEMultipart
import nglib
verbose = 0
logger = logging.getLogger(__name__)
# Generate Alerts on new networks
def gen_new_network_alerts():
"""Generate New Network Alerts"""
gAlert = dict()
newNets = []
# Find any new networks
results = nglib.py2neo_ses.cypher.execute(
'MATCH(n:NewNetwork) return n.vrfcidr AS vrfcidr')
ncount = len(results)
if ncount > 0:
logger.info("Found %s New Networks to Alert On", ncount)
# Load groups in to dictionary to add list of networks to
loadGroups(gAlert)
# Append all networks to a list
for network in results:
#print(network.vrfcidr)
newNets.append(network.vrfcidr)
# Filter Nets for each group and add to their list to alert
loadNetAlerts(gAlert, newNets)
# Send Alerts to each group
for g in gAlert.keys():
if verbose > 1:
print("\n\nALERTS for {0}:\n".format(g))
acount = len(gAlert[g])
logger.info("Sending %s New Network Alerts to %s", acount, g)
if not verbose:
sendEmailAlert(g, gAlert[g])
# Deleting NewNetwork Objects
if not verbose:
results = nglib.py2neo_ses.cypher.execute(
'MATCH(n:NewNetwork) delete n')
def gen_new_vlan_alerts():
""" Send new VLAN Alerts to all groups that receive 'all' alerts """
newVlans = []
groups = []
# Find any new networks
results = nglib.py2neo_ses.cypher.execute(
'MATCH(v:NewVLAN) return v.name AS name')
ncount = len(results)
if ncount > 0:
logger.info("Found %s New Vlans to Alert On", ncount)
#
for group in nglib.config['NetAlertGroups']:
if nglib.config['NetAlertFilter'][group] == 'all':
groups.append(group)
# Append all vlans to a list
for vlan in results:
newVlans.append(vlan.name)
if nglib.verbose:
print(groups)
print(newVlans)
for group in groups:
sendEmailAlert(group, newVlans, vlan=True)
# Deleting NewVLAN Objects
if not verbose:
results = nglib.py2neo_ses.cypher.execute(
'MATCH(n:NewVLAN) delete n')
def loadGroups(gAlert):
"""Load all groups from config file into a dictionary of lists"""
for group in nglib.config['NetAlertGroups']:
gAlert[group] = []
def loadNetAlerts(gAlert, newNets):
"""Add networks to each group after filtering attributes"""
for net in newNets:
netDict = nglib.query.net.get_net_props(net)
if len(netDict.keys()):
for group in gAlert.keys():
if nglib.query.check_net_filter(netDict, group):
logger.debug("Adding " + netDict['CIDR'] + " to alerts for " + group)
gAlert[group].append(netDict)
def sendEmailAlert(group, nList, vlan=False):
"""Send group Network Alert from matched list"""
if nglib.verbose:
print("Emailing {0} New Network List Alert:\n {1}".format(group, str(nList)))
emailAddress = nglib.config['NetAlertGroups'][group]
fromAddress = nglib.config['NetAlert']['from']
mailServer = nglib.config['NetAlert']['mailServer']
subject = nglib.config['NetAlert']['subject']
if vlan:
subject = nglib.config['NetAlert']['vlansubject']
nTable = ""
for n in nList:
nTable = nTable + "\n" + str(n)
nTable = "<br />".join(nTable.split("\n"))
contents = '<font face="Courier New, Courier, monospace">' + nTable + '</font>'
msg = MIMEText(contents, 'html')
msg['Subject'] = subject
msg['From'] = fromAddress
msg['To'] = emailAddress
s = smtplib.SMTP(mailServer)
s.sendmail(fromAddress, [emailAddress], msg.as_string())
s.quit()
if nglib.verbose:
print(emailAddress, fromAddress, subject, contents)
# END