changeset 25:1943c1071d45

add cpu model to data collected by the system backend
author Omair Majid <omajid@redhat.com>
date Fri, 23 Dec 2011 11:49:58 -0500
parents 3387781e66ff
children 5adb9bc270e2
files src/com/redhat/thermostat/backend/system/HostInfoBuilder.java src/com/redhat/thermostat/backend/system/SystemBackend.java src/com/redhat/thermostat/common/HostInfo.java
diffstat 3 files changed, 43 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/src/com/redhat/thermostat/backend/system/HostInfoBuilder.java	Thu Dec 22 18:45:12 2011 -0500
+++ b/src/com/redhat/thermostat/backend/system/HostInfoBuilder.java	Fri Dec 23 11:49:58 2011 -0500
@@ -36,14 +36,39 @@
         String osKernel = System.getProperty("os.name") + " " + System.getProperty("os.version");
         logger.log(Level.FINEST, "osKernel: " + osKernel);
 
-        int cpuCount = getProcessorCountFromProc();
+        final String KEY_PROCESSOR_ID = "processor";
+        final String KEY_CPU_MODEL = "model name";
+        int cpuCount = 0;
+        String cpuModel = null;
+        BufferedReader cpuInfoReader = null;
+        try {
+            cpuInfoReader = new BufferedReader(new FileReader(CPUINFO_FILE));
+            String line = null;
+            while ((line = cpuInfoReader.readLine()) != null) {
+                if (line.startsWith(KEY_PROCESSOR_ID)) {
+                    cpuCount++;
+                } else if (line.startsWith(KEY_CPU_MODEL)) {
+                    cpuModel = line.substring(line.indexOf(":") + 1).trim();
+                }
+            }
+        } catch (IOException e) {
+            logger.log(Level.WARNING, "unable to read " + CPUINFO_FILE);
+        } finally {
+            if (cpuInfoReader != null) {
+                try {
+                    cpuInfoReader.close();
+                } catch (IOException e) {
+                    logger.log(Level.WARNING, "unable to close " + CPUINFO_FILE);
+                }
+            }
+        }
         logger.log(Level.FINEST, "cpuCount: " + cpuCount);
 
         long totalMemory = -1;
-        BufferedReader reader = null;
+        BufferedReader memInfoReader = null;
         try {
-            reader = new BufferedReader(new FileReader(MEMINFO_FILE));
-            String[] memTotalParts = reader.readLine().split(" +");
+            memInfoReader = new BufferedReader(new FileReader(MEMINFO_FILE));
+            String[] memTotalParts = memInfoReader.readLine().split(" +");
             long data = Long.valueOf(memTotalParts[1]);
             String units = memTotalParts[2];
             if (units.equals("kB")) {
@@ -52,9 +77,9 @@
         } catch (IOException e) {
             logger.log(Level.WARNING, "unable to read " + MEMINFO_FILE);
         } finally {
-            if (reader != null) {
+            if (memInfoReader != null) {
                 try {
-                    reader.close();
+                    memInfoReader.close();
                 } catch (IOException e) {
                     logger.log(Level.WARNING, "unable to close " + MEMINFO_FILE);
                 }
@@ -62,33 +87,7 @@
         }
         logger.log(Level.FINEST, "totalMemory: " + totalMemory + " bytes");
 
-        return new HostInfo(hostname, osName, osKernel, cpuCount, totalMemory);
-
+        return new HostInfo(hostname, osName, osKernel, cpuModel, cpuCount, totalMemory);
     }
 
-    private static int getProcessorCountFromProc() {
-        final String KEY_PROCESSOR_ID = "processor";
-        int totalCpus = 0;
-        BufferedReader reader = null;
-        try {
-            reader = new BufferedReader(new FileReader(CPUINFO_FILE));
-            String line = null;
-            while ((line = reader.readLine()) != null) {
-                if (line.startsWith(KEY_PROCESSOR_ID)) {
-                    totalCpus++;
-                }
-            }
-        } catch (IOException e) {
-            logger.log(Level.WARNING, "unable to read " + CPUINFO_FILE);
-        } finally {
-            if (reader != null) {
-                try {
-                    reader.close();
-                } catch (IOException e) {
-                    logger.log(Level.WARNING, "unable to close " + CPUINFO_FILE);
-                }
-            }
-        }
-        return totalCpus;
-    }
 }
--- a/src/com/redhat/thermostat/backend/system/SystemBackend.java	Thu Dec 22 18:45:12 2011 -0500
+++ b/src/com/redhat/thermostat/backend/system/SystemBackend.java	Fri Dec 23 11:49:58 2011 -0500
@@ -49,6 +49,7 @@
     private Key osNameKey = new Key("os_name", false);
     private Key osKernelKey = new Key("os_kernel", false);
     private Key cpuCountKey = new Key("cpu_num", false);
+    private Key cpuModelKey = new Key("cpu_model", false);
     private Key hostMemoryTotalKey = new Key("memory_total", false);
 
     private Category networkInfoCategory = new Category("network-info");
@@ -87,7 +88,7 @@
         networkInfoCategory.addKey(ip6AddrKey);
         networkInfoCategory.lock();
         categories.add(networkInfoCategory);
-        
+
         // cpu-stats category.
         cpuStatCategory.addKey(Key.TIMESTAMP);
         cpuStatCategory.addKey(cpu5LoadKey);
@@ -228,6 +229,7 @@
         chunk.put(osNameKey, hostInfo.getOsName());
         chunk.put(osKernelKey, hostInfo.getOsKernel());
         chunk.put(cpuCountKey, Integer.toString(hostInfo.getCpuCount()));
+        chunk.put(cpuModelKey, hostInfo.getCpuModel());
         chunk.put(hostMemoryTotalKey, Long.toString(hostInfo.getTotalMemory()));
         return chunk;
     }
--- a/src/com/redhat/thermostat/common/HostInfo.java	Thu Dec 22 18:45:12 2011 -0500
+++ b/src/com/redhat/thermostat/common/HostInfo.java	Fri Dec 23 11:49:58 2011 -0500
@@ -5,13 +5,15 @@
     private final String hostname;
     private final String osName;
     private final String osKernel;
+    private final String cpuModel;
     private final int cpuCount;
     private final long totalMemory;
 
-    public HostInfo(String hostname, String osName, String osKernel, int cpuCount, long totalMemory) {
+    public HostInfo(String hostname, String osName, String osKernel, String cpuModel, int cpuCount, long totalMemory) {
         this.hostname = hostname;
         this.osName = osName;
         this.osKernel = osKernel;
+        this.cpuModel = cpuModel;
         this.cpuCount = cpuCount;
         this.totalMemory = totalMemory;
     }
@@ -28,6 +30,10 @@
         return osKernel;
     }
 
+    public String getCpuModel() {
+        return cpuModel;
+    }
+
     public int getCpuCount() {
         return cpuCount;
     }
@@ -38,4 +44,5 @@
     public long getTotalMemory() {
         return totalMemory;
     }
+
 }