| 1 | package SANtricity::SMcli; | ||
| 2 | # $Id: SMcli.pm,v 1.6 2004/12/14 19:42:23 rbishop Exp $ | ||
| 3 | |||
| 4 | use 5.008005; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use File::Temp; | ||
| 8 | |||
| 9 | require Exporter; | ||
| 10 | |||
| 11 | our @ISA = qw(Exporter); | ||
| 12 | |||
| 13 | # Items to export into callers namespace by default. Note: do not export | ||
| 14 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 15 | # Do not simply export all your public functions/methods/constants. | ||
| 16 | |||
| 17 | # This allows declaration use SMcli ':all'; | ||
| 18 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 19 | # will save memory. | ||
| 20 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 21 | |||
| 22 | ) ] ); | ||
| 23 | |||
| 24 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 25 | |||
| 26 | our @EXPORT = qw( | ||
| 27 | |||
| 28 | ); | ||
| 29 | |||
| 30 | our $VERSION = '0.02'; | ||
| 31 | |||
| 32 | |||
| 33 | # Preloaded methods go here. | ||
| 34 | |||
| 35 | sub new { | ||
| 36 | my $invocant = shift; | ||
| 37 | my $class = ref($invocant) || $invocant; | ||
| 38 | my $self = { debug => 0, | ||
| 39 | pass => '', | ||
| 40 | @_ }; | ||
| 41 | bless ($self,$class); | ||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | ############################################################### | ||
| 47 | # Method: arrayStatus # | ||
| 48 | # # | ||
| 49 | # Queries storage array status # | ||
| 50 | # Returns: 0 if array is OK # | ||
| 51 | # Array reference to output if a problem is detected # | ||
| 52 | ############################################################### | ||
| 53 | sub arrayStatus { | ||
| 54 | my $self = shift; | ||
| 55 | |||
| 56 | my @output = $self->runCmd('show storageArray healthStatus'); | ||
| 57 | foreach my $line (@output) { | ||
| 58 | if ($line =~ /^Storage array health status = (.+)\.$/) { | ||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | return \@output; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | ########################################################## | ||
| 68 | # Method: checkVol # | ||
| 69 | # # | ||
| 70 | # Gets information on a volume # | ||
| 71 | # Args: Volume to check # | ||
| 72 | # # | ||
| 73 | # Returns: Hash ref with all volume information returned # | ||
| 74 | ########################################################## | ||
| 75 | sub checkVol { | ||
| 76 | my $self = shift; | ||
| 77 | |||
| 78 | my $vol=shift; | ||
| 79 | $vol=cleanInput($vol); | ||
| 80 | my %results; | ||
| 81 | foreach my $line ($self->runCmd("show volumes volume[\"$vol\"]")) { | ||
| 82 | next if ($line =~ /^$/ || $line =~ /^VOLUME DETAILS$/); | ||
| 83 | |||
| 84 | my ($key,$value)= $line =~ /^\s+([^:]+): (.+)$/; | ||
| 85 | $results{$key}=$value; | ||
| 86 | |||
| 87 | |||
| 88 | } | ||
| 89 | return \%results; | ||
| 90 | } | ||
| 91 | |||
| 92 | ################################################################# | ||
| 93 | # Method: showController # | ||
| 94 | # # | ||
| 95 | # Shows controller information # | ||
| 96 | # Args: Optional hash containing: controller => (a | b) or # | ||
| 97 | # allControllers => 1 (default) # | ||
| 98 | # summary => 1 # | ||
| 99 | # Returns: Array reference containing command output # | ||
| 100 | ################################################################# | ||
| 101 | sub showController { | ||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | my %args=@_; | ||
| 105 | my $cmd; | ||
| 106 | if (%args) { | ||
| 107 | if ($args{controller}) { | ||
| 108 | $cmd = " controller [ ". cleanInput($args{controller}) ." ]"; | ||
| 109 | } else { | ||
| 110 | $cmd = " allControllers"; | ||
| 111 | } | ||
| 112 | $cmd .= " summary" if ($args{summary}); | ||
| 113 | } else { | ||
| 114 | $cmd = " allControllers"; | ||
| 115 | } | ||
| 116 | |||
| 117 | my @output = $self->runCmd("show $cmd"); | ||
| 118 | foreach my $line (@output) { | ||
| 119 | if ($line =~ /^Storage array health status = (.+)\.$/) { | ||
| 120 | return 0; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | return \@output; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | ################################################################################################## | ||
| 128 | # Method: getEvents # | ||
| 129 | # # | ||
| 130 | # Gets storage array events # | ||
| 131 | # Args: (optional) hash containing eventType: all or critical (defaults to all) # | ||
| 132 | # count: number of events to get (don't specify to get all) # | ||
| 133 | # Returns: File::Temp object of the tempfile containing the event log # | ||
| 134 | ################################################################################################## | ||
| 135 | sub getEvents { | ||
| 136 | my $self = shift; | ||
| 137 | |||
| 138 | my %args = @_; | ||
| 139 | my $type = defined $args{eventType} ? cleanInput($args{eventType}) : "all"; | ||
| 140 | my $count = defined $args{count} ? "count=". cleanInput($args{count}) : ''; | ||
| 141 | my $file = new File::Temp; | ||
| 142 | my $cmd = "save storageArray ${type}Events file=\"". $file->filename ."\" $count"; | ||
| 143 | foreach my $line ($self->runCmd($cmd)) { | ||
| 144 | next if ($line =~ /^$/); | ||
| 145 | warn "Unexpected output in downloading events on Array $self->{array}"; | ||
| 146 | return 0; | ||
| 147 | } | ||
| 148 | return $file; | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | ################################################################################################## | ||
| 154 | # Method: getConfig # | ||
| 155 | # # | ||
| 156 | # Gets storage array configuration # | ||
| 157 | # Args: (optional) hash containing any of the following keys: # | ||
| 158 | # globalSettings, volumeConfigAndSettings, hostTopology, lunMappings # | ||
| 159 | # # | ||
| 160 | # Returns: File::Temp object of the tempfile containing the event log # | ||
| 161 | ################################################################################################## | ||
| 162 | sub getConfig { | ||
| 163 | my $self = shift; | ||
| 164 | |||
| 165 | my %args = @_; | ||
| 166 | my $opts; | ||
| 167 | |||
| 168 | if (%args) { | ||
| 169 | foreach my $option qw(globalSettings volumeConfigAndSettings hostTopology lunMappings) { | ||
| 170 | $opts .= " $option="; | ||
| 171 | $opts .= $args{$option} ? "TRUE" : "FALSE"; | ||
| 172 | } | ||
| 173 | } else { | ||
| 174 | $opts=" allConfig"; | ||
| 175 | } | ||
| 176 | |||
| 177 | my $file = new File::Temp; | ||
| 178 | my $cmd = "save storageArray configuration file=\"". $file->filename ."\" $opts"; | ||
| 179 | foreach my $line ($self->runCmd($cmd)) { | ||
| 180 | next if ($line =~ /^$/); | ||
| 181 | warn "Unexpected output in downloading configuration on Array $self->{array}"; | ||
| 182 | return 0; | ||
| 183 | } | ||
| 184 | return $file; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | ######################################################################################### | ||
| 191 | # Method: monitorPerformance # | ||
| 192 | # # | ||
| 193 | # Monitor's array performance & returns stats # | ||
| 194 | # Args: Optional Hash containing: interval - seconds between data capture (default: 5) # | ||
| 195 | # iterations - # of data points to collect (default: 5) # | ||
| 196 | # Returns: File::Temp object to file containing performance data # | ||
| 197 | ######################################################################################### | ||
| 198 | sub monitorPerformance { | ||
| 199 | my $self = shift; | ||
| 200 | my %args = @_; | ||
| 201 | |||
| 202 | my $cmd; | ||
| 203 | if (%args) { | ||
| 204 | my $int = defined $args{interval} ? $args{interval} : 5; | ||
| 205 | $int = $int =~ tr/0-9//cd; | ||
| 206 | |||
| 207 | my $iter = defined $args{iterations} ? $args{iterations} : 5; | ||
| 208 | $iter = $iter =~ tr/0-9//cd; | ||
| 209 | |||
| 210 | $cmd = "set session performanceMonitorInterval=$int performanceMonitorIterations=$iter ; "; | ||
| 211 | } | ||
| 212 | my $file = new File::Temp; | ||
| 213 | $cmd .= "save storageArray performanceStats file=\"". $file->filename ."\""; | ||
| 214 | foreach my $line ($self->runCmd($cmd)) { | ||
| 215 | next if ($line =~ /^$/); | ||
| 216 | warn "Unexpected output in downloading configuration on Array $self->{array}"; | ||
| 217 | return 0; | ||
| 218 | } | ||
| 219 | return $file; | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | ##################################### | ||
| 227 | # Method: stopSnap # | ||
| 228 | # # | ||
| 229 | # Stops (suspends) a snapshot # | ||
| 230 | # Args: Snapshot to stop # | ||
| 231 | # Returns: 0 if snapshot stopped OK # | ||
| 232 | # 1 if problem detected # | ||
| 233 | ##################################### | ||
| 234 | sub stopSnap { | ||
| 235 | my $self = shift; | ||
| 236 | |||
| 237 | my $vol=shift; | ||
| 238 | $vol=cleanInput($vol); | ||
| 239 | foreach my $line ($self->runCmd("stop snapshot volume [\"$vol\"]")) { | ||
| 240 | next if ($line =~ /^$/); | ||
| 241 | warn "Unexpected output in stopping snapshot $vol on Array $self->{array}"; | ||
| 242 | return 1; | ||
| 243 | } | ||
| 244 | |||
| 245 | return 0; | ||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | |||
| 250 | ####################################### | ||
| 251 | # Method: recreateSnap # | ||
| 252 | # # | ||
| 253 | # Recreates a snapshot # | ||
| 254 | # Args: Snapshot to recreate # | ||
| 255 | # Returns: 0 if snapshot recreated OK # | ||
| 256 | # 1 if problem detected # | ||
| 257 | ####################################### | ||
| 258 | sub recreateSnap { | ||
| 259 | my $self = shift; | ||
| 260 | |||
| 261 | my $vol=shift; | ||
| 262 | $vol=cleanInput($vol); | ||
| 263 | foreach my $line ($self->runCmd("recreate snapshot volume [\"$vol\"]")) { | ||
| 264 | next if ($line =~ /^$/); | ||
| 265 | warn "Unexpected output in recreating snapshot $vol on Array $self->{array}"; | ||
| 266 | return 1; | ||
| 267 | } | ||
| 268 | |||
| 269 | return 0; | ||
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | ######################################################### | ||
| 275 | # Method: suspendRVM # | ||
| 276 | # # | ||
| 277 | # Suspends an RVM mirror (must be run on primary array) # | ||
| 278 | # Args: Primary side of mirror to the suspended # | ||
| 279 | # Returns: 0 if successful # | ||
| 280 | # 1 if problem detected # | ||
| 281 | ######################################################### | ||
| 282 | sub suspendRVM { | ||
| 283 | my $self = shift; | ||
| 284 | |||
| 285 | my $vol=shift; | ||
| 286 | $vol=cleanInput($vol); | ||
| 287 | foreach my $line ($self->runCmd("suspend remoteMirror primary [\"$vol\"]")) { | ||
| 288 | next if ($line =~ /^$/); | ||
| 289 | warn "Unexpected output in suspending RVM mirror $vol on Array $self->{array}"; | ||
| 290 | return 1; | ||
| 291 | } | ||
| 292 | |||
| 293 | return 0; | ||
| 294 | } | ||
| 295 | |||
| 296 | |||
| 297 | |||
| 298 | |||
| 299 | ######################################################## | ||
| 300 | # Method: resumeRVM # | ||
| 301 | # # | ||
| 302 | # Resumes an RVM mirror (must be run on primary array) # | ||
| 303 | # Args: Primary side of mirror to the resumed # | ||
| 304 | # Returns: 0 if successful # | ||
| 305 | # 1 if problem detected # | ||
| 306 | ######################################################## | ||
| 307 | sub resumeRVM { | ||
| 308 | my $self = shift; | ||
| 309 | |||
| 310 | my $vol=shift; | ||
| 311 | foreach my $line ($self->runCmd("resume remoteMirror primary [\"$vol\"]")) { | ||
| 312 | next if ($line =~ /^$/); | ||
| 313 | warn "Unexpected output in resuming RVM mirror $vol on Array $self->{array}"; | ||
| 314 | return 1; | ||
| 315 | } | ||
| 316 | |||
| 317 | return 0; | ||
| 318 | } | ||
| 319 | |||
| 320 | |||
| 321 | |||
| 322 | |||
| 323 | ######################################################### | ||
| 324 | # Method: runCmd # | ||
| 325 | # # | ||
| 326 | # Builds and runs an SMcli command # | ||
| 327 | # Args: smcli string to run # | ||
| 328 | # # | ||
| 329 | # Returns: Array containing all SMcli output # | ||
| 330 | # This should not be called from outside of this module # | ||
| 331 | ######################################################### | ||
| 332 | sub runCmd { | ||
| 333 | my $self=shift; | ||
| 334 | |||
| 335 | my $smcli_string=shift; | ||
| 336 | my $cmd = "SMcli -n $self->{array} "; | ||
| 337 | $cmd .= "-p $self->{pass} " if ($self->{pass}); | ||
| 338 | $cmd .= "-c '$smcli_string;'"; | ||
| 339 | |||
| 340 | print "$cmd\n" if ($self->{debug}); | ||
| 341 | |||
| 342 | |||
| 343 | open SMCLI,"$cmd 2>&1 |" or die "Can't run SMcli: $!"; | ||
| 344 | my (@return,$data); | ||
| 345 | while (<SMCLI>) { | ||
| 346 | print $_ if ($self->{debug}); | ||
| 347 | $data = 0 if (/Script execution complete.$/); | ||
| 348 | push @return,$_ if ($data); | ||
| 349 | warn "SMcli error" if (/^SMcli failed.$/); | ||
| 350 | $data=1 if (/^Executing script...$/); | ||
| 351 | } | ||
| 352 | close SMCLI; | ||
| 353 | |||
| 354 | return @return; | ||
| 355 | } | ||
| 356 | |||
| 357 | |||
| 358 | |||
| 359 | ############################################################################ | ||
| 360 | # Method: cleanInput # | ||
| 361 | # # | ||
| 362 | # Makes a stab at cleaning up input before it's passed to the command line # | ||
| 363 | # Args: String to clean # | ||
| 364 | # # | ||
| 365 | # Returns: Cleaned string # | ||
| 366 | ############################################################################ | ||
| 367 | sub cleanInput { | ||
| 368 | my $string=shift; | ||
| 369 | $string =~ tr/-_+A-Za-z0-9//cd; | ||
| 370 | return $string; | ||
| 371 | } | ||
| 372 | |||
| 373 | 1; | ||
| 374 | __END__ |