]> granicus.if.org Git - icinga2/commitdiff
Update ReleaseHelper to work with most Linux distributions
authorJean-Marcel Flach <jean-marcel.flach@netways.de>
Mon, 27 Apr 2015 14:21:59 +0000 (16:21 +0200)
committerJean-Marcel Flach <jean-marcel.flach@netways.de>
Tue, 28 Apr 2015 10:48:41 +0000 (12:48 +0200)
refs #8888

lib/base/application.cpp

index af71a8725a8a4f6890ff551c32ffbee4967581e7..2aee26ee99328700b9be48ab7e22f2ebb09c0e54 100644 (file)
@@ -32,6 +32,7 @@
 #include <boost/algorithm/string/classification.hpp>
 #include <boost/foreach.hpp>
 #include <boost/algorithm/string/split.hpp>
+#include <boost/algorithm/string/trim.hpp>
 #include <boost/exception/errinfo_api_function.hpp>
 #include <boost/exception/errinfo_errno.hpp>
 #include <boost/exception/errinfo_file_name.hpp>
@@ -485,23 +486,58 @@ static String UnameHelper(char type)
        return result;
 }
 
-static String LsbReleaseHelper(void)
-{
-       FILE *fp = popen("lsb_release -s -d 2>&1", "r");
-
-       char line[1024];
-       std::ostringstream msgbuf;
-
-       while (fgets(line, sizeof(line), fp) != NULL)
-               msgbuf << line;
-
-       pclose(fp);
-
-       String result = msgbuf.str();
-       result.Trim();
-
-       return result;
+int ReleaseHelper(std::string &result)
+{
+    /* You are useing *some* distribution */
+    FILE *fp = popen("lsb_release -s -d 2>&1", "r");
+    std::ostringstream msgbuf;
+
+    if (fp != NULL) {
+        char line[1024];
+        while (fgets(line, sizeof(line), fp) != NULL)
+            msgbuf << line;
+        int status = pclose(fp);
+        if (WEXITSTATUS(status) == 0) { 
+            result = msgbuf.str();
+                       boost::trim(result);
+            return result.length();
+        }    
+    }    
+
+    /* You have systemd or Ubuntu etc. */
+    std::ifstream release("/etc/os-release");
+    std::string release_line;
+    if (release.is_open()) {
+        while (getline(release, release_line)) {
+            if (release_line.find("PRETTY_NAME") != std::string::npos) {
+                result = release_line.substr(13, release_line.length() - 14); 
+                return result.length();
+            }    
+        }    
+    }    
+
+    /* Centos < 7 */
+    release.close();
+    release.open("/etc/redhat-release");
+    if (release.is_open()) {
+        getline(release, release_line);
+               result = release_line;
+        return result.length();
+    }    
+
+    /* sles 11 sp3, opensuse w/e */
+    release.close();
+    release.open("etc/SuSE-release");
+    if (release.is_open()) {
+        getline(release, release_line);
+        result = release_line;
+        return result.length();
+    }    
+
+    /* Just give up */
+    return 0;
 }
+
 #endif /* _WIN32 */
 
 /**
@@ -534,7 +570,9 @@ void Application::DisplayInfoMessage(std::ostream& os, bool skipVersion)
 #endif /* _WIN32 */
 
 #ifdef __linux__
-       os << "  Distribution: " << LsbReleaseHelper() << "\n";
+       std::string release;
+       if (ReleaseHelper(release))
+               os << "  Distribution: " << release << "\n";
 #endif /* __linux__ */
 }