blob: 742da9bdddcbd85add1cb7f80c2db4a988cac03f [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 = "";
Ratan Guptac7366702017-02-22 18:05:44 +053015my $metaDataFile = "";
16
17# Command line argument parsing
18GetOptions(
19"i=s" => \$serverwizFile, # string
20"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
Ratan Gupta39747a02017-02-22 18:16:23 +053026if (($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' $!";
Ratan Guptac7366702017-02-22 18:05:44 +053040my $sensorTypeConfig = LoadFile($metaDataFile);
41
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 = '';
56
57 if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
58
59 $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID");
60
61 $sensorType = $targetObj->getAttribute($target, "IPMI_SENSOR_TYPE");
62
63 $sensorReadingType = $targetObj->getAttribute($target,
64 "IPMI_SENSOR_READING_TYPE");
65
66 $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
67
68 #not interested in this sensortype
Ratan Gupta39747a02017-02-22 18:16:23 +053069 next if (not exists $types{$sensorType});
Ratan Guptac7366702017-02-22 18:05:44 +053070
71 #if there is ipmi sensor without sensorid or sensorReadingType or
72 #Instance path then die
73
74 if ($sensorID eq '' or $sensorReadingType eq '' or $path eq '') {
Ratan Gupta39747a02017-02-22 18:16:23 +053075 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +053076 die("sensor without info for target=$target");
77 }
78
79 #removing the string "instance:" from path
80 $path =~ s/^instance:/\//;
81
82 $obmcPath = Util::getObmcName(\@inventory, $path);
83
84 #if unable to get the obmc path then die
85 if (not defined $obmcPath) {
Ratan Gupta39747a02017-02-22 18:16:23 +053086 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +053087 die("Unable to get the obmc path for path=$path");
88 }
89
Ratan Gupta39747a02017-02-22 18:16:23 +053090 print $fh $sensorID.":\n";
91
Ratan Guptac7366702017-02-22 18:05:44 +053092 printDebug("$sensorID : $sensorType : $sensorReadingType :$obmcPath \n");
93
Ratan Gupta39747a02017-02-22 18:16:23 +053094 writeToFile($sensorType,$sensorReadingType,$obmcPath,$sensorTypeConfig,$fh);
95
Ratan Guptac7366702017-02-22 18:05:44 +053096 }
97
98}
Ratan Gupta39747a02017-02-22 18:16:23 +053099close $fh;
100
101
102#Get the metadata for the incoming sensortype from the loaded config file.
103#Write the sensor data into the output file
104
105sub writeToFile
106{
107 my ($sensorType,$sensorReadingType,$path,$sensorTypeConfig,$fh) = @_;
108 print $fh " sensorType: ".$sensorType."\n";
109 print $fh " path: ".$path."\n";
110 print $fh " sensorReadingType: ".$sensorReadingType."\n";
111 print $fh " interfaces:"."\n";
112
113 my $interfaces = $sensorTypeConfig->{$sensorType};
114 #Walk over all the interfces as it needs to be written
115 while (my ($interface,$properties) = each %{$interfaces}) {
116 print $fh " ".$interface.":\n";
117 #walk over all the properties as it needs to be written
118 while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) {
119 #will write property named "Property" first then
120 #other properties.
121 print $fh " ".$dbusProperty.":\n";
122 while (my ( $offset,$values) = each %{$dbusPropertyValue}) {
123 print $fh " $offset:\n";
124 while (my ( $key,$value) = each %{$values}) {
125 print $fh " $key: ". $value."\n";
126 }
127 }
128 }
129 }
130}
Ratan Guptac7366702017-02-22 18:05:44 +0530131
132# Usage
133sub printUsage
134{
135 print "
Ratan Gupta39747a02017-02-22 18:16:23 +0530136 $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptac7366702017-02-22 18:05:44 +0530137Options:
138 -d = debug mode
139 \n";
140 exit(1);
141}
142
143# Helper function to put debug statements.
144sub printDebug
145{
146 my $str = shift;
147 print "DEBUG: ", $str, "\n" if $debug;
148}