blob: a54341e76aceb12716824c46e7befa1cd3f6184b [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 Subhashchandran6a6bd292017-07-12 06:39:09 -050015my $metaDir = "";
Ratan Guptac7366702017-02-22 18:05:44 +053016
17# Command line argument parsing
18GetOptions(
19"i=s" => \$serverwizFile, # string
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -050020"m=s" => \$metaDir, # 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 Subhashchandran6a6bd292017-07-12 06:39:09 -050026if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDir 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
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -050039my $sensorTypeConfig;
40my $tmpSensor;
41opendir my $dir,$metaDir or die "Cannot open directory: $!";
42my @files = readdir $dir;
43foreach my $file (@files){
44 my ($filename,$extn) = split(/\.([^\.]+)$/,$file);
45 if((defined($extn)) and ( $extn eq "yaml")) {
46 my $metaDataFile = $metaDir."/".$file;
47 my $tmpSensor = LoadFile($metaDataFile);
48 if(!keys %{$sensorTypeConfig}) {
49 %{$sensorTypeConfig} = %{$tmpSensor};
50 }
51 else {
52 %{$sensorTypeConfig} = (%{$sensorTypeConfig},%{$tmpSensor});
53 }
54 }
55}
56
57
Ratan Gupta39747a02017-02-22 18:16:23 +053058open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
Ratan Guptac7366702017-02-22 18:05:44 +053059
60my @interestedTypes = keys %{$sensorTypeConfig};
61my %types;
62
63@types{@interestedTypes} = ();
64
65my @inventory = Inventory::getInventory($targetObj);
66#Process all the targets in the XML
67foreach my $target (sort keys %{$targetObj->getAllTargets()})
68{
69 my $sensorID = '';
70 my $sensorType = '';
71 my $sensorReadingType = '';
72 my $path = '';
73 my $obmcPath = '';
74
75 if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
76
77 $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID");
78
79 $sensorType = $targetObj->getAttribute($target, "IPMI_SENSOR_TYPE");
80
81 $sensorReadingType = $targetObj->getAttribute($target,
82 "IPMI_SENSOR_READING_TYPE");
83
84 $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
85
86 #not interested in this sensortype
Ratan Gupta39747a02017-02-22 18:16:23 +053087 next if (not exists $types{$sensorType});
Ratan Guptac7366702017-02-22 18:05:44 +053088
89 #if there is ipmi sensor without sensorid or sensorReadingType or
90 #Instance path then die
91
92 if ($sensorID eq '' or $sensorReadingType eq '' or $path eq '') {
Ratan Gupta39747a02017-02-22 18:16:23 +053093 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +053094 die("sensor without info for target=$target");
95 }
96
97 #removing the string "instance:" from path
98 $path =~ s/^instance:/\//;
99
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500100 #get the last word from the path to check whether it is an occ or
101 #something without a proper instance path.
102 #if instance path is sys0 then get the path value from the yaml
103 #if it is a occ path, get the path from yaml and add the occ instance
104 #number to it.
105 $obmcPath = Util::getObmcName(\@inventory,$path);
106 #if unable to get the obmc path then get from yaml
107 if (not defined $obmcPath) {
Dhruvaraj Subhashchandran0c1aa512017-07-12 08:28:33 -0500108 if ($path eq "/sys-0") {
109 $obmcPath = $sensorTypeConfig->{$sensorType}->{"path"};
110 }
111 else {
112 my @pathelements =split(/\//,$path);
113 foreach my $elem (@pathelements) {
114 #split element-instance_number
115 my ($elemName,$elemNum) = split(/-([^-]+)$/,$elem);
116 if ((defined $elemName) and ($elemName eq "proc_socket")) {
117 $obmcPath = $sensorTypeConfig->{$sensorType}->{"path"}."occ".$elemNum;
118 last;
119 }
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500120 }
121 }
122 }
Ratan Guptac7366702017-02-22 18:05:44 +0530123
Ratan Guptac7366702017-02-22 18:05:44 +0530124 if (not defined $obmcPath) {
Ratan Gupta39747a02017-02-22 18:16:23 +0530125 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +0530126 die("Unable to get the obmc path for path=$path");
127 }
128
Ratan Gupta39747a02017-02-22 18:16:23 +0530129 print $fh $sensorID.":\n";
130
Ratan Guptac7366702017-02-22 18:05:44 +0530131 printDebug("$sensorID : $sensorType : $sensorReadingType :$obmcPath \n");
132
Ratan Gupta39747a02017-02-22 18:16:23 +0530133 writeToFile($sensorType,$sensorReadingType,$obmcPath,$sensorTypeConfig,$fh);
134
Ratan Guptac7366702017-02-22 18:05:44 +0530135 }
136
137}
Ratan Gupta39747a02017-02-22 18:16:23 +0530138close $fh;
139
140
141#Get the metadata for the incoming sensortype from the loaded config file.
142#Write the sensor data into the output file
143
144sub writeToFile
145{
146 my ($sensorType,$sensorReadingType,$path,$sensorTypeConfig,$fh) = @_;
147 print $fh " sensorType: ".$sensorType."\n";
148 print $fh " path: ".$path."\n";
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500149
Ratan Gupta39747a02017-02-22 18:16:23 +0530150 print $fh " sensorReadingType: ".$sensorReadingType."\n";
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500151 print $fh " updateInterface: ".$sensorTypeConfig->{$sensorType}->{"updateInterface"}."\n";
Dhruvaraj Subhashchandran0c1aa512017-07-12 08:28:33 -0500152 if (defined($sensorTypeConfig->{$sensorType}->{"readingType"})) {
153 print $fh " readingType: ".$sensorTypeConfig->{$sensorType}->{"readingType"}."\n";
154 }
155 if (defined($sensorTypeConfig->{$sensorType}->{"byteOffset"})) {
156 print $fh " byteOffset: ".$sensorTypeConfig->{$sensorType}->{"byteOffset"}."\n";
157 }
Ratan Gupta39747a02017-02-22 18:16:23 +0530158 print $fh " interfaces:"."\n";
159
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500160 my $interfaces = $sensorTypeConfig->{$sensorType}->{"interfaces"};
Ratan Gupta39747a02017-02-22 18:16:23 +0530161 #Walk over all the interfces as it needs to be written
162 while (my ($interface,$properties) = each %{$interfaces}) {
163 print $fh " ".$interface.":\n";
164 #walk over all the properties as it needs to be written
165 while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) {
166 #will write property named "Property" first then
167 #other properties.
168 print $fh " ".$dbusProperty.":\n";
169 while (my ( $offset,$values) = each %{$dbusPropertyValue}) {
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500170 print $fh " $offset:\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530171 while (my ( $key,$value) = each %{$values}) {
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500172 print $fh " $key: ". $value."\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530173 }
174 }
175 }
176 }
177}
Ratan Guptac7366702017-02-22 18:05:44 +0530178
179# Usage
180sub printUsage
181{
182 print "
Ratan Gupta39747a02017-02-22 18:16:23 +0530183 $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptac7366702017-02-22 18:05:44 +0530184Options:
185 -d = debug mode
186 \n";
187 exit(1);
188}
189
190# Helper function to put debug statements.
191sub printDebug
192{
193 my $str = shift;
194 print "DEBUG: ", $str, "\n" if $debug;
195}