-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch-switch-config.pl
executable file
·165 lines (135 loc) · 4.76 KB
/
watch-switch-config.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
#!/usr/bin/perl -w -T
use strict;
use Fcntl; # for sysopen
use File::Path;
use Sys::Syslog qw( :DEFAULT setlogsock);
use Proc::Daemon;
use SNMP;
Proc::Daemon::Init;
$ENV{"PATH"}="/usr/bin";
chdir("/tftpboot");
my $fpath = "/var/run/watch-switch-config.fifo";
# create a fifo to communicate with rsyslog
setlogsock('unix');
openlog($0,'','user');
#open(STDERR,">/tmp/logfile");
unless (-p $fpath) { # not a pipe
if (-e _) { # but a something else
die "$0: won't overwrite .signature\n";
} else {
require POSIX; POSIX::mkfifo($fpath, 0600) or die "can't mknod $fpath: $!"; warn "$0: created $fpath as a named pipe\n";
}
}
# This script watches the network devices log for configuration changes. Once
# a configuration has been udpated the conifg is sync'd via tftp and committed
# to SVN
my $rwcomm = "private";
my $svnuser = "svn_netcfg";
my $svnpass = "svnpasswd";
while(1){
sysopen(FIFO, $fpath, O_RDONLY) or die "can't read from $fpath: $!";
while(my $line = <FIFO>){
my $ciscoconfiged = '%SYS-5-CONFIG_I:';
if($line =~ /$ciscoconfiged/){
my @fields = split(' ',$line);
my $fqdn = $fields[1];
if ($fqdn =~ /^([-\d\w.]+)$/){
$fqdn = $1;
} else {
syslog('err', "Unsafe fqdn parsed from log\n");
next;
};
my $user = $fields[12];
if ($user =~ /^([-\d\w._]+)$/){
$user = $1;
} else {
syslog('err', "Unsafe user parsed from log\n");
next;
};
my $host;
my $site;
chomp($fqdn);
if ($fqdn =~ /^(\d+\.\d+\.\d+\.\d+)/){
$host = $1;
} else {
my @hostdetails = split('\.',$fqdn);
$host = $hostdetails[0];
$site = $hostdetails[1];
};
syslog('info', "Configuration on $host updated by $user");
# We can use this info to vary how we pull data, or where it gets stored.
my ($sess, $sesserror) = new SNMP::Session(DestHost => $fqdn,
Version => "2c",
Community => $rwcomm);
unless($sess){
syslog('err', "SNMP::Session creation failed: $sesserror\n");
next;
};
my $hostdetails_vars = new SNMP::VarList(
["sysName" , 0 ],
["sysLocation" , 0 ]);
my @hostdetails_vals = $sess->get($hostdetails_vars);
unless(@hostdetails_vals){
syslog('err', "Retrieval of host details for $fqdn failed\n");
next;
};
my $snmphostname = $hostdetails_vals[0];
if ($snmphostname =~ /^([-\d\w._]+)$/) {
$snmphostname = $1; # $code now untainted
} else {
syslog('err', "Skipping device reporting iffy host name\n");
next;
}
# We occasionally hit issues "-confg" files being created, probably during
# provisioning new devices
unless(length($snmphostname) > 0){
syslog('err', "Skipping device reporting blank host name\n");
next;
};
my $file = $snmphostname . "-confg";
my $location = $hostdetails_vals[1];
my @locparts = split(/\;/,$location);
if (@locparts > 0){
$location = join('/',@locparts);
my @created = mkpath($location,1,0666);
if(@created > 0){
foreach my $dir (@created){
my $svnadd = "/usr/bin/svn add --non-interactive --username $svnuser --password $svnpass \"" . $dir . "\"";
system($svnadd);
my $commitcommand = "/usr/bin/svn commit --non-interactive --username $svnuser --password $svnpass --message \"Creating directory $dir\" \"$dir\"";
system($commitcommand);
};
};
$file = $location . '/' . $file;
};
my $fileexisted = 0;
if(-e $file){
$fileexisted = 1
};
my $pullcommand = "/usr/local/scripts/snmp-get-config.pl $fqdn \"$file\"" ;
syslog('info', "Running $pullcommand");
system($pullcommand);
if($? == 0){
syslog('info', "Retrieved configuration for $host to $file");
if($fileexisted == 0){
my $svnadd = "svn add --non-interactive --username $svnuser --password $svnpass \"$file\"";
system($svnadd);
};
my $commitcommand = "/usr/bin/svn commit --non-interactive --username $svnuser --password $svnpass --message \"Configuration for host $host updated by $user\" \"$file\"";
my @svnout = system($commitcommand);
if($? != 0){
syslog('err', "Error commiting configurtaion for $host: $!");
syslog('err', "Error: " . join(@svnout));
}else{
syslog('info', "Commited $file to svn");
};
}else{
syslog('err', "Error retrieving configurtaion from $host: $!");
};
};
};
sleep 10;
close FIFO;
};
closelog;
exit 0;