View Javadoc
1   /*
2    * Copyright 2020-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.linux;
6   
7   import oshi.annotation.concurrent.ThreadSafe;
8   import oshi.util.ExecutingCommand;
9   import oshi.util.ParseUtil;
10  import oshi.util.UserGroupInfo;
11  
12  /**
13   * Utility to read info from {@code lshw}
14   */
15  @ThreadSafe
16  public final class Lshw {
17  
18      private Lshw() {
19      }
20  
21      private static final String MODEL;
22      private static final String SERIAL;
23      private static final String UUID;
24      static {
25          String model = null;
26          String serial = null;
27          String uuid = null;
28  
29          if (UserGroupInfo.isElevated()) {
30              String modelMarker = "product:";
31              String serialMarker = "serial:";
32              String uuidMarker = "uuid:";
33  
34              for (String checkLine : ExecutingCommand.runNative("lshw -C system")) {
35                  if (checkLine.contains(modelMarker)) {
36                      model = checkLine.split(modelMarker)[1].trim();
37                  } else if (checkLine.contains(serialMarker)) {
38                      serial = checkLine.split(serialMarker)[1].trim();
39                  } else if (checkLine.contains(uuidMarker)) {
40                      uuid = checkLine.split(uuidMarker)[1].trim();
41                  }
42              }
43          }
44          MODEL = model;
45          SERIAL = serial;
46          UUID = uuid;
47      }
48  
49      /**
50       * Query the model from lshw
51       *
52       * @return The model if available, null otherwise
53       */
54      public static String queryModel() {
55          return MODEL;
56      }
57  
58      /**
59       * Query the serial number from lshw
60       *
61       * @return The serial number if available, null otherwise
62       */
63      public static String querySerialNumber() {
64          return SERIAL;
65      }
66  
67      /**
68       * Query the UUID from lshw
69       *
70       * @return The UUID if available, null otherwise
71       */
72      public static String queryUUID() {
73          return UUID;
74      }
75  
76      /**
77       * Query the CPU capacity (max frequency) from lshw
78       *
79       * @return The CPU capacity (max frequency) if available, -1 otherwise
80       */
81      public static long queryCpuCapacity() {
82          String capacityMarker = "capacity:";
83          for (String checkLine : ExecutingCommand.runNative("lshw -class processor")) {
84              if (checkLine.contains(capacityMarker)) {
85                  return ParseUtil.parseHertz(checkLine.split(capacityMarker)[1].trim());
86              }
87          }
88          return -1L;
89      }
90  }