<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dmitry Shnayder on Medium]]></title>
        <description><![CDATA[Stories by Dmitry Shnayder on Medium]]></description>
        <link>https://medium.com/@dmitryshnayder?source=rss-6b123dfb1f34------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GJcowFI3M_WwajcCm-Oiyw.jpeg</url>
            <title>Stories by Dmitry Shnayder on Medium</title>
            <link>https://medium.com/@dmitryshnayder?source=rss-6b123dfb1f34------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 04:10:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dmitryshnayder/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Network monitoring using InfluxDB and Grafana]]></title>
            <link>https://medium.com/@dmitryshnayder/network-monitoring-using-influxdb-and-grafana-8397abb91f1e?source=rss-6b123dfb1f34------2</link>
            <guid isPermaLink="false">https://medium.com/p/8397abb91f1e</guid>
            <category><![CDATA[grafana]]></category>
            <category><![CDATA[extreme-networks]]></category>
            <category><![CDATA[influxdb]]></category>
            <dc:creator><![CDATA[Dmitry Shnayder]]></dc:creator>
            <pubDate>Sun, 05 May 2024 00:47:54 GMT</pubDate>
            <atom:updated>2024-09-06T13:57:35.191Z</atom:updated>
            <content:encoded><![CDATA[<p>I am a technology enthusiast. My house is stuffed with whole zoo of electronics, from smart switches to smartphones. My home WiFi network is powered, provisioned and monitored by Extreme Networks <a href="https://www.extremenetworks.com/products/wi-fi-access-points/universal-aps-indoor/ap5010">enterprise-grade access points</a> and Extreme CloudIQ <a href="http://I am a technology enthusiast. My house is stuffed with whole zoo of electronics, from smart switches to smartphones.">wireless controller</a>. While controller provides wide variety or reporting, I still have a wishlist:</p><ul><li>Two minute granularity is provided for the last 3 hours. After that the reports are aggregated over larger period of time. I wish I could have 2 minutes granularity over month or year.</li><li>Maximum duration of reports is 14 days. Month or even year would be better.</li><li>Sometimes I want custom reports. For example, I want to see throughput for all clients, except surveilance cameras.</li></ul><p>I’m planning to use the following data flow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*l0pWznFM6BuQebG5p76irQ.png" /></figure><p>My wireless deployment resides behand NAT and firewall, so access it directly from anywhere is difficult and unsecure. To overcome this access limitation, I will use cloud-managed time series database and reporting service, that can be accessed from any location. Network stats, produced by wireless deployment and collected by wireless controller, can be scrapped by a process, running on campus, and sent to cloud. To simplify development and improve portability, I will use a script, running in <a href="https://www.docker.com/">Docker</a> container.</p><p>The controller provides documented REST API to retrieve stats. I already have a <a href="https://medium.com/@dmitryshnayder/historical-monitoring-with-cloud-hosted-influxdb-and-grafana-9ce2434d8bc0">monitoring system</a>, that uses InfluxDB and Grafana to show CPU and memory utilizaton of Linux server. InfluxDB provides as much data retention as I have disk space on Linux server (or as much as I have money to use <a href="https://www.influxdata.com/products/influxdb-cloud/">cloud-hosted InfluxDB</a>). Grafana privides almost unlimited reporting capabilities. Lets get all these parts together.</p><p>First, lets create a cloud instance of InfluxDB. Navigate to <a href="https://www.influxdata.com/get-influxdb/">InfluxDB</a> page and click “Try for free”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/617/1*sbdXzFUNWJUv272ldjtImg.png" /></figure><p>I logged in with Google single sign-on and get to my InfluxDB instance. Then navigate to the “Buckets” page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*pOK1cSqc4mqAeq-6aB10Rg.png" /></figure><p>Then click “Create bucket”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*j95AgYDPdYhHgxGMk8hIyQ.png" /></figure><p>I named the bucket “iqc” and set data retention policy to 30 days. This is maximum for free instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*f76QJI8xL6iAbVvHsnghtw.png" /></figure><p>Then I navigated to “API Tokens”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/231/1*uASiRxHyOcI718Y-i6ktJA.png" /></figure><p>Clicked “Generate API Token” and selected “Custom API Token”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/281/1*wHrRbzWtODcJ72GG0GFDXw.png" /></figure><p>The best practice is to have separate API token for publisher and subscriber. So I generated one “Read” token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*7lC3SDaosrkqggi1PjH0dQ.png" /></figure><p>and then one write token:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*MEQ3w9axsQzbjvpfP8zu9Q.png" /></figure><p>Now database is ready and I need to retrieve wireless stats via REST API and upload it to InfluxDB. My wireless controller supports running <a href="https://www.docker.com/">Docker</a> containers. I created a Docker container, that polls REST API and publish the metrics to InfluxDB. The container is based on <a href="https://hub.docker.com/_/telegraf">telegraf docker image</a>, so I don’t need to build telegraf myself. Since telegraf don’t have plugin for my controller (yet), I use <em>execd</em> plugin.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*y5g15RuAcJPhsNF4YNq6FQ.png" /></figure><p>This plugin is a convenient was to extend Telegraf for consuming any custom metrics. The input script <em>exporter.py</em> need to collect stats and print them in the Influx line format. InfluxDB has no fixed row format, so I’m flexible in what the script reports to Telegraf. I choose to make the script future and backward compatible. Just read the stats payload and print key=value for each element in the payload. This way I don’t need to change the script in the future, when new fields are added to the reports API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*zfALZZfd8Fk_OK2A7COiVQ.png" /></figure><p>Packed the reporter script and telegraf config is a simple docker file, and installed it on the controller.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*zVHP9TzlVzjsXHBT0YluxA.png" /></figure><p>Telegraf config file contains write application key, that I generated in the cloud instance of InfluxDB.</p><p>To check if stats are comming to InfluxDB, I opened Data Explorer in the cloud InfluxDB. I see all my measurements here, so the container works and successfully publishes stats to the cloud InfluxDB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*2UyIN2q7lEXdB1Y7EaNnzw.png" /></figure><p>Now it time for Grafana. I already created a free login in the <a href="https://grafana.com/products/cloud/">cloud-managed Grafana</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uY1cGZsQegtrH3cFjD-lfQ.png" /></figure><p>So I logged in to my cloud Grafan instance and navigated to “Conenctions” -&gt; “Data sources”. Then clicked “Add new data source”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XV6zFBkUoK9KGt43iN3Txg.png" /></figure><p>In the list of suported data sources select “InfluxDB”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*VvpOvnOv4_FPUfP09vLXEw.png" /></figure><p>Cloud-managed InfluxDB supports only SQL query language. Fo rHTTP URL enter URL of cloud-managed InfluxDB instance. Its the same URL, used for accessing web UI, but with path removed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*w74sxE1h0hwZaBiMBgn7mQ.png" /></figure><p>Scroll to the end of the page and enter database details. Database name is bucket name, in my case its “iqc”. “Token” is read token, that I obtained from my cloud-managed instance of InfluxDB:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*1d7otZwLaaPEjkrDXXkngA.png" /></figure><p>Thats it for data source, press “Save&amp; Test” and highly lileky get “OK”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/282/1*NVnecHgVM4w5yhp8DmuySQ.png" /></figure><p>Now I can proceed to visualizations. Navigate to “Dashboards” and click “New dashboard”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_oy_AqHa3fJRlS82f1rYig.png" /></figure><p>Then click “Add visualization”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*3Pb84LO9bHUWHP-lJKkL4g.png" /></figure><p>Then select the data source, that was just created:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*wCrQJZuEhj2MdM7J7dcSKg.png" /></figure><p>First thing is to define a query:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*Nv9PEOWcpQfYolgvxnxaog.png" /></figure><p>I started with something simple. Lets create a pie chart — number of clients per radio protocol. I need a table, that holds wireless clients data. It called “MuStats” in my case (MU is Mobile Unit). I select just two fields — MAC address and radio protocol.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*PSGU3CCjzsSRB3SVY1O3cg.png" /></figure><p>To check if query returns what I expect, I switched to table view:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/1*7V0G_5nvYLWaOB-FvRG3Og.png" /></figure><p>Looks good, the result has protocols (for example 11bgn), and MAC addresses of wireless clients. Now I can turn off table view and start nassaging the data. Change visualization type to “Pie chart”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*CbtVWyJCmEeANLZrJtMdTQ.png" /></figure><p>Then switch to “Transform data” tab, next to “Query”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/287/1*KAG6WC8A8Ng8HQjEJS7NtA.png" /></figure><p>On the “Transform data” tab click “Add transformation”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/1*tPBrIgD19JnPoWfhWbwcbg.png" /></figure><p>First I need to group the data by radio protocol and count number of unique MAC addresses. So I selected “Group by” transformation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/237/1*5oPtEMVRqBX0KdycRVWkaA.png" /></figure><p>It brings a simple section to define how the groupping should be done. I selected group by protocol, and calculate distinct count for MAC:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*QWYcpAJIzjjDbz3haVlPNg.png" /></figure><p>For pie chart each category should be a field name. In my case category is a value of the “Protocol” field. I need a transformation, that creates field for each value of the “Protocol”. Click “Add another transformation”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/272/1*jURSQ5H-95UJeAU8o7Ervg.png" /></figure><p>and select “Rows to fields”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/232/1*OHJBYiP73cUy2jiVVu3vtg.png" /></figure><p>Here I specify, that values of “Protocol” will be used as field names. MAC address count will be field values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/993/1*_Ke9WGtRJYyYzTojf-rkHw.png" /></figure><p>To verify the results of transformations, enable “Table view”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/903/1*6Sp3R5MIkFi3k840NgzRQw.png" /></figure><p>Here I noticed “NA” value. Whatever it measn, I don’t want to pollute my pie chart with “NA”. Lets filter out “NA”. Add one more transformation — “Filter data by values”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/234/1*Qc6bA5cfrrvNgMynX8cHaw.png" /></figure><p>Its better to filter the data as soon as possible, so I dragged this transformation to the top of the list, so it becomes #1, and specified to exclude rows, where “Protocol” is equals to “NA”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7_scdlKj4a4RKsSdYssnZA.png" /></figure><p>Final touch is to beautify the chart. Name it “Clients by protocol”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*a6R9H6t7Mb5Q5OQ-NrPjXA.png" /></figure><p>Select to display name of the field in th epie chart. In my case its protocol name:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/385/1*H3h8ikA5GOwiIY2fZr2GjQ.png" /></figure><p>Placed legend to the right and display percent of client count next to the protocol name:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*3pgKaQ8GYnMaiSRO6poHtA.png" /></figure><p>Looks nice:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/1*BxvrZPjDr2ge4uelKc1f-Q.png" /></figure><p>Click “Apply” in the top-right corner:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/253/1*qjzteaOWpQsO-iCelNRu2Q.png" /></figure><p>Grafana provides a wide variety of visualization. In a short time I managed to make a decent dashboard, that give me a good overview of my wireless network:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oX3LXjanbpRlocP2XjvL2Q.png" /></figure><p>Visualizations are saved only when dashboard is saved, so a good practive is to save the dashboard often when updating charts:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/159/1*CgoBMZ8hjTlpXa2zCOZE2w.png" /></figure><p>When saving dashboard, its possible to see the changes, made since the last save.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*BmcCWz1fHCeeQ8ulKGYvjA.png" /></figure><p>Grafana dashboard can be shared with other users. Dashbaords are stored in JSON format, so sharing dashboard definition is as simple as sharing JSON file. Click “Share” button:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/212/1*t8uO2no3UOxnOIL54WpMoQ.png" /></figure><p>Select tab “Export” and enable “Export for sharing externally”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*9z_GK1HyQYhtt3LIggCXEg.png" /></figure><p>Then save the dashboard to a file, or click “View JSON”, copy the content to clipboard and send via email.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*z6jva9SJpOgCiSsOs0A22Q.png" /></figure><p>Time interval for retrieving stats for dashboards can be changed by clicking time selection menu on the top right corner of the screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/569/1*1w-cMKrTvzFdpm2hxYGvig.png" /></figure><p>Selecting “Last X minutes” effectively makes dashboard live, so it display the most recent data when refreshed. Also an arbitrary perion in the past can be selected on the left side of the popup, in the “Absolute time range” section. My favorite way to select time interval is to select time range on a time series chart. It automatically updates selection interval and redraw all other charts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*40huxF03p5D8QTp6BOKl7A.png" /></figure><p>Now I have 2 minute data granularity for the whole 30 days period, and I can create any chart I want without need to deal with REST API or any custom code. I have Grafana power at my fingertips.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8397abb91f1e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Historical monitoring with cloud-hosted InfluxDB and Grafana]]></title>
            <link>https://medium.com/@dmitryshnayder/historical-monitoring-with-cloud-hosted-influxdb-and-grafana-9ce2434d8bc0?source=rss-6b123dfb1f34------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ce2434d8bc0</guid>
            <category><![CDATA[influxdb]]></category>
            <category><![CDATA[grafana]]></category>
            <category><![CDATA[telegraf]]></category>
            <dc:creator><![CDATA[Dmitry Shnayder]]></dc:creator>
            <pubDate>Fri, 05 Apr 2024 18:41:15 GMT</pubDate>
            <atom:updated>2024-06-02T17:19:01.562Z</atom:updated>
            <content:encoded><![CDATA[<p>I’m enjoing my <a href="https://medium.com/@dmitryshnayder/historical-monitoring-with-telegraf-influxdb-and-grafana-b5e677147361">monitoring setup</a>, that gives my live and historical view on the health of home automation system. Its convenient, but available only on my local network. How I can check my setup when I’m in the office, or at a North Carolina beach? I’m hesitant to open ports from Internet to my local network. It requires a lot of security discipline and gives more headache than piece of mind. What if I host InfluxDB and Grafana in the cloud, so they are accessible from anywhere, but my local setup is still behind firewall and NAT?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*JwqYjBI-H_m_SiGoQVxBpA.png" /></figure><p>But why should I host anything, lets see if somebody already host these software as a service. Sure enough, <a href="https://www.influxdata.com/lp/why-influxdb-cloud/">InfluxDB has cloud-hosted service</a>, and they even have free tier. Signup is three mouse-clicks away.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wocd6nGnpaQd1FAHybrFWg.png" /></figure><p>I registered with Google single sign-on, selected data storage region, and have my free cloud instance running in the cloud. Cloud-based interface is exactly the same as local one. I created API token, updated <em>telegraf.conf</em> with new URL and API token. I also increased reporting interval to 3o seconds, no need to know per-second temperature anyways. URL is the same as I use to open web GUI with path removed, for example: <a href="https://us-east-1-1.aws.cloud2.influxdata.com/">https://us-east-1-1.aws.cloud2.influxdata.com</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*czFKAZZMV_YGHzBBKN-tTQ.png" /></figure><p>Now my metrics are in the cloud InfluxDB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SYY8zbOVkbWrCqw11MDkeQ.png" /></figure><p>Now I’m done with cloud-based persistent database. Free tier has limitation for number of records and data retention is limited to 30 days, but it’s good enough for me. Lets see if cloud-based Grafana is available.</p><p><a href="https://grafana.com/products/cloud/">Grafana SaaS</a> is also available and also has free tier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0dDrA9RA7-W-avwJr5nHoA.png" /></figure><p>I registered free acount via Google single-signon:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uORbMg_CCCc68Fn7whU9XQ.png" /></figure><p>As part of setup, selected domain name for my account and deployment region:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*RsL-ZEHq05xbsNZcGXZ1aQ.png" /></figure><p>Cloud-hosted Grafana also looks and feels like locally deployed. There is one difference, not in Grafana, but in InfluxDB and how Grafana accesses it. Cloud-hosted InfluxDB exposes SQL interface only, so Grafana have to use SQL query language in the data source. As of April 2024 InfluxDB SQL interface is alpha quality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*QeVEG0cL9U17DodZOzbnUw.png" /></figure><p>URL is the same as I use for InfluxDB web interface (with path removed). Database is bucket name. “Token” is where new API token is needed.</p><p>The token, that I use for telegraf, allows only writes. Token for Grafana need read access. In the InfluxDB web interface, I generated new API token with read permissions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*gqMOtio-OF469MubdUX3BQ.png" /></figure><p>Enter all the setting, then “Save &amp; test”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*hPoDTd3KlZU4y-thvATh5Q.png" /></figure><p>If everything is good, then test passes and Grafana is ready to create dashboards.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*tR7myKxusrXe3C33KE6xWw.png" /></figure><p>Unlike local deployment, cloud-based Grafana have to use SQL query language, so I cannot just export local dashboards and import them to the cloud. Recreating visualisation is not that complicated. For example, CPU usage chart requires to select table “cpu”, fields “time” and “usage_idle”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zy7pTlGDAiobUkfBWiNH0A.png" /></figure><p>Then define data transformation to calculate total usage:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*NJ1rdHIc6XYiG03Ajk6Q5w.png" /></figure><p>Repeat all <a href="https://medium.com/@dmitryshnayder/historical-monitoring-with-telegraf-influxdb-and-grafana-b5e677147361">on-premise steps</a> for creating dashboard, and I got fully cloud-hosted free-tier monitoring:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SElsDBab51yX1e6bxHycow.png" /></figure><p>Now I can remove local InfluxDB from <em>telegraf.conf</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lTiB3Tt33IzoltBMy-gqWg.png" /></figure><p>Restart telegraf: <em>systemctl restart telegraf</em>, and shutdown local instances of InfluxDB and Grafana:</p><blockquote>docker-compose stop<br>docker-compose rm</blockquote><p>Now I can see health of my local home setup from anywhere. And thanks to <a href="https://www.influxdata.com/lp/why-influxdb-cloud/">InfluxDB</a> and <a href="https://grafana.com/products/cloud/">Grafana</a>, it doesn’t cost me a cent.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ce2434d8bc0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Historical monitoring with Telegraf, InfluxDB and Grafana]]></title>
            <link>https://medium.com/@dmitryshnayder/historical-monitoring-with-telegraf-influxdb-and-grafana-b5e677147361?source=rss-6b123dfb1f34------2</link>
            <guid isPermaLink="false">https://medium.com/p/b5e677147361</guid>
            <category><![CDATA[grafana]]></category>
            <category><![CDATA[telegraf]]></category>
            <category><![CDATA[influxdb]]></category>
            <dc:creator><![CDATA[Dmitry Shnayder]]></dc:creator>
            <pubDate>Fri, 05 Apr 2024 18:00:02 GMT</pubDate>
            <atom:updated>2024-04-12T21:14:26.457Z</atom:updated>
            <content:encoded><![CDATA[<p>In my home automation setup I already have <a href="https://medium.com/@dmitryshnayder/live-monitoring-with-telegraf-and-grafana-bb368bfb74a7">live monitoring</a>. It provides me an overview of health of the Linux box, including CPU and memory usage, as well as temperature of the box. While its important to know if current utilization is within a reawonable range, live dashboards don’t provide an information of what happen overnight, or any other time when I was away from Grafana dashboard. It is possible to add a time series chart to <a href="https://medium.com/@dmitryshnayder/live-monitoring-with-telegraf-and-grafana-bb368bfb74a7">my current setup</a>, but it will only show historical data for the time, when dashboard was open.</p><p>Adding persistent storage is the next logical step to keep history of the utilization data. In my setupTelegraf sends metrics in InluxDB line format, the easiest way to add persistence is to plug InfluxDB between Telegraf and Grafana.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/341/1*A5lhz65thR9UzXCY5TvxCw.png" /></figure><p>InfluxDB is available as <a href="https://hub.docker.com/_/influxdb">docker container</a>. I just need to find a LInux box with sufficient amount of storage, and install the container. I choose to have InfluxDB database located in a separate docker volume. To simplify creation of volumes and container, I wrote a docker-compose.yaml:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*EZZ5LrjWX0l9GSqhKQejvw.png" /></figure><p>Started the container with <em>docker-compose up -d</em>, then opened InluxDB GUI on port 8086 (http://ipaddress:8086)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/737/1*DhLFnmMupQRu4S8F4uM9oA.png" /></figure><p>Setup initial user credentials, organization name and bucket name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*evbIyEls2KknmkuBxudhkg.png" /></figure><p>Then click “Continue”. The first screen presents administrative token, I copied it to a safe place:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V_dH-mSGn1VJ2CVIR92Rsg.png" /></figure><p>The pressed “Quick Start” and get home screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cnGu5XgCxLvd-9BTgU8-zA.png" /></figure><p>First , lets get API token, so Telegraf can push the data. Click “Upload” icon, then choose “API Tokens”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/355/1*6qPu4YPLz5nZ9xBlatYCfA.png" /></figure><p>Then click “Generate API token”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/1*OPwEd0csgtcvBLjljQAVLA.png" /></figure><p>As a good security practice, the token should be allow as little as required to push metrics to the database. I allowed writing to the bucket “kvm”, and allowed all Telegrafs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*t23WtwpkJ4jwYK9wgpqlGg.png" /></figure><p>Click “Generate”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*xafLzXx50NopQ4X0FqRCvA.png" /></figure><p>Copy the token, then close the popup and verify, that the API key is in the list:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GuZG12n0qOBrsjDrqt_LeA.png" /></figure><p>Add the output plugin to the <em>telegraf.conf</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uWaYTLAh3MI5m-J5UqeJwA.png" /></figure><p>Then restart Telegraf: <em>systemctl restart telegraf</em>, and check if any data is coming to InluxDB. Navigate to “Data explorer”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/1*THaeuSvY0TyzkOb6ZgLMYQ.png" /></figure><p>Seelct “kvm” bucket, then open drop-down for the list of keys, and check if “cpu” is there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*HUeatour12Js-hLMgkOysA.png" /></figure><p>Thats it for InluxDB. It convenient, that it derive structure of bucket from the Telegraf payloads, so no effort required to create a structure or adjust it when source format changes.</p><p>Now lets attach Grafana to the InfluxDB. Navigate to Grafana web interface and open “Data sources”. Click “Add new datasource”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/229/1*xdWRiA8VKDfggon49gn4vw.png" /></figure><p>Select “InfluxDB”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*9ZHcKsWK-SfMhky25tQ0_g.png" /></figure><p>I named this datasource “influxdb”. I choose “InfluxQL” query language, that is most recent out of stable query interfaces to InfluxDB as of April 2024. In the URL field I entered the same URL as I use for InfluxDB <a href="http://192.168.8.5:8086."><em>http://192.168.8.5:8086</em>.</a> Leave all “Auth” settings disabled.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*jMilp3Myap83xoG7sRAy_A.png" /></figure><p>Scroll further down there are connection settings. “Database” is bucket name. “User” is my user name in the InfluxDB. Password is API token, that I generated in InfluxDB earlier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/1*n6dwOMIpS_Pcifxg69w_3g.png" /></figure><p>Click “Save &amp; test”. With enough luck the following message appear:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*IJgBG4E_2LGYto2MNUx3_A.png" /></figure><p>Now lets create a new dashboard to display historical data. First, add visualization (chart) for CPU usage. In the “Query” tab I select CPU utilization metrics (from = cpu), field — “usage_idle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*0TVX5V84njMQkcmyNpb8Ug.png" /></figure><p>Similarly to live dashboards, I added data transformation to calculate total CPU usage:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*DPObOgiXjNtEgKnpV63aTA.png" /></figure><p>Similar to CPU usage chart, I added the remaining charts for memory usage and temperature. Done and saved, here is the historical dashboard:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jnr2eDAavMJcfa3nOlKA2Q.png" /></figure><p>Now I have two dashboards and two data paths. But since InfluxDB has the most recent metrics, I don’t even need live dashboard. I can add latest gauges to the historical dashboard. Lets start with CPU usage. Duplicate CPU usage time series:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/1*NswovCC6J219Y6Sq2ze8NA.png" /></figure><p>Then edit the duplicated chart and change representation to “Gauge”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/424/1*nzYsdW2A_BEJloDfSN_gIw.png" /></figure><p>Grafana is smart enough to update “Calculation” field on the right pane to “Last*” (represent last reading, skipping NULL values):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/384/1*PnVCYun0M2ZlXo5keWKnEw.png" /></figure><p>Repeating these steps for the other charts and get nice live and historical dashboard:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mF8kmu6yLaK-wjyl3n91PA.png" /></figure><p>Now I can delete old live dashboard:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/305/1*Q4tqUq_O8ZpOLI36LSHSmw.png" /></figure><p>And remove output plugin from <em>telegraf.conf</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ob9QnDAqJTr8y9ILnNhu3g.png" /></figure><p>Done. Now I can see how healthy my setup now and before.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b5e677147361" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Live monitoring with Telegraf and Grafana]]></title>
            <link>https://medium.com/@dmitryshnayder/live-monitoring-with-telegraf-and-grafana-bb368bfb74a7?source=rss-6b123dfb1f34------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb368bfb74a7</guid>
            <category><![CDATA[grafana]]></category>
            <category><![CDATA[telegraf]]></category>
            <dc:creator><![CDATA[Dmitry Shnayder]]></dc:creator>
            <pubDate>Tue, 02 Apr 2024 19:16:10 GMT</pubDate>
            <atom:updated>2024-04-05T18:02:31.736Z</atom:updated>
            <content:encoded><![CDATA[<p>I have an automated and connected home. My <a href="https://medium.com/@dmitryshnayder/how-i-automated-nest-thermostat-1c257eaa1383">thermostat</a> talks with weather forecast. My light switches talk to my Apple Watch. My <a href="https://medium.com/@dmitryshnayder/teslamate-04556a59b4d4">car keeps record</a> of its movement. All of that runs on a <a href="https://medium.com/@dmitryshnayder/qemu-kvm-networking-in-ubuntu-752d8a2b1af6">single fanless computer</a>, sitting in my basement. While it doesn’t require ane maintenance, I still want to keep an eye on it. Does it have enough memory? How busy is CPU? Does it overheat?</p><p>Since the computer has some sensitive information, I don’t want to allow any additional access to it. So usual <a href="https://prometheus.io/docs/prometheus/latest/getting_started/">Prometheus scraping</a> doesn’t excite me. I prefer if the box export metrics itself. This way no ports need to be open, less security to worry about, and the box can be located behind NAT.</p><p>There is an open-source solution for collecting and pushing various metrics — <a href="https://www.influxdata.com/time-series-platform/telegraf/">Telegraf</a>. It has many <a href="https://github.com/influxdata/telegraf/tree/master/plugins">plugins</a>, to collect metrics from different sources, processing and publishing them to many types of destinations. Telegraf has plugins for collecting temperature, as well as CPU and memory usage.</p><p>Obvious solution for displaying the collected metrics is <a href="https://grafana.com/">Grafana</a>. Based on my experience with <a href="https://medium.com/@dmitryshnayder/teslamate-04556a59b4d4">TeslaMate</a>, there are unlimited possibilities to visually represent stats using Grafana. I found that Grafana since version 8 supports <a href="https://grafana.com/tutorials/stream-metrics-from-telegraf-to-grafana/">receiving live stream of events</a> from Telegraf. The flow of messages would be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/341/1*APz1CF-18WZpj_Y561VkFg.png" /></figure><p>A separate Telegraf process collects required metrics (CPU and memory usage, temperature), and pushes it to another computer, where dashboards are displayed. So server machine doesn’t require any additional access or open ports. Grafana box, even if gets hacked, will provide attacker information about my CPU temperature. Not a big deal.</p><p>I start with Grafana box. The simplest way of getting Grafana up and running is docker compose. On a Linux box, in an empty directory, create file named <em>docker-compose.yaml</em> with content:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*XGXD293KHip5c8e2MdOXpQ.png" /></figure><p>I decided to provide Grafana container with separate docker volume, so I can update or change container image without losing my dashboards.</p><p>Then start the container: <em>docker-compose up -d</em> and check if its running: <em>docker container ls</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TcCNhF_uTETj_-yNeieaeg.png" /></figure><p>Lets open wen interface and login to Grafana. Open <a href="http://ipaddress:3000/">http://ipaddress:3000</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1004/1*IyP01IeiYq50dKMHT73v9g.png" /></figure><p>Defaut username is <em>admin</em>, and defaul password is also <em>admin</em>.</p><p>Grafana asks to change default password, lets do it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*yAQyKePm-3yENa4-6QwZTQ.png" /></figure><p>If everything went well, then we get the welcome screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NKK6M17DEIBizleBfdpzxg.png" /></figure><p>Lets leave Grafana alone for a while and install Telegraf. First, find a Linux box with Golang compiler and clone Telegraf Git repository:</p><blockquote>git clone <a href="/service/mailto:git@github.com">git@github.com</a>:influxdata/telegraf.git</blockquote><p>Then build Telegraf:</p><blockquote>cd telegraf<br>make</blockquote><p>It may take a while to build all the goodies, provided with telegraf. When <em>make</em> is done, there will be a binary file called <em>telegraf</em> in the current directory. Copy it to the computer, where metrics need to be collected. I copied the binary to <em>/usr/local/bin</em>.</p><p>Next, we need to configure telegraf to collect CPU, memory and temperature metrics. Create file <em>/etc/telegraf/telegraf.conf</em> with content:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*TjwaquMMV7Q4AXDLPHbzaw.png" /></figure><p>In my case <em>192.168.8.5</em> is IP address of the Grafana box. The path “/api/live/push/kvm” contains two parts:</p><ul><li>/api/live/push — fixed part, indicates to Grafana, that its a live data</li><li>kvm — identifies topic. Choose any reasonable topic name here.</li></ul><p>Collection and reporting intervals are set to 1 second. This gives practically live data.</p><p>One thing we still missing here is authorization token. We don’t want anybody, who knows IP address of Grafana box, to be able to update content of dashboards. To create authorization token, lets get back to Grafana web interface. Navigate to Administration -&gt; Users and Access -&gt; Service Accounts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CcTW46oxlxnug_5gume5Zg.png" /></figure><p>Click “Add service account”. Lets name the account “telegraf” and give it a “Viewer” role:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*Gl7OFAQq2eQLpiTLRWSzFw.png" /></figure><p>The we need to add service account token:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RDUyVd-b__Z_VzpdbGkpxg.png" /></figure><p>Click “Add service account token”, pick any name for the new token, and click “Generate token”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*SZAUnkTYFyi9cECTsUwWmQ.png" /></figure><p>A long string will be generated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*p6pcn7ohnYRPxeSa8NijMg.png" /></figure><p>Click “Copy to clipboard and close”. We need to add this token to the <em>telegraf.conf</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*vgJxjxzGbMwi6sS79EOEjg.png" /></figure><p>We are done with telegraf config. Lets start telegraf. I prefer to define telegraf as systemd service. To do that, I create file <em>/etc/systemd/system/telegraf.service</em> with content</p><blockquote>[Unit]<br>Description=Telegraf<br>StartLimitIntervalSec=0<br>[Service]<br>ExecStart=/usr/local/bin/telegraf<br>Restart=always<br>RestartSec=5<br>[Install]<br>WantedBy=multi-user.target</blockquote><p>Then enable and start telegraf service:</p><blockquote>systemctl daemon-reload<br>systemctl start telegraf<br>systemctl enable telegraf</blockquote><p>Lets check if telegraf is running: <em>systemctl status telegraf</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hIs0WNierzhDl_kiMv-n2g.png" /></figure><p>Everything looks good. Lets see what we get in Grafana. Click “Dashboards” -&gt; “Create dahsboard”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6PFbFvYxrIRIUUE80jMIFg.png" /></figure><p>We need to add first visualization, that I used to call chart.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/1*agf6aoTMjRWn0j7FgyrzEg.png" /></figure><p>Now we need to choose datasource. There is a “ — Grafana — “ predefined datasource on the righ side of the screen, this is where our live data comes. Click on it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DUyUvq7wbY0lN9tAn7mHxQ.png" /></figure><p>Initially it shows some sample data. To see what telegraf send to us, select “Live Measurements” as “Query type”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*sroRLEequZvwmkT0xXcmnQ.png" /></figure><p>Then select a channel</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*4QbNTQ9ppywsNbTn1WDzQg.png" /></figure><p>At the bottom of the list there are three channels. Channel name consists of three parts:</p><ul><li>stream — indicates that data comes from live streams</li><li>kvm — this is what I choose to put to the path in the telegraf config</li><li>cpu, mem, temp — particular metric, that is what collected by telegraf</li></ul><p>Lets start with memory utilization chart. Select “stream/kvm/mem”. Telegraf collected a lot of memory metrics:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*WM_mx1gr__iqHGLHECP2Vw.png" /></figure><p>Lets show only percent of used memory. In the “Fields” drop-down select “user_percent”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*Whnij2011Hwm0Gip9sgdbA.png" /></figure><p>The chart starts displaying timeseries of available memory:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/713/1*6w3ohXCg0UtVx0S8PKztew.png" /></figure><p>With live data timeseries is not much useful, as it shows only time when dashboard was open. Lets change the visualization to something more live. In the top-right corner open selection of visializations:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/505/1*3m3CuTXWcfGvFiBd3NyfOQ.png" /></figure><p>I like how gauges are displayed in Grafana, lets use it for memory usage</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*0EmMXTJ8mfZeZ2o42lNXDQ.png" /></figure><p>Looks better. However, “Panel Title” == 25.7 is not too informative. Lets change title. In the right pane find “Panel options” -&gt; “Title” and name the chart “Memory usage”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/225/1*gThcXwH3OsPnG3qnIWr4zQ.png" /></figure><p>Then scroll down to “Standard options” and select “Unit” — “Percent”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*I_QhFI0fWZNvBZCLpNZfIw.png" /></figure><p>Looks better:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*sS4Du1iwpQStwS919FYl5A.png" /></figure><p>Lets apply the new chart to the dashboard. Click “Apply” in the top-right corner. Now we have a dashboard with one chart on it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*2OFCIfZwVUC_DbzY2eLRJw.png" /></figure><p>Lets add more. Click “Add” -&gt; “Visualization”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/306/1*MXVv3SInCXPh0nITdsUvZQ.png" /></figure><p>Now select channel “stream/kvm/cpu”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*IvTWMcx8KdpLkvknA24NMQ.png" /></figure><p>There are a lot of metrics, but no total CPU usage:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*QbeJvpFmLzPhuc3dfTxM-w.png" /></figure><p>We need to calculate it as <em>100 — usage_idle</em>.Select tab “Transform data”, then click “Add transformation”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*O-3TNDfZ9fHUUm5nDp7uqA.png" /></figure><p>Out of variety of transformations we need “Calculation”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*BdSDDjNtQjkUu9Y6jqujxA.png" /></figure><p>Mode will be “Binary operation”, “Operation” is “100 — usage_idle”, and the alias (result field name) I choose “usage_total”. Enable “Replae all fields”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*sN35Txjc1VMGpnaBku130g.png" /></figure><p>Select viaualization type “Gauge”. On the “Query” tab select “All fields”, and here we go, we get CPU usage gauge:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*OxehYlU5vfcVNP5C-oIfWQ.png" /></figure><p>As with memory usage, update title and unit and apply. Now we get two charts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*fpIf87D82kEb6jypBDwXHg.png" /></figure><p>Last chart (visualisation), that I need to add is temperature. As ususal, click “Add” -&gt; “Visualisation”, select query type “Live measurement”, and select channel “stream/kvm/temp”. Looks like I have nine temperature sensors in this fanless box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*ms91AKnvMDlmgOAAZV6J8g.png" /></figure><p>I don’t want to monitor nine gauges. Lets create one, that shows maximum temperature acress all sensors. We will add data transformation, similar to CPU usage. But instead of “Binary operation” will select “Reduce row” mode. In the “Operation” field, select all “temp” metrics. Select “Max” calculation and name the resulting field “max_temp” in the “Alias” field. Select “Replace all fields” to use only calculated maximum temperature for display.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*lRhwp2-y9hz0nCd-7gK_nw.png" /></figure><p>Change the chart title to “Maximum temperature” and select units “Celsius”. Also I specified expected minimum and maximum temperature to have consistent relative position of the gauge:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/1*fBB6KGayS4KhCHLfjaZ7TA.png" /></figure><p>To make the chart more lively, I specified thresholds for cold / normal / hot:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*-WBeYukHh0CMvlKSlRrOsg.png" /></figure><p>We are done. Lets save the dashboard</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/138/1*ghRtXg2key5EjO1ikrQl6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/657/1*QuFsSo4ytJYyO-g2NBJUsw.png" /></figure><p>Charts can be repositioned on the dashboard by dragging on the title.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wKN14ecciZYNRm-fVsJgdA.png" /></figure><p>Now I have critical utilization metrics of my home automation box at glance. Looks great. Next step will be <a href="https://medium.com/@dmitryshnayder/historical-monitoring-with-telegraf-influxdb-and-grafana-b5e677147361">adding historical data</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb368bfb74a7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Linux bridge interfaces and Docker]]></title>
            <link>https://medium.com/@dmitryshnayder/linux-bridge-interfaces-and-docker-19039a02ba63?source=rss-6b123dfb1f34------2</link>
            <guid isPermaLink="false">https://medium.com/p/19039a02ba63</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Dmitry Shnayder]]></dc:creator>
            <pubDate>Mon, 11 Dec 2023 00:14:44 GMT</pubDate>
            <atom:updated>2023-12-11T00:16:33.019Z</atom:updated>
            <content:encoded><![CDATA[<p>I am a technology enthusiast. My house is stuffed with whole zoo of electronics, from smart switches to smartphones. All this zoo is hosted on a single Linux box, where I run <a href="https://medium.com/@dmitryshnayder/qemu-kvm-networking-in-ubuntu-752d8a2b1af6">wireless controller as a KVM</a>, and a couple of containers — <a href="https://medium.com/@dmitryshnayder/how-i-automated-nest-thermostat-1c257eaa1383">HomeBridge</a> and <a href="https://medium.com/@dmitryshnayder/teslamate-04556a59b4d4">TeslaMate</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*b8qrTr1QAOM-O3Lw.png" /></figure><p>Until I installed Docker, there was no issues with bridge interfaces, bytes were going back and forth between wired network and VM. After installing Docker and starting containers I notied, that the VM lost connectivity to the network.</p><p>It appeared, that Docker setup <a href="https://en.wikipedia.org/wiki/Iptables">iptables</a> rules to limit acces to the containers. Docker uses Linux bridge interfaces for communication between containers. Docker’s iptables rules are configured in such a way it blocks all packets on the custom bridge interface, that I use for the wireless controller.</p><p><a href="https://docs.docker.com/network/packet-filtering-firewalls/">Docker documentation</a> describes how iptables are maintained by docker daemon. The documentation mentioned, that a rule need to be added to the <em>DOCKER-USER</em> chain, that allow specific interfaces to forward traffic.</p><p>I added the rule in the <em>docker.service</em> file via <em>ExecStartPost</em> command:</p><blockquote># cat /lib/systemd/system/docker.service <br>[Unit]<br>Description=Docker Application Container Engine<br>Documentation=<a href="https://docs.docker.com/">https://docs.docker.com</a><br>After=network-online.target firewalld.service containerd.service<br>Wants=network-online.target<br>Requires=docker.socket<br>Wants=containerd.service</blockquote><blockquote>[Service]<br>Type=notify<br># the default is not to use systemd for cgroups because the delegate issues still<br># exists and systemd currently does not support the cgroup feature set required<br># for containers run by docker<br>ExecStart=/usr/bin/dockerd -H fd:// — containerd=/run/containerd/containerd.sock<br>ExecReload=/bin/kill -s HUP $MAINPID<br>ExecStartPost=<strong>/usr/sbin/iptables -I DOCKER-USER -p all -i iqcbr0 -j ACCEPT</strong><br>TimeoutSec=0<br>RestartSec=2<br>Restart=always</blockquote><p>The rule, that I added, permits traffic on all ports on the bridge interface <em>iqcbr0</em>, the one, that I use for bridging traffic between wired network and virtual machine.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19039a02ba63" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TeslaMate]]></title>
            <link>https://medium.com/@dmitryshnayder/teslamate-04556a59b4d4?source=rss-6b123dfb1f34------2</link>
            <guid isPermaLink="false">https://medium.com/p/04556a59b4d4</guid>
            <category><![CDATA[tesla]]></category>
            <category><![CDATA[teslamate]]></category>
            <dc:creator><![CDATA[Dmitry Shnayder]]></dc:creator>
            <pubDate>Sun, 10 Dec 2023 22:30:17 GMT</pubDate>
            <atom:updated>2023-12-10T22:30:17.389Z</atom:updated>
            <content:encoded><![CDATA[<p>A while ago I started <a href="https://medium.com/@dmitryshnayder/how-i-automated-nest-thermostat-1c257eaa1383">home automation project</a>. It runs on a Linux box,</p><p>For the last few month I own a premium brand self-propelled smartphone with some people-moving capabilities — Tesla Model Y. From hardware point of view its a mixed bag, but software-wise it has a lot of capabilities. As any modern software product it has APIs. They are not documented, but well known for the community. The APIs can be used to track location, charge state, speed, power and many other parameters.</p><p>There is an open-source project <a href="https://docs.teslamate.org/docs/installation/docker">TeslaMate</a>, that uses Tesla APIs to collect driving and charding stats, and present them via nice <a href="https://grafana.com/">Grafana</a> dashboards.</p><p>I already has a dedicated Linux box, that runs my home <a href="https://medium.com/@dmitryshnayder/qemu-kvm-networking-in-ubuntu-752d8a2b1af6">wireless network controller</a> and <a href="https://medium.com/@dmitryshnayder/how-i-automated-nest-thermostat-1c257eaa1383">HomeBridge</a>. The box still has a lot of available resorces, so lets connect it with my vehicle.</p><p>As usual with docker compose, everything starts with creating <em>docker-compose.yml</em> file. Content of the file is copied from the <a href="https://docs.teslamate.org/docs/installation/docker">documentation page</a>. Just replaced key and password with some hard-to-guess strings:</p><blockquote>version: “3”</blockquote><blockquote>services:<br> teslamate:<br> image: teslamate/teslamate:latest<br> restart: always<br> environment:<br> — ENCRYPTION_KEY=secretkey #replace with a secure key to encrypt your Tesla API tokens<br> — DATABASE_USER=teslamate<br> — DATABASE_PASS=password #insert your secure database password!<br> — DATABASE_NAME=teslamate<br> — DATABASE_HOST=database<br> — MQTT_HOST=mosquitto<br> ports:<br> — 4000:4000<br> volumes:<br> — ./import:/opt/app/import<br> cap_drop:<br> — all</blockquote><blockquote>database:<br> image: postgres:15<br> restart: always<br> environment:<br> — POSTGRES_USER=teslamate<br> — POSTGRES_PASSWORD=password #insert your secure database password!<br> — POSTGRES_DB=teslamate<br> volumes:<br> — teslamate-db:/var/lib/postgresql/data</blockquote><blockquote>grafana:<br> image: teslamate/grafana:latest<br> restart: always<br> environment:<br> — DATABASE_USER=teslamate<br> — DATABASE_PASS=password #insert your secure database password!<br> — DATABASE_NAME=teslamate<br> — DATABASE_HOST=database<br> ports:<br> — 3000:3000<br> volumes:<br> — teslamate-grafana-data:/var/lib/grafana</blockquote><blockquote>mosquitto:<br> image: eclipse-mosquitto:2<br> restart: always<br> command: mosquitto -c /mosquitto-no-auth.conf<br> # ports:<br> # — 1883:1883<br> volumes:<br> — mosquitto-conf:/mosquitto/config<br> — mosquitto-data:/mosquitto/data</blockquote><blockquote>volumes:<br> teslamate-db:<br> teslamate-grafana-data:<br> mosquitto-conf:<br> mosquitto-data:</blockquote><p>Then started the application:</p><blockquote>docker compose up -d</blockquote><p>The command automatically create disk volumes, pull the code from the internet and start the containers:</p><blockquote>Creating volume “teslamate_teslamate-db” with default driver<br>Creating volume “teslamate_teslamate-grafana-data” with default driver<br>Creating volume “teslamate_mosquitto-conf” with default driver<br>Creating volume “teslamate_mosquitto-data” with default driver<br>Creating teslamate_database_1 … done<br>Creating teslamate_grafana_1 … done<br>Creating teslamate_mosquitto_1 … done<br>Creating teslamate_teslamate_1 … done</blockquote><p>A GUI page is available on port 4000 on the Linux box, for example: <a href="http://192.168.8.6:4000/"><em>http://192.168.8.6:4000</em></a></p><p>Next step is to give the application access to the car. TeslaMate requires access and refresh tokens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/991/1*WqbsURjxKsCHiHjT_Xy4aw.png" /></figure><p>There are many ways to generate the tokens. I choose a simple one — install an application on the phone:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/0*LXswvWOd4RafRvCq.jpg" /></figure><p>The application lets me login to my Tesla account, including 2FA, and gives the required tokens:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/186/1*Qrtb4eDD9ow0x8zpXD-6wA.png" /></figure><p>Obviously its better to open TeslaMate page on the same phone as the authentication application, so tokens can be copy/pasted.</p><p>When tokens are entered to TeslaMate, it starts polling the car and collect the stats. ALthough not required, it can be confirmed by checking logs of the main TeslaMate container:</p><blockquote>docker logs teslamate_teslamate_1</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/929/1*Ean9kETJbUJC2QanTD6xXw.png" /></figure><p>The car notifies when the data is polled, so this icon (a phone with locetion arrow) is displayed on the car’s screen once in a while:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IJPm3EILZ6jFs4iB.jpg" /></figure><p>I didn’t try myself, but most likely functionality of TeslaMate will be limited if location sharing is disabled. I keep it enabled.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0ivHnIoT0SH7veYo.jpg" /></figure><p>A status screen is provided on port 4000:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*n2xy5FtYqntGWL9W63s8wg.png" /></figure><p>But most interesting information is provided by Grafana deshboards on port 3000. TeslaMate bundles a lot of useful and less-than-useful dashboards.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*BvJwcEnKLhmul3dydlQneA.png" /></figure><p>One of most useful is Drive Stats:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v1hCNJvsLz78qptNnpo7Og.png" /></figure><p>Interesting to see how efficient the drives, depending on factors like temperature or speed. The most useless to me is Battery Health:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W64WJAhpM4T6EkmJENaD4A.png" /></figure><p>In almost 23,000 km the battery lost 4.4% of capacity, and most of the drop happened around 13,000 km. Why it happen nobody knows, better to close this dashboard and not at it ever again.</p><p>Why do I need TeslaMate? Hard to say, mostly for fun. In 21st century data is the new oil, never know what may be useful tomorrow.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=04556a59b4d4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How I automated Nest thermostat]]></title>
            <link>https://medium.com/@dmitryshnayder/how-i-automated-nest-thermostat-1c257eaa1383?source=rss-6b123dfb1f34------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c257eaa1383</guid>
            <category><![CDATA[nest]]></category>
            <category><![CDATA[apple-homekit]]></category>
            <category><![CDATA[homebridge]]></category>
            <dc:creator><![CDATA[Dmitry Shnayder]]></dc:creator>
            <pubDate>Sun, 10 Dec 2023 15:52:07 GMT</pubDate>
            <atom:updated>2023-12-10T15:52:07.410Z</atom:updated>
            <content:encoded><![CDATA[<p>For many years I am using Nest thermostat in my home. I am happy about its appearance and functionality. Well, almost. I got used to my car’s automatic climate control, where I set desired temperature, and it’s not my bussiness anymore to decide if heater or AC is required. It just keeps my favorite 22 degree celsius, regardless if it rains or shine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S_UtgrTz68WU0R2TyhKrPg.jpeg" /></figure><p>This is not a case with Nest thermostat. I need to set heating or cooling mode manually. There is an “Auto” mode, that requires to set lower and upper limit and control them almost independently.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J5PlOlbVFPTFSrL4chXu-A.jpeg" /></figure><p>For some reason <a href="http://www.google.com/">Google</a> doesn’t provide automation around heating/cooling mode, at least at the time of writing this article. In Ontario, Canada, it may be freezing at night, and sizzling hot during the day. Constantly switching smart thermostat between heating and cooling is annoying.</p><p>I knew that <a href="http://www.apple.com/">Apple</a> is good at home tasks automation using their <a href="https://www.apple.com/ios/home/">HomeKit</a>. However, none of my dozens of home devices are supported by HomeKit natively. Fortunately this problem is solved by third-party developers of <a href="https://homebridge.io/">HomeBridge</a>. HomeBridge allows me to integrate all my home devices with HomeKit without being fully locked to Apple ecosystem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/0*GSB0okC6NV1pZrv5.png" /></figure><p>I decided to install <a href="https://github.com/homebridge/homebridge/wiki/Install-Homebridge-on-Docker">HomeBridge as Docker container</a>. It gives me more flexibility for choosing host machine — I don’t need separate hardware to run HomeBridge, and the installation can be easily backed up or moved between machines. The only limitation in case of HomeBridge — it <strong>cannot</strong> run on MacOS due to <a href="https://github.com/docker/for-mac/issues/68">this</a> bug, and Windows is also <strong>not supported</strong> due to <a href="https://github.com/docker/for-win/issues/543">this</a> bug. So I need to find a Linux machine, that is always powered on. I could use some old laptop with Ubuntu, but it requires a place to live, and power to run.</p><p>I <a href="https://medium.com/@dmitryshnayder/qemu-kvm-networking-in-ubuntu-752d8a2b1af6">recently deployed</a> a wireless controller on a Linux box. It runs 24x7 and has plenty of resources available for other applications. All it need is <em>docker</em> and <em>docker-compose</em>:</p><blockquote>apt-get install docker docker-compose</blockquote><p>As suggested by <a href="https://github.com/homebridge/homebridge/wiki/Install-Homebridge-on-Docker#install-homebridge">HomeBridge documentation</a>, I created a file docker-<em>compose.yml</em> with the following content:</p><blockquote>version: ‘2’<br>services:<br> homebridge:<br> image: oznu/homebridge:ubuntu<br> container_name: homebridge<br> restart: unless-stopped<br> network_mode: host<br> environment:<br> — TZ=America/Toronto<br> — PGID=1000<br> — PUID=1000<br> — HOMEBRIDGE_CONFIG_UI=1<br> — HOMEBRIDGE_CONFIG_UI_PORT=8581<br> volumes:<br> — ./homebridge:/homebridge</blockquote><p>Then started the application in background:</p><blockquote>docker-compose up -d</blockquote><p>That’s it! HomeBridge is running on port 8581 on the Linux box. Time to open its GUI and see what happen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*JO1vMWMj2mM8RvqwVOgP3A.png" /></figure><p>Setup process is extremely user-friendly. I create admin account and get access to the dashbord.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*Ei_AN8zJP4OJWXL6Wd4pnw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*I7u5_sJhHE33ENrWEwyzJg.png" /></figure><p>Now I’m good to go ahead and start adding plugins.</p><p>I started with Nest. Just search for “nest” on the plugins page and find the “Homebridge Nest” plugin, ready to install.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ar0Y9R9i8VGjJsQfb_bwfw.png" /></figure><p>Click “INSTALL”. No typing of fancy commands, installation is done in single click.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*V5qIErkNSaN2SrgnPu6ewA.png" /></figure><p>Now its time for more fancy stuff. I need to configure the Nest plugin to be able to talk to my Nest thermostat.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/1*3Q1CTZZZxWUm-2zkn01sAg.png" /></figure><p>I use Google account to access all my Nest devices. So I expand “Google Account — Refresh Token” option. It may be complicated task to get refresh token, but thanks to plugin developers, they provided a script to make my life easier. To get refresh token I have to get into container to run the script:</p><blockquote>kvm-fanless$ docker exec -it homebridge bash</blockquote><blockquote>root@(none):/homebridge# node node_modules/.pnpm/homebridge-nest@4.6.9/node_modules/homebridge-nest/login.js</blockquote><blockquote>1. Open the url below in a browser to continue:</blockquote><blockquote><a href="https://accounts.google.com/o/oauth2/auth/oauthchooseaccount?access_type=offline&amp;response_type=code&amp;scope=openid%20profile%20email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fnest-account&amp;redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&amp;client_id=733249279899-1gpkq9duqmdp55a7e5lft1pr2smumdla.apps.googleusercontent.com">https://accounts.google.com/o/oauth2/auth/oauthchooseaccount?access_type=offline&amp;response_type=code&amp;scope=openid%20profile%20email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fnest-account&amp;redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&amp;client_id=733249279899-1gpkq9duqmdp55a7e5lft1pr2smumdla.apps.googleusercontent.com</a></blockquote><blockquote>2. Copy the authorization code from the browser, and paste it here:</blockquote><p>The script asks me to open the URL. The URL brings me to a usual Google SSO screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*O6YwdsyC_sBYSMEi9J8RAg.png" /></figure><p>I choose my home account, where all my home devices are connected. Then confirm the request for permissions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*n0jISgK11FTD-k6bWKbIcg.png" /></figure><p>The page gives me the authorization code. There is a “Copy” icon next to it, so I don’t have to worry about missing characters or copying extra spaces:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*j4clgbPPh6gSWxiIJUaAAQ.png" /></figure><p>Copied the authorization code and pasted it to the login.js. It generates refresh token:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xQiQOWSyBfDxpno3A5mEpA.png" /></figure><p>Copy/paste the refresh token to the config page of HomeBridge and click “Save”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*y5XzBoYIzh4Mmuqq50Dadw.png" /></figure><p>Now I have Nest plugin installed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tlYQVZQQk3k2o-4XU_DC7w.png" /></figure><p>Restarted the container for changes to take effect:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/206/1*wEPBq6xCuGNTZzPwvwv9Kg.png" /></figure><p>Then something unexpected, but pleasant happened. After restart I see all my Google devices available to the HomeBridge, not just thermostat:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ojGN7smE5stMEEzvyRBc6A.png" /></figure><p>This is exciting. Why limit myself with Google devices? Let go further. The procedure of adding devices to HomeBridge is so well documented and easy to follow, that I added to HomeBridge all smart devices, that I have.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YKBVGkyTRqwjAc2evIG_-w.png" /></figure><p>I also added <a href="https://github.com/werthdavid/homebridge-weather#readme">homebridge-weather</a> plugin to be able to read outside temperature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5tEwiUX7ec_fHLnLR4jueg.png" /></figure><p>Now it’s time to see all these devices in the Apple’s “Home” application. Actually I could add the bridge to the HomeKit right after installing the container, but I was so excited of adding various plugins, it’s only now I remembered to introduce HomeBridge to the HomeKit. I opened the “Home” application on my iPhone, clicked “+” button and selected “Add Accessory”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xr23ojY1fWKZE3WR4g9E-w.png" /></figure><p>Then scanned the QR code on the status page of the HomeBridge GUI. Application recognized the QR code as a “Bridge” and offered to add it to Home.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hDCQembhS9VwreFSd64l9g.jpeg" /></figure><p>Since HomeBridge is not officially certified for HomeKit, I get a warning:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O_U3EUfBQmW_5nOP9QnkNQ.png" /></figure><p>Click “Add Anyway”. Opensource projects often work better, than certified commercial applications. Then answer a few formal questions about location and name of the bridge:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ND6D0cAQFIpD7zbBlE62rw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ilvIM_Smg2znAv3U-TN1JA.png" /></figure><p>Done. It simple as always with Apple products.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k5kpNbdRiWUaf-hm6Nw10w.jpeg" /></figure><p>This is how the container properties look in the “Home” application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EZlDkajZl0rNlIwKYiB19A.png" /></figure><p>Now I can see my Nest thermostat in the Apple’s HomeKit!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sHxlZomKjU42FZwY7SpoWA.png" /></figure><p>Its not just thermostat. Now I have all my gadgets at my fingertips. Even better, no fingers needed for routine tasks such as checking doors and lights before going to bed. Just tell “Good Night” to Siri, and it locks my front door, close garage door and turn on external light.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0zT_X6xKDovY3v8cfGmowg.jpeg" /></figure><p>Now for the final step — make Nest termostat automatically switch between heating and cooling mode. There is a nice tab “Automation” in the “<a href="https://support.apple.com/en-ca/guide/iphone/iph22d98bbca/ios">Home</a>” application on iPhone. I added two scenarios:</p><ul><li>When outside temperature drops below +14 celsius, assume its winter and switch to heating.</li><li>When outside temperature raises above +20 celsius, assume its summer and switch to cooling.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yqgvFS4Q5yBiowrVtr-Lhg.jpeg" /></figure><p>Now the smart thermostat becomes really smart. I don’t have to bother with heating/cooling mode, let HomeKit and HomeBridge do it for me. After using this heterogeneous setup for a few days I can say — it’s nice when things just works!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c257eaa1383" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Moving server to The Cloud]]></title>
            <link>https://medium.com/@dmitryshnayder/moving-server-to-the-cloud-9358d62265dd?source=rss-6b123dfb1f34------2</link>
            <guid isPermaLink="false">https://medium.com/p/9358d62265dd</guid>
            <category><![CDATA[cloud-networking]]></category>
            <category><![CDATA[nested-virtualization]]></category>
            <category><![CDATA[qemu]]></category>
            <dc:creator><![CDATA[Dmitry Shnayder]]></dc:creator>
            <pubDate>Thu, 20 Apr 2023 22:33:15 GMT</pubDate>
            <atom:updated>2023-04-20T22:33:15.555Z</atom:updated>
            <content:encoded><![CDATA[<p>I am a technology enthusiast. My house is stuffed with whole zoo of electronics, from smart switches to smartphones. My WiFi network runs on enterprise-grade access points, controlled by a software-sophisticated, but hardware-simplified <a href="https://medium.com/@dmitryshnayder/qemu-kvm-networking-in-ubuntu-752d8a2b1af6">wireless controller</a>.</p><p>The controller runs flawlessly for the last few month. But what will happen if the hardware fails? Its not that complicated to have a backup of the whole RAW image of the virtualized controller. But setting up hardware box is time consuming. Why wouldn’t I move to the cloud?</p><p>I picked Google Cloud Platform for a simple reason — I already have account there and feel comfortable with Google Cloud. The same setup can be done in any other cloud, AWS or Azure.</p><p>This is what I was planning to deploy - a controller VM, running inside Google Cloud VM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/1*j3yqLbNiMGJQmACSr684Mg.png" /></figure><p>Fortunately, nested virtualization is <a href="https://cloud.google.com/compute/docs/instances/nested-virtualization/overview">supported in the Google Cloud</a>. Nested virtualization may be disabled by organization policy. But I’m lucky, it’s enabled.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*klMPXiL_LyePLvbsfqdxFg.png" /></figure><p>First step is creating cloud virtual machine. This can be done via GUI, but I feel that CLI is faster and better, as it can be copy-pasted. It requires <a href="https://cloud.google.com/sdk/docs/install">gcloud utilities</a> installed on a local laptop.</p><blockquote>gcloud compute instances create iqc-primary — enable-nested-virtualization — boot-disk-size=64GB — can-ip-forward — zone=us-central1-a — machine-type=n1-standard-8 — min-cpu-platform “Intel Haswell”</blockquote><p>The key parameter here is <em>enable-nested-virtualization</em>. This will allow running VMs inside VM. Not every CPU platform support nested virtualization, this is why <em>min-cpu-platform</em> is important. <em>can-ip-forward</em> is needed to provide access to the nested VM from outside world. The rest of parameters define how many resources should be allocated to the Google VM and where the VM should be located.</p><p>Since I’m deploying a server, I need to allow access to it from outside world. Therefore I need to configure firewall to allow external connections.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/257/1*sEWOE8_PPLVdl2rlqBaygQ.png" /></figure><p>Click “Create Firewall Rule”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*MQpp0rlsVWlLdlQbz4AJ_g.png" /></figure><p>For simplicy I specified that the new rule should be applied to all VM instances on the default network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*qQORhDrhKe7ZOP4WXM4Wlw.png" /></figure><p>It allows incoming traffic from any IPv4 host on ports 5825/TCP and 4500/UDP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*Pm2OG7heGSQRqnb7dpsKmQ.png" /></figure><p>To verify that the new rule actually applies to my new VM, I navigated to the VM instances page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*83lScgaqhbKifRmKABFktw.png" /></figure><p>Then find and click my new VM, scroll down to the ”Network Interfaces” section:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*Y5WO1sUEtaW9sZbXCALgww.png" /></figure><p>Then click on the network interface name, expand “Firewall and routes details” and check that the new rule is there:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l4ACt8B3twE390SBh7Gffg.png" /></figure><p>Now its time to login the the cloud VM and start installing the nested virtual machine. I use gcloud CLI to login to the VM, but GUI screen can be also used.</p><blockquote>gcloud compute ssh iqc-primary</blockquote><p>This gives a usual shell access to a Debian box:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/1*uBo06-qAxAzeCbBpAdXISw.png" /></figure><p>Installing nested VM is <a href="https://cloud.google.com/compute/docs/instances/nested-virtualization/creating-nested-vms">documented by Google</a>. However, some steps didn’t work for me exactly as documented. Here is what I did. First installed all the packages, requied for running QEMU and setting up networking:</p><blockquote>sudo apt update &amp;&amp; sudo apt install uml-utilities qemu-kvm bridge-utils virtinst libvirt-daemon-system libvirt-clients -y</blockquote><p>Since the Google VM is managed by CloudInit, I choose not to create additional config files in /etc. Therefore setting up networking is different from the case when a physical Linux box is used for running the controller.</p><p>I used <em>virsh</em> to create a local bridge:</p><blockquote>sudo virsh net-start default</blockquote><p>Default bridge interface is created in NAT mode and connections from host VM to nested VMs are blocked by iptables rules. This is what not mentioned in the Google documentation and cost me a bit of grey hair. Switching bridge forward mode from “nat” to “open” allows incoming connections. To update the forward mode I ran:</p><blockquote>sudo virsh net-edit default</blockquote><p>XML tag “forward” needs to change from “mode=’nat’” to “mode=’open’”. Although not required, but better disable DHCP on the “ip” setting to save a bit of resources by stopping embedded DHCP server:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*frG1DgrpVTZX92NxKTo5wg.png" /></figure><p>Save the settings. The <em>default</em> network need to start on boot, lets enable autostart:</p><blockquote>virsh net-autostart default</blockquote><p>Now I have a bridge interface called <em>virbr0</em>. I will use it to attach virtual network interfaces of my nested VM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rT-oI_34F6aa8z1AnALBng.png" /></figure><p>Before starting the nested VM a tricky part needs to be done — forwarding external connections over the bridge interface. The outer (Google) VM does not have direct L2 connection to the Internet. It has an external address assigned to it. The traffic from external hosts goes through DNAT iptables and arrives to the network interface in the VM, that has private address from 10.0.0.0/8 address range.</p><p>I used creative approach, inspired by <a href="https://cloud.google.com/compute/docs/instances/nested-virtualization/creating-nested-vms#creating_an_l2_vm_with_network_access_from_outside_the_l1_vm">Google documentation</a>. I didn’t create IP address alias. Instead I created iptable rules to forward some connections to the nested VM, while allowing SSH access to the host VM.</p><blockquote>sudo iptables -t nat -A PREROUTING -p tcp -m tcp — dport 5825 -j DNAT — to-destination 192.168.122.129 -i ens4<br>sudo iptables -t nat -A PREROUTING -p udp -m udp — dport 4500 -j DNAT — to-destination 192.168.122.129 -i ens4<br>sudo iptables -t nat -A PREROUTING -p tcp -m tcp — dport 20506 -j DNAT — to-destination 192.168.122.129 -i ens4<br>sudo iptables -t nat -A PREROUTING -p udp -m udp — dport 13907 -j DNAT — to-destination 192.168.122.129 -i ens4<br>sudo iptables -t nat -A PREROUTING -p udp -m udp — dport 13910 -j DNAT — to-destination 192.168.122.129 -i ens4<br>sudo iptables -t nat -A PREROUTING -p udp -m udp — dport 13911 -j DNAT — to-destination 192.168.122.129 -i ens4<br>sudo iptables -t nat -A POSTROUTING -s 192.168.122.129 -j MASQUERADE</blockquote><p>The last rule allows nested VM to access the Internet by NATing the outgoin traffic. The other rules forward traffic on selected ports to the nested VM. I choose IP address 192.168.122.129 for the nested VM. This is the address that I will assign later to the nested VM interface.</p><p>Ports 5825/TCP and 4500/UDP accessible for outside world, according to the firewall rules. The other ports 20506/TCP, 13907/UDP, etc I was planning to use for internal communication between nested VMs in the cloud environment.</p><p><em>iptables</em> rules need to be persisted, I don’t want to configure them manually every time when the cloud VM reboots. I installed package called <em>iptables-persistent</em></p><blockquote>sudo apt-get install iptables-persistent</blockquote><p>It conviniently asked if I want to save current iptable rules, and I happily agreed. If iptables rules need changes after this pakage installed, then the new rules can be saved manually:</p><blockquote>sudo iptables-save &gt; /etc/iptables/rules.v4</blockquote><p>Last networking step is to allow QEMU access to the bridge interface. Similarly to hardware setup I created file <em>/etc/qemu/bridge.conf</em> with the following content:</p><blockquote>allow virbr0</blockquote><p>Thats all that needs to be done for network access to the nested VM from the Internet. The bridge is here, firewall and iptables are ready to pass the traffic. Now its time to start the VM.</p><p>I copied the “raw” disk image from my local hardware setup to the cloud VM:</p><blockquote>gcloud compute scp iqc-disk-0.raw iqc-primary:</blockquote><p>This takes a while, the RAW disk image is 85 Gb.</p><p>When the copy is done, on the cloud VM I moved the RAW disk image to the final location:</p><blockquote>sudo mv ~/iqc-disk-0.raw /storage/images</blockquote><p>I slightly modified command line to refer virbr0 interface:</p><blockquote>/usr/bin/qemu-system-x86_64 \<br>-name VE6120K \<br>-chardev ‘socket,id=qmp,path=/var/run/iqc.qmp,server=on,wait=off’ \<br>-mon ‘chardev=qmp,mode=control’ \<br>-chardev ‘socket,id=qmp-event,path=/var/run/qmeventd.sock,reconnect=5’ \<br>-mon ‘chardev=qmp-event,mode=control’ \<br>-pidfile /var/run/iqc.pid \<br>-daemonize \<br>-smbios ‘type=1,uuid=c8cfd099-e370–4905-bf83-afa1e628c3ba’ \<br>-smp ‘8,sockets=1,cores=8,maxcpus=8’ \<br>-nodefaults \<br>-boot ‘menu=on,strict=on,reboot-timeout=1000’ \<br>-vga none \<br>-nographic \<br>-accel kvm \<br>-cpu host,+kvm_pv_eoi,+kvm_pv_unhalt \<br>-m 24576 \<br>-device ‘pci-bridge,id=pci.1,chassis_nr=1,bus=pci.0,addr=0x1e’ \<br>-device ‘pci-bridge,id=pci.2,chassis_nr=2,bus=pci.0,addr=0x1f’ \<br>-device ‘vmgenid,guid=965377a1–862e-4bd4–94f2–96362dd522a7’ \<br>-device ‘piix3-usb-uhci,id=uhci,bus=pci.0,addr=0x1.0x2’ \<br>-device ‘usb-tablet,id=tablet,bus=uhci.0,port=1’ \<br>-device ‘i6300esb,bus=pci.0,addr=0x7’ \<br>-iscsi ‘initiator-name=iqn.1993–08.org.debian:01:c2a1f23f7fc1’ \<br>-device ‘virtio-scsi-pci,id=scsihw0,bus=pci.0,addr=0x5’ \<br>-drive ‘file=/storage/images/iqc-disk-0.raw,if=none,id=drive-scsi0,cache=writeback,format=raw,aio=threads,detect-zeroes=on’ \<br>-device ‘scsi-hd,bus=scsihw0.0,channel=0,scsi-id=0,lun=0,drive=drive-scsi0,id=scsi0,rotation_rate=1,bootindex=100’ \<br>-machine ‘type=pc’ \<br>-chardev ‘socket,id=serial0,port=56031,host=0.0.0.0,server=on,wait=off,telnet=on’ \<br>-device ‘isa-serial,chardev=serial0’ \<br>-netdev bridge,id=eth0,br=virbr0 -device e1000,netdev=eth0,mac=52:54:00:58:25:e0,bus=pci.0,addr=0x2,id=net0 \<br>-netdev bridge,id=esa0,br=virbr0 -device virtio-net-pci,netdev=esa0,mac=52:54:00:58:25:e1,mq=on,vectors=6,bus=pci.0,addr=0x3,id=net1 \<br>-netdev bridge,id=esa1,br=virbr0 -device virtio-net-pci,netdev=esa1,mac=52:54:00:58:25:e2,mq=on,vectors=6,bus=pci.0,addr=0x4,id=net2</blockquote><p>Amazingly, the nested VM started. I didn’t configure separate bridge for admin interface, so no SSH access to the nested VM CLI for initial setup. But the specified virtual serial console in the QEMU command line:</p><blockquote>-chardev ‘socket,id=serial0,port=56031,host=0.0.0.0,server=on,wait=off,telnet=on’</blockquote><p>This is what I use to access CLI in the nested VM:</p><blockquote>telnet localhost 56031</blockquote><p>Enter user name “admin”a and default high-secure password <em>abc123</em> and here we go, a healthy CLI</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*CkAdhO3EQyq4V0JH9XPi6g.png" /></figure><p>I configured data port to use IP address 192.168.122.129. This is the address from <em>virbr0</em> address range, that I picked to forward external traffic to.</p><p>Now the moment of truth. Network setup looks complicated — Google forwards external traffic via DNAT to my cloud VM. Cloud VM forwards this traffic via another DNAT to the nested VM. Will it work? Got external IP address of the cloud VM on the VM’s config page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_vFEvADPXBgl7Hdgcs8VMw.png" /></figure><p>Opened <a href="https://34.69.xx.xx:5825/"><em>https://34.69.xx.xx:5825</em></a> in a browser, and the magic happen. GUI is here, everything works:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kq3cQgR3lVPxFvPP1sHR2A.png" /></figure><p>The last, but important step is to reserve the IP addresses. Otherwise after every reboot of the host VM the address will change and URL to access the GUI have to be updated.</p><p>Dynamic (ephemeral) IP address can be reserved on the IP addresses page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/1*j_J9xmRocqGglNYGgfVcHw.png" /></figure><p>Just find the external and internal addresses in the list and click “Reserve”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bxQDOXazjAPPcDPaOFPJag.png" /></figure><p>That it, the VM that used to be running on premise is alive in the cloud.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9358d62265dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[QEMU/KVM networking in Ubuntu]]></title>
            <link>https://medium.com/@dmitryshnayder/qemu-kvm-networking-in-ubuntu-752d8a2b1af6?source=rss-6b123dfb1f34------2</link>
            <guid isPermaLink="false">https://medium.com/p/752d8a2b1af6</guid>
            <dc:creator><![CDATA[Dmitry Shnayder]]></dc:creator>
            <pubDate>Thu, 23 Feb 2023 23:17:34 GMT</pubDate>
            <atom:updated>2024-07-22T19:17:19.320Z</atom:updated>
            <content:encoded><![CDATA[<p>I am a technology enthusiast. My house is stuffed with whole zoo of electronics, from smart switches to smartphones. As many other people I have WiFi network at home. But I’m not running consumer-grade routers. My home WiFi is based on enterprise-grade access points.</p><p>During recent upgrade of the network I decided to deploy a wireless controller locally on my network.</p><p>The controller is available as a KVM disk image and requires direct access to the wired network. I got used to deploy KVM images behind NATed interfaces, that easy to configure with QEMU. But NATed interfaces will not serve my needs, as access points need to connect to the controller directly. Let me visualize what I need. I have a virtualized server, that expects three network interfaces: <em>e1000</em> for out-of-band management, and two <em>virtio</em> for managing access points and tunneling wireless traffic:</p><figure><img alt="Controller VM" src="https://cdn-images-1.medium.com/max/374/1*rckNV_kh5yqTJof3wxt-Cw.png" /></figure><p>While admin interface is not required any external connectivity, at least one of the data interfaces needs to have direct connection to the same L2 broadcast domain as all the devices on the network. For my needs I don’t need to use both interfaces.</p><p>First, I need to choose a hardware. The software, that I was planning to use, successfully runs large-scale netwoks. It supports operations of large retailers across the world. It manages thousands of access point, passing fan traffic during NFL and NASCAR events.</p><p>Amazingly, the same software can scale down to a consumer-grade hardware without any code changes. My VM requires at least 4 CPU cores and at least 8 Gb of RAM. I’m not expecting high CPU usage, so CPU cores can be shared between the VM and base OS. But RAM is really needed. I was looking for a boxes with at least 12 Gb of RAM to run the VM and have comfortable spare RAM for the base OS. I bought a fanless mini PC on Amazon: <a href="https://a.co/d/05X5tHL">https://a.co/d/05X5tHL</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*u3cXR18UXsnP0JOH.jpg" /></figure><p>It comes with 4 core CPU, four NICs and no RAM or SSD. I don’t really need four NICs now, but who knows what I invent in a future. I bought RAM and SSD separately to satisfy my view on what a good and cheap server should look like: 32 Gb of RAM and 1 Tb SSD. Not need that much, but I’m planning to use remaining resources for something else in a future, so better get right specs now.</p><p>Installation of Ubuntu 22.04 is straightforward on this box. I choose minimal desktop installation. This may sound counter-intuitive for a server platform, but I want to have GUI out of the box, in case some configuration is easier to perform via GUI.</p><p>Before starting playing with QEMU, lets summarize what I got and what I need in terms of networking. By default, Ubuntu configures IP address on an ethernet card directly. NIC is exclusively used by kernel IP stack.</p><figure><img alt="Default Linux network" src="https://cdn-images-1.medium.com/max/632/1*tczyWjnmRnBmvnBApz2Hrw.png" /></figure><p>To share access to ethernet NIC, a bridge needs to be configured. The ethernet NIC is then attached to the bridge. Bridge acts as a virtual switch inside the host OS, forwarding L2 packets between interfaces, attached to the bridge. The bridge assigned IP address either manually or via DHCP, providing host OS access to the network. I use DHCP to assign IP address, and configured static MAC/IP mapping on the DHCP server.</p><figure><img alt="Linux with bridge" src="https://cdn-images-1.medium.com/max/628/1*hD-4ByrInt-9z-g7Lgjafw.png" /></figure><p>When QEMU starts virtual machine, it creates TAP interfaces for each network interface, configured for the VM. One end of the TAP is represented as a network interface inside the VM, the other end of TAP is attached to the bridge, providing VM direct connectivity to the ethernet network. TAP interface acts like a virtual ethernet cable, connecting virtual switch (bridge interface) with virtual network card in a VM.</p><figure><img alt="VM network" src="https://cdn-images-1.medium.com/max/648/1*uhX049d5Dnp6xP4uMZ8Aag.png" /></figure><p>Now lets express this setup in configuration files. Desktop install of Ubuntu uses Network Manager by default to configure network interfaces. If you navigate to the directory “<em>/etc/netplan</em>”, you’ll find one or more files there. Number of files and their names may be different between version of Ubuntu. I have one called “<em>01-network-manager-all.yaml</em>”. All it does is delegating managing of network interfaces to <em>NetworkManager</em>:</p><blockquote># Let NetworkManager manage all devices on this system<br>network:<br> version: 2<br> renderer: NetworkManager</blockquote><p>Lets create a custom configuration file, where all the bridges will be defined. I need two bridges:</p><ul><li>Management interface bridge. It is not connected to external network I will use it to access out-of-band management interface of the controller from host machine.</li><li>Data bridge. It needs access to the network.</li></ul><p>First, remove all the original configuration files. You may want to move them to a safe place, in case it may be needed in a future.</p><blockquote>rm <em>*.yaml</em></blockquote><p>Then create a new config file <em>01-iqc.yaml. </em>File name can be anything, as long as such a file did not originally exist. The content of the configuration file is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/323/1*EGx4KhKnD43hCEz8PDsGiQ.png" /></figure><p>I set static IP address for the bridge named iqceth0, because by default the controller assigns IP address <em>192.168.10.1/24 </em>to its admin interface. This way I can <em>ssh admin@192.168.10.1</em> from host machine to the controller. The second bridge expects to get IP address from DHCP server and attaches to the ethernet interface <em>enp2s0</em> on the host machine. Ubuntu gives names to interfaces automatically, based on the order they found on the PCI bus. Other machines may have different names, but the idea is the same — attach the ethernet interfaces to the bridge, so bridge can be connected to the network.</p><p>Then apply NetPlan configuration to the system:</p><blockquote>netplan apply</blockquote><p>Now is a scary part — reboot the box and hope it gets network connection. This is why I selected desktop installation of Ubuntu — if anything goes wrong, I have a nicce graphical tools to fix files. Since MAC address of the bridge interface will be different from MAC address of NIC, the Linux box will get different IP address from DHCP server. Again, I created static MAC/IP mapping for the bridge MAC address to be able to login to my host machine on predictable IP address.</p><p>Assuming the box rebooted and still have network access. Lets check that interfaces are properly connected and ready to provide conenction for virtial machine. Run <em>ip addr</em> command:</p><blockquote># ip addr</blockquote><blockquote>1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever<br>2: <strong>enp2s0</strong>: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel <strong>master iqcbr0</strong> state UP group default qlen 1000 link/ether 7c:83:34:b7:c2:6f brd ff:ff:ff:ff:ff:ff<br>3: enp3s0: &lt;NO-CARRIER,BROADCAST,MULTICAST,UP&gt; mtu 1500 qdisc fq_codel state DOWN group default qlen 1000 link/ether 7c:83:34:b7:c2:6e brd ff:ff:ff:ff:ff:ff<br>4: wlp1s0: &lt;NO-CARRIER,BROADCAST,MULTICAST,UP&gt; mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 2c:0d:a7:d4:97:6b brd ff:ff:ff:ff:ff:ff<br>5: <strong>iqceth0</strong>: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default qlen 1000<br>link/ether 2a:ef:36:db:77:c8 brd ff:ff:ff:ff:ff:ff<br>inet <strong>192.168.10.100/24</strong> brd 192.168.10.255 scope global iqceth0<br>valid_lft forever preferred_lft forever<br>inet6 fe80::28ef:36ff:fedb:77c8/64 scope link<br>valid_lft forever preferred_lft forever<br>6: <strong>iqcbr0</strong>: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default qlen 1000<br>link/ether 0a:1c:b7:fc:8a:c5 brd ff:ff:ff:ff:ff:ff<br>inet <strong>192.168.8.100/24</strong> brd 192.168.8.255 scope global dynamic iqcbr0<br>valid_lft 83976sec preferred_lft 83976sec<br>inet6 fe80::81c:b7ff:fefc:8ac5/64 scope link<br>valid_lft forever preferred_lft forever</blockquote><p>Based on the output above:</p><ul><li>Interface enp2s0 has no IP address and has master iqcbr0</li><li>Interface iqceth0 has IP address 192.168.10.100/24</li><li>Interface iqcbr0 has IP address configured manually or obtained from DHCP server.</li></ul><p>The network setup looks good now. Let use it to connect the VM to the network.</p><p>Desktop installation of Ubuntu 22.04 doesn’t instal QEMU/KVM by default. Let’s install it</p><blockquote>apt install qemu-kvm</blockquote><p>I copied the raw disk image image to /storage/images. The VM start I want to run has strict requirements on PCI IDs for all its peripherals, so the command line is complicated:</p><blockquote>/usr/bin/qemu-system-x86_64 \<br>-name VE6120K \<br>-chardev ‘socket,id=qmp,path=/var/run/qemu/iqc.qmp,server=on,wait=off’ \<br>-mon ‘chardev=qmp,mode=control’ \<br>-chardev ‘socket,id=qmp-event,path=/var/run/qmeventd.sock,reconnect=5’ \<br>-mon ‘chardev=qmp-event,mode=control’ \<br>-pidfile /var/run/qemu/iqc.pid \<br>-daemonize \<br>-smbios ‘type=1,uuid=c8cfd099-e370–4905-bf83-afa1e628c3ba’ \<br>-smp ‘4,sockets=1,cores=4,maxcpus=4’ \<br>-nodefaults \<br>-boot ‘menu=on,strict=on,reboot-timeout=1000’ \<br>-no-shutdown \<br>-vga none \<br>-nographic \<br>-accel kvm \<br>-cpu host,+kvm_pv_eoi,+kvm_pv_unhalt \<br>-m 8192 \<br>-device ‘pci-bridge,id=pci.1,chassis_nr=1,bus=pci.0,addr=0x1e’ \<br>-device ‘pci-bridge,id=pci.2,chassis_nr=2,bus=pci.0,addr=0x1f’ \<br>-device ‘vmgenid,guid=965377a1–862e-4bd4–94f2–96362dd522a7’ \<br>-device ‘piix3-usb-uhci,id=uhci,bus=pci.0,addr=0x1.0x2’ \<br>-device ‘usb-tablet,id=tablet,bus=uhci.0,port=1’ \<br>-device ‘i6300esb,bus=pci.0,addr=0x7’ \<br>-iscsi ‘initiator-name=iqn.1993–08.org.debian:01:c2a1f23f7fc1’ \<br>-device ‘virtio-scsi-pci,id=scsihw0,bus=pci.0,addr=0x5’ \<br>-drive ‘file=/storage/images/iqc-disk-0.raw,if=none,id=drive-scsi0,cache=writeback,format=raw,aio=threads,detect-zeroes=on’ \<br>-device ‘scsi-hd,bus=scsihw0.0,channel=0,scsi-id=0,lun=0,drive=drive-scsi0,id=scsi0,rotation_rate=1,bootindex=100’ \<br>-machine ‘type=pc’ \<br>-chardev ‘socket,id=serial0,port=56031,host=0.0.0.0,server=on,wait=off,telnet=on’ \<br>-device ‘isa-serial,chardev=serial0’ \<br>-netdev bridge,id=eth0,br=iqceth0 \<br>-device e1000,netdev=eth0,mac=52:54:00:05:06:10,bus=pci.0,addr=0x2,id=net0 \<br>-netdev bridge,id=esa0,br=iqcbr0 \<br>-device virtio-net-pci,netdev=esa0,mq=on,vectors=6,bus=pci.0,addr=0x3,id=net1\<br>-netdev bridge,id=esa1,br=iqcbr0 \<br>-device virtio-net-pci,netdev=esa1,mq=on,vectors=6,bus=pci.0,addr=0x4,id=net2</blockquote><p>There are a lot of parameters passed to QEMU to run the virtual machine. I will focus today on networking. The network connections are defined in the last few lines. Definition of each network interface in the KVM consist of two comamnd line parameters. Admin data port is defined as:</p><ul><li>netdev: Define network link (TAP interface). Link type is bridge, it connects to the Linux bridge named <em>iqceth0</em>, and name is <em>eth0</em>: <em>-netdev bridge,id=eth0,br=iqceth0</em></li><li>device: Define device inside the VM. Device hardware type is <em>e1000</em> and it uses netdev named <em>eth0.</em> Some additional parameters are set, which required for this particular controller (PCI address, number of queues). I set interface MAC address manually via <em>mac=</em> parameter to have predictable identifiers of the box. If MAC address is not specified, then QEMU will assign MAC address in range 52:54:00:XX:XX:XX. The MAC address is assigned sequentially, so they are unique within one host machine, but will be duplicate if the same network has more than one host machine. In the latter case it’s better to specify MAC address manually for each network interface: <em>-device e1000,netdev=eth0,mac=52:54:00:05:06:10,bus=pci.0,addr=0x2,id=net0</em></li></ul><p>Data ports defined as:</p><ul><li>netdev: Define network link (TAP interface). Link type is bridge, it connects to the Linux bridge named <em>iqcbr0</em>, and name is <em>esa0</em>: <em>-netdev bridge,id=esa0,br=iqcbr0</em></li><li>device: Define device inside the VM. Device hardware type is virtio-net-pci and it uses netdev named <em>esa0.</em> Some additional parameters are set, which required for this particular controller (PCI address, number of queues). I do not specify MAC address for the interfaces and let QEMU assign addresses. I don’t have other VMs on the same network, so I’m safe with automatic addresses for now: <em>-device virtio-net-pci,netdev=esa0,mq=on,vectors=6,bus=pci.0,addr=0x3,id=net1</em></li></ul><p>These three netwotk interfaces create the following topology:</p><figure><img alt="Network topology" src="https://cdn-images-1.medium.com/max/708/1*0-R1I_3fc-g8NVOgxGpuPQ.png" /></figure><p>Admin interface has no access to external network and can be used from the host machine only. Two data interfaces attached to the main wired network via common bridge <em>iqcbr0</em>.</p><p>The VM starts in background as the command line requested <em>daemonize</em> and GUI is disabled. Fingers crossed, do <em>ps -efwww | grep qemu</em> and see the VM up and running. I check if TAP interfaces are created using <em>ip link</em> command. They are here, <em>tap0</em> is attached to my bridge <em>iqceth0</em>, <em>tap1</em> and <em>tap2</em> attached to the bridge <em>iqcbr0</em>:</p><blockquote># ip link<br>……<br>25: tap0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel master iqceth0 state UNKNOWN mode DEFAULT group default qlen 1000<br> link/ether fe:a6:cd:a9:54:e2 brd ff:ff:ff:ff:ff:ff<br>26: tap1: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel master iqcbr0 state UNKNOWN mode DEFAULT group default qlen 1000<br> link/ether ba:b5:a7:15:33:4d brd ff:ff:ff:ff:ff:ff<br>27: tap2: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel master iqcbr0 state UNKNOWN mode DEFAULT group default qlen 1000<br> link/ether fe:e3:19:31:bb:31 brd ff:ff:ff:ff:ff:ff</blockquote><p>Let’s login to the admin interface. It connected to the bridge <em>iqceth0</em> with static IP address 192.168.10.100/24. The controller configures static IP address on the same subnet — 192.168.10.1/24. Try SSH to it:</p><blockquote># ssh admin@192.168.10.1</blockquote><p>Enter default high-secure password <em>abc123</em> and here we go, a healthy CLI</p><figure><img alt="CLI" src="https://cdn-images-1.medium.com/max/984/1*cexEwnRqlBHX8RdojS0EtQ.png" /></figure><p>Configured the controller to use the first data interface with static IP address from my network range:</p><figure><img alt="Data interfaces" src="https://cdn-images-1.medium.com/max/771/1*5GFJnWpm9qHhWQ76XFNfZQ.png" /></figure><p>Now moment of truth. Pinging the controller from a laptop on the network:</p><figure><img alt="Ping" src="https://cdn-images-1.medium.com/max/606/1*8GlFdoY01YeGjxuEGq7oyQ.png" /></figure><p>It just works. The fancy VM is running on plain, cheap and simple Ubuntu box, it has direct acces to the ethernet network, and the packets are going back and forth.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=752d8a2b1af6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>