1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5
6use mrw::Targets; # Set of APIs allowing access to parsed ServerWiz2 XML output
7use mrw::Inventory; # To get list of Inventory targets
8use Getopt::Long; # For parsing command line arguments
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        # YAML with list of {Instance:SensorID} dictionary
93        print $fh "- Instance: ";
94        print $fh "$instance\n";
95        print $fh "  SensorID: ";
96        print $fh "$occHash{$instance}\n";
97    }
98    close $fh;
99}
100
101# Helper function to put debug statements.
102sub printDebug
103{
104    my $str = shift;
105    print "DEBUG: ", $str, "\n" if $debug;
106}
107
108# Usage
109sub printUsage
110{
111    print "
112    $0 -i [XML filename] -o [Output filename] [OPTIONS]
113Options:
114    -f = force output file creation even when errors
115    -d = debug mode
116    -v = verbose mode - for verbose o/p from Targets.pm
117
118PS: mrw::Targets can be found in https://github.com/open-power/serverwiz/
119    mrw::Inventory can be found in https://github.com/openbmc/phosphor-mrw-tools/
120    \n";
121    exit(1);
122}
123#------------------------------------END OF SUB-----------------------
124