View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.unix.aix;
6   
7   import java.util.List;
8   
9   import oshi.annotation.concurrent.ThreadSafe;
10  import oshi.util.ExecutingCommand;
11  import oshi.util.ParseUtil;
12  import oshi.util.tuples.Pair;
13  import oshi.util.tuples.Triplet;
14  
15  /**
16   * Utility to query lscfg
17   */
18  @ThreadSafe
19  public final class Lscfg {
20  
21      private Lscfg() {
22      }
23  
24      /**
25       * Query {@code lscfg -vp} to get all hardware devices
26       *
27       * @return A list of the output
28       */
29      public static List<String> queryAllDevices() {
30          return ExecutingCommand.runNative("lscfg -vp");
31      }
32  
33      /**
34       * Parse the output of {@code lscfg -vp} to get backplane info
35       *
36       * @param lscfg The output of a previous call to {@code lscfg -vp}
37       * @return A triplet with backplane model, serial number, and version
38       */
39      public static Triplet<String, String, String> queryBackplaneModelSerialVersion(List<String> lscfg) {
40          final String planeMarker = "WAY BACKPLANE";
41          final String modelMarker = "Part Number";
42          final String serialMarker = "Serial Number";
43          final String versionMarker = "Version";
44          final String locationMarker = "Physical Location";
45  
46          // 1 WAY BACKPLANE :
47          // Serial Number...............YL10243490FB
48          // Part Number.................80P4315
49          // Customer Card ID Number.....26F4
50          // CCIN Extender...............1
51          // FRU Number.................. 80P4315
52          // Version.....................RS6K
53          // Hardware Location Code......U0.1-P1
54          // Physical Location: U0.1-P1
55  
56          String model = null;
57          String serialNumber = null;
58          String version = null;
59          boolean planeFlag = false;
60          for (final String checkLine : lscfg) {
61              if (!planeFlag && checkLine.contains(planeMarker)) {
62                  planeFlag = true;
63              } else if (planeFlag) {
64                  if (checkLine.contains(modelMarker)) {
65                      model = ParseUtil.removeLeadingDots(checkLine.split(modelMarker)[1].trim());
66                  } else if (checkLine.contains(serialMarker)) {
67                      serialNumber = ParseUtil.removeLeadingDots(checkLine.split(serialMarker)[1].trim());
68                  } else if (checkLine.contains(versionMarker)) {
69                      version = ParseUtil.removeLeadingDots(checkLine.split(versionMarker)[1].trim());
70                  } else if (checkLine.contains(locationMarker)) {
71                      break;
72                  }
73              }
74          }
75          return new Triplet<>(model, serialNumber, version);
76      }
77  
78      /**
79       * Query {@code lscfg -vl device} to get hardware info
80       *
81       * @param device The disk to get the model and serial from
82       * @return A pair containing the model and serial number for the device, or null if not found
83       */
84      public static Pair<String, String> queryModelSerial(String device) {
85          String modelMarker = "Machine Type and Model";
86          String serialMarker = "Serial Number";
87          String model = null;
88          String serial = null;
89          for (String s : ExecutingCommand.runNative("lscfg -vl " + device)) {
90              // Default model to description at end of first line
91              if (model == null && s.contains(device)) {
92                  String locDesc = s.split(device)[1].trim();
93                  int idx = locDesc.indexOf(' ');
94                  if (idx > 0) {
95                      model = locDesc.substring(idx).trim();
96                  }
97              }
98              if (s.contains(modelMarker)) {
99                  model = ParseUtil.removeLeadingDots(s.split(modelMarker)[1].trim());
100             } else if (s.contains(serialMarker)) {
101                 serial = ParseUtil.removeLeadingDots(s.split(serialMarker)[1].trim());
102             }
103         }
104         return new Pair<>(model, serial);
105     }
106 }