Vishwanatha Subbanna | 7b93a16 | 2017-07-21 17:03:18 +0530 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | |
| 5 | use mrw::Targets; # Set of APIs allowing access to parsed ServerWiz2 XML output |
| 6 | use mrw::Inventory; # To get list of Inventory targets |
| 7 | use Getopt::Long; # For parsing command line arguments |
Vishwanatha Subbanna | 7b93a16 | 2017-07-21 17:03:18 +0530 | [diff] [blame] | 8 | |
| 9 | # Globals |
| 10 | my $force = 0; |
| 11 | my $serverwizFile = ""; |
| 12 | my $debug = 0; |
| 13 | my $outputFile = ""; |
| 14 | my $verbose = 0; |
| 15 | |
| 16 | # Command line argument parsing |
| 17 | GetOptions( |
| 18 | "f" => \$force, # numeric |
| 19 | "i=s" => \$serverwizFile, # string |
| 20 | "o=s" => \$outputFile, # string |
| 21 | "d" => \$debug, |
| 22 | "v" => \$verbose, |
| 23 | ) |
| 24 | or printUsage(); |
| 25 | |
| 26 | if (($serverwizFile eq "") or ($outputFile eq "")) |
| 27 | { |
| 28 | printUsage(); |
| 29 | } |
| 30 | |
| 31 | # Hashmap of all the OCCs and their sensor IDs |
| 32 | my %occHash; |
| 33 | |
| 34 | # API used to access parsed XML data |
| 35 | my $targetObj = Targets->new; |
| 36 | if($verbose == 1) |
| 37 | { |
| 38 | $targetObj->{debug} = 1; |
| 39 | } |
| 40 | |
| 41 | if($force == 1) |
| 42 | { |
| 43 | $targetObj->{force} = 1; |
| 44 | } |
| 45 | |
| 46 | $targetObj->loadXML($serverwizFile); |
| 47 | print "Loaded MRW XML: $serverwizFile \n"; |
| 48 | |
| 49 | # Process all the targets in the XML |
| 50 | foreach my $target (sort keys %{$targetObj->getAllTargets()}) |
| 51 | { |
| 52 | # Only take the instances having 'OCC" as TYPE |
| 53 | if ("OCC" ne $targetObj->getAttribute($target, "TYPE")) |
| 54 | { |
| 55 | next; |
| 56 | } |
| 57 | |
| 58 | # OCC instance and sensor ID to insert into output file |
| 59 | my $instance = ""; |
| 60 | my $sensor = ""; |
| 61 | |
| 62 | # Now that we are in OCC target instance, get the instance number |
| 63 | $instance = $targetObj->getAttribute($target, "IPMI_INSTANCE"); |
| 64 | |
| 65 | # Each OCC would have occ_active_sensor child that would have |
| 66 | # more information, such as Sensor ID. |
| 67 | # This would be an array of children targets |
| 68 | my $children = $targetObj->getTargetChildren($target); |
| 69 | |
| 70 | for my $child (@{$children}) |
| 71 | { |
| 72 | $sensor = $targetObj->getAttribute($child, "IPMI_SENSOR_ID"); |
| 73 | } |
| 74 | |
| 75 | # Populate a hashmap with OCC and its sensor ID |
| 76 | $occHash{$instance} = $sensor; |
| 77 | |
| 78 | } # All the targets |
| 79 | |
| 80 | # Generate the yaml file |
| 81 | generateYamlFile(); |
| 82 | ##------------------------------------END OF MAIN----------------------- |
| 83 | |
| 84 | sub generateYamlFile |
| 85 | { |
| 86 | my $fileName = $outputFile; |
| 87 | open(my $fh, '>', $fileName) or die "Could not open file '$fileName' $!"; |
| 88 | |
| 89 | foreach my $instance (sort keys %occHash) |
| 90 | { |
Vishwanatha Subbanna | 7b93a16 | 2017-07-21 17:03:18 +0530 | [diff] [blame] | 91 | # YAML with list of {Instance:SensorID} dictionary |
| 92 | print $fh "- Instance: "; |
| 93 | print $fh "$instance\n"; |
| 94 | print $fh " SensorID: "; |
| 95 | print $fh "$occHash{$instance}\n"; |
| 96 | } |
| 97 | close $fh; |
| 98 | } |
| 99 | |
| 100 | # Helper function to put debug statements. |
| 101 | sub printDebug |
| 102 | { |
| 103 | my $str = shift; |
| 104 | print "DEBUG: ", $str, "\n" if $debug; |
| 105 | } |
| 106 | |
| 107 | # Usage |
| 108 | sub printUsage |
| 109 | { |
| 110 | print " |
| 111 | $0 -i [XML filename] -o [Output filename] [OPTIONS] |
| 112 | Options: |
| 113 | -f = force output file creation even when errors |
| 114 | -d = debug mode |
| 115 | -v = verbose mode - for verbose o/p from Targets.pm |
| 116 | |
| 117 | PS: mrw::Targets can be found in https://github.com/open-power/serverwiz/ |
| 118 | mrw::Inventory can be found in https://github.com/openbmc/phosphor-mrw-tools/ |
| 119 | \n"; |
| 120 | exit(1); |
| 121 | } |
| 122 | #------------------------------------END OF SUB----------------------- |