Info/CiscoConfig.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 78 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 208 CC 40 SCAR 60.8
CC
40
Cov
0%
copy_run_tftp
600.0
copy_run_start
306.0
BEGIN
2.0
CRAP 435.8
1 # SNMP::Info::CiscoConfig
2 #
3 # Copyright (c) 2008 Eric Miller
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 #
9 # * Redistributions of source code must retain the above copyright notice,
10 # this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of the University of California, Santa Cruz nor the
15 # names of its contributors may be used to endorse or promote products
16 # derived from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 # LIABLE FOR # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29  
30 package SNMP::Info::CiscoConfig;
31  
32 use strict;
33 use warnings;
34 use Exporter;
35 use SNMP::Info;
36  
37 @SNMP::Info::CiscoConfig::ISA = qw/SNMP::Info Exporter/;
38 @SNMP::Info::CiscoConfig::EXPORT_OK = qw//;
39  
40 our ($VERSION, %MIBS, %FUNCS, %GLOBALS, %MUNGE);
41  
42 $VERSION = '3.985003';
43  
44 %MIBS = (
45 'CISCO-CONFIG-COPY-MIB' => 'ccCopyTable',
46 'CISCO-FLASH-MIB' => 'ciscoFlashCopyTable',
47 'OLD-CISCO-SYS-MIB' => 'writeMem',
48 );
49  
50 %GLOBALS = (
51  
52 # OLD-CISCO-SYS-MIB
53 'old_write_mem' => 'writeMem',
54 'old_write_net' => 'writeNet',
55 );
56  
57 %FUNCS = (
58  
59 # CISCO-COPY-CONFIG-MIB::ccCopyTable
60 'config_protocol' => 'ccCopyProtocol',
61 'config_source_type' => 'ccCopySourceFileType',
62 'config_dest_type' => 'ccCopyDestFileType',
63 'config_server_addr' => 'ccCopyServerAddress',
64 'config_filename' => 'ccCopyFileName',
65 'config_username' => 'ccCopyUserName',
66 'config_password' => 'ccCopyUserPassword',
67 'config_notify_complete' => 'ccCopyNotificationOnCompletion',
68 'config_copy_state' => 'ccCopyState',
69 'config_copy_start_time' => 'ccCopyTimeStarted',
70 'config_copy_complete_time' => 'ccCopyTimeCompleted',
71 'config_fail_cause' => 'ccCopyFailCause',
72 'config_row_status' => 'ccCopyEntryRowStatus',
73  
74 # CISCO-FLASH-MIB::ciscoFlashCopyTable
75 'flash_copy_cmd' => 'ciscoFlashCopyCommand',
76 'flash_copy_protocol' => 'ciscoFlashCopyProtocol',
77 'flash_copy_address' => 'ciscoFlashCopyServerAddress',
78 'flash_copy_source' => 'ciscoFlashCopySourceName',
79 'flash_copy_dest' => 'ciscoFlashCopyDestinationName',
80 'flash_copy_row_status' => 'ciscoFlashCopyEntryStatus',
81 );
82  
83 %MUNGE = ();
84  
85 sub copy_run_tftp {
86 my $ciscoconfig = shift;
87 my ( $tftphost, $tftpfile ) = @_;
88  
89 srand( time() ^ ( $$ + ( $$ << 15 ) ) );
90 my $rand = int( rand( 1 << 24 ) );
91  
92 print "Saving running config to $tftphost as $tftpfile\n"
93 if $ciscoconfig->debug();
94  
95 #Try new method first fall back to old method
96 if ( $ciscoconfig->set_config_protocol( 1, $rand ) ) {
97 print "Using new method, row iid: $rand\n" if $ciscoconfig->debug();
98  
99 #Check each set, delete created row if any fail
100 unless ( $ciscoconfig->set_config_row_status( 5, $rand ) ) {
101 $ciscoconfig->error_throw("Initializing config copy instruction failed");
102 return;
103 }
104 unless ( $ciscoconfig->set_config_source_type( 4, $rand ) ) {
105 $ciscoconfig->error_throw("Setting source type failed");
106 unless ( $ciscoconfig->set_config_row_status( 6, $rand ) ) {
107 $ciscoconfig->error_throw(
108 "Setting source type failed and failed to delete row $rand"
109 );
110 }
111 return;
112 }
113 unless ( $ciscoconfig->set_config_dest_type( 1, $rand ) ) {
114 $ciscoconfig->error_throw("Setting destination type failed");
115 unless ( $ciscoconfig->set_config_row_status( 6, $rand ) ) {
116 $ciscoconfig->error_throw(
117 "Setting dest type failed and failed to delete row $rand"
118 );
119 }
120 return;
121 }
122 unless ( $ciscoconfig->set_config_server_addr( $tftphost, $rand ) ) {
123 $ciscoconfig->error_throw("Setting tftp server failed");
124 unless ( $ciscoconfig->set_config_row_status( 6, $rand ) ) {
125 $ciscoconfig->error_throw(
126 "Setting tftp server failed and failed to delete row $rand"
127 );
128 }
129 return;
130 }
131 unless ( $ciscoconfig->set_config_filename( $tftpfile, $rand ) ) {
132 $ciscoconfig->error_throw("Setting file name failed");
133 unless ( $ciscoconfig->set_config_row_status( 6, $rand ) ) {
134 $ciscoconfig->error_throw(
135 "Setting file name failed and failed to delete row $rand"
136 );
137 }
138 return;
139 }
140 unless ( $ciscoconfig->set_config_row_status( 1, $rand ) ) {
141 $ciscoconfig->error_throw("Initiating transfer failed");
142 unless ( $ciscoconfig->set_config_row_status( 6, $rand ) ) {
143 $ciscoconfig->error_throw(
144 "Initiating transfer failed and failed to delete row $rand"
145 );
146 }
147 return;
148 }
149 my $status = 0;
150 my $timer = 0;
151  
152 # Hard-coded timeout of approximately 5 minutes, we can wrap this in an
153 # option later if needed
154 my $timeout = 300;
155 while ( $status !~ /successful|failed/ ) {
156 my $t = $ciscoconfig->config_copy_state($rand);
157 $status = $t->{$rand};
158 last if $status =~ /successful|failed/;
159 $timer += 1;
160 if ( $timer >= $timeout ) {
161 $status = 'failed';
162 last;
163 }
164 sleep 1;
165 }
166  
167 unless ( $ciscoconfig->set_config_row_status( 6, $rand ) ) {
168 print "Failed deleting row, iid $rand\n" if $ciscoconfig->debug();
169 }
170  
171 if ( $status eq 'successful' ) {
172 print "Save operation successful\n" if $ciscoconfig->debug();
173 return 1;
174 }
175 if ( $status eq 'failed' ) {
176 $ciscoconfig->error_throw("Save operation failed");
177 return;
178 }
179  
180 }
181  
182 print "Using old method\n" if $ciscoconfig->debug();
183 unless ( $ciscoconfig->set_old_write_net( $tftpfile, $tftphost ) ) {
184 $ciscoconfig->error_throw("Save operation failed");
185 return;
186 }
187  
188 return 1;
189 }
190  
191 sub copy_run_start {
192 my $ciscoconfig = shift;
193  
194 srand( time() ^ ( $$ + ( $$ << 15 ) ) );
195 my $rand = int( rand( 1 << 24 ) );
196  
197 print "Saving running config to memory\n" if $ciscoconfig->debug();
198  
199 if ( $ciscoconfig->set_config_source_type( 4, $rand ) ) {
200 print "Using new method, row iid: $rand\n" if $ciscoconfig->debug();
201  
202 #Check each set, delete created row if any fail
203 unless ( $ciscoconfig->set_config_dest_type( 3, $rand ) ) {
204 $ciscoconfig->error_throw("Setting dest type failed");
205 unless ( $ciscoconfig->set_config_row_status( 6, $rand ) ) {
206 $ciscoconfig->error_throw(
207 "Setting dest type failed and failed to delete row $rand"
208 );
209 }
210 return;
211 }
212 unless ( $ciscoconfig->set_config_row_status( 1, $rand ) ) {
213 $ciscoconfig->error_throw("Initiating save failed");
214 unless ( $ciscoconfig->set_config_row_status( 6, $rand ) ) {
215 $ciscoconfig->error_throw(
216 "Initiating save failed and failed to delete row $rand");
217 }
218 return;
219 }
220 my $status = 0;
221 my $timer = 0;
222  
223 # Hard-coded timeout of approximately 5 minutes, we can wrap this in an
224 # option later if needed
225 my $timeout = 300;
226 while ( $status !~ /successful|failed/ ) {
227 my $t = $ciscoconfig->config_copy_state($rand);
228 $status = $t->{$rand};
229 last if $status =~ /successful|failed/;
230 $timer += 1;
231 if ( $timer >= $timeout ) {
232 $status = 'failed';
233 last;
234 }
235 sleep 1;
236 }
237  
238 unless ( $ciscoconfig->set_config_row_status( 6, $rand ) ) {
239 print "Failed deleting row, iid $rand\n" if $ciscoconfig->debug();
240 }
241  
242 if ( $status eq 'successful' ) {
243 print "Save operation successful\n" if $ciscoconfig->debug();
244 return 1;
245 }
246 if ( $status eq 'failed' ) {
247 $ciscoconfig->error_throw("Save operation failed");
248 return;
249 }
250  
251 }
252  
253 print "Using old method\n" if $ciscoconfig->debug();
254 unless ( $ciscoconfig->set_old_write_mem(1) ) {
255 $ciscoconfig->error_throw("Save operation failed");
256 return;
257 }
258  
259 return 1;
260 }
261  
262 1;
263 __END__