Commit 0fc6786e authored by jan.koester's avatar jan.koester
Browse files

ab ported

parent a9024baa
Loading
Loading
Loading
Loading
+87 −2
Changes for plugins/nodeinfo/nodeinfo.cpp: 87 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@
#else
#include <unistd.h>
#include <sys/utsname.h>
#include <sys/types.h>
#endif // Windows


@@ -169,14 +170,98 @@ namespace blogi {
            htmltable << libhtmlpp::HtmlTable::Row() << "Release Version: " << rver;
            htmltable << libhtmlpp::HtmlTable::Row() << "Hardware: " << getArchText(usysinfo.wProcessorArchitecture);
#endif
#ifndef Windows
#ifdef FreeBSD
            long psize;
            if((psize=sysconf(_SC_PAGESIZE))>0){
                // BSD/sysctl implementation (Your previous update)
                long total_pages = 0;
                long free_pages = 0;
                size_t len;

                // Get Total Pages
                len = sizeof(total_pages);
                sysctlbyname("vm.stats.vm.v_page_count", &total_pages, &len, NULL, 0);

                // Get Free Pages
                len = sizeof(free_pages);
                sysctlbyname("vm.stats.vm.v_free_count", &free_pages, &len, NULL, 0);

                // Calculation and Formatting
                char ab[512];
                long total_mb = (total_pages * psize) / 1048576;
                long free_mb = (free_pages * psize) / 1048576;

                snprintf(ab, 512, "%ld/%ld MB", free_mb, total_mb);
                htmltable << (libhtmlpp::HtmlTable::Row() << "Arbeitspeicher: " << ab);
            }
#elifdef Linux // New Linux-specific implementation
            // Linux implementation: Read /proc/meminfo
            std::ifstream meminfo("/proc/meminfo");
            long total_mem_kb = 0;
            long free_mem_kb = 0;
            std::string line;

            if (meminfo.is_open()) {
                while (std::getline(meminfo, line)) {
                    // Check for MemTotal
                    if (line.compare(0, 9, "MemTotal:") == 0) {
                        try {
                            // Skip "MemTotal:" and " " and parse the number
                            total_mem_kb = std::stol(line.substr(9));
                        } catch (...) {
                            // Handle parsing error
                        }
                    }
                    // Check for MemFree (or MemAvailable for a more accurate free value)
                    if (line.compare(0, 8, "MemFree:") == 0) {
                        try {
                            free_mem_kb = std::stol(line.substr(8));
                        } catch (...) {
                            // Handle parsing error
                        }
                    }
                }
                meminfo.close();
            }

            if (total_mem_kb > 0) {
                char ab[512];
                // Convert kB to MB (1024 kB = 1 MB)
                long total_mb = total_mem_kb / 1024;
                long free_mb = free_mem_kb / 1024;

                snprintf(ab, 512, "%ld/%ld MB", free_mb, total_mb);
                htmltable << (libhtmlpp::HtmlTable::Row() << "Arbeitspeicher: " << ab);
            }
#elifdef Windows
            MEMORYSTATUSEX status;
            status.dwLength = sizeof(status);

            // Call the Windows API to get memory status
            if (GlobalMemoryStatusEx(&status)) {
                    char ab[512];

                    // Convert bytes to MB (1024 * 1024 = 1048576)
                    // dwTotalPhys is total physical RAM in bytes
                    long total_mb = status.ullTotalPhys / 1048576;
                    // ullAvailPhys is available physical RAM in bytes
                    long free_mb = status.ullAvailPhys / 1048576;

                    snprintf(ab, 512, "%ld/%ld MB", free_mb, total_mb);
                    htmltable << (libhtmlpp::HtmlTable::Row() << "Arbeitspeicher: " << ab);
            }
#else // Fallback for generic Unix/Unknown
            // Use the generic POSIX sysconf if Linux/FreeBSD aren't defined,
            // though this might fail as seen previously.

            long psize;
            if((psize=sysconf(_SC_PAGESIZE))>0){
                char ab[512];
                // Note: _SC_AVPHYS_PAGES may still be undeclared on some systems
                snprintf(ab,512,"%ld/%ld MB",(sysconf(_SC_AVPHYS_PAGES)*psize)/(1048576),((sysconf(_SC_PHYS_PAGES)*psize)/1048576));
                htmltable << (libhtmlpp::HtmlTable::Row() << "Arbeitspeicher: " <<  ab);
            }
#endif
#endif // Linux/FreeBSD/Windows/else

            libhtmlpp::HtmlElement table;