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 | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 15 | my $metaDataFile = ""; |
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 | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 20 | "m=s" => \$metaDataFile, # 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 | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 26 | if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile 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 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 39 | open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!"; |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 40 | my $sensorTypeConfig = LoadFile($metaDataFile); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 41 | |
| 42 | my @interestedTypes = keys %{$sensorTypeConfig}; |
| 43 | my %types; |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 44 | my %entityDict; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 45 | |
| 46 | @types{@interestedTypes} = (); |
| 47 | |
| 48 | my @inventory = Inventory::getInventory($targetObj); |
| 49 | #Process all the targets in the XML |
| 50 | foreach my $target (sort keys %{$targetObj->getAllTargets()}) |
| 51 | { |
| 52 | my $sensorID = ''; |
| 53 | my $sensorType = ''; |
| 54 | my $sensorReadingType = ''; |
| 55 | my $path = ''; |
| 56 | my $obmcPath = ''; |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 57 | my $sensorName = ''; |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 58 | my $entityID = ''; |
| 59 | my $entityInstance = ''; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 60 | |
| 61 | if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") { |
| 62 | |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 63 | $sensorName = $targetObj->getInstanceName($target); |
| 64 | #not interested in this sensortype |
| 65 | next if (not exists $types{$sensorName}); |
| 66 | |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 67 | $entityID = $targetObj->getAttribute($target, "IPMI_ENTITY_ID"); |
| 68 | if (exists ($entityDict{$entityID})) |
| 69 | { |
| 70 | $entityDict{$entityID}++; |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | $entityDict{$entityID} = '1'; |
| 75 | } |
| 76 | $entityInstance = $entityDict{$entityID}; |
| 77 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 78 | $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID"); |
| 79 | |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 80 | $sensorType = hex($targetObj->getAttribute($target, |
| 81 | "IPMI_SENSOR_TYPE")); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 82 | |
| 83 | $sensorReadingType = $targetObj->getAttribute($target, |
| 84 | "IPMI_SENSOR_READING_TYPE"); |
| 85 | |
| 86 | $path = $targetObj->getAttribute($target, "INSTANCE_PATH"); |
| 87 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 88 | #if there is ipmi sensor without sensorid or sensorReadingType or |
| 89 | #Instance path then die |
| 90 | |
| 91 | if ($sensorID eq '' or $sensorReadingType eq '' or $path eq '') { |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 92 | close $fh; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 93 | die("sensor without info for target=$target"); |
| 94 | } |
| 95 | |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 96 | if (exists $sensorTypeConfig->{$sensorName}{"path"}) { |
| 97 | $obmcPath = $sensorTypeConfig->{$sensorName}->{"path"}; |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 98 | } |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 99 | else { |
| 100 | #removing the string "instance:" from path |
| 101 | $path =~ s/^instance:/\//; |
| 102 | $obmcPath = Util::getObmcName(\@inventory,$path); |
| 103 | } |
| 104 | |
| 105 | # TODO via openbmc/openbmc#2144 - this fixup shouldn't be needed. |
| 106 | $obmcPath = checkOccPathFixup($obmcPath); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 107 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 108 | if (not defined $obmcPath) { |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 109 | close $fh; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 110 | die("Unable to get the obmc path for path=$path"); |
| 111 | } |
| 112 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 113 | print $fh $sensorID.":\n"; |
| 114 | |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 115 | my $serviceInterface = |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 116 | $sensorTypeConfig->{$sensorName}->{"serviceInterface"}; |
| 117 | my $readingType = $sensorTypeConfig->{$sensorName}->{"readingType"}; |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 118 | my $sensorNamePattern = $sensorTypeConfig->{$sensorName}->{"sensorNamePattern"}; |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 119 | |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 120 | my $debug = "$sensorID : $sensorName : $sensorType : "; |
| 121 | $debug .= "$sensorReadingType : $entityID : $entityInstance : "; |
| 122 | $debug .= "$obmcPath \n"; |
| 123 | printDebug("$debug"); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 124 | |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 125 | writeToFile($sensorName,$sensorType,$sensorReadingType,$obmcPath,$serviceInterface, |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 126 | $readingType,$sensorTypeConfig,$entityID,$entityInstance,$sensorNamePattern,$fh); |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 127 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | } |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 131 | close $fh; |
| 132 | |
| 133 | |
| 134 | #Get the metadata for the incoming sensortype from the loaded config file. |
| 135 | #Write the sensor data into the output file |
| 136 | |
| 137 | sub writeToFile |
| 138 | { |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 139 | my ($sensorName,$sensorType,$sensorReadingType,$path,$serviceInterface, |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 140 | $readingType,$sensorTypeConfig,$entityID,$entityInstance, |
| 141 | $sensorNamePattern,$fh) = @_; |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 142 | |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 143 | print $fh " entityID: ".$entityID."\n"; |
| 144 | print $fh " entityInstance: ".$entityInstance."\n"; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 145 | print $fh " sensorType: ".$sensorType."\n"; |
| 146 | print $fh " path: ".$path."\n"; |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 147 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 148 | print $fh " sensorReadingType: ".$sensorReadingType."\n"; |
Dhruvaraj Subhashchandran | 611b90f | 2017-07-27 08:05:42 -0500 | [diff] [blame] | 149 | print $fh " serviceInterface: ".$serviceInterface."\n"; |
| 150 | print $fh " readingType: ".$readingType."\n"; |
Tom Joseph | 98be5ac | 2018-01-24 01:37:30 +0530 | [diff] [blame] | 151 | print $fh " sensorNamePattern: ".$sensorNamePattern."\n"; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 152 | print $fh " interfaces:"."\n"; |
| 153 | |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 154 | my $interfaces = $sensorTypeConfig->{$sensorName}->{"interfaces"}; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 155 | #Walk over all the interfces as it needs to be written |
| 156 | while (my ($interface,$properties) = each %{$interfaces}) { |
| 157 | print $fh " ".$interface.":\n"; |
| 158 | #walk over all the properties as it needs to be written |
| 159 | while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) { |
| 160 | #will write property named "Property" first then |
| 161 | #other properties. |
| 162 | print $fh " ".$dbusProperty.":\n"; |
Dhruvaraj Subhashchandran | bb3c54a | 2017-10-02 04:51:20 -0500 | [diff] [blame] | 163 | while (my ($condition,$offsets) = each %{$dbusPropertyValue}) { |
| 164 | print $fh " $condition:\n"; |
| 165 | while (my ($offset,$values) = each %{$offsets}) { |
| 166 | print $fh " $offset:\n"; |
Dhruvaraj Subhashchandran | a93e209 | 2017-09-18 05:50:59 -0500 | [diff] [blame] | 167 | while (my ($key,$value) = each %{$values}) { |
Dhruvaraj Subhashchandran | bb3c54a | 2017-10-02 04:51:20 -0500 | [diff] [blame] | 168 | print $fh " $key: ". $value."\n"; |
Dhruvaraj Subhashchandran | a93e209 | 2017-09-18 05:50:59 -0500 | [diff] [blame] | 169 | } |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 175 | |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame] | 176 | # Convert MRW OCC inventory path to application d-bus path |
| 177 | sub checkOccPathFixup |
| 178 | { |
| 179 | my ($path) = @_; |
| 180 | if ("/system/chassis/motherboard/cpu0/occ" eq $path) { |
| 181 | return "/org/open_power/control/occ0"; |
| 182 | } |
| 183 | if ("/system/chassis/motherboard/cpu1/occ" eq $path) { |
| 184 | return "/org/open_power/control/occ1"; |
| 185 | } |
| 186 | return $path; |
| 187 | } |
| 188 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 189 | # Usage |
| 190 | sub printUsage |
| 191 | { |
| 192 | print " |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 193 | $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS] |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 194 | Options: |
| 195 | -d = debug mode |
| 196 | \n"; |
| 197 | exit(1); |
| 198 | } |
| 199 | |
| 200 | # Helper function to put debug statements. |
| 201 | sub printDebug |
| 202 | { |
| 203 | my $str = shift; |
| 204 | print "DEBUG: ", $str, "\n" if $debug; |
| 205 | } |