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