blob: 74c95da4ff807f65ad566676cb014cc6599a4f80 [file] [log] [blame]
Ratan Guptac7366702017-02-22 18:05:44 +05301#! /usr/bin/perl
2use strict;
3use warnings;
Ratan Guptac7366702017-02-22 18:05:44 +05304use mrw::Targets;
5use mrw::Inventory;
6use mrw::Util;
7use Getopt::Long; # For parsing command line arguments
8use YAML::Tiny qw(LoadFile);
9
10# Globals
11my $serverwizFile = "";
12my $debug = 0;
Ratan Gupta39747a02017-02-22 18:16:23 +053013my $outputFile = "";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050014my $metaDataFile = "";
Ratan Guptac7366702017-02-22 18:05:44 +053015
16# Command line argument parsing
17GetOptions(
18"i=s" => \$serverwizFile, # string
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050019"m=s" => \$metaDataFile, # string
Ratan Gupta39747a02017-02-22 18:16:23 +053020"o=s" => \$outputFile, # string
Ratan Guptac7366702017-02-22 18:05:44 +053021"d" => \$debug,
22)
23or printUsage();
24
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050025if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
Ratan Guptac7366702017-02-22 18:05:44 +053026{
27 printUsage();
28}
29
30my $targetObj = Targets->new;
31$targetObj->loadXML($serverwizFile);
32
33#open the mrw xml and the metaData file for the sensor.
34#Fetch the sensorid,sensortype,class,object path from the mrw.
Ratan Gupta39747a02017-02-22 18:16:23 +053035#Get the metadata for that sensor from the metadata file.
36#Merge the data into the outputfile
Ratan Guptac7366702017-02-22 18:05:44 +053037
Ratan Gupta39747a02017-02-22 18:16:23 +053038open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050039my $sensorTypeConfig = LoadFile($metaDataFile);
Ratan Guptac7366702017-02-22 18:05:44 +053040
41my @interestedTypes = keys %{$sensorTypeConfig};
42my %types;
Tom Joseph98be5ac2018-01-24 01:37:30 +053043my %entityDict;
Ratan Guptac7366702017-02-22 18:05:44 +053044
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 = '';
Tom Joseph98be5ac2018-01-24 01:37:30 +053057 my $entityID = '';
58 my $entityInstance = '';
Ratan Guptac7366702017-02-22 18:05:44 +053059
60 if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
61
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050062 $sensorName = $targetObj->getInstanceName($target);
63 #not interested in this sensortype
64 next if (not exists $types{$sensorName});
65
Tom Joseph98be5ac2018-01-24 01:37:30 +053066 $entityID = $targetObj->getAttribute($target, "IPMI_ENTITY_ID");
67 if (exists ($entityDict{$entityID}))
68 {
69 $entityDict{$entityID}++;
70 }
71 else
72 {
73 $entityDict{$entityID} = '1';
74 }
75 $entityInstance = $entityDict{$entityID};
76
Ratan Guptac7366702017-02-22 18:05:44 +053077 $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID");
78
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050079 $sensorType = hex($targetObj->getAttribute($target,
80 "IPMI_SENSOR_TYPE"));
Ratan Guptac7366702017-02-22 18:05:44 +053081
82 $sensorReadingType = $targetObj->getAttribute($target,
83 "IPMI_SENSOR_READING_TYPE");
84
85 $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
86
Ratan Guptac7366702017-02-22 18:05:44 +053087 #if there is ipmi sensor without sensorid or sensorReadingType or
88 #Instance path then die
89
90 if ($sensorID eq '' or $sensorReadingType eq '' or $path eq '') {
Ratan Gupta39747a02017-02-22 18:16:23 +053091 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +053092 die("sensor without info for target=$target");
93 }
94
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050095 if (exists $sensorTypeConfig->{$sensorName}{"path"}) {
96 $obmcPath = $sensorTypeConfig->{$sensorName}->{"path"};
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -050097 }
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050098 else {
99 #removing the string "instance:" from path
100 $path =~ s/^instance:/\//;
101 $obmcPath = Util::getObmcName(\@inventory,$path);
102 }
103
104 # TODO via openbmc/openbmc#2144 - this fixup shouldn't be needed.
105 $obmcPath = checkOccPathFixup($obmcPath);
Ratan Guptac7366702017-02-22 18:05:44 +0530106
Ratan Guptac7366702017-02-22 18:05:44 +0530107 if (not defined $obmcPath) {
Ratan Gupta39747a02017-02-22 18:16:23 +0530108 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +0530109 die("Unable to get the obmc path for path=$path");
110 }
111
Ratan Gupta39747a02017-02-22 18:16:23 +0530112 print $fh $sensorID.":\n";
113
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500114 my $serviceInterface =
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500115 $sensorTypeConfig->{$sensorName}->{"serviceInterface"};
116 my $readingType = $sensorTypeConfig->{$sensorName}->{"readingType"};
Tom Joseph98be5ac2018-01-24 01:37:30 +0530117 my $sensorNamePattern = $sensorTypeConfig->{$sensorName}->{"sensorNamePattern"};
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500118
Tom Joseph98be5ac2018-01-24 01:37:30 +0530119 my $debug = "$sensorID : $sensorName : $sensorType : ";
120 $debug .= "$sensorReadingType : $entityID : $entityInstance : ";
121 $debug .= "$obmcPath \n";
122 printDebug("$debug");
Ratan Guptac7366702017-02-22 18:05:44 +0530123
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500124 writeToFile($sensorName,$sensorType,$sensorReadingType,$obmcPath,$serviceInterface,
Tom Joseph98be5ac2018-01-24 01:37:30 +0530125 $readingType,$sensorTypeConfig,$entityID,$entityInstance,$sensorNamePattern,$fh);
Ratan Gupta39747a02017-02-22 18:16:23 +0530126
Ratan Guptac7366702017-02-22 18:05:44 +0530127 }
128
129}
Ratan Gupta39747a02017-02-22 18:16:23 +0530130close $fh;
131
Marri Devender Rao17f06792018-03-20 03:15:10 -0500132#Write the interfaces data into the output file
133sub writeInterfaces
134{
135 my ($interfaces, $fh) = @_;
136 print $fh " interfaces:"."\n";
137 #Walk over all the interfces as it needs to be written
138 while (my ($interface,$properties) = each %{$interfaces}) {
139 print $fh " ".$interface.":\n";
140 #walk over all the properties as it needs to be written
141 while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) {
142 #will write property named "Property" first then
143 #other properties.
144 print $fh " ".$dbusProperty.":\n";
145 while (my ($condition,$offsets) = each %{$dbusPropertyValue}) {
146 print $fh " $condition:\n";
147 while (my ($offset,$values) = each %{$offsets}) {
148 print $fh " $offset:\n";
149 while (my ($key,$value) = each %{$values}) {
150 print $fh " $key: ". $value."\n";
151 }
152 }
153 }
154 }
155 }
156}
Ratan Gupta39747a02017-02-22 18:16:23 +0530157
158#Get the metadata for the incoming sensortype from the loaded config file.
159#Write the sensor data into the output file
Ratan Gupta39747a02017-02-22 18:16:23 +0530160sub writeToFile
161{
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500162 my ($sensorName,$sensorType,$sensorReadingType,$path,$serviceInterface,
Tom Joseph98be5ac2018-01-24 01:37:30 +0530163 $readingType,$sensorTypeConfig,$entityID,$entityInstance,
164 $sensorNamePattern,$fh) = @_;
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500165
Tom Joseph98be5ac2018-01-24 01:37:30 +0530166 print $fh " entityID: ".$entityID."\n";
167 print $fh " entityInstance: ".$entityInstance."\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530168 print $fh " sensorType: ".$sensorType."\n";
169 print $fh " path: ".$path."\n";
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500170
Ratan Gupta39747a02017-02-22 18:16:23 +0530171 print $fh " sensorReadingType: ".$sensorReadingType."\n";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500172 print $fh " serviceInterface: ".$serviceInterface."\n";
173 print $fh " readingType: ".$readingType."\n";
Tom Joseph98be5ac2018-01-24 01:37:30 +0530174 print $fh " sensorNamePattern: ".$sensorNamePattern."\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530175
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500176 my $interfaces = $sensorTypeConfig->{$sensorName}->{"interfaces"};
Marri Devender Rao17f06792018-03-20 03:15:10 -0500177 writeInterfaces($interfaces, $fh);
Ratan Gupta39747a02017-02-22 18:16:23 +0530178}
Ratan Guptac7366702017-02-22 18:05:44 +0530179
Marri Devender Rao17f06792018-03-20 03:15:10 -0500180#Convert MRW OCC inventory path to application d-bus path
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500181sub checkOccPathFixup
182{
183 my ($path) = @_;
184 if ("/system/chassis/motherboard/cpu0/occ" eq $path) {
185 return "/org/open_power/control/occ0";
186 }
187 if ("/system/chassis/motherboard/cpu1/occ" eq $path) {
188 return "/org/open_power/control/occ1";
189 }
190 return $path;
191}
192
Ratan Guptac7366702017-02-22 18:05:44 +0530193# Usage
194sub printUsage
195{
196 print "
Ratan Gupta39747a02017-02-22 18:16:23 +0530197 $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptac7366702017-02-22 18:05:44 +0530198Options:
199 -d = debug mode
200 \n";
201 exit(1);
202}
203
204# Helper function to put debug statements.
205sub printDebug
206{
207 my $str = shift;
208 print "DEBUG: ", $str, "\n" if $debug;
209}