From eb5b992965ae701ff75564efe98d9550fb179d77 Mon Sep 17 00:00:00 2001 From: Shuo Chen Date: Sat, 22 Mar 2014 15:45:21 -0700 Subject: [PATCH] add SystemInspector::overview() --- muduo/base/Timestamp.cc | 21 +++++--- muduo/base/Timestamp.h | 2 +- muduo/net/inspect/ProcessInspector.cc | 27 +++++++--- muduo/net/inspect/SystemInspector.cc | 72 ++++++++++++++++++++++++++- muduo/net/protobuf/CMakeLists.txt | 4 +- 5 files changed, 108 insertions(+), 18 deletions(-) diff --git a/muduo/base/Timestamp.cc b/muduo/base/Timestamp.cc index 76a06757f..4f80570e3 100644 --- a/muduo/base/Timestamp.cc +++ b/muduo/base/Timestamp.cc @@ -26,18 +26,27 @@ string Timestamp::toString() const return buf; } -string Timestamp::toFormattedString() const +string Timestamp::toFormattedString(bool showMicroseconds) const { char buf[32] = {0}; time_t seconds = static_cast(microSecondsSinceEpoch_ / kMicroSecondsPerSecond); - int microseconds = static_cast(microSecondsSinceEpoch_ % kMicroSecondsPerSecond); struct tm tm_time; gmtime_r(&seconds, &tm_time); - snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d", - tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday, - tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, - microseconds); + if (showMicroseconds) + { + int microseconds = static_cast(microSecondsSinceEpoch_ % kMicroSecondsPerSecond); + snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d.%06d", + tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday, + tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, + microseconds); + } + else + { + snprintf(buf, sizeof(buf), "%4d%02d%02d %02d:%02d:%02d", + tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday, + tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec); + } return buf; } diff --git a/muduo/base/Timestamp.h b/muduo/base/Timestamp.h index 4c936b097..d5c75917a 100644 --- a/muduo/base/Timestamp.h +++ b/muduo/base/Timestamp.h @@ -41,7 +41,7 @@ class Timestamp : public muduo::copyable, // default copy/assignment/dtor are Okay string toString() const; - string toFormattedString() const; + string toFormattedString(bool showMicroseconds = true) const; bool valid() const { return microSecondsSinceEpoch_ > 0; } diff --git a/muduo/net/inspect/ProcessInspector.cc b/muduo/net/inspect/ProcessInspector.cc index 107d7bca6..08c02cb44 100644 --- a/muduo/net/inspect/ProcessInspector.cc +++ b/muduo/net/inspect/ProcessInspector.cc @@ -18,20 +18,30 @@ using namespace muduo; using namespace muduo::net; -namespace +namespace muduo +{ +namespace inspect { -string uptime(Timestamp now) +string uptime(Timestamp now, Timestamp start, bool showMicroseconds) { char buf[256]; - int64_t age = now.microSecondsSinceEpoch() - ProcessInfo::startTime().microSecondsSinceEpoch(); - int microseconds = static_cast(age % Timestamp::kMicroSecondsPerSecond); + int64_t age = now.microSecondsSinceEpoch() - start.microSecondsSinceEpoch(); int seconds = static_cast(age / Timestamp::kMicroSecondsPerSecond); int days = seconds/86400; int hours = (seconds % 86400) / 3600; int minutes = (seconds % 3600) / 60; - snprintf(buf, sizeof buf, "%d days %02d:%02d:%02d.%06d", - days, hours, minutes, seconds % 60, microseconds); + if (showMicroseconds) + { + int microseconds = static_cast(age % Timestamp::kMicroSecondsPerSecond); + snprintf(buf, sizeof buf, "%d days %02d:%02d:%02d.%06d", + days, hours, minutes, seconds % 60, microseconds); + } + else + { + snprintf(buf, sizeof buf, "%d days %02d:%02d:%02d", + days, hours, minutes, seconds % 60); + } return buf; } @@ -105,6 +115,9 @@ int stringPrintf(string* out, const char* fmt, ...) } } +} + +using namespace muduo::inspect; string ProcessInspector::username_ = ProcessInfo::username(); @@ -127,7 +140,7 @@ string ProcessInspector::overview(HttpRequest::Method, const Inspector::ArgList& result += " (UTC)\nStarted at "; result += ProcessInfo::startTime().toFormattedString(); result += " (UTC), up for "; - result += uptime(now); + result += uptime(now, ProcessInfo::startTime(), true/* show microseconds */); result += "\n"; string procStatus = ProcessInfo::procStatus(); diff --git a/muduo/net/inspect/SystemInspector.cc b/muduo/net/inspect/SystemInspector.cc index cb7de5fa0..6889e1efb 100644 --- a/muduo/net/inspect/SystemInspector.cc +++ b/muduo/net/inspect/SystemInspector.cc @@ -10,9 +10,25 @@ #include #include +#include + using namespace muduo; using namespace muduo::net; +namespace muduo +{ +namespace inspect +{ + +string uptime(Timestamp now, Timestamp start, bool showMicroseconds); +long getLong(const string& content, const char* key); +int stringPrintf(string* out, const char* fmt, ...) __attribute__ ((format (printf, 2, 3))); + +} +} + +using namespace muduo::inspect; + void SystemInspector::registerCommands(Inspector* ins) { ins->add("sys", "overview", SystemInspector::overview, "print system overview"); @@ -60,5 +76,59 @@ string SystemInspector::stat(HttpRequest::Method, const Inspector::ArgList&) string SystemInspector::overview(HttpRequest::Method, const Inspector::ArgList&) { - return "Not implemented."; + string result; + result.reserve(1024); + Timestamp now = Timestamp::now(); + result += "Page generated at "; + result += now.toFormattedString(); + result += " (UTC)\n"; + // Hardware and OS + { + struct utsname un; + if (::uname(&un) == 0) + { + stringPrintf(&result, "Hostname: %s\n", un.nodename); + stringPrintf(&result, "Machine: %s\n", un.machine); + stringPrintf(&result, "OS: %s %s %s\n", un.sysname, un.release, un.version); + } + } + string stat; + FileUtil::readFile("/proc/stat", 65536, &stat); + Timestamp bootTime(Timestamp::kMicroSecondsPerSecond * getLong(stat, "btime ")); + result += "Boot time: "; + result += bootTime.toFormattedString(false /* show microseconds */); + result += " (UTC)\n"; + result += "Up time: "; + result += uptime(now, bootTime, false /* show microseconds */); + result += "\n"; + + // CPU load + { + string loadavg; + FileUtil::readFile("/proc/loadavg", 65536, &loadavg); + stringPrintf(&result, "Processes created: %ld\n", getLong(stat, "processes ")); + stringPrintf(&result, "Loadavg: %s\n", loadavg.c_str()); + } + + // Memory + { + string meminfo; + FileUtil::readFile("/proc/meminfo", 65536, &meminfo); + long total_kb = getLong(meminfo, "MemTotal:"); + long free_kb = getLong(meminfo, "MemFree:"); + long buffers_kb = getLong(meminfo, "Buffers:"); + long cached_kb = getLong(meminfo, "Cached:"); + + stringPrintf(&result, "Total Memory: %6ld MiB\n", total_kb / 1024); + stringPrintf(&result, "Free Memory: %6ld MiB\n", free_kb / 1024); + stringPrintf(&result, "Buffers: %6ld MiB\n", buffers_kb / 1024); + stringPrintf(&result, "Cached: %6ld MiB\n", cached_kb / 1024); + stringPrintf(&result, "Real Used: %6ld MiB\n", (total_kb - free_kb - buffers_kb - cached_kb) / 1024); + stringPrintf(&result, "Real Free: %6ld MiB\n", (free_kb + buffers_kb + cached_kb) / 1024); + + // Swap + } + // Disk + // Network + return result; } diff --git a/muduo/net/protobuf/CMakeLists.txt b/muduo/net/protobuf/CMakeLists.txt index 9b9801bce..34dde3b84 100644 --- a/muduo/net/protobuf/CMakeLists.txt +++ b/muduo/net/protobuf/CMakeLists.txt @@ -10,8 +10,6 @@ target_link_libraries(muduo_protobuf_codec_cpp11 muduo_net_cpp11 protobuf z) install(TARGETS muduo_protobuf_codec DESTINATION lib) install(TARGETS muduo_protobuf_codec_cpp11 DESTINATION lib) -set(HEADERS - ProtobufCodecLite.h - ) +file(GLOB HEADERS "*.h") install(FILES ${HEADERS} DESTINATION include/muduo/net/protobuf)