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"; |
| 141 | while (my ( $offset,$values) = each %{$dbusPropertyValue}) { |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 142 | print $fh " $offset:\n"; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 143 | while (my ( $key,$value) = each %{$values}) { |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame] | 144 | print $fh " $key: ". $value."\n"; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 150 | |
Deepak Kodihalli | 00cef2c | 2017-08-12 11:34:37 -0500 | [diff] [blame^] | 151 | # Convert MRW OCC inventory path to application d-bus path |
| 152 | sub checkOccPathFixup |
| 153 | { |
| 154 | my ($path) = @_; |
| 155 | if ("/system/chassis/motherboard/cpu0/occ" eq $path) { |
| 156 | return "/org/open_power/control/occ0"; |
| 157 | } |
| 158 | if ("/system/chassis/motherboard/cpu1/occ" eq $path) { |
| 159 | return "/org/open_power/control/occ1"; |
| 160 | } |
| 161 | return $path; |
| 162 | } |
| 163 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 164 | # Usage |
| 165 | sub printUsage |
| 166 | { |
| 167 | print " |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 168 | $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS] |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 169 | Options: |
| 170 | -d = debug mode |
| 171 | \n"; |
| 172 | exit(1); |
| 173 | } |
| 174 | |
| 175 | # Helper function to put debug statements. |
| 176 | sub printDebug |
| 177 | { |
| 178 | my $str = shift; |
| 179 | print "DEBUG: ", $str, "\n" if $debug; |
| 180 | } |