{"id":817,"date":"2020-08-01T12:38:59","date_gmt":"2020-08-01T19:38:59","guid":{"rendered":"https:\/\/finaldie.com\/blog\/?p=817"},"modified":"2020-12-07T21:21:00","modified_gmt":"2020-12-08T04:21:00","slug":"tmux-network-rtt-detection","status":"publish","type":"post","link":"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/","title":{"rendered":"Tmux: Network RTT Detection"},"content":{"rendered":"<h2>1 Overview<\/h2>\n<p>I deal with multiple remote hosts everyday, sometimes, to better understand the current network situation clearly, I want to know the RTTs  (Round-Trip Time) between the multiple hops quickly without run any commands against the target hosts manually again and again, which is painful and low efficient.<\/p>\n<p>In this article, I&#8217;ll share my current setup, and walk through how to detect the network RTTs in the tmux sessions, then shows it in the status bar.<\/p>\n<p><strong>Screenshots:<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/finaldie.com\/blog\/wp-content\/uploads\/2020\/08\/tmux_status_bar3.png\"><\/p>\n<h3>1.1 Motivation<\/h3>\n<p>For my working environment, I will login to a dev host first (as a jump host), and from there, jump to other places like database, testing machines or production machines.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/finaldie.com\/blog\/wp-content\/uploads\/2020\/08\/image-1596304915647.png\" width=500><\/p>\n<p>Sometimes, I also login to the machines across different regions, but a normal case is, after login to a host, then I realized that the network lag is quite noticeable, since I&#8217;m sensitive to the network lag, I will create a new session and run the <code>ping<\/code> command there.<\/p>\n<p>But the process is painful and breaks my workflow, because I have to do the following steps:<\/p>\n<ol>\n<li>Create a new session<\/li>\n<li>Copy the <code>hostname<\/code> again<\/li>\n<li>Run the <code>ping<\/code> command<\/li>\n<li>Switch back<\/li>\n<\/ol>\n<p>So, what if I can list RTT value in the status bar directly without manually run any commands separately, that will make the process smoothly. So that I want to know the below items:<\/p>\n<ul>\n<li>What\u2019s the ping rtt between my computer to a lighthouse website (e.g. google.com)<\/li>\n<li>What\u2019s the ping rtt between my computer and the dev box<\/li>\n<li>What\u2019s the ping rtt between the dev box and the remote production\/database boxes<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/finaldie.com\/blog\/wp-content\/uploads\/2020\/08\/image-1596305004032.png\" width=600><\/p>\n<h2>2 How to Detect RTT in Tmux<\/h2>\n<h3>2.1 RTT Between Local to Lighthouse Server<\/h3>\n<p>This one is quite simple, a ping command will help us:<\/p>\n<pre><code>ping www.google.com -c 1 | tail -1 | cut -d \"\/\" -f5\n<\/code><\/pre>\n<h3>2.2 RTT Between Local to Remote Server<\/h3>\n<p>For this one, we have to figure out two problems:<\/p>\n<ol>\n<li>Which host we connected in the current tmux session?<\/li>\n<li>What&#8217;s the ping RTT between local to the target host?<\/li>\n<\/ol>\n<p>The second one is the <code>ping<\/code> command in the previous section, and the only problem left is how to detect <code>which host we connected to<\/code>, then need to solve a few sub-problems:<\/p>\n<ol>\n<li>How to identify the current tmux session name?<\/li>\n<li>Which host we connected to in the current tmux session?<\/li>\n<\/ol>\n<p>For the first one, we can filter the process by the current tmux&#8217;s tty name:<\/p>\n<pre><code>tmux display -p #{pane_tty}\n<\/code><\/pre>\n<p>Now, for the second one, we will use above tty name to filter the <code>ssh process<\/code> and extract the hostname from the command, then use <code>ping<\/code> command to detect the RTT, so the workflow will be like this:<\/p>\n<pre><code>ping ${hostname} -c 1 | tail -1 | cut -d \"\/\" -f5\n\n${hostname} = ps -af | grep ${tty_name} | grep ${hostname_pattern}\n${hostname_pattern} = grep ssh | grep ${pattern}\n<\/code><\/pre>\n<p>Then we have:<\/p>\n<pre><code>ping -c 1 #(ps -af | grep \"<code>tmux display -p \\&quot;#{pane_tty}\\&quot; | cut -d \\&quot;\/\\&quot; -f3,4<\/code> \" | grep -v grep | grep -oP \"ssh [a-zA-Z0-9.@\\-]+\" | cut -d \" \" -f2 | grep -oP \"(?=@*)[\\w\\d.\\-]*\" | tail -1) | tail -1 | cut -d \"\/\" -f5\n<\/code><\/pre>\n<h2>3. Configuration<\/h2>\n<pre><code class=\"language-tmux\">set -g status-bg 'colour235'\nset -g status 'on'\n\nset -g status-left-length '25'\nset -g status-left '#(whoami) #[fg=colour121,bg=colour235] \uf1a0 #(ping www.google.com -c 1 | tail -1 | cut -d \"\/\" -f5)ms'\n\nset -g status-right-length '140'\nset -g status-right '#(ps -af | grep \"<code>tmux display -p \\&quot;#{pane_tty}\\&quot; | cut -d \\&quot;\/\\&quot; -f3,4<\/code> \" | grep -v grep | grep -oP \"ssh [a-zA-Z0-9.@\\-]+\" | cut -d \" \" -f2) #[fg=colour232,bg=colour150] &lt; #(ping -c 1 #(ps -af | grep \"<code>tmux display -p \\&quot;#{pane_tty}\\&quot; | cut -d \\&quot;\/\\&quot; -f3,4<\/code> \" | grep -v grep | grep -oP \"ssh [a-zA-Z0-9.@\\-]+\" | cut -d \" \" -f2 | grep -oP \"(?=@*)[\\w\\d.\\-]*\" | tail -1) | tail -1 | cut -d \"\/\" -f5)'\n\n<\/code><\/pre>\n<p>The full configuration please refer to <a href=\"https:\/\/github.com\/finaldie\/final_dev_env\/blob\/master\/tmux\/tmux.conf#L102-L106\">here<\/a>.<\/p>\n<h3>3.1 Tips for the MacOS<\/h3>\n<p>The above <code>grep hostname<\/code> pattern only works in Linux (with <code>GNU grep<\/code>), due to the <code>grep<\/code> program in the MacOS is <code>BSD grep<\/code> which has different behavior in certain cases. To better align with the Linux distributions and make it works in the MacOS, we can install the <code>GNU grep<\/code> in the MacOS:<\/p>\n<pre><code>brew install grep\n\nln -s \/usr\/local\/bin\/ggrep \/usr\/local\/bin\/grep\n<\/code><\/pre>\n<p>Then reload or restart the tmux server will fix the MacOS issue.<\/p>\n<h2>4. References<\/h2>\n<ul>\n<li><a href=\"https:\/\/github.com\/finaldie\/final_dev_env\/blob\/master\/tmux\/tmux.conf\">Tmux.conf<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1 Overview I deal with multiple remote hosts everyday, sometimes, to better understand the current network situation clearly, I want to know the RTTs (Round-Trip Time) between the multiple hops quickly without run any commands against the target hosts manually again and again, which is painful and low efficient. In this article, I&#8217;ll share my &#8230; <a title=\"Tmux: Network RTT Detection\" class=\"read-more\" href=\"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/\" aria-label=\"More on Tmux: Network RTT Detection\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":825,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55,2],"tags":[57,58,56],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Tmux: Network RTT Detection - Final Blog<\/title>\n<meta name=\"description\" content=\"Detect network rtt in the tmux status bar\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tmux: Network RTT Detection - Final Blog\" \/>\n<meta property=\"og:description\" content=\"Detect network rtt in the tmux status bar\" \/>\n<meta property=\"og:url\" content=\"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/\" \/>\n<meta property=\"og:site_name\" content=\"Final Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hu.yuzhang\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-01T19:38:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-08T04:21:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/finaldie.com\/blog\/wp-content\/uploads\/2020\/08\/tmux_status_bar4.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2862\" \/>\n\t<meta property=\"og:image:height\" content=\"412\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"final\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hyzwowtools\" \/>\n<meta name=\"twitter:site\" content=\"@hyzwowtools\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"final\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/\",\"url\":\"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/\",\"name\":\"Tmux: Network RTT Detection - Final Blog\",\"isPartOf\":{\"@id\":\"https:\/\/finaldie.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/finaldie.com\/blog\/wp-content\/uploads\/2020\/08\/tmux_status_bar4.png\",\"datePublished\":\"2020-08-01T19:38:59+00:00\",\"dateModified\":\"2020-12-08T04:21:00+00:00\",\"author\":{\"@id\":\"https:\/\/finaldie.com\/blog\/#\/schema\/person\/2d4c840d6e8e197f8ade98af2bd2fab3\"},\"description\":\"Detect network rtt in the tmux status bar\",\"breadcrumb\":{\"@id\":\"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/#primaryimage\",\"url\":\"https:\/\/finaldie.com\/blog\/wp-content\/uploads\/2020\/08\/tmux_status_bar4.png\",\"contentUrl\":\"https:\/\/finaldie.com\/blog\/wp-content\/uploads\/2020\/08\/tmux_status_bar4.png\",\"width\":2862,\"height\":412},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/finaldie.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tmux: Network RTT Detection\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/finaldie.com\/blog\/#website\",\"url\":\"https:\/\/finaldie.com\/blog\/\",\"name\":\"Final Blog\",\"description\":\"As simple as possible...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/finaldie.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/finaldie.com\/blog\/#\/schema\/person\/2d4c840d6e8e197f8ade98af2bd2fab3\",\"name\":\"final\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/finaldie.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4c720545b79ddb0f23b527e0bbcfd9bc?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4c720545b79ddb0f23b527e0bbcfd9bc?s=96&r=g\",\"caption\":\"final\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tmux: Network RTT Detection - Final Blog","description":"Detect network rtt in the tmux status bar","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/","og_locale":"en_US","og_type":"article","og_title":"Tmux: Network RTT Detection - Final Blog","og_description":"Detect network rtt in the tmux status bar","og_url":"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/","og_site_name":"Final Blog","article_publisher":"https:\/\/www.facebook.com\/hu.yuzhang","article_published_time":"2020-08-01T19:38:59+00:00","article_modified_time":"2020-12-08T04:21:00+00:00","og_image":[{"width":2862,"height":412,"url":"https:\/\/finaldie.com\/blog\/wp-content\/uploads\/2020\/08\/tmux_status_bar4.png","type":"image\/png"}],"author":"final","twitter_card":"summary_large_image","twitter_creator":"@hyzwowtools","twitter_site":"@hyzwowtools","twitter_misc":{"Written by":"final","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/","url":"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/","name":"Tmux: Network RTT Detection - Final Blog","isPartOf":{"@id":"https:\/\/finaldie.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/#primaryimage"},"image":{"@id":"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/#primaryimage"},"thumbnailUrl":"https:\/\/finaldie.com\/blog\/wp-content\/uploads\/2020\/08\/tmux_status_bar4.png","datePublished":"2020-08-01T19:38:59+00:00","dateModified":"2020-12-08T04:21:00+00:00","author":{"@id":"https:\/\/finaldie.com\/blog\/#\/schema\/person\/2d4c840d6e8e197f8ade98af2bd2fab3"},"description":"Detect network rtt in the tmux status bar","breadcrumb":{"@id":"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/#primaryimage","url":"https:\/\/finaldie.com\/blog\/wp-content\/uploads\/2020\/08\/tmux_status_bar4.png","contentUrl":"https:\/\/finaldie.com\/blog\/wp-content\/uploads\/2020\/08\/tmux_status_bar4.png","width":2862,"height":412},{"@type":"BreadcrumbList","@id":"https:\/\/finaldie.com\/blog\/tmux-network-rtt-detection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/finaldie.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Tmux: Network RTT Detection"}]},{"@type":"WebSite","@id":"https:\/\/finaldie.com\/blog\/#website","url":"https:\/\/finaldie.com\/blog\/","name":"Final Blog","description":"As simple as possible...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/finaldie.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/finaldie.com\/blog\/#\/schema\/person\/2d4c840d6e8e197f8ade98af2bd2fab3","name":"final","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/finaldie.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4c720545b79ddb0f23b527e0bbcfd9bc?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4c720545b79ddb0f23b527e0bbcfd9bc?s=96&r=g","caption":"final"}}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/finaldie.com\/blog\/wp-content\/uploads\/2020\/08\/tmux_status_bar4.png","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/posts\/817"}],"collection":[{"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/comments?post=817"}],"version-history":[{"count":0,"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/posts\/817\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/media\/825"}],"wp:attachment":[{"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/media?parent=817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/categories?post=817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/finaldie.com\/blog\/wp-json\/wp\/v2\/tags?post=817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}