blob: d4eaf2507a373dc3da14bd53d7593a2b633a426f [file] [log] [blame]
Vishwanatha Subbanna7b93a162017-07-21 17:03:18 +05301#!/usr/bin/env perl
2use strict;
3use warnings;
4
5use mrw::Targets; # Set of APIs allowing access to parsed ServerWiz2 XML output
6use mrw::Inventory; # To get list of Inventory targets
7use Getopt::Long; # For parsing command line arguments
Vishwanatha Subbanna7b93a162017-07-21 17:03:18 +05308
9# Globals
10my $force = 0;
11my $serverwizFile = "";
12my $debug = 0;
13my $outputFile = "";
14my $verbose = 0;
15
16# Command line argument parsing
17GetOptions(
18"f" => \$force, # numeric
19"i=s" => \$serverwizFile, # string
20"o=s" => \$outputFile, # string
21"d" => \$debug,
22"v" => \$verbose,
23)
24or printUsage();
25
26if (($serverwizFile eq "") or ($outputFile eq ""))
27{
28 printUsage();
29}
30
31# Hashmap of all the OCCs and their sensor IDs
32my %occHash;
33
34# API used to access parsed XML data
35my $targetObj = Targets->new;
36if($verbose == 1)
37{
38 $targetObj->{debug} = 1;
39}
40
41if($force == 1)
42{
43 $targetObj->{force} = 1;
44}
45
46$targetObj->loadXML($serverwizFile);
47print "Loaded MRW XML: $serverwizFile \n";
48
49# Process all the targets in the XML
50foreach 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
81generateYamlFile();
82##------------------------------------END OF MAIN-----------------------
83
84sub 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 Subbanna7b93a162017-07-21 17:03:18 +053091 # 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.
101sub printDebug
102{
103 my $str = shift;
104 print "DEBUG: ", $str, "\n" if $debug;
105}
106
107# Usage
108sub printUsage
109{
110 print "
111 $0 -i [XML filename] -o [Output filename] [OPTIONS]
112Options:
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
117PS: 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-----------------------