Updated Branches: refs/heads/master 8f75a092f -> 6fd9e1744
TS-2350: Enhancements to traffic_top Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/6fd9e174 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/6fd9e174 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/6fd9e174 Branch: refs/heads/master Commit: 6fd9e174476bf8e4f7cf925df6604cb417afcbb7 Parents: 8f75a09 Author: Bryan Call <[email protected]> Authored: Thu Nov 14 15:59:18 2013 -0800 Committer: Bryan Call <[email protected]> Committed: Thu Nov 14 15:59:18 2013 -0800 ---------------------------------------------------------------------- CHANGES | 1 + cmd/traffic_top/stats.h | 90 ++++++++-- cmd/traffic_top/traffic_top.cc | 337 +++++++++++++++++++++++------------- 3 files changed, 291 insertions(+), 137 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/6fd9e174/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index 56b3d62..1d39202 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,7 @@ -*- coding: utf-8 -*- Changes with Apache Traffic Server 4.2.0 + *) [TS-2350] Enhancements to traffic_top *) [TS-2348] Rename tstop to traffic_top. http://git-wip-us.apache.org/repos/asf/trafficserver/blob/6fd9e174/cmd/traffic_top/stats.h ---------------------------------------------------------------------- diff --git a/cmd/traffic_top/stats.h b/cmd/traffic_top/stats.h index 35295c8..87e27a2 100644 --- a/cmd/traffic_top/stats.h +++ b/cmd/traffic_top/stats.h @@ -54,11 +54,33 @@ namespace constant { const char end[] = "\",\n"; }; + //---------------------------------------------------------------------------- class Stats { public: - Stats(const string &host) { - _host = host; + Stats(const string &url): _url(url) { + + if (url != "") { + if (_url.substr(0, 4) != "http") { + // looks like it is a host using it the old way + _url = "http://" + _url + "/_stats"; + } + + // set the host + int start = _url.find(":"); + int end = _url.find("/", start + 3); + _host = _url.substr(start + 3, end - start - 3); + end = _host.find(":"); + if (end != string::npos) { + _host = _host.substr(0, end); + } + } else { + char hostname[25]; + hostname[sizeof(hostname) - 1] = '\0'; + gethostname(hostname, sizeof(hostname) - 1); + _host = hostname; + } + _stats = NULL; _old_stats = NULL; _absolute = false; @@ -130,18 +152,51 @@ public: lookup_table.insert(make_pair("get", LookupItem("GET", "proxy.process.http.get_requests", 5))); lookup_table.insert(make_pair("head", LookupItem("HEAD", "proxy.process.http.head_requests", 5))); lookup_table.insert(make_pair("post", LookupItem("POST", "proxy.process.http.post_requests", 5))); - lookup_table.insert(make_pair("2xx", LookupItem("2xx", "proxy.process.http.2xx_responses", 5))); - lookup_table.insert(make_pair("3xx", LookupItem("3xx", "proxy.process.http.3xx_responses", 5))); - lookup_table.insert(make_pair("4xx", LookupItem("4xx", "proxy.process.http.4xx_responses", 5))); - lookup_table.insert(make_pair("5xx", LookupItem("5xx", "proxy.process.http.5xx_responses", 5))); + lookup_table.insert(make_pair("100", LookupItem("100", "proxy.process.http.100_responses", 5))); + lookup_table.insert(make_pair("101", LookupItem("101", "proxy.process.http.101_responses", 5))); + lookup_table.insert(make_pair("1xx", LookupItem("1xx", "proxy.process.http.1xx_responses", 5))); lookup_table.insert(make_pair("200", LookupItem("200", "proxy.process.http.200_responses", 5))); + lookup_table.insert(make_pair("201", LookupItem("201", "proxy.process.http.201_responses", 5))); + lookup_table.insert(make_pair("202", LookupItem("202", "proxy.process.http.202_responses", 5))); + lookup_table.insert(make_pair("203", LookupItem("203", "proxy.process.http.203_responses", 5))); + lookup_table.insert(make_pair("204", LookupItem("204", "proxy.process.http.204_responses", 5))); + lookup_table.insert(make_pair("205", LookupItem("205", "proxy.process.http.205_responses", 5))); lookup_table.insert(make_pair("206", LookupItem("206", "proxy.process.http.206_responses", 5))); + lookup_table.insert(make_pair("2xx", LookupItem("2xx", "proxy.process.http.2xx_responses", 5))); + lookup_table.insert(make_pair("300", LookupItem("300", "proxy.process.http.300_responses", 5))); lookup_table.insert(make_pair("301", LookupItem("301", "proxy.process.http.301_responses", 5))); lookup_table.insert(make_pair("302", LookupItem("302", "proxy.process.http.302_responses", 5))); + lookup_table.insert(make_pair("303", LookupItem("303", "proxy.process.http.303_responses", 5))); lookup_table.insert(make_pair("304", LookupItem("304", "proxy.process.http.304_responses", 5))); + lookup_table.insert(make_pair("305", LookupItem("305", "proxy.process.http.305_responses", 5))); + lookup_table.insert(make_pair("307", LookupItem("307", "proxy.process.http.307_responses", 5))); + lookup_table.insert(make_pair("3xx", LookupItem("3xx", "proxy.process.http.3xx_responses", 5))); + lookup_table.insert(make_pair("400", LookupItem("400", "proxy.process.http.400_responses", 5))); + lookup_table.insert(make_pair("401", LookupItem("401", "proxy.process.http.401_responses", 5))); + lookup_table.insert(make_pair("402", LookupItem("402", "proxy.process.http.402_responses", 5))); + lookup_table.insert(make_pair("403", LookupItem("403", "proxy.process.http.403_responses", 5))); lookup_table.insert(make_pair("404", LookupItem("404", "proxy.process.http.404_responses", 5))); + lookup_table.insert(make_pair("405", LookupItem("405", "proxy.process.http.405_responses", 5))); + lookup_table.insert(make_pair("406", LookupItem("406", "proxy.process.http.406_responses", 5))); + lookup_table.insert(make_pair("407", LookupItem("407", "proxy.process.http.407_responses", 5))); + lookup_table.insert(make_pair("408", LookupItem("408", "proxy.process.http.408_responses", 5))); + lookup_table.insert(make_pair("409", LookupItem("409", "proxy.process.http.409_responses", 5))); + lookup_table.insert(make_pair("410", LookupItem("410", "proxy.process.http.410_responses", 5))); + lookup_table.insert(make_pair("411", LookupItem("411", "proxy.process.http.411_responses", 5))); + lookup_table.insert(make_pair("412", LookupItem("412", "proxy.process.http.412_responses", 5))); + lookup_table.insert(make_pair("413", LookupItem("413", "proxy.process.http.413_responses", 5))); + lookup_table.insert(make_pair("414", LookupItem("414", "proxy.process.http.414_responses", 5))); + lookup_table.insert(make_pair("415", LookupItem("415", "proxy.process.http.415_responses", 5))); + lookup_table.insert(make_pair("416", LookupItem("416", "proxy.process.http.416_responses", 5))); + lookup_table.insert(make_pair("4xx", LookupItem("4xx", "proxy.process.http.4xx_responses", 5))); + lookup_table.insert(make_pair("500", LookupItem("500", "proxy.process.http.500_responses", 5))); + lookup_table.insert(make_pair("501", LookupItem("501", "proxy.process.http.501_responses", 5))); lookup_table.insert(make_pair("502", LookupItem("502", "proxy.process.http.502_responses", 5))); + lookup_table.insert(make_pair("503", LookupItem("503", "proxy.process.http.503_responses", 5))); + lookup_table.insert(make_pair("504", LookupItem("504", "proxy.process.http.504_responses", 5))); + lookup_table.insert(make_pair("505", LookupItem("505", "proxy.process.http.505_responses", 5))); + lookup_table.insert(make_pair("5xx", LookupItem("5xx", "proxy.process.http.5xx_responses", 5))); lookup_table.insert(make_pair("s_100", LookupItem("100 B", "proxy.process.http.response_document_size_100", 5))); lookup_table.insert(make_pair("s_1k", LookupItem("1 KB", "proxy.process.http.response_document_size_1K", 5))); @@ -169,7 +224,7 @@ public: void getStats() { - if (_host == "") { + if (_url == "") { int64_t value = 0; if (_old_stats != NULL) { delete _old_stats; @@ -187,7 +242,7 @@ public: if (item.type == 1 || item.type == 2 || item.type == 5 || item.type == 8) { if (strcmp(item.pretty, "Version") == 0) { - // special case for Version informaion + // special case for Version information TSString strValue = NULL; assert(TSRecordGetString(item.name, &strValue) == TS_ERR_OKAY); string key = item.name; @@ -211,8 +266,7 @@ public: curl = curl_easy_init(); if (curl) { - string url = "http://" + _host + "/_stats"; - curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl, CURLOPT_URL, _url.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_error); @@ -238,6 +292,9 @@ public: _old_time = _now; _now = now; _time_diff = _now - _old_time; + } else { + fprintf(stderr, "Can't fetch url %s", _url.c_str()); + abort(); } /* always cleanup */ @@ -248,7 +305,9 @@ public: int64_t getValue(const string &key, const map<string, string> *stats) const { map<string, string>::const_iterator stats_it = stats->find(key); - assert(stats_it != stats->end()); + printf("trying to find %s\n", key.c_str()); + if (stats_it == stats->end()) + return 0; int64_t value = atoll(stats_it->second.c_str()); return value; } @@ -266,8 +325,10 @@ public: const LookupItem &item = lookup_it->second; map<string, string>::const_iterator stats_it = _stats->find(item.name); - assert(stats_it != _stats->end()); - value = stats_it->second.c_str(); + if (stats_it == _stats->end()) + value = ""; + else + value = stats_it->second.c_str(); } void getStat(const string &key, double &value, string &prettyName, int &type, int overrideType = 0) { @@ -371,6 +432,8 @@ public: } } + const string& getHost() const { return _host; } + ~Stats() { if (_stats != NULL) { delete _stats; @@ -384,6 +447,7 @@ private: map<string, string> *_stats; map<string, string> *_old_stats; map<string, LookupItem> lookup_table; + string _url; string _host; double _old_time; double _now; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/6fd9e174/cmd/traffic_top/traffic_top.cc ---------------------------------------------------------------------- diff --git a/cmd/traffic_top/traffic_top.cc b/cmd/traffic_top/traffic_top.cc index a801520..4e4b66e 100644 --- a/cmd/traffic_top/traffic_top.cc +++ b/cmd/traffic_top/traffic_top.cc @@ -143,8 +143,69 @@ size_t write_data(void *ptr, size_t size, size_t nmemb, void * /* stream */) } //---------------------------------------------------------------------------- -static void help(const string &host, const string &version) { +static void response_code_page(Stats &stats) { + attron(COLOR_PAIR(colorPair::border)); + attron(A_BOLD); + mvprintw(0, 0, " RESPONSE CODES "); + attroff(COLOR_PAIR(colorPair::border)); + attroff(A_BOLD); + + list<string> response1; + response1.push_back("100"); + response1.push_back("101"); + response1.push_back("1xx"); + response1.push_back("200"); + response1.push_back("201"); + response1.push_back("202"); + response1.push_back("203"); + response1.push_back("204"); + response1.push_back("205"); + response1.push_back("206"); + response1.push_back("2xx"); + response1.push_back("300"); + response1.push_back("301"); + response1.push_back("302"); + response1.push_back("303"); + response1.push_back("304"); + response1.push_back("305"); + response1.push_back("307"); + response1.push_back("3xx"); + makeTable(0, 1, response1, stats); + + list<string> response2; + response2.push_back("400"); + response2.push_back("401"); + response2.push_back("402"); + response2.push_back("403"); + response2.push_back("404"); + response2.push_back("405"); + response2.push_back("406"); + response2.push_back("407"); + response2.push_back("408"); + response2.push_back("409"); + response2.push_back("410"); + response2.push_back("411"); + response2.push_back("412"); + response2.push_back("413"); + response2.push_back("414"); + response2.push_back("415"); + response2.push_back("416"); + response2.push_back("4xx"); + makeTable(21, 1, response2, stats); + + list<string> response3; + response3.push_back("500"); + response3.push_back("501"); + response3.push_back("502"); + response3.push_back("503"); + response3.push_back("504"); + response3.push_back("505"); + response3.push_back("5xx"); + makeTable(42, 1, response3, stats); +} +//---------------------------------------------------------------------------- +static void help(const string &host, const string &version) { timeout(1000); while(1) { @@ -184,11 +245,124 @@ static void help(const string &host, const string &version) { static void usage() { - fprintf(stderr, "Usage: traffic_top [-s seconds] hostname|hostname:port\n"); + fprintf(stderr, "Usage: traffic_top [-s seconds] [URL|hostname|hostname:port]\n"); exit(1); } //---------------------------------------------------------------------------- +void main_stats_page(Stats &stats) +{ + attron(COLOR_PAIR(colorPair::border)); + attron(A_BOLD); + mvprintw(0, 0, " CACHE INFORMATION "); + mvprintw(0, 40, " CLIENT REQUEST & RESPONSE "); + mvprintw(16, 0, " CLIENT "); + mvprintw(16, 40, " ORIGIN SERVER "); + + for (int i = 0; i <= 22; ++i) { + mvprintw(i, 39, " "); + } + attroff(COLOR_PAIR(colorPair::border)); + attroff(A_BOLD); + + list<string> cache1; + cache1.push_back("disk_used"); + cache1.push_back("disk_total"); + cache1.push_back("ram_used"); + cache1.push_back("ram_total"); + cache1.push_back("lookups"); + cache1.push_back("cache_writes"); + cache1.push_back("cache_updates"); + cache1.push_back("cache_deletes"); + cache1.push_back("read_active"); + cache1.push_back("write_active"); + cache1.push_back("update_active"); + cache1.push_back("entries"); + cache1.push_back("avg_size"); + cache1.push_back("dns_lookups"); + cache1.push_back("dns_hits"); + makeTable(0, 1, cache1, stats); + + list<string> cache2; + cache2.push_back("ram_ratio"); + cache2.push_back("fresh"); + cache2.push_back("reval"); + cache2.push_back("cold"); + cache2.push_back("changed"); + cache2.push_back("not"); + cache2.push_back("no"); + cache2.push_back("fresh_time"); + cache2.push_back("reval_time"); + cache2.push_back("cold_time"); + cache2.push_back("changed_time"); + cache2.push_back("not_time"); + cache2.push_back("no_time"); + cache2.push_back("dns_ratio"); + cache2.push_back("dns_time"); + makeTable(21, 1, cache2, stats); + + list<string> response1; + response1.push_back("get"); + response1.push_back("head"); + response1.push_back("post"); + response1.push_back("2xx"); + response1.push_back("3xx"); + response1.push_back("4xx"); + response1.push_back("5xx"); + response1.push_back("conn_fail"); + response1.push_back("other_err"); + response1.push_back("abort"); + makeTable(41, 1, response1, stats); + + list<string> response2; + response2.push_back("200"); + response2.push_back("206"); + response2.push_back("301"); + response2.push_back("302"); + response2.push_back("304"); + response2.push_back("404"); + response2.push_back("502"); + response2.push_back("s_100"); + response2.push_back("s_1k"); + response2.push_back("s_3k"); + response2.push_back("s_5k"); + response2.push_back("s_10k"); + response2.push_back("s_1m"); + response2.push_back("s_>1m"); + makeTable(62, 1, response2, stats); + + list<string> client1; + client1.push_back("client_req"); + client1.push_back("client_req_conn"); + client1.push_back("client_conn"); + client1.push_back("client_curr_conn"); + client1.push_back("client_actv_conn"); + makeTable(0, 17, client1, stats); + + list<string> client2; + client2.push_back("client_head"); + client2.push_back("client_body"); + client2.push_back("client_avg_size"); + client2.push_back("client_net"); + client2.push_back("client_req_time"); + makeTable(21, 17, client2, stats); + + list<string> server1; + server1.push_back("server_req"); + server1.push_back("server_req_conn"); + server1.push_back("server_conn"); + server1.push_back("server_curr_conn"); + makeTable(41, 17, server1, stats); + + list<string> server2; + server2.push_back("server_head"); + server2.push_back("server_body"); + server2.push_back("server_avg_size"); + server2.push_back("server_net"); + makeTable(62, 17, server2, stats); +} + +//---------------------------------------------------------------------------- int main(int argc, char **argv) { int sleep_time = 5000; @@ -204,23 +378,18 @@ int main(int argc, char **argv) } } - string host = ""; + string url = ""; if (optind >= argc) { if (TS_ERR_OKAY != TSInit(NULL, static_cast<TSInitOptionT>(TS_MGMT_OPT_NO_EVENTS | TS_MGMT_OPT_NO_SOCK_TESTS))) { - fprintf(stderr, "Error: missing hostname on command line or error connecting to the local manager\n"); + fprintf(stderr, "Error: missing URL on command line or error connecting to the local manager\n"); usage(); } } else { - host = argv[optind]; - } - Stats stats(host); - - if (host == "") { - char hostname[25]; - hostname[sizeof(hostname) - 1] = '\0'; - gethostname(hostname, sizeof(hostname) - 1); - host = hostname; + url = argv[optind]; } + Stats stats(url); + stats.getStats(); + const string &host = stats.getHost(); initscr(); curs_set(0); @@ -236,140 +405,60 @@ int main(int argc, char **argv) init_pair(colorPair::border, COLOR_WHITE, COLOR_BLUE); // mvchgat(0, 0, -1, A_BLINK, 1, NULL); - stats.getStats(); + + enum Page {MAIN_PAGE, RESPONSE_PAGE}; + Page page = MAIN_PAGE; + string page_alt = "(r)esponse"; + while(1) { attron(COLOR_PAIR(colorPair::border)); attron(A_BOLD); - for (int i = 0; i <= 22; ++i) { - mvprintw(i, 39, " "); - } + string version; time_t now = time(NULL); struct tm *nowtm = localtime(&now); char timeBuf[32]; strftime(timeBuf, sizeof(timeBuf), "%H:%M:%S", nowtm); stats.getStat("version", version); - mvprintw(0, 0, " CACHE INFORMATION "); - mvprintw(0, 40, " CLIENT REQUEST & RESPONSE "); - mvprintw(16, 0, " CLIENT "); - mvprintw(16, 40, " ORIGIN SERVER "); - mvprintw(23, 0, "%8.8s - %-10.10s - %-24.24s (q)uit (h)elp (%c)bsolute ", timeBuf, version.c_str(), host.c_str(), absolute ? 'A' : 'a'); + + mvprintw(23, 0, "%-20.20s %30s (q)uit (h)elp (%c)bsolute ", host.c_str(), page_alt.c_str(), absolute ? 'A' : 'a'); attroff(COLOR_PAIR(colorPair::border)); attroff(A_BOLD); - list<string> cache1; - cache1.push_back("disk_used"); - cache1.push_back("disk_total"); - cache1.push_back("ram_used"); - cache1.push_back("ram_total"); - cache1.push_back("lookups"); - cache1.push_back("cache_writes"); - cache1.push_back("cache_updates"); - cache1.push_back("cache_deletes"); - cache1.push_back("read_active"); - cache1.push_back("write_active"); - cache1.push_back("update_active"); - cache1.push_back("entries"); - cache1.push_back("avg_size"); - cache1.push_back("dns_lookups"); - cache1.push_back("dns_hits"); - makeTable(0, 1, cache1, stats); - - list<string> cache2; - cache2.push_back("ram_ratio"); - cache2.push_back("fresh"); - cache2.push_back("reval"); - cache2.push_back("cold"); - cache2.push_back("changed"); - cache2.push_back("not"); - cache2.push_back("no"); - cache2.push_back("fresh_time"); - cache2.push_back("reval_time"); - cache2.push_back("cold_time"); - cache2.push_back("changed_time"); - cache2.push_back("not_time"); - cache2.push_back("no_time"); - cache2.push_back("dns_ratio"); - cache2.push_back("dns_time"); - makeTable(21, 1, cache2, stats); - - list<string> response1; - response1.push_back("get"); - response1.push_back("head"); - response1.push_back("post"); - response1.push_back("2xx"); - response1.push_back("3xx"); - response1.push_back("4xx"); - response1.push_back("5xx"); - response1.push_back("conn_fail"); - response1.push_back("other_err"); - response1.push_back("abort"); - makeTable(41, 1, response1, stats); - - list<string> response2; - response2.push_back("200"); - response2.push_back("206"); - response2.push_back("301"); - response2.push_back("302"); - response2.push_back("304"); - response2.push_back("404"); - response2.push_back("502"); - response2.push_back("s_100"); - response2.push_back("s_1k"); - response2.push_back("s_3k"); - response2.push_back("s_5k"); - response2.push_back("s_10k"); - response2.push_back("s_1m"); - response2.push_back("s_>1m"); - makeTable(62, 1, response2, stats); - - list<string> client1; - client1.push_back("client_req"); - client1.push_back("client_req_conn"); - client1.push_back("client_conn"); - client1.push_back("client_curr_conn"); - client1.push_back("client_actv_conn"); - makeTable(0, 17, client1, stats); - - list<string> client2; - client2.push_back("client_head"); - client2.push_back("client_body"); - client2.push_back("client_avg_size"); - client2.push_back("client_net"); - client2.push_back("client_req_time"); - makeTable(21, 17, client2, stats); - - list<string> server1; - server1.push_back("server_req"); - server1.push_back("server_req_conn"); - server1.push_back("server_conn"); - server1.push_back("server_curr_conn"); - makeTable(41, 17, server1, stats); - - list<string> server2; - server2.push_back("server_head"); - server2.push_back("server_body"); - server2.push_back("server_avg_size"); - server2.push_back("server_net"); - makeTable(62, 17, server2, stats); + + if (page == MAIN_PAGE) { + main_stats_page(stats); + } else if (page == RESPONSE_PAGE) { + response_code_page(stats); + } curs_set(0); refresh(); timeout(sleep_time); int x = getch(); - if (x == 'q') - break; - if (x == 'h') { + switch (x) { + case 'h': help(host, version); - } - if (x == 'a') { + break; + case 'q': + goto quit; + case 'm': + page = MAIN_PAGE; + page_alt = "(r)esponse"; + break; + case 'r': + page = RESPONSE_PAGE; + page_alt = "(m)ain"; + break; + case 'a': absolute = stats.toggleAbsolute(); } - if (x == -1) - stats.getStats(); + stats.getStats(); clear(); } + +quit: endwin(); return 0;
