Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 1 | #! /usr/bin/perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | |
| 5 | use mrw::Targets; |
| 6 | use mrw::Inventory; |
| 7 | use mrw::Util; |
| 8 | use Getopt::Long; # For parsing command line arguments |
| 9 | use YAML::Tiny qw(LoadFile); |
| 10 | |
| 11 | # Globals |
| 12 | my $serverwizFile = ""; |
| 13 | my $debug = 0; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 14 | my $outputFile = ""; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 15 | my $metaDataFile = ""; |
| 16 | |
| 17 | # Command line argument parsing |
| 18 | GetOptions( |
| 19 | "i=s" => \$serverwizFile, # string |
| 20 | "m=s" => \$metaDataFile, # string |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 21 | "o=s" => \$outputFile, # string |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 22 | "d" => \$debug, |
| 23 | ) |
| 24 | or printUsage(); |
| 25 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 26 | if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq "")) |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 27 | { |
| 28 | printUsage(); |
| 29 | } |
| 30 | |
| 31 | my $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 Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 36 | #Get the metadata for that sensor from the metadata file. |
| 37 | #Merge the data into the outputfile |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 38 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 39 | open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!"; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 40 | my $sensorTypeConfig = LoadFile($metaDataFile); |
| 41 | |
| 42 | my @interestedTypes = keys %{$sensorTypeConfig}; |
| 43 | my %types; |
| 44 | |
| 45 | @types{@interestedTypes} = (); |
| 46 | |
| 47 | my @inventory = Inventory::getInventory($targetObj); |
| 48 | #Process all the targets in the XML |
| 49 | foreach 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 Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 69 | next if (not exists $types{$sensorType}); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 70 | |
| 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 Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 75 | close $fh; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 76 | 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 Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 86 | close $fh; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 87 | die("Unable to get the obmc path for path=$path"); |
| 88 | } |
| 89 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 90 | print $fh $sensorID.":\n"; |
| 91 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 92 | printDebug("$sensorID : $sensorType : $sensorReadingType :$obmcPath \n"); |
| 93 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 94 | writeToFile($sensorType,$sensorReadingType,$obmcPath,$sensorTypeConfig,$fh); |
| 95 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 99 | close $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 | |
| 105 | sub 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 Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 131 | |
| 132 | # Usage |
| 133 | sub printUsage |
| 134 | { |
| 135 | print " |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 136 | $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS] |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 137 | Options: |
| 138 | -d = debug mode |
| 139 | \n"; |
| 140 | exit(1); |
| 141 | } |
| 142 | |
| 143 | # Helper function to put debug statements. |
| 144 | sub printDebug |
| 145 | { |
| 146 | my $str = shift; |
| 147 | print "DEBUG: ", $str, "\n" if $debug; |
| 148 | } |