blob: 766bab1c8f278c579bae72087712bd00083bdb19 [file] [log] [blame]
Ratan Guptac7366702017-02-22 18:05:44 +05301#! /usr/bin/perl
2use strict;
3use warnings;
4
5use mrw::Targets;
6use mrw::Inventory;
7use mrw::Util;
8use Getopt::Long; # For parsing command line arguments
9use YAML::Tiny qw(LoadFile);
10
11# Globals
12my $serverwizFile = "";
13my $debug = 0;
Ratan Gupta39747a02017-02-22 18:16:23 +053014my $outputFile = "";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050015my $metaDataFile = "";
Ratan Guptac7366702017-02-22 18:05:44 +053016
17# Command line argument parsing
18GetOptions(
19"i=s" => \$serverwizFile, # string
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050020"m=s" => \$metaDataFile, # string
Ratan Gupta39747a02017-02-22 18:16:23 +053021"o=s" => \$outputFile, # string
Ratan Guptac7366702017-02-22 18:05:44 +053022"d" => \$debug,
23)
24or printUsage();
25
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050026if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
Ratan Guptac7366702017-02-22 18:05:44 +053027{
28 printUsage();
29}
30
31my $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 Gupta39747a02017-02-22 18:16:23 +053036#Get the metadata for that sensor from the metadata file.
37#Merge the data into the outputfile
Ratan Guptac7366702017-02-22 18:05:44 +053038
Ratan Gupta39747a02017-02-22 18:16:23 +053039open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050040my $sensorTypeConfig = LoadFile($metaDataFile);
Ratan Guptac7366702017-02-22 18:05:44 +053041
42my @interestedTypes = keys %{$sensorTypeConfig};
43my %types;
44
45@types{@interestedTypes} = ();
46
47my @inventory = Inventory::getInventory($targetObj);
48#Process all the targets in the XML
49foreach my $target (sort keys %{$targetObj->getAllTargets()})
50{
51 my $sensorID = '';
52 my $sensorType = '';
53 my $sensorReadingType = '';
54 my $path = '';
55 my $obmcPath = '';
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050056 my $sensorName = '';
Ratan Guptac7366702017-02-22 18:05:44 +053057
58 if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
59
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050060 $sensorName = $targetObj->getInstanceName($target);
61 #not interested in this sensortype
62 next if (not exists $types{$sensorName});
63
Ratan Guptac7366702017-02-22 18:05:44 +053064 $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID");
65
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050066 $sensorType = hex($targetObj->getAttribute($target,
67 "IPMI_SENSOR_TYPE"));
Ratan Guptac7366702017-02-22 18:05:44 +053068
69 $sensorReadingType = $targetObj->getAttribute($target,
70 "IPMI_SENSOR_READING_TYPE");
71
72 $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
73
Ratan Guptac7366702017-02-22 18:05:44 +053074 #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 Gupta39747a02017-02-22 18:16:23 +053078 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +053079 die("sensor without info for target=$target");
80 }
81
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050082 if (exists $sensorTypeConfig->{$sensorName}{"path"}) {
83 $obmcPath = $sensorTypeConfig->{$sensorName}->{"path"};
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -050084 }
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050085 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 Guptac7366702017-02-22 18:05:44 +053093
Ratan Guptac7366702017-02-22 18:05:44 +053094 if (not defined $obmcPath) {
Ratan Gupta39747a02017-02-22 18:16:23 +053095 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +053096 die("Unable to get the obmc path for path=$path");
97 }
98
Ratan Gupta39747a02017-02-22 18:16:23 +053099 print $fh $sensorID.":\n";
100
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500101 my $serviceInterface =
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500102 $sensorTypeConfig->{$sensorName}->{"serviceInterface"};
103 my $readingType = $sensorTypeConfig->{$sensorName}->{"readingType"};
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500104
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500105 printDebug("$sensorID : $sensorName : $sensorType : $sensorReadingType :$obmcPath \n");
Ratan Guptac7366702017-02-22 18:05:44 +0530106
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500107 writeToFile($sensorName,$sensorType,$sensorReadingType,$obmcPath,$serviceInterface,
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500108 $readingType,$sensorTypeConfig,$fh);
Ratan Gupta39747a02017-02-22 18:16:23 +0530109
Ratan Guptac7366702017-02-22 18:05:44 +0530110 }
111
112}
Ratan Gupta39747a02017-02-22 18:16:23 +0530113close $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
119sub writeToFile
120{
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500121 my ($sensorName,$sensorType,$sensorReadingType,$path,$serviceInterface,
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500122 $readingType,$sensorTypeConfig,$fh) = @_;
123
Ratan Gupta39747a02017-02-22 18:16:23 +0530124 print $fh " sensorType: ".$sensorType."\n";
125 print $fh " path: ".$path."\n";
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500126
Ratan Gupta39747a02017-02-22 18:16:23 +0530127 print $fh " sensorReadingType: ".$sensorReadingType."\n";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500128 print $fh " serviceInterface: ".$serviceInterface."\n";
129 print $fh " readingType: ".$readingType."\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530130 print $fh " interfaces:"."\n";
131
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500132 my $interfaces = $sensorTypeConfig->{$sensorName}->{"interfaces"};
Ratan Gupta39747a02017-02-22 18:16:23 +0530133 #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";
Dhruvaraj Subhashchandrana93e2092017-09-18 05:50:59 -0500141 while (my ($offset,$values) = each %{$dbusPropertyValue}) {
142 if ($offset eq "prereq") {
143 print $fh " $offset:\n";
144 while (my ($preOffset,$preValues) = each %{$values}) {
145 print $fh " $preOffset:\n";
146 while (my ($key,$value) = each %{$preValues}) {
147 print $fh " $key: ". $value."\n";
148 }
149 }
150 } else {
151 print $fh " $offset:\n";
152 while (my ($key,$value) = each %{$values}) {
153 print $fh " $key: ". $value."\n";
154 }
Ratan Gupta39747a02017-02-22 18:16:23 +0530155 }
156 }
157 }
158 }
159}
Ratan Guptac7366702017-02-22 18:05:44 +0530160
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500161# Convert MRW OCC inventory path to application d-bus path
162sub checkOccPathFixup
163{
164 my ($path) = @_;
165 if ("/system/chassis/motherboard/cpu0/occ" eq $path) {
166 return "/org/open_power/control/occ0";
167 }
168 if ("/system/chassis/motherboard/cpu1/occ" eq $path) {
169 return "/org/open_power/control/occ1";
170 }
171 return $path;
172}
173
Ratan Guptac7366702017-02-22 18:05:44 +0530174# Usage
175sub printUsage
176{
177 print "
Ratan Gupta39747a02017-02-22 18:16:23 +0530178 $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptac7366702017-02-22 18:05:44 +0530179Options:
180 -d = debug mode
181 \n";
182 exit(1);
183}
184
185# Helper function to put debug statements.
186sub printDebug
187{
188 my $str = shift;
189 print "DEBUG: ", $str, "\n" if $debug;
190}