|
10 | 10 | #include <muduo/net/inspect/SystemInspector.h> |
11 | 11 | #include <muduo/base/FileUtil.h> |
12 | 12 |
|
| 13 | +#include <sys/utsname.h> |
| 14 | + |
13 | 15 | using namespace muduo; |
14 | 16 | using namespace muduo::net; |
15 | 17 |
|
| 18 | +namespace muduo |
| 19 | +{ |
| 20 | +namespace inspect |
| 21 | +{ |
| 22 | + |
| 23 | +string uptime(Timestamp now, Timestamp start, bool showMicroseconds); |
| 24 | +long getLong(const string& content, const char* key); |
| 25 | +int stringPrintf(string* out, const char* fmt, ...) __attribute__ ((format (printf, 2, 3))); |
| 26 | + |
| 27 | +} |
| 28 | +} |
| 29 | + |
| 30 | +using namespace muduo::inspect; |
| 31 | + |
16 | 32 | void SystemInspector::registerCommands(Inspector* ins) |
17 | 33 | { |
18 | 34 | ins->add("sys", "overview", SystemInspector::overview, "print system overview"); |
@@ -60,5 +76,59 @@ string SystemInspector::stat(HttpRequest::Method, const Inspector::ArgList&) |
60 | 76 |
|
61 | 77 | string SystemInspector::overview(HttpRequest::Method, const Inspector::ArgList&) |
62 | 78 | { |
63 | | - return "Not implemented."; |
| 79 | + string result; |
| 80 | + result.reserve(1024); |
| 81 | + Timestamp now = Timestamp::now(); |
| 82 | + result += "Page generated at "; |
| 83 | + result += now.toFormattedString(); |
| 84 | + result += " (UTC)\n"; |
| 85 | + // Hardware and OS |
| 86 | + { |
| 87 | + struct utsname un; |
| 88 | + if (::uname(&un) == 0) |
| 89 | + { |
| 90 | + stringPrintf(&result, "Hostname: %s\n", un.nodename); |
| 91 | + stringPrintf(&result, "Machine: %s\n", un.machine); |
| 92 | + stringPrintf(&result, "OS: %s %s %s\n", un.sysname, un.release, un.version); |
| 93 | + } |
| 94 | + } |
| 95 | + string stat; |
| 96 | + FileUtil::readFile("/proc/stat", 65536, &stat); |
| 97 | + Timestamp bootTime(Timestamp::kMicroSecondsPerSecond * getLong(stat, "btime ")); |
| 98 | + result += "Boot time: "; |
| 99 | + result += bootTime.toFormattedString(false /* show microseconds */); |
| 100 | + result += " (UTC)\n"; |
| 101 | + result += "Up time: "; |
| 102 | + result += uptime(now, bootTime, false /* show microseconds */); |
| 103 | + result += "\n"; |
| 104 | + |
| 105 | + // CPU load |
| 106 | + { |
| 107 | + string loadavg; |
| 108 | + FileUtil::readFile("/proc/loadavg", 65536, &loadavg); |
| 109 | + stringPrintf(&result, "Processes created: %ld\n", getLong(stat, "processes ")); |
| 110 | + stringPrintf(&result, "Loadavg: %s\n", loadavg.c_str()); |
| 111 | + } |
| 112 | + |
| 113 | + // Memory |
| 114 | + { |
| 115 | + string meminfo; |
| 116 | + FileUtil::readFile("/proc/meminfo", 65536, &meminfo); |
| 117 | + long total_kb = getLong(meminfo, "MemTotal:"); |
| 118 | + long free_kb = getLong(meminfo, "MemFree:"); |
| 119 | + long buffers_kb = getLong(meminfo, "Buffers:"); |
| 120 | + long cached_kb = getLong(meminfo, "Cached:"); |
| 121 | + |
| 122 | + stringPrintf(&result, "Total Memory: %6ld MiB\n", total_kb / 1024); |
| 123 | + stringPrintf(&result, "Free Memory: %6ld MiB\n", free_kb / 1024); |
| 124 | + stringPrintf(&result, "Buffers: %6ld MiB\n", buffers_kb / 1024); |
| 125 | + stringPrintf(&result, "Cached: %6ld MiB\n", cached_kb / 1024); |
| 126 | + stringPrintf(&result, "Real Used: %6ld MiB\n", (total_kb - free_kb - buffers_kb - cached_kb) / 1024); |
| 127 | + stringPrintf(&result, "Real Free: %6ld MiB\n", (free_kb + buffers_kb + cached_kb) / 1024); |
| 128 | + |
| 129 | + // Swap |
| 130 | + } |
| 131 | + // Disk |
| 132 | + // Network |
| 133 | + return result; |
64 | 134 | } |
0 commit comments