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