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 | |
| 62 | $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID"); |
| 63 | $sensorType = $targetObj->getAttribute($target, "IPMI_SENSOR_TYPE"); |
| 64 | $eventReadingType = $targetObj->getAttribute($target, |
| 65 | "IPMI_SENSOR_READING_TYPE"); |
| 66 | $path = $targetObj->getAttribute($target, "INSTANCE_PATH"); |
| 67 | $entityID = $targetObj->getAttribute($target, "IPMI_ENTITY_ID"); |
| 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 | |
| 104 | # Usage |
| 105 | sub printUsage |
| 106 | { |
| 107 | print " |
| 108 | $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS] |
| 109 | Options: |
| 110 | -d = debug mode |
| 111 | \n"; |
| 112 | exit(1); |
| 113 | } |
| 114 | |
| 115 | # Helper function to put debug statements. |
| 116 | sub printDebug |
| 117 | { |
| 118 | my $str = shift; |
| 119 | print "DEBUG: ", $str, "\n" if $debug; |
| 120 | } |