blob: 1d3a1dd0cbc07ab2f75d94394750b3a78bfe6108 [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 = '';
56
57 if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
58
59 $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID");
60
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050061 $sensorType = hex($targetObj->getAttribute($target,
62 "IPMI_SENSOR_TYPE"));
Ratan Guptac7366702017-02-22 18:05:44 +053063
64 $sensorReadingType = $targetObj->getAttribute($target,
65 "IPMI_SENSOR_READING_TYPE");
66
67 $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
68
69 #not interested in this sensortype
Ratan Gupta39747a02017-02-22 18:16:23 +053070 next if (not exists $types{$sensorType});
Ratan Guptac7366702017-02-22 18:05:44 +053071
72 #if there is ipmi sensor without sensorid or sensorReadingType or
73 #Instance path then die
74
75 if ($sensorID eq '' or $sensorReadingType eq '' or $path eq '') {
Ratan Gupta39747a02017-02-22 18:16:23 +053076 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +053077 die("sensor without info for target=$target");
78 }
79
80 #removing the string "instance:" from path
81 $path =~ s/^instance:/\//;
82
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -050083 #get the last word from the path to check whether it is an occ or
84 #something without a proper instance path.
85 #if instance path is sys0 then get the path value from the yaml
86 #if it is a occ path, get the path from yaml and add the occ instance
87 #number to it.
88 $obmcPath = Util::getObmcName(\@inventory,$path);
89 #if unable to get the obmc path then get from yaml
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -050090 if ((not defined $obmcPath) or ($obmcPath eq "/system")){
Dhruvaraj Subhashchandran0c1aa512017-07-12 08:28:33 -050091 if ($path eq "/sys-0") {
92 $obmcPath = $sensorTypeConfig->{$sensorType}->{"path"};
93 }
94 else {
95 my @pathelements =split(/\//,$path);
96 foreach my $elem (@pathelements) {
97 #split element-instance_number
98 my ($elemName,$elemNum) = split(/-([^-]+)$/,$elem);
99 if ((defined $elemName) and ($elemName eq "proc_socket")) {
100 $obmcPath = $sensorTypeConfig->{$sensorType}->{"path"}."occ".$elemNum;
101 last;
102 }
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500103 }
104 }
105 }
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 =
115 $sensorTypeConfig->{$sensorType}->{"serviceInterface"};
116 my $readingType = $sensorTypeConfig->{$sensorType}->{"readingType"};
117
Ratan Guptac7366702017-02-22 18:05:44 +0530118 printDebug("$sensorID : $sensorType : $sensorReadingType :$obmcPath \n");
119
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500120 writeToFile($sensorType,$sensorReadingType,$obmcPath,$serviceInterface,
121 $readingType,$sensorTypeConfig,$fh);
Ratan Gupta39747a02017-02-22 18:16:23 +0530122
Ratan Guptac7366702017-02-22 18:05:44 +0530123 }
124
125}
Ratan Gupta39747a02017-02-22 18:16:23 +0530126close $fh;
127
128
129#Get the metadata for the incoming sensortype from the loaded config file.
130#Write the sensor data into the output file
131
132sub writeToFile
133{
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500134 my ($sensorType,$sensorReadingType,$path,$serviceInterface,
135 $readingType,$sensorTypeConfig,$fh) = @_;
136
Ratan Gupta39747a02017-02-22 18:16:23 +0530137 print $fh " sensorType: ".$sensorType."\n";
138 print $fh " path: ".$path."\n";
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500139
Ratan Gupta39747a02017-02-22 18:16:23 +0530140 print $fh " sensorReadingType: ".$sensorReadingType."\n";
Dhruvaraj Subhashchandran611b90f2017-07-27 08:05:42 -0500141 print $fh " serviceInterface: ".$serviceInterface."\n";
142 print $fh " readingType: ".$readingType."\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530143 print $fh " interfaces:"."\n";
144
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500145 my $interfaces = $sensorTypeConfig->{$sensorType}->{"interfaces"};
Ratan Gupta39747a02017-02-22 18:16:23 +0530146 #Walk over all the interfces as it needs to be written
147 while (my ($interface,$properties) = each %{$interfaces}) {
148 print $fh " ".$interface.":\n";
149 #walk over all the properties as it needs to be written
150 while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) {
151 #will write property named "Property" first then
152 #other properties.
153 print $fh " ".$dbusProperty.":\n";
154 while (my ( $offset,$values) = each %{$dbusPropertyValue}) {
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500155 print $fh " $offset:\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530156 while (my ( $key,$value) = each %{$values}) {
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500157 print $fh " $key: ". $value."\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530158 }
159 }
160 }
161 }
162}
Ratan Guptac7366702017-02-22 18:05:44 +0530163
164# Usage
165sub printUsage
166{
167 print "
Ratan Gupta39747a02017-02-22 18:16:23 +0530168 $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptac7366702017-02-22 18:05:44 +0530169Options:
170 -d = debug mode
171 \n";
172 exit(1);
173}
174
175# Helper function to put debug statements.
176sub printDebug
177{
178 my $str = shift;
179 print "DEBUG: ", $str, "\n" if $debug;
180}