View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.unix.aix;
6   
7   import static oshi.util.Memoizer.memoize;
8   
9   import java.util.List;
10  import java.util.function.Supplier;
11  
12  import oshi.annotation.concurrent.Immutable;
13  import oshi.hardware.Baseboard;
14  import oshi.hardware.Firmware;
15  import oshi.hardware.common.AbstractComputerSystem;
16  import oshi.util.Constants;
17  import oshi.util.ExecutingCommand;
18  import oshi.util.ParseUtil;
19  import oshi.util.Util;
20  
21  /**
22   * Hardware data obtained from lsattr
23   */
24  @Immutable
25  final class AixComputerSystem extends AbstractComputerSystem {
26  
27      private final Supplier<LsattrStrings> lsattrStrings = memoize(AixComputerSystem::readLsattr);
28      private final Supplier<List<String>> lscfg;
29  
30      AixComputerSystem(Supplier<List<String>> lscfg) {
31          this.lscfg = lscfg;
32      }
33  
34      @Override
35      public String getManufacturer() {
36          return lsattrStrings.get().manufacturer;
37      }
38  
39      @Override
40      public String getModel() {
41          return lsattrStrings.get().model;
42      }
43  
44      @Override
45      public String getSerialNumber() {
46          return lsattrStrings.get().serialNumber;
47      }
48  
49      @Override
50      public String getHardwareUUID() {
51          return lsattrStrings.get().uuid;
52      }
53  
54      @Override
55      public Firmware createFirmware() {
56          return new AixFirmware(lsattrStrings.get().biosVendor, lsattrStrings.get().biosPlatformVersion,
57                  lsattrStrings.get().biosVersion);
58      }
59  
60      @Override
61      public Baseboard createBaseboard() {
62          return new AixBaseboard(lscfg);
63      }
64  
65      private static LsattrStrings readLsattr() {
66          String fwVendor = "IBM";
67          String fwVersion = null;
68          String fwPlatformVersion = null;
69  
70          String manufacturer = fwVendor;
71          String model = null;
72          String serialNumber = null;
73          String uuid = null;
74  
75          /*-
76          fwversion       IBM,RG080425_d79e22_r                Firmware version and revision levels                False
77          modelname       IBM,9114-275                         Machine name                                        False
78          os_uuid         789f930f-b15c-4639-b842-b42603862704 N/A                                                 True
79          rtasversion     1                                    Open Firmware RTAS version                          False
80          systemid        IBM,0110ACFDE                        Hardware system identifier                          False
81          */
82  
83          final String fwVersionMarker = "fwversion";
84          final String modelMarker = "modelname";
85          final String systemIdMarker = "systemid";
86          final String uuidMarker = "os_uuid";
87          final String fwPlatformVersionMarker = "Platform Firmware level is";
88  
89          for (final String checkLine : ExecutingCommand.runNative("lsattr -El sys0")) {
90              if (checkLine.startsWith(fwVersionMarker)) {
91                  fwVersion = checkLine.split(fwVersionMarker)[1].trim();
92                  int comma = fwVersion.indexOf(',');
93                  if (comma > 0 && fwVersion.length() > comma) {
94                      fwVendor = fwVersion.substring(0, comma);
95                      fwVersion = fwVersion.substring(comma + 1);
96                  }
97                  fwVersion = ParseUtil.whitespaces.split(fwVersion)[0];
98              } else if (checkLine.startsWith(modelMarker)) {
99                  model = checkLine.split(modelMarker)[1].trim();
100                 int comma = model.indexOf(',');
101                 if (comma > 0 && model.length() > comma) {
102                     manufacturer = model.substring(0, comma);
103                     model = model.substring(comma + 1);
104                 }
105                 model = ParseUtil.whitespaces.split(model)[0];
106             } else if (checkLine.startsWith(systemIdMarker)) {
107                 serialNumber = checkLine.split(systemIdMarker)[1].trim();
108                 serialNumber = ParseUtil.whitespaces.split(serialNumber)[0];
109             } else if (checkLine.startsWith(uuidMarker)) {
110                 uuid = checkLine.split(uuidMarker)[1].trim();
111                 uuid = ParseUtil.whitespaces.split(uuid)[0];
112             }
113         }
114         for (final String checkLine : ExecutingCommand.runNative("lsmcode -c")) {
115             /*-
116              Platform Firmware level is 3F080425
117              System Firmware level is RG080425_d79e22_regatta
118              */
119             if (checkLine.startsWith(fwPlatformVersionMarker)) {
120                 fwPlatformVersion = checkLine.split(fwPlatformVersionMarker)[1].trim();
121                 break;
122             }
123         }
124         return new LsattrStrings(fwVendor, fwPlatformVersion, fwVersion, manufacturer, model, serialNumber, uuid);
125     }
126 
127     private static final class LsattrStrings {
128         private final String biosVendor;
129         private final String biosPlatformVersion;
130         private final String biosVersion;
131 
132         private final String manufacturer;
133         private final String model;
134         private final String serialNumber;
135         private final String uuid;
136 
137         private LsattrStrings(String biosVendor, String biosPlatformVersion, String biosVersion, String manufacturer,
138                 String model, String serialNumber, String uuid) {
139             this.biosVendor = Util.isBlank(biosVendor) ? Constants.UNKNOWN : biosVendor;
140             this.biosPlatformVersion = Util.isBlank(biosPlatformVersion) ? Constants.UNKNOWN : biosPlatformVersion;
141             this.biosVersion = Util.isBlank(biosVersion) ? Constants.UNKNOWN : biosVersion;
142 
143             this.manufacturer = Util.isBlank(manufacturer) ? Constants.UNKNOWN : manufacturer;
144             this.model = Util.isBlank(model) ? Constants.UNKNOWN : model;
145             this.serialNumber = Util.isBlank(serialNumber) ? Constants.UNKNOWN : serialNumber;
146             this.uuid = Util.isBlank(uuid) ? Constants.UNKNOWN : uuid;
147         }
148     }
149 }