Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 1 | #! /usr/bin/perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | |
| 5 | use mrw::Targets; |
| 6 | use mrw::Inventory; |
| 7 | use mrw::Util; |
| 8 | use Getopt::Long; # For parsing command line arguments |
| 9 | use YAML::Tiny qw(LoadFile); |
| 10 | |
| 11 | # Globals |
| 12 | my $serverwizFile = ""; |
| 13 | my $debug = 0; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 14 | my $outputFile = ""; |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 15 | my $metaDir = ""; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 16 | |
| 17 | # Command line argument parsing |
| 18 | GetOptions( |
| 19 | "i=s" => \$serverwizFile, # string |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 20 | "m=s" => \$metaDir, # string |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 21 | "o=s" => \$outputFile, # string |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 22 | "d" => \$debug, |
| 23 | ) |
| 24 | or printUsage(); |
| 25 | |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 26 | if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDir eq "")) |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 27 | { |
| 28 | printUsage(); |
| 29 | } |
| 30 | |
| 31 | my $targetObj = Targets->new; |
| 32 | $targetObj->loadXML($serverwizFile); |
| 33 | |
| 34 | #open the mrw xml and the metaData file for the sensor. |
| 35 | #Fetch the sensorid,sensortype,class,object path from the mrw. |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 36 | #Get the metadata for that sensor from the metadata file. |
| 37 | #Merge the data into the outputfile |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 38 | |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 39 | my $sensorTypeConfig; |
| 40 | my $tmpSensor; |
| 41 | opendir my $dir,$metaDir or die "Cannot open directory: $!"; |
| 42 | my @files = readdir $dir; |
| 43 | foreach my $file (@files){ |
| 44 | my ($filename,$extn) = split(/\.([^\.]+)$/,$file); |
| 45 | if((defined($extn)) and ( $extn eq "yaml")) { |
| 46 | my $metaDataFile = $metaDir."/".$file; |
| 47 | my $tmpSensor = LoadFile($metaDataFile); |
| 48 | if(!keys %{$sensorTypeConfig}) { |
| 49 | %{$sensorTypeConfig} = %{$tmpSensor}; |
| 50 | } |
| 51 | else { |
| 52 | %{$sensorTypeConfig} = (%{$sensorTypeConfig},%{$tmpSensor}); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 58 | open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!"; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 59 | |
| 60 | my @interestedTypes = keys %{$sensorTypeConfig}; |
| 61 | my %types; |
| 62 | |
| 63 | @types{@interestedTypes} = (); |
| 64 | |
| 65 | my @inventory = Inventory::getInventory($targetObj); |
| 66 | #Process all the targets in the XML |
| 67 | foreach my $target (sort keys %{$targetObj->getAllTargets()}) |
| 68 | { |
| 69 | my $sensorID = ''; |
| 70 | my $sensorType = ''; |
| 71 | my $sensorReadingType = ''; |
| 72 | my $path = ''; |
| 73 | my $obmcPath = ''; |
| 74 | |
| 75 | if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") { |
| 76 | |
| 77 | $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID"); |
| 78 | |
| 79 | $sensorType = $targetObj->getAttribute($target, "IPMI_SENSOR_TYPE"); |
| 80 | |
| 81 | $sensorReadingType = $targetObj->getAttribute($target, |
| 82 | "IPMI_SENSOR_READING_TYPE"); |
| 83 | |
| 84 | $path = $targetObj->getAttribute($target, "INSTANCE_PATH"); |
| 85 | |
| 86 | #not interested in this sensortype |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 87 | next if (not exists $types{$sensorType}); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 88 | |
| 89 | #if there is ipmi sensor without sensorid or sensorReadingType or |
| 90 | #Instance path then die |
| 91 | |
| 92 | if ($sensorID eq '' or $sensorReadingType eq '' or $path eq '') { |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 93 | close $fh; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 94 | die("sensor without info for target=$target"); |
| 95 | } |
| 96 | |
| 97 | #removing the string "instance:" from path |
| 98 | $path =~ s/^instance:/\//; |
| 99 | |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 100 | #get the last word from the path to check whether it is an occ or |
| 101 | #something without a proper instance path. |
| 102 | #if instance path is sys0 then get the path value from the yaml |
| 103 | #if it is a occ path, get the path from yaml and add the occ instance |
| 104 | #number to it. |
| 105 | $obmcPath = Util::getObmcName(\@inventory,$path); |
| 106 | #if unable to get the obmc path then get from yaml |
| 107 | if (not defined $obmcPath) { |
Dhruvaraj Subhashchandran | 0c1aa51 | 2017-07-12 08:28:33 -0500 | [diff] [blame] | 108 | if ($path eq "/sys-0") { |
| 109 | $obmcPath = $sensorTypeConfig->{$sensorType}->{"path"}; |
| 110 | } |
| 111 | else { |
| 112 | my @pathelements =split(/\//,$path); |
| 113 | foreach my $elem (@pathelements) { |
| 114 | #split element-instance_number |
| 115 | my ($elemName,$elemNum) = split(/-([^-]+)$/,$elem); |
| 116 | if ((defined $elemName) and ($elemName eq "proc_socket")) { |
| 117 | $obmcPath = $sensorTypeConfig->{$sensorType}->{"path"}."occ".$elemNum; |
| 118 | last; |
| 119 | } |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 123 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 124 | if (not defined $obmcPath) { |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 125 | close $fh; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 126 | die("Unable to get the obmc path for path=$path"); |
| 127 | } |
| 128 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 129 | print $fh $sensorID.":\n"; |
| 130 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 131 | printDebug("$sensorID : $sensorType : $sensorReadingType :$obmcPath \n"); |
| 132 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 133 | writeToFile($sensorType,$sensorReadingType,$obmcPath,$sensorTypeConfig,$fh); |
| 134 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | } |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 138 | close $fh; |
| 139 | |
| 140 | |
| 141 | #Get the metadata for the incoming sensortype from the loaded config file. |
| 142 | #Write the sensor data into the output file |
| 143 | |
| 144 | sub writeToFile |
| 145 | { |
| 146 | my ($sensorType,$sensorReadingType,$path,$sensorTypeConfig,$fh) = @_; |
| 147 | print $fh " sensorType: ".$sensorType."\n"; |
| 148 | print $fh " path: ".$path."\n"; |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 149 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 150 | print $fh " sensorReadingType: ".$sensorReadingType."\n"; |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 151 | print $fh " updateInterface: ".$sensorTypeConfig->{$sensorType}->{"updateInterface"}."\n"; |
Dhruvaraj Subhashchandran | 0c1aa51 | 2017-07-12 08:28:33 -0500 | [diff] [blame] | 152 | if (defined($sensorTypeConfig->{$sensorType}->{"readingType"})) { |
| 153 | print $fh " readingType: ".$sensorTypeConfig->{$sensorType}->{"readingType"}."\n"; |
| 154 | } |
| 155 | if (defined($sensorTypeConfig->{$sensorType}->{"byteOffset"})) { |
| 156 | print $fh " byteOffset: ".$sensorTypeConfig->{$sensorType}->{"byteOffset"}."\n"; |
| 157 | } |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 158 | print $fh " interfaces:"."\n"; |
| 159 | |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 160 | my $interfaces = $sensorTypeConfig->{$sensorType}->{"interfaces"}; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 161 | #Walk over all the interfces as it needs to be written |
| 162 | while (my ($interface,$properties) = each %{$interfaces}) { |
| 163 | print $fh " ".$interface.":\n"; |
| 164 | #walk over all the properties as it needs to be written |
| 165 | while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) { |
| 166 | #will write property named "Property" first then |
| 167 | #other properties. |
| 168 | print $fh " ".$dbusProperty.":\n"; |
| 169 | while (my ( $offset,$values) = each %{$dbusPropertyValue}) { |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 170 | print $fh " $offset:\n"; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 171 | while (my ( $key,$value) = each %{$values}) { |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 172 | print $fh " $key: ". $value."\n"; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 178 | |
| 179 | # Usage |
| 180 | sub printUsage |
| 181 | { |
| 182 | print " |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 183 | $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS] |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 184 | Options: |
| 185 | -d = debug mode |
| 186 | \n"; |
| 187 | exit(1); |
| 188 | } |
| 189 | |
| 190 | # Helper function to put debug statements. |
| 191 | sub printDebug |
| 192 | { |
| 193 | my $str = shift; |
| 194 | print "DEBUG: ", $str, "\n" if $debug; |
| 195 | } |