blob: 84b00c89076cc19809142cc00850a91088181da1 [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;
Tom Joseph98be5ac2018-01-24 01:37:30 +053044my %entityDict;
Ratan Guptac7366702017-02-22 18:05:44 +053045
46@types{@interestedTypes} = ();
47
48my @inventory = Inventory::getInventory($targetObj);
49#Process all the targets in the XML
50foreach my $target (sort keys %{$targetObj->getAllTargets()})
51{
52 my $sensorID = '';
53 my $sensorType = '';
54 my $sensorReadingType = '';
55 my $path = '';
56 my $obmcPath = '';
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050057 my $sensorName = '';
Tom Joseph98be5ac2018-01-24 01:37:30 +053058 my $entityID = '';
59 my $entityInstance = '';
Ratan Guptac7366702017-02-22 18:05:44 +053060
61 if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
62
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050063 $sensorName = $targetObj->getInstanceName($target);
64 #not interested in this sensortype
65 next if (not exists $types{$sensorName});
66
Tom Joseph98be5ac2018-01-24 01:37:30 +053067 $entityID = $targetObj->getAttribute($target, "IPMI_ENTITY_ID");
68 if (exists ($entityDict{$entityID}))
69 {
70 $entityDict{$entityID}++;
71 }
72 else
73 {
74 $entityDict{$entityID} = '1';
75 }
76 $entityInstance = $entityDict{$entityID};
77
Ratan Guptac7366702017-02-22 18:05:44 +053078 $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID");
79
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050080 $sensorType = hex($targetObj->getAttribute($target,
81 "IPMI_SENSOR_TYPE"));
Ratan Guptac7366702017-02-22 18:05:44 +053082
83 $sensorReadingType = $targetObj->getAttribute($target,
84 "IPMI_SENSOR_READING_TYPE");
85
86 $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
87
Ratan Guptac7366702017-02-22 18:05:44 +053088 #if there is ipmi sensor without sensorid or sensorReadingType or
89 #Instance path then die
90
91 if ($sensorID eq '' or $sensorReadingType eq '' or $path eq '') {
Ratan Gupta39747a02017-02-22 18:16:23 +053092 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +053093 die("sensor without info for target=$target");
94 }
95
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050096 if (exists $sensorTypeConfig->{$sensorName}{"path"}) {
97 $obmcPath = $sensorTypeConfig->{$sensorName}->{"path"};
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -050098 }
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -050099 else {
100 #removing the string "instance:" from path
101 $path =~ s/^instance:/\//;
102 $obmcPath = Util::getObmcName(\@inventory,$path);
103 }
104
105 # TODO via openbmc/openbmc#2144 - this fixup shouldn't be needed.
106 $obmcPath = checkOccPathFixup($obmcPath);
Ratan Guptac7366702017-02-22 18:05:44 +0530107
Ratan Guptac7366702017-02-22 18:05:44 +0530108 if (not defined $obmcPath) {
Ratan Gupta39747a02017-02-22 18:16:23 +0530109 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +0530110 die("Unable to get the obmc path for path=$path");
111 }
112
Ratan Gupta39747a02017-02-22 18:16:23 +0530113 print $fh $sensorID.":\n";
114
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500115 my $serviceInterface =
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500116 $sensorTypeConfig->{$sensorName}->{"serviceInterface"};
117 my $readingType = $sensorTypeConfig->{$sensorName}->{"readingType"};
Tom Joseph98be5ac2018-01-24 01:37:30 +0530118 my $sensorNamePattern = $sensorTypeConfig->{$sensorName}->{"sensorNamePattern"};
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500119
Tom Joseph98be5ac2018-01-24 01:37:30 +0530120 my $debug = "$sensorID : $sensorName : $sensorType : ";
121 $debug .= "$sensorReadingType : $entityID : $entityInstance : ";
122 $debug .= "$obmcPath \n";
123 printDebug("$debug");
Ratan Guptac7366702017-02-22 18:05:44 +0530124
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500125 writeToFile($sensorName,$sensorType,$sensorReadingType,$obmcPath,$serviceInterface,
Tom Joseph98be5ac2018-01-24 01:37:30 +0530126 $readingType,$sensorTypeConfig,$entityID,$entityInstance,$sensorNamePattern,$fh);
Ratan Gupta39747a02017-02-22 18:16:23 +0530127
Ratan Guptac7366702017-02-22 18:05:44 +0530128 }
129
130}
Ratan Gupta39747a02017-02-22 18:16:23 +0530131close $fh;
132
133
134#Get the metadata for the incoming sensortype from the loaded config file.
135#Write the sensor data into the output file
136
137sub writeToFile
138{
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500139 my ($sensorName,$sensorType,$sensorReadingType,$path,$serviceInterface,
Tom Joseph98be5ac2018-01-24 01:37:30 +0530140 $readingType,$sensorTypeConfig,$entityID,$entityInstance,
141 $sensorNamePattern,$fh) = @_;
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500142
Tom Joseph98be5ac2018-01-24 01:37:30 +0530143 print $fh " entityID: ".$entityID."\n";
144 print $fh " entityInstance: ".$entityInstance."\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530145 print $fh " sensorType: ".$sensorType."\n";
146 print $fh " path: ".$path."\n";
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500147
Ratan Gupta39747a02017-02-22 18:16:23 +0530148 print $fh " sensorReadingType: ".$sensorReadingType."\n";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500149 print $fh " serviceInterface: ".$serviceInterface."\n";
150 print $fh " readingType: ".$readingType."\n";
Tom Joseph98be5ac2018-01-24 01:37:30 +0530151 print $fh " sensorNamePattern: ".$sensorNamePattern."\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530152 print $fh " interfaces:"."\n";
153
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500154 my $interfaces = $sensorTypeConfig->{$sensorName}->{"interfaces"};
Ratan Gupta39747a02017-02-22 18:16:23 +0530155 #Walk over all the interfces as it needs to be written
156 while (my ($interface,$properties) = each %{$interfaces}) {
157 print $fh " ".$interface.":\n";
158 #walk over all the properties as it needs to be written
159 while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) {
160 #will write property named "Property" first then
161 #other properties.
162 print $fh " ".$dbusProperty.":\n";
Dhruvaraj Subhashchandranbb3c54a2017-10-02 04:51:20 -0500163 while (my ($condition,$offsets) = each %{$dbusPropertyValue}) {
164 print $fh " $condition:\n";
165 while (my ($offset,$values) = each %{$offsets}) {
166 print $fh " $offset:\n";
Dhruvaraj Subhashchandrana93e2092017-09-18 05:50:59 -0500167 while (my ($key,$value) = each %{$values}) {
Dhruvaraj Subhashchandranbb3c54a2017-10-02 04:51:20 -0500168 print $fh " $key: ". $value."\n";
Dhruvaraj Subhashchandrana93e2092017-09-18 05:50:59 -0500169 }
Ratan Gupta39747a02017-02-22 18:16:23 +0530170 }
171 }
172 }
173 }
174}
Ratan Guptac7366702017-02-22 18:05:44 +0530175
Deepak Kodihalli00cef2c2017-08-12 11:34:37 -0500176# Convert MRW OCC inventory path to application d-bus path
177sub checkOccPathFixup
178{
179 my ($path) = @_;
180 if ("/system/chassis/motherboard/cpu0/occ" eq $path) {
181 return "/org/open_power/control/occ0";
182 }
183 if ("/system/chassis/motherboard/cpu1/occ" eq $path) {
184 return "/org/open_power/control/occ1";
185 }
186 return $path;
187}
188
Ratan Guptac7366702017-02-22 18:05:44 +0530189# Usage
190sub printUsage
191{
192 print "
Ratan Gupta39747a02017-02-22 18:16:23 +0530193 $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptac7366702017-02-22 18:05:44 +0530194Options:
195 -d = debug mode
196 \n";
197 exit(1);
198}
199
200# Helper function to put debug statements.
201sub printDebug
202{
203 my $str = shift;
204 print "DEBUG: ", $str, "\n" if $debug;
205}