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 = ""; |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame^] | 15 | my $metaDir = ""; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 16 | |
| 17 | # Command line argument parsing |
| 18 | GetOptions( |
| 19 | "i=s" => \$serverwizFile, # string |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame^] | 20 | "m=s" => \$metaDir, # 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 | |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame^] | 26 | if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDir 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 | |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame^] | 39 | my $sensorTypeConfig; |
| 40 | my $tmpSensor; |
| 41 | opendir my $dir,$metaDir or die "Cannot open directory: $!"; |
| 42 | my @files = readdir $dir; |
| 43 | foreach 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 Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 58 | open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!"; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 59 | |
| 60 | my @interestedTypes = keys %{$sensorTypeConfig}; |
| 61 | my %types; |
| 62 | |
| 63 | @types{@interestedTypes} = (); |
| 64 | |
| 65 | my @inventory = Inventory::getInventory($targetObj); |
| 66 | #Process all the targets in the XML |
| 67 | foreach 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 Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 87 | next if (not exists $types{$sensorType}); |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 88 | |
| 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 Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 93 | close $fh; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 94 | die("sensor without info for target=$target"); |
| 95 | } |
| 96 | |
| 97 | #removing the string "instance:" from path |
| 98 | $path =~ s/^instance:/\//; |
| 99 | |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame^] | 100 | #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 Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 118 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 119 | if (not defined $obmcPath) { |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 120 | close $fh; |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 121 | die("Unable to get the obmc path for path=$path"); |
| 122 | } |
| 123 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 124 | print $fh $sensorID.":\n"; |
| 125 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 126 | printDebug("$sensorID : $sensorType : $sensorReadingType :$obmcPath \n"); |
| 127 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 128 | writeToFile($sensorType,$sensorReadingType,$obmcPath,$sensorTypeConfig,$fh); |
| 129 | |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 133 | close $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 | |
| 139 | sub writeToFile |
| 140 | { |
| 141 | my ($sensorType,$sensorReadingType,$path,$sensorTypeConfig,$fh) = @_; |
| 142 | print $fh " sensorType: ".$sensorType."\n"; |
| 143 | print $fh " path: ".$path."\n"; |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame^] | 144 | |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 145 | print $fh " sensorReadingType: ".$sensorReadingType."\n"; |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame^] | 146 | print $fh " updateInterface: ".$sensorTypeConfig->{$sensorType}->{"updateInterface"}."\n"; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 147 | print $fh " interfaces:"."\n"; |
| 148 | |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame^] | 149 | my $interfaces = $sensorTypeConfig->{$sensorType}->{"interfaces"}; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 150 | #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 Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame^] | 159 | print $fh " $offset:\n"; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 160 | while (my ( $key,$value) = each %{$values}) { |
Dhruvaraj Subhashchandran | 6a6bd29 | 2017-07-12 06:39:09 -0500 | [diff] [blame^] | 161 | print $fh " $key: ". $value."\n"; |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 167 | |
| 168 | # Usage |
| 169 | sub printUsage |
| 170 | { |
| 171 | print " |
Ratan Gupta | 39747a0 | 2017-02-22 18:16:23 +0530 | [diff] [blame] | 172 | $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS] |
Ratan Gupta | c736670 | 2017-02-22 18:05:44 +0530 | [diff] [blame] | 173 | Options: |
| 174 | -d = debug mode |
| 175 | \n"; |
| 176 | exit(1); |
| 177 | } |
| 178 | |
| 179 | # Helper function to put debug statements. |
| 180 | sub printDebug |
| 181 | { |
| 182 | my $str = shift; |
| 183 | print "DEBUG: ", $str, "\n" if $debug; |
| 184 | } |