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