File Coverage

blib/lib/Ipmitool.pm
Criterion Covered Total %
statement 15 141 10.6
branch 0 42 0.0
condition 0 9 0.0
subroutine 5 14 35.7
pod 3 8 37.5
total 23 214 10.7


line stmt bran cond sub pod time code
1             package Ipmitool;
2              
3 1     1   22323 use 5.010000;
  1         4  
  1         36  
4 1     1   5 use strict;
  1         2  
  1         29  
5 1     1   5 use warnings;
  1         6  
  1         25  
6 1     1   1100 use Net::Ping;
  1         52216  
  1         83  
7 1     1   1122 use Data::Dumper;
  1         11649  
  1         2073  
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 Ipmitool ':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             our %FRU;
33             # Preloaded methods go here.
34              
35             # Autoload methods go after =cut, and are processed by the autosplit program.
36             sub new
37             {
38 0     0 1   my $class = shift;
39 0           my $self = {};
40 0           bless($self,$class);
41 0           $self->_init(@_);
42 0           return ($self);
43             }
44              
45             sub remove_spaces
46             {
47 0     0 0   my $string = shift;
48 0           chomp($$string);
49 0           $$string =~ s/^\s+//; #remove leading spaces
50 0           $$string =~ s/\s+$//; #remove trailing spaces
51             #print "String passed: $string\n";
52             }
53              
54             sub fru
55             {
56 0     0 1   my $self = shift;
57 0           my $h = $self->{"ipaddress"};
58 0           my $u = $self->{"username"};
59 0           my $p = $self->{"password"};
60 0 0         open PASSFILE, ">file" or die "Unable to create password file\n";
61 0           print PASSFILE "$p";
62 0           close(PASSFILE);
63 0           system("ipmitool -H $h -U $u -f file fru > .fru.out");
64 0 0         if ( $? == -1 )
65             {
66 0           print "command failed: $!\n";
67             }else
68             {
69 0           printf "command exited with value %d\n", $? >> 8;
70             }
71              
72 0 0         open FRU,".fru.out" or die "Unable to open .fru.out file\n";
73              
74 0           my @fru_line_numbers;
75 0           while()
76             {
77 0 0         if(/FRU Device Description/)
78             {
79 0           push(@fru_line_numbers,$.);
80 0           my @a1 = split(":");
81 0           chomp($a1[1]);
82             #print "element $a1[1]\n";
83 0           my @a2 = split(/[()]/,$a1[1]);
84             #print "name : $a2[1]\n";
85 0           my $fruname = $a2[0];
86 0           my $id = $a2[1];
87 0           $id =~ s/ID//g;
88 0           print "$. fruname : $fruname and ID is : $id\n";
89             }
90             }
91 0           close(FRU);
92            
93 0           for (my $j=0;$j<$#fru_line_numbers;$j=$j+1)
94             {
95 0           create_fru_hash(\$self,$fru_line_numbers[$j], $fru_line_numbers[$j+1]);
96             }
97            
98             #print Dumper(\%FRU);
99 0           return ( %FRU );
100             }
101              
102             sub find_records
103             {
104 0     0 0   my $start = shift;
105 0           my $end = shift;
106 0           my $array1 = shift;
107 0           my $flag = 0;
108             #print "start : $start and end is : $end\n";
109 0 0         open FRU1,".fru.out" or die "Unable to open .fru.out file\n";
110 0           while()
111             {
112 0 0 0       if (($. > $start) && ($. < $end))
113             {
114 0 0         if ( ! (/:/ | /^\s*$/) )
115             {
116 0           push(@$array1,$.);
117 0           $flag = 1;
118             }
119             }
120             }
121 0           close(FRU1);
122 0           return($flag);
123             }
124              
125             sub create_fru_hash
126             {
127 0     0 0   my $self = shift;
128 0           my $start = shift;
129 0           my $end = shift;
130 0           my $temp_fru_name;
131            
132 0           print "start : $start and end : $end\n";
133            
134 0           my @rec_line_numbers;
135 0           my $ret= find_records($start,$end,\@rec_line_numbers);
136 0           foreach(@rec_line_numbers)
137             {
138 0           print "line numbers: $_\n";
139             }
140 0 0         open FRU,".fru.out" or die "Unable to open .fru.out file\n";
141            
142 0 0         if ( $ret == 1 )
143             {
144 0           my $backup_end = $end;
145 0           $end = $rec_line_numbers[0];
146             }
147 0           while()
148             {
149 0 0 0       if ( ($. >= $start) && ($. < $end) )
150             {
151 0 0         if(! /^\s*$/)
152             {
153             #print $_;
154 0 0         if ( /FRU Device Description/ )
155             {
156 0           my @a1 = split(":");
157 0           chomp($a1[1]);
158 0           my @a2 = split(/[()]/,$a1[1]);
159 0           $temp_fru_name = $a2[0];
160 0           remove_spaces(\$temp_fru_name);
161 0           my $id = $a2[1];
162 0           $id =~ s/ID//g;
163 0           remove_spaces(\$id);
164 0           $FRU{$temp_fru_name}{ID} = $id;
165             }else
166             {
167             #print "inside else : $_\n";
168 0           my @a1 = split(":");
169             #print "$a1[0] : $a1[1]\n";
170 0           remove_spaces(\$a1[0]);
171 0           remove_spaces(\$a1[1]);
172 0           $FRU{$temp_fru_name}{$a1[0]} = $a1[1];
173             }
174            
175             }
176             }
177             }
178 0 0         if($ret == 1)
179             {
180 0           seek(FRU,0,0);
181 0           for(my $i = 0; $i<$#rec_line_numbers; $i++)
182             {
183 0           print "inside for loop \n";
184 0           add_sub_record($temp_fru_name,$rec_line_numbers[$i],$rec_line_numbers[$i+1]);
185             }
186             }
187 0           close(FRU);
188             }
189              
190             sub add_sub_record
191             {
192 0     0 0   my $fruname = shift;
193 0           my $start = shift;
194 0           my $end = shift;
195 0           my $rec_name;
196              
197 0 0         open F1, ".fru.out" or die "Unable to open .fru.out file\n";
198             #print "inside add_sub_record func $fruname $start : $end\n";
199 0           while()
200             {
201 0 0         if ( $. == $start )
202             {
203 0           $rec_name = $_;
204 0           remove_spaces(\$rec_name);
205             #print "rec_name : $rec_name\n";
206             }
207 0 0 0       if ( ($. > $start ) && ( $. < $end ) )
208             {
209             #print "inside if block\n";
210 0           my @a1 = split(":");
211 0           remove_spaces(\$a1[0]);
212 0           remove_spaces(\$a1[1]);
213             #print "$fruname : $rec_name : $a1[0] : $a1[1]\n";
214 0           $FRU{$fruname}{$rec_name}{$a1[0]} = $a1[1];
215             }
216             }
217 0           close(F1);
218             }
219              
220             sub print
221             {
222 0     0 0   my $self= shift;
223 0           foreach (keys (%{$self}))
  0            
224             {
225 0           print "$_: $self->{$_}\n"
226             }
227             #print "IP Address : $self->{ipaddress}\n" if (defined($self->{ipaddress}));
228             #print "Username : $self->{username}\n" if (defined($self->{username}));
229             #print "Password : $self->{password}\n" if (defined($self->{password}));
230 0           return 0;
231             }
232              
233             sub bmc
234             {
235 0     0 1   my $self = shift;
236 0           my $cmd = shift;
237 0           my @op = `ipmitool -H $self->{ipaddress} -U $self->{username} -f file bmc $cmd`;
238             #print @op;
239 0           foreach(@op)
240             {
241 0           print "$_";
242             }
243             }
244              
245             sub _init
246             {
247              
248 0     0     my ($self,@args)=@_;
249 0           while (@args)
250             {
251 0           my ($x);
252 0           ($x)=shift(@args);
253 0 0         if ($x eq "-ipaddress")
    0          
    0          
254             {
255 0           $self->{"ipaddress"}=shift(@args);
256 0           my $p = Net::Ping->new();
257 0 0         if (!$p->ping($self->{"ipaddress"}))
258             {
259             #print " Machine is alive\n";
260 0           die "$self->{ipaddress} : invalid ip address or machine is unreachable\n";
261             }
262 0           $p->close();
263             } elsif ($x eq "-username")
264             {
265 0           $self->{"username"}=shift(@args);
266             } elsif ($x eq "-password")
267             {
268 0           $self->{"password"}=shift(@args);
269 0 0         open PASSFILE, ">file" or die "Unable to create password file\n";
270 0           print PASSFILE "$self->{password}";
271 0           close(PASSFILE);
272             } else
273             {
274 0           die "Invalid argument : $x\n";
275             }
276             }
277              
278 0           return(0);
279              
280             }
281              
282              
283             # Preloaded methods go here.
284              
285             1;
286             __END__