blob: 0da8be8e8e7d896932aae1da2459b495721d8904 [file] [log] [blame]
Andrew Geisslerb4edc272022-07-19 11:24:45 -04001#!/usr/bin/env perl
Andrew Geissler65ca8e12022-07-16 11:36:14 -04002
Tom Joseph58825912017-05-05 11:53:54 +05303use strict;
4use warnings;
5
6use mrw::Targets;
7use mrw::Inventory;
8use mrw::Util;
9use Getopt::Long; # For parsing command line arguments
10use YAML::Tiny qw(LoadFile);
11
12# Globals
13my $serverwizFile = "";
14my $debug = 0;
15my $outputFile = "";
16my $metaDataFile = "";
Santosh Puranik51041fc2019-10-08 09:45:15 -050017my $skipBrokenMrw = 0;
Tom Joseph58825912017-05-05 11:53:54 +053018
19# Command line argument parsing
20GetOptions(
21"i=s" => \$serverwizFile, # string
22"m=s" => \$metaDataFile, # string
23"o=s" => \$outputFile, # string
Santosh Puranik51041fc2019-10-08 09:45:15 -050024"skip-broken-mrw" => \$skipBrokenMrw,
Tom Joseph58825912017-05-05 11:53:54 +053025"d" => \$debug,
26)
27or printUsage();
28
29if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
30{
31 printUsage();
32}
33
34my $targetObj = Targets->new;
35$targetObj->loadXML($serverwizFile);
36
37# Open the MRW xml and the Metadata file for the sensor.
38# Get the IPMI sensor information based on the Entity ID and Sensor Type.
39# Fetch the Sensor ID, Event/Reading Type and Object Path from MRW.
40# Get the Sensor Type and Offset from the metadata file.
41# Merge and generate an output YAML with inventory object path as the key.
42
43open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
44my $metaDataConfig = LoadFile($metaDataFile);
45
46my @interestedTypes = keys %{$metaDataConfig};
47my %types;
48
49@types{@interestedTypes} = ();
50
51my @inventory = Inventory::getInventory($targetObj);
52#Process all the targets in the XML
53foreach my $target (sort keys %{$targetObj->getAllTargets()})
54{
55 my $sensorID = '';
56 my $sensorType = '';
57 my $eventReadingType = '';
58 my $path = '';
59 my $obmcPath = '';
60 my $entityID = '';
61 my $base = "/xyz/openbmc_project/inventory";
62
63 if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
64
Brad Bishop5102e572019-06-28 10:45:23 -040065 $sensorID = getNumeric($targetObj, $target, "IPMI_SENSOR_ID");
66 $sensorType = getNumeric($targetObj, $target, "IPMI_SENSOR_TYPE");
67 $eventReadingType = getNumeric(
68 $targetObj, $target, "IPMI_SENSOR_READING_TYPE");
Tom Joseph58825912017-05-05 11:53:54 +053069 $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
Brad Bishop5102e572019-06-28 10:45:23 -040070 $entityID = getNumeric($targetObj, $target, "IPMI_ENTITY_ID");
Tom Joseph58825912017-05-05 11:53:54 +053071
72 # Look only for the interested Entity ID & Sensor Type
73 next if (not exists $types{$entityID});
74 next if ($sensorType ne $metaDataConfig->{$entityID}->{SensorType});
75
76 #if there is ipmi sensor without sensorid or sensorReadingType or
77 #Instance path then die
78
79 if ($sensorID eq '' or $eventReadingType eq '' or $path eq '') {
Santosh Puranik51041fc2019-10-08 09:45:15 -050080 next if $skipBrokenMrw;
Tom Joseph58825912017-05-05 11:53:54 +053081 close $fh;
82 die("sensor without info for target=$target");
83 }
84
85 # Removing the string "instance:" from path
86 $path =~ s/^instance:/\//;
87 $obmcPath = Util::getObmcName(\@inventory, $path);
88
89 # If unable to get the obmc path then die
90 if (not defined $obmcPath) {
91 close $fh;
92 die("Unable to get the obmc path for path=$path");
93 }
94
95 $base .= $obmcPath;
96
97 print $fh $base.":"."\n";
98 print $fh " sensorID: ".$sensorID."\n";
99 print $fh " sensorType: ".$sensorType."\n";
100 print $fh " eventReadingType: ".$eventReadingType."\n";
101 print $fh " offset: ".$metaDataConfig->{$entityID}->{Offset}."\n";
102
103 printDebug("$sensorID : $sensorType : $eventReadingType : $entityID : $metaDataConfig->{$entityID}->{Offset}")
104 }
105}
106close $fh;
107
Brad Bishop5102e572019-06-28 10:45:23 -0400108sub getNumeric
109{
110 my $obj = shift;
111 my $target = shift;
112 my $attr = shift;
113 my $val = $obj->getAttribute($target, $attr);
114 $val = oct($val) if $val =~ /^0/;
115 return $val;
116}
117
Tom Joseph58825912017-05-05 11:53:54 +0530118# Usage
119sub printUsage
120{
121 print "
122 $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
123Options:
124 -d = debug mode
Santosh Puranik51041fc2019-10-08 09:45:15 -0500125 --skip-broken-mrw = Skip broken MRW targets
Tom Joseph58825912017-05-05 11:53:54 +0530126 \n";
127 exit(1);
128}
129
130# Helper function to put debug statements.
131sub printDebug
132{
133 my $str = shift;
134 print "DEBUG: ", $str, "\n" if $debug;
135}