From 3a694760ca22d1e5c6e8c62a9026a732f38634ca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Clara=20Daia=20Hilgenberg=20Dar=C3=BA?= <cdhd12@inf.ufpr.br>
Date: Thu, 13 Oct 2016 11:52:35 -0300
Subject: [PATCH] Issue #46: Separate open_file from get_distro and add
 get_machine_processor function

---
 include/agent/linux/get_distro.h          |  4 +--
 include/agent/linux/get_processor_model.h |  6 ++++
 include/agent/linux/inventory.h           |  7 ++--
 include/agent/linux/open_file.h           |  5 +++
 src/linux/get_distro.cpp                  |  9 +----
 src/linux/get_processor_model.cpp         | 44 +++++++++++++++++++++++
 src/linux/inventory.cpp                   | 16 +++++----
 src/linux/open_file.cpp                   | 10 ++++++
 8 files changed, 79 insertions(+), 22 deletions(-)
 create mode 100644 include/agent/linux/get_processor_model.h
 create mode 100644 include/agent/linux/open_file.h
 create mode 100644 src/linux/get_processor_model.cpp
 create mode 100644 src/linux/open_file.cpp

diff --git a/include/agent/linux/get_distro.h b/include/agent/linux/get_distro.h
index ae5556a3..e9730beb 100644
--- a/include/agent/linux/get_distro.h
+++ b/include/agent/linux/get_distro.h
@@ -1,7 +1,5 @@
 #pragma once
 #include <sys/utsname.h>
-#include <fstream>
-#include <cstring>
+#include "agent/linux/open_file.h"
 
-int open_file(std::string name, std::ifstream& fstream);
 std::string get_distro();
diff --git a/include/agent/linux/get_processor_model.h b/include/agent/linux/get_processor_model.h
new file mode 100644
index 00000000..e3008313
--- /dev/null
+++ b/include/agent/linux/get_processor_model.h
@@ -0,0 +1,6 @@
+#pragma once
+#include <fstream>
+#include <string>
+#include "agent/linux/open_file.h"
+
+std::string get_processor_model();
diff --git a/include/agent/linux/inventory.h b/include/agent/linux/inventory.h
index 3d96c0cf..b57d6f45 100644
--- a/include/agent/linux/inventory.h
+++ b/include/agent/linux/inventory.h
@@ -3,15 +3,12 @@
 #include <time.h>
 #include <iostream>
 #include "agent/linux/get_date.h"
+#include "agent/linux/get_distro.h"
 #include "agent/linux/get_macaddr.h"
 #include "agent/linux/get_machine_type.h"
+#include "agent/linux/get_processor_model.h"
 #include "agent/linux/get_time.h"
 #include "agent/linux/get_user_count.h"
 #include "json/json.h"
 
-int open_file(std::string name, std::ifstream& fstream);
-
-std::string get_distro();
-std::string get_processor_model();
-
 Json::Value get_inventory();
diff --git a/include/agent/linux/open_file.h b/include/agent/linux/open_file.h
new file mode 100644
index 00000000..48637750
--- /dev/null
+++ b/include/agent/linux/open_file.h
@@ -0,0 +1,5 @@
+#pragma once
+#include <fstream>
+#include <string>
+
+int open_file(std::string name, std::ifstream& fstream);
diff --git a/src/linux/get_distro.cpp b/src/linux/get_distro.cpp
index 4f8ecd3c..a7c47703 100644
--- a/src/linux/get_distro.cpp
+++ b/src/linux/get_distro.cpp
@@ -20,14 +20,7 @@
  */
 
 #include "agent/linux/get_distro.h"
-
-int open_file(std::string name, std::ifstream& fstream) {
-        fstream.open(name);
-        if (fstream.is_open())
-            return 1;
-        else
-            return 0;
-}
+#include "agent/linux/open_file.h"
 
 std::string get_distro() {
     /* currently recorded distros are
diff --git a/src/linux/get_processor_model.cpp b/src/linux/get_processor_model.cpp
new file mode 100644
index 00000000..65a1ce87
--- /dev/null
+++ b/src/linux/get_processor_model.cpp
@@ -0,0 +1,44 @@
+/* Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+ * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+ *
+ * This file is part of simmc-agent
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
+ */
+
+#include "agent/linux/get_processor_model.h"
+
+std::string get_processor_model() {
+
+    std::ifstream cpuinfo_file;
+    std::string line;
+    if (open_file("/proc/cpuinfo", cpuinfo_file)) {
+        const std::string key = "model name";
+        std::size_t found;
+
+        while (getline(cpuinfo_file, line)) {
+            found = line.find(key);
+            if (found != std::string::npos) {
+                // remove ':' and space at the beginning
+                found = line.find(":");
+                return (line.substr(found+2));
+            }
+        }
+        throw std::string("Coudn't find model name in /proc/cpuinfo");
+    }
+
+    throw std::string("Coudn't open /proc/cpuinfo");
+}
diff --git a/src/linux/inventory.cpp b/src/linux/inventory.cpp
index ff41037b..1863056b 100644
--- a/src/linux/inventory.cpp
+++ b/src/linux/inventory.cpp
@@ -21,12 +21,9 @@
 
 #include "agent/linux/inventory.h"
 
-std::string get_processor_model() {
-    return "unknown";
-}
+Json::Value get_inventory(){
 
-Json::Value get_inventory() {
-    Json::Value inv;
+	Json::Value inv;
 
     // os info
 
@@ -46,9 +43,16 @@ Json::Value get_inventory() {
     inv["data"]["os_kernel"] = sysinfo.release;
     inv["data"]["mirror_timestamp"] = sysinfo.version;
 
+    try {
+        inv["data"]["processor"] = get_processor_model();
+    }
+    catch ( std::string err ) {
+        inv["data"]["processor"] = Json::nullValue;
+        inv["error"]["processor"] = err;
+    }
+
     // hw info
     /*
-    inv["processor"] =
     inv["memory"] =
     inv["disk1_model"] =
     inv["disk1_size"] =
diff --git a/src/linux/open_file.cpp b/src/linux/open_file.cpp
new file mode 100644
index 00000000..8585b2fc
--- /dev/null
+++ b/src/linux/open_file.cpp
@@ -0,0 +1,10 @@
+#include "agent/linux/open_file.h"
+
+int open_file(std::string name, std::ifstream& fstream){
+		fstream.open(name);
+		if (fstream.is_open())
+			return 1;
+		else
+			return 0;
+}
+
-- 
GitLab