-
Notifications
You must be signed in to change notification settings - Fork 8
/
dataDeletion.pl
executable file
·270 lines (238 loc) · 8.88 KB
/
dataDeletion.pl
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/perl
###########################################################################
# dataDeletion.pl - Database cleaning/trimming Plugin
# Author: Andrew Loss <[email protected]>
# Copyright (C) 2013 Andrew Loss
###########################################################################
#
# Used to trim old data from the database, recommended that this runs as a
# scheduled job. If your network is extremely large, and/or have a large
# number of new network users consistently.
#
# How to use:
# The -d for days option deletes all data older than the number of days
# specified by the -d option. This will vary depending on the records
# keeping for your organization.
# An IP address can be used to narrow the deletion of MAC and IP address,
# specified as: eg. 10.1.1.%
#
# Debugging:
# Standard output is silent and errors are logged to the control.log file.
# For more extensive output use the -debug option, levels 0-5 available
#
###########################################################################
# License:
#
# 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 2 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:
# http://www.gnu.org/licenses/gpl.txt
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
###########################################################################
#
use strict;
use warnings;
use NetDB;
use Getopt::Long;
# Sanity
no warnings 'uninitialized';
# currently only roughly supporting levels 0-4
our $DEBUG;
our $scriptName = "dataDeletion.pl";
our $logfile ="/var/log/netdb/control.log";
# command line options
my ( $optType, $optdays, $optipfilter );
# other vars
my ( $hours, $del_mac, $del_sp, $del_wifi, $del_arp );
# Print usage if no input
if ( $ARGV[0] eq "" ) { &usage(); }
GetOptions(
'd=i' => \$optdays,
'ip=s' => \$optipfilter,
'debug=i' => \$DEBUG,
'v' => \$DEBUG,
) or &usage();
if ( $optdays ) {
$hours = $optdays*24;
}
# Connect to the database with read/write access
my $dbh = connectDBrw();
if( $hours ){
&logMessage("Starting database cleanup of anything older then ".($optdays)." days.");
$del_mac = delMAC( $dbh, $hours, $optipfilter );
$del_sp = delSwitchPort( $dbh, $hours, $optipfilter );
$del_wifi = delWifi( $dbh, $hours, $optipfilter );
$del_arp = delARP( $dbh, $hours, $optipfilter );
&logMessage("Removed: \n");
&logMessage("\t$del_mac MAC addresses\n");
&logMessage("\t$del_sp switchport entries\n");
&logMessage("\t$del_wifi WiFi entries\n");
&logMessage("\t$del_arp entries\n");
&logMessage("\t\t totaling in ".($del_mac+$del_sp+$del_wifi+$del_arp)." rows deleted from the NetDB database.");
}
########################
## Deletion Functions ##
########################
#-----------------------------------------------------------
# Deletes all MAC address entries older then the hours
# database.
# Input:
# dbh - database handle
# hours - hours in the past
# ipfilter - optional SQL parameter to narrow deletion
# Output:
# number of entries deleted from database
#-----------------------------------------------------------
sub delMAC {
my $dbh = shift;
my $hours = shift;
my $ipfilter = shift;
my $records = undef;
if (!$dbh){ &logMessage("|ERROR|: No database connection!"); return undef; }
if (!$hours){ &logMessage("|ERROR|: Number of days not specified!"); return undef; }
my $netdb_ref = deleteMacs( $dbh, $hours, undef, $ipfilter );
if ( $$netdb_ref[1] ) {
$records = @$netdb_ref;
&logMessage("|Notice|: Deleting $records MAC addresses older then ".($hours/24)." days from database...");
deleteMacs( $dbh, $hours, 1, $ipfilter );
&logMessage("|Notice|: $records MAC addresses removed from database");
}
else{
$records = 0;
&logMessage("|Notice|: No MAC addresses older then ".($hours/24)." days found in database");
}
return $records;
}
#-----------------------------------------------------------
# Deletes all switchport entries older then the hours
# database.
# Input:
# dbh - database handle
# hours - hours in the past
# Output:
# number of entries deleted from database
#-----------------------------------------------------------
sub delSwitchPort {
my $dbh = shift;
my $hours = shift;
my $records = undef;
if (!$dbh){ &logMessage("|ERROR|: No database connection!"); return undef; }
if (!$hours){ &logMessage("|ERROR|: Number of days not specified!"); return undef; }
my $netdb_ref = deleteSwitch( $dbh, $hours, undef );
if ( $$netdb_ref[1] ) {
$records = @$netdb_ref;
&logMessage("|Notice|: Deleting $records switchport entries older then ".($hours/24)." days from database...");
deleteSwitch( $dbh, $hours, 1 );
&logMessage("|Notice|: $records switchport entries removed from database");
}
else{
$records = 0;
&logMessage("|Notice|: No switchport entries older then ".($hours/24)." days found in database");
}
return $records;
}
#-----------------------------------------------------------
# Deletes all WiFi entries older then the hours database.
# Input:
# dbh - database handle
# hours - hours in the past
# Output:
# number of entries deleted from database
#-----------------------------------------------------------
sub delWifi {
my $dbh = shift;
my $hours = shift;
my $records = undef;
if (!$dbh){ &logMessage("|ERROR|: No database connection!"); return undef; }
if (!$hours){ &logMessage("|ERROR|: Number of days not specified!"); return undef; }
my $netdb_ref = deleteWifi( $dbh, $hours, undef );
if ( $$netdb_ref[1] ) {
$records = @$netdb_ref;
&logMessage("|Notice|: Deleting $records WiFi entries older then ".($hours/24)." days from database...");
deleteWifi( $dbh, $hours, 1 );
&logMessage("|Notice|: $records WiFi entries removed from database");
}
else{
$records = 0;
&logMessage("|Notice|: No WiFi entries older then ".($hours/24)." days found in database");
}
return $records;
}
#-----------------------------------------------------------
# Deletes all ARP entries older then the hours database.
# Input:
# dbh - database handle
# hours - hours in the past
# ipfilter - optional SQL parameter to narrow deletion
# Output:
# number of entries deleted from database
#-----------------------------------------------------------
sub delARP {
my $dbh = shift;
my $hours = shift;
my $ipfilter = shift;
my $records = undef;
if (!$dbh){ &logMessage("|ERROR|: No database connection!"); return undef; }
if (!$hours){ &logMessage("|ERROR|: Number of days not specified!"); return undef; }
my $netdb_ref = deleteArp( $dbh, $hours, undef, $ipfilter );
if ( $$netdb_ref[1] ) {
$records = @$netdb_ref;
&logMessage("|Notice|: Deleting $records ARP entries older then ".($hours/24)." days from database...");
deleteArp( $dbh, $hours, 1, $ipfilter );
&logMessage("|Notice|: $records ARP entries removed from database");
}
else{
$records = 0;
&logMessage("|Notice|: No ARP entries older then ".($hours/24)." days found in database");
}
return $records;
}
######################
## Helper Functions ##
######################
#-----------------------------------------------------------
# Print to the logfile
#-----------------------------------------------------------
sub logMessage {
my @message = @_;
my $date = localtime;
open ( LOG, ">>$logfile" ) or die "Can't log to $logfile\n";
foreach my $line (@message) {
chomp( $line );
if ( $line ) {
print LOG "$date: $scriptName($$): $line\n";
print "$date: $scriptName($$): $line\n" if $DEBUG;
}
}
close LOG;
}
#-----------------------------------------------------------
# Generates timestamp in same format as NetDB
#-----------------------------------------------------------
sub netdbTimestamp {
my $timestamp = `date +'%a %b %d %T %Y'`;
chomp($timestamp);
return $timestamp;
}
sub usage {
print <<USAGE;
About: Deletes database entries in the NetDB database
Usage: dataDeletion.pl [options]
Deletion Options:
-d days Days in the past
-ip Optional SQL parameter to narrow deletion for MAC and ARP entries, eg. 10.1.1.%
Misc Options:
-debug Set debuging level (0-5)
-v Verbose output
USAGE
exit;
}