-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher_scripts.pl
executable file
·334 lines (269 loc) · 7.1 KB
/
watcher_scripts.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use Switch;
use lib '/usr/local/bin/opsadmin/perl/';
use DBI;
use Data::Dumper;
use Getopt::Long qw(:config no_ignore_case bundling);
use XML::Simple;
use XML::XPath;
require "ctime.pl";
our $AUTHOR = 'John_Vossler@McAfee.com';
our $VERSION = 0.9;
our $CONFIG = '/home/watcher/watcher.xml';
our $WATCHER = '/home/watcher';
our $XMLPWD = '/usr/local/bin/opsadmin/watcher';
our $CONFPWD = '/home/watcher/conf.scripts';
our %flags = ();
our @allargs = @ARGV;
sub get_opts();
sub usage();
&get_opts();
if($flags{'debug'})
{
print "\nflags\n";
print Dumper(\%flags);
}
##
## variables
##
my @classes = ();
my $class = ();
my $timestamp = ();
my $record_list = ();
my $record = ();
my $xp = ();
my @ips = ();
my $ip = ();
my %seen = ();
my @files = ();
my $file = ();
my $basefile = ();
my $latest = ();
my $dateext = ();
my $cmd = ();
##
## Get backup copies of any changed .pl files
##
#
# Get a list of all the sites
#
$xp = XML::XPath->new(filename => $CONFIG) or die "FATAL ERROR: Can't read $CONFIG as XML:\n";
if($flags{'debug'})
{
print "\nxp value\n";
print Dumper $xp;
}
$record_list = $xp->find("/watcher/DBpull/table");
foreach $record ($record_list->get_nodelist)
{
$ip=$record->getAttribute("ip");
push ( @ips, $ip );
}
if(($flags{'debug'}) || ($flags{'verbose'}))
{
print "\nDB source routable ip\n";
print Dumper \@ips;
}
#
# Make the site list unique
#
undef %seen;
@ips = grep(!$seen{$_}++, @ips);
if(($flags{'debug'}) || ($flags{'verbose'}))
{
print "\nUnique IPs to look at\n";
print Dumper \@ips;
}
#
# For each of the sites
#
foreach $ip (@ips)
{
@files = `/usr/bin/ssh $ip /bin/ls -1 $XMLPWD/*pl`;
chomp(@files);
if(($flags{'debug'}) || ($flags{'verbose'}))
{
print "\nfiles to backup for $ip \n";
print Dumper \@files;
}
#
# Check each file for differences, if different backup else remove it
#
foreach $file (@files)
{
if($flags{'debug'})
{
print "\nfile name to process \n";
print Dumper $file;
}
$basefile = `/bin/basename $file`;
chomp($basefile);
if($flags{'debug'})
{
print "\nbase name to process \n";
print Dumper $basefile;
}
$cmd= "/bin/ls -1rt $CONFPWD/$basefile.$ip* 2>/dev/null | /usr/bin/tail -1";
if($flags{'debug'})
{
print "\ncommand to run \n";
print Dumper $cmd;
}
$latest = `$cmd`;
chomp($latest);
if($flags{'debug'})
{
print "\nlatest name to compare to \n";
print Dumper $latest;
}
if($flags{'verbose'})
{
print "\n/usr/bin/scp $ip:$file $CONFPWD/$basefile \n";
}
system("/usr/bin/scp $ip:$file $CONFPWD/$basefile") == 0 or die "FATAL ERROR: Can't scp file /usr/bin/scp $ip:$file $CONFPWD/$basefile \n";
if ( $latest eq "" )
{
print "\n NO latest FILE FOUND \n";
$dateext = `/bin/date +%d%b%Y-%H:%M:%S`;
chomp($dateext);
system("/bin/mv $CONFPWD/$basefile $CONFPWD/$basefile.$ip.$dateext") == 0 or die "FATAL ERROR: Can't move file /bin/mv $CONFPWD/$basefile $CONFPWD/$basefile.$ip.$dateext :\n";
}
else
{
if ( `/usr/bin/cmp $CONFPWD/$basefile $latest` )
{
if($flags{'verbose'})
{
print "\n FILE CHANGED - saving a copy \n";
}
$dateext = `/bin/date +%d%b%Y-%H:%M:%S`;
chomp($dateext);
system("/bin/mv $CONFPWD/$basefile $CONFPWD/$basefile.$ip.$dateext") == 0 or die "FATAL ERROR: Can't move file /bin/mv $CONFPWD/$basefile $CONFPWD/$basefile.$ip.$dateext :\n";
}
else
{
if($flags{'verbose'})
{
print "\n NO CHANGE - removing scp file \n";
}
system("/bin/rm $CONFPWD/$basefile") == 0 or die "FATAL ERROR: Can't remove file /bin/rm $CONFPWD/$basefile :\n";
}
}
}
}
#
# do all of that for the local files
#
@files = `/bin/ls -1 /home/watcher/*pl`;
chomp(@files);
if(($flags{'debug'}) || ($flags{'verbose'}))
{
print "\nfiles to backup for localhost \n";
print Dumper \@files;
}
foreach $file (@files)
{
if($flags{'debug'})
{
print "\nfile name to process \n";
print Dumper $file;
}
$basefile = `/bin/basename $file`;
chomp($basefile);
if($flags{'debug'})
{
print "\nbase name to process \n";
print Dumper $basefile;
}
$cmd= "/bin/ls -1rt /home/watcher/conf.scripts/$basefile.localhost* 2>/dev/null | /usr/bin/tail -1";
if($flags{'debug'})
{
print "\ncommand to run \n";
print Dumper $cmd;
}
$latest = `$cmd`;
chomp($latest);
if($flags{'debug'})
{
print "\nlatest name to compare to \n";
print Dumper $latest;
}
if ( $latest eq "" )
{
print "\n NO latest FILE FOUND \n";
$dateext = `/bin/date +%d%b%Y-%H:%M:%S`;
chomp($dateext);
system("/bin/cp /home/watcher/$basefile /home/watcher/conf.scripts/$basefile.localhost.$dateext") == 0 or die "FATAL ERROR: Can't copy file /bin/cp /home/watcher/$basefile /home/watcher/conf.scripts/$basefile.localhost.$dateext :\n";
}
else
{
if ( `/usr/bin/cmp /home/watcher/$basefile $latest` )
{
if($flags{'verbose'})
{
print "\n FILE CHANGED - saving a copy \n";
}
$dateext = `/bin/date +%d%b%Y-%H:%M:%S`;
chomp($dateext);
system("/bin/cp /home/watcher/$basefile /home/watcher/conf.scripts/$basefile.localhost.$dateext") == 0 or die "FATAL ERROR: Can't copy file /bin/cp /home/watcher/$basefile /home/watcher/conf.scripts/$basefile.localhost.$dateext :\n";
}
else
{
if($flags{'verbose'})
{
print "\n NO CHANGE - no action on localhost perl file \n";
}
}
}
}
#
# Put in an entry indicating successful completion
#
$timestamp = time();
system("/usr/bin/psql -At -h localhost -U postgres watcher -c \"insert into scripts_time (epochtime) values (to_timestamp($timestamp))\"") == 0 or die "FATAL ERROR: Can't add database entry in scripts_time \n";
##
## End main - Begin subroutines
##
#
# Get all command line options into flags hash
#
sub get_opts()
{
use Getopt::Long qw(:config no_ignore_case bundling);
Getopt::Long::Configure("bundling");
GetOptions(
'debug|d' => \$flags{'debug'},
'verbose|v' => \$flags{'verbose'},
'graph|g' => \$flags{'graph'},
'sync|s' => \$flags{'sync'},
'help|usage|h' => sub {warn &usage; exit 1;})
or die &usage;
defined($flags{'debug'}) || ($flags{'debug'} = 0);
defined($flags{'verbose'}) || ($flags{'verbose'} = 0);
defined($flags{'graph'}) || ($flags{'graph'} = 0);
defined($flags{'sync'}) || ($flags{'sync'} = 0);
if ( $flags{'graph'} && $flags{'sync'} )
{
print "\n\nERROR: You cannot specify both --graph and --sync, they are mutually exclusive.\n";
exit 2;
}
}
# Subroutine: usage
# Args: <void>
# Return Value: <void>
# Purpose: Write the appropriate usage to STDOUT.
sub usage()
{
my $usage = <<EOF;
Usage: $0 [OPTIONS]
-v, --verbose Verbose Mode
-d, --debug Debug Mode
-s, --sync Only sync the DB, do not output graphs
-g, --graph Only print out graphs
-h, --help Print this help
EOF
print $usage;
}