Tom Joseph | 5882591 | 2017-05-05 11:53:54 +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; |
| 14 | my $outputFile = ""; |
| 15 | my $metaDataFile = ""; |
Santosh Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame^] | 16 | my $skipBrokenMrw = 0; |
Tom Joseph | 5882591 | 2017-05-05 11:53:54 +0530 | [diff] [blame] | 17 | |
| 18 | # Command line argument parsing |
| 19 | GetOptions( |
| 20 | "i=s" => \$serverwizFile, # string |
| 21 | "m=s" => \$metaDataFile, # string |
| 22 | "o=s" => \$outputFile, # string |
Santosh Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame^] | 23 | "skip-broken-mrw" => \$skipBrokenMrw, |
Tom Joseph | 5882591 | 2017-05-05 11:53:54 +0530 | [diff] [blame] | 24 | "d" => \$debug, |
| 25 | ) |
| 26 | or printUsage(); |
| 27 | |
| 28 | if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq "")) |
| 29 | { |
| 30 | printUsage(); |
| 31 | } |
| 32 | |
| 33 | my $targetObj = Targets->new; |
| 34 | $targetObj->loadXML($serverwizFile); |
| 35 | |
| 36 | # Open the MRW xml and the Metadata file for the sensor. |
| 37 | # Get the IPMI sensor information based on the Entity ID and Sensor Type. |
| 38 | # Fetch the Sensor ID, Event/Reading Type and Object Path from MRW. |
| 39 | # Get the Sensor Type and Offset from the metadata file. |
| 40 | # Merge and generate an output YAML with inventory object path as the key. |
| 41 | |
| 42 | open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!"; |
| 43 | my $metaDataConfig = LoadFile($metaDataFile); |
| 44 | |
| 45 | my @interestedTypes = keys %{$metaDataConfig}; |
| 46 | my %types; |
| 47 | |
| 48 | @types{@interestedTypes} = (); |
| 49 | |
| 50 | my @inventory = Inventory::getInventory($targetObj); |
| 51 | #Process all the targets in the XML |
| 52 | foreach my $target (sort keys %{$targetObj->getAllTargets()}) |
| 53 | { |
| 54 | my $sensorID = ''; |
| 55 | my $sensorType = ''; |
| 56 | my $eventReadingType = ''; |
| 57 | my $path = ''; |
| 58 | my $obmcPath = ''; |
| 59 | my $entityID = ''; |
| 60 | my $base = "/xyz/openbmc_project/inventory"; |
| 61 | |
| 62 | if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") { |
| 63 | |
Brad Bishop | 5102e57 | 2019-06-28 10:45:23 -0400 | [diff] [blame] | 64 | $sensorID = getNumeric($targetObj, $target, "IPMI_SENSOR_ID"); |
| 65 | $sensorType = getNumeric($targetObj, $target, "IPMI_SENSOR_TYPE"); |
| 66 | $eventReadingType = getNumeric( |
| 67 | $targetObj, $target, "IPMI_SENSOR_READING_TYPE"); |
Tom Joseph | 5882591 | 2017-05-05 11:53:54 +0530 | [diff] [blame] | 68 | $path = $targetObj->getAttribute($target, "INSTANCE_PATH"); |
Brad Bishop | 5102e57 | 2019-06-28 10:45:23 -0400 | [diff] [blame] | 69 | $entityID = getNumeric($targetObj, $target, "IPMI_ENTITY_ID"); |
Tom Joseph | 5882591 | 2017-05-05 11:53:54 +0530 | [diff] [blame] | 70 | |
| 71 | # Look only for the interested Entity ID & Sensor Type |
| 72 | next if (not exists $types{$entityID}); |
| 73 | next if ($sensorType ne $metaDataConfig->{$entityID}->{SensorType}); |
| 74 | |
| 75 | #if there is ipmi sensor without sensorid or sensorReadingType or |
| 76 | #Instance path then die |
| 77 | |
| 78 | if ($sensorID eq '' or $eventReadingType eq '' or $path eq '') { |
Santosh Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame^] | 79 | next if $skipBrokenMrw; |
Tom Joseph | 5882591 | 2017-05-05 11:53:54 +0530 | [diff] [blame] | 80 | close $fh; |
| 81 | die("sensor without info for target=$target"); |
| 82 | } |
| 83 | |
| 84 | # Removing the string "instance:" from path |
| 85 | $path =~ s/^instance:/\//; |
| 86 | $obmcPath = Util::getObmcName(\@inventory, $path); |
| 87 | |
| 88 | # If unable to get the obmc path then die |
| 89 | if (not defined $obmcPath) { |
| 90 | close $fh; |
| 91 | die("Unable to get the obmc path for path=$path"); |
| 92 | } |
| 93 | |
| 94 | $base .= $obmcPath; |
| 95 | |
| 96 | print $fh $base.":"."\n"; |
| 97 | print $fh " sensorID: ".$sensorID."\n"; |
| 98 | print $fh " sensorType: ".$sensorType."\n"; |
| 99 | print $fh " eventReadingType: ".$eventReadingType."\n"; |
| 100 | print $fh " offset: ".$metaDataConfig->{$entityID}->{Offset}."\n"; |
| 101 | |
| 102 | printDebug("$sensorID : $sensorType : $eventReadingType : $entityID : $metaDataConfig->{$entityID}->{Offset}") |
| 103 | } |
| 104 | } |
| 105 | close $fh; |
| 106 | |
Brad Bishop | 5102e57 | 2019-06-28 10:45:23 -0400 | [diff] [blame] | 107 | sub getNumeric |
| 108 | { |
| 109 | my $obj = shift; |
| 110 | my $target = shift; |
| 111 | my $attr = shift; |
| 112 | my $val = $obj->getAttribute($target, $attr); |
| 113 | $val = oct($val) if $val =~ /^0/; |
| 114 | return $val; |
| 115 | } |
| 116 | |
Tom Joseph | 5882591 | 2017-05-05 11:53:54 +0530 | [diff] [blame] | 117 | # Usage |
| 118 | sub printUsage |
| 119 | { |
| 120 | print " |
| 121 | $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS] |
| 122 | Options: |
| 123 | -d = debug mode |
Santosh Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame^] | 124 | --skip-broken-mrw = Skip broken MRW targets |
Tom Joseph | 5882591 | 2017-05-05 11:53:54 +0530 | [diff] [blame] | 125 | \n"; |
| 126 | exit(1); |
| 127 | } |
| 128 | |
| 129 | # Helper function to put debug statements. |
| 130 | sub printDebug |
| 131 | { |
| 132 | my $str = shift; |
| 133 | print "DEBUG: ", $str, "\n" if $debug; |
| 134 | } |