blob: 8dceaf4ddb783b40322b64fe05401e352e9f2d26 [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) {
108 my @pathelements =split(/\//,$path);
109 foreach my $elem (@pathelements) {
110 #split element-instance_number
111 my ($elemName,$elemNum) = split(/-([^-]+)$/,$elem);
112 if((defined $elemName) and ($elemName eq "proc_socket")) {
113 $obmcPath = $sensorTypeConfig->{$sensorType}->{"path"}."occ".$elemNum;
114 last;
115 }
116 }
117 }
Ratan Guptac7366702017-02-22 18:05:44 +0530118
Ratan Guptac7366702017-02-22 18:05:44 +0530119 if (not defined $obmcPath) {
Ratan Gupta39747a02017-02-22 18:16:23 +0530120 close $fh;
Ratan Guptac7366702017-02-22 18:05:44 +0530121 die("Unable to get the obmc path for path=$path");
122 }
123
Ratan Gupta39747a02017-02-22 18:16:23 +0530124 print $fh $sensorID.":\n";
125
Ratan Guptac7366702017-02-22 18:05:44 +0530126 printDebug("$sensorID : $sensorType : $sensorReadingType :$obmcPath \n");
127
Ratan Gupta39747a02017-02-22 18:16:23 +0530128 writeToFile($sensorType,$sensorReadingType,$obmcPath,$sensorTypeConfig,$fh);
129
Ratan Guptac7366702017-02-22 18:05:44 +0530130 }
131
132}
Ratan Gupta39747a02017-02-22 18:16:23 +0530133close $fh;
134
135
136#Get the metadata for the incoming sensortype from the loaded config file.
137#Write the sensor data into the output file
138
139sub writeToFile
140{
141 my ($sensorType,$sensorReadingType,$path,$sensorTypeConfig,$fh) = @_;
142 print $fh " sensorType: ".$sensorType."\n";
143 print $fh " path: ".$path."\n";
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500144
Ratan Gupta39747a02017-02-22 18:16:23 +0530145 print $fh " sensorReadingType: ".$sensorReadingType."\n";
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500146 print $fh " updateInterface: ".$sensorTypeConfig->{$sensorType}->{"updateInterface"}."\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530147 print $fh " interfaces:"."\n";
148
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500149 my $interfaces = $sensorTypeConfig->{$sensorType}->{"interfaces"};
Ratan Gupta39747a02017-02-22 18:16:23 +0530150 #Walk over all the interfces as it needs to be written
151 while (my ($interface,$properties) = each %{$interfaces}) {
152 print $fh " ".$interface.":\n";
153 #walk over all the properties as it needs to be written
154 while (my ($dbusProperty,$dbusPropertyValue) = each %{$properties}) {
155 #will write property named "Property" first then
156 #other properties.
157 print $fh " ".$dbusProperty.":\n";
158 while (my ( $offset,$values) = each %{$dbusPropertyValue}) {
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500159 print $fh " $offset:\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530160 while (my ( $key,$value) = each %{$values}) {
Dhruvaraj Subhashchandran6a6bd292017-07-12 06:39:09 -0500161 print $fh " $key: ". $value."\n";
Ratan Gupta39747a02017-02-22 18:16:23 +0530162 }
163 }
164 }
165 }
166}
Ratan Guptac7366702017-02-22 18:05:44 +0530167
168# Usage
169sub printUsage
170{
171 print "
Ratan Gupta39747a02017-02-22 18:16:23 +0530172 $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptac7366702017-02-22 18:05:44 +0530173Options:
174 -d = debug mode
175 \n";
176 exit(1);
177}
178
179# Helper function to put debug statements.
180sub printDebug
181{
182 my $str = shift;
183 print "DEBUG: ", $str, "\n" if $debug;
184}