{"id":261363,"date":"2021-05-27T16:00:00","date_gmt":"2021-05-27T13:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-monitor-cpu-usage-in-linux-over-time-cloudsavvy-it\/"},"modified":"2021-05-27T16:00:00","modified_gmt":"2021-05-27T13:00:00","slug":"how-to-monitor-cpu-usage-in-linux-over-time-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-cpu-usage-in-linux-over-time-cloudsavvy-it\/","title":{"rendered":"#How to Monitor CPU Usage in Linux Over Time \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a40b6b57f30d\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a40b6b57f30d\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-cpu-usage-in-linux-over-time-cloudsavvy-it\/#The_Trivial_Solution_Use_Your_Cloud_Providers_Graphs\" >The Trivial Solution: Use Your Cloud Provider\u2019s Graphs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-cpu-usage-in-linux-over-time-cloudsavvy-it\/#Using_procloadavg\" >Using \/proc\/loadavg<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-cpu-usage-in-linux-over-time-cloudsavvy-it\/#Install_sysstat\" >Install sysstat<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-monitor-cpu-usage-in-linux-over-time-cloudsavvy-it\/#Monit_Can_Alarm_You_If_CPU_Usage_Is_Too_High\" >Monit Can Alarm You If CPU Usage Is Too High<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Monitor CPU Usage in Linux Over Time \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage  alignnone wp-image-2124 size-full\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/09\/c64942de.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"top\u00a0utility displaying current CPU usage for each running process\" width=\"700\" height=\"300\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The <code>top<\/code>\u00a0utility displays current CPU usage for each running process, but what if you wanted to monitor this over time and display it on a graph? There are a few utilities for this if your cloud provider doesn\u2019t have one already.<\/p>\n<p>As a side note, if you don\u2019t have it installed already, the <code>htop<\/code>\u00a0utility (pictured above) is a lot nicer to use than default <code>top<\/code>.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"The_Trivial_Solution_Use_Your_Cloud_Providers_Graphs\"><\/span>The Trivial Solution: Use Your Cloud Provider\u2019s Graphs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>This solution is by far the easiest to use, but it won\u2019t be available for everyone. If you\u2019re on AWS, CloudWatch makes monitoring CPU usage very easy.<\/p>\n<p>From the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/console.aws.amazon.com\/cloudwatch\/home?tag=reviewgeek-20\">CloudWatch Management Console<\/a>, you select \u201cMetrics\u201d and then view metrics for EC2. The \u201cCPUUtilization\u201d metric displays your average CPU utilization:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignnone wp-image-2101 size-full\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/09\/7e58e098-12.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"In the CloudWatch Management Console, select &quot;Metrics&quot; and then view metrics for EC2. \" width=\"700\" height=\"300\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Your average CPU utilization is measured in 5-minute increments, but you can enable extended monitoring for the instance and bump it up to 1-minute increments. Doing so does cost extra though. You\u2019re also able to easily set alarms for when CPU usage gets too high as well.<\/p>\n<p>If you\u2019re on Google Cloud Platform, a graph <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ears under the \u201cMonitoring\u201d tab when you select an instance.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignnone wp-image-2100 size-full\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/09\/1f25b9b5-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Graph that appears on Google Cloud Platform under the &quot;Monitoring&quot; tab when an instance is selected.\" width=\"700\" height=\"300\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Azure has <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/azure-monitor\/insights\/vminsights-performance\">Azure Monitor<\/a>, which displays similar info:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignnone wp-image-2121 size-full\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/09\/764d0fb2-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Azure Monitor displays similar info as Google Cloud Platform.\" width=\"700\" height=\"300\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>For most other cloud providers, they\u2019ll likely have a graph like this as well.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_procloadavg\"><\/span>Using \/proc\/loadavg<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The best way to do this natively is to look at where <code>top<\/code>\u00a0gets its information from. <code>\/proc\/loadavg<\/code>\u00a0contains 1-minute, 5-minute, and 15-minute averages. You can log it with <code>cat<\/code><\/p>\n<pre>cat \/proc\/loadavg\/&#13;\n1.71 1.32 1.38 2\/97 6429<\/pre>\n<p>You can use this to generate a graph by printing each line into a comma-seperated CSV file, using some <code>awk<\/code>\u00a0magic:<\/p>\n<pre>cat \/proc\/loadavg | awk '{print $1\",\"$2\",\"$3}' &gt;&gt; cpu.csv<\/pre>\n<p>Hook this up to a <code>cron<\/code>\u00a0job running every minute, rotate logs with logrotate, and you\u2019ve got yourself a jerry-rigged CPU monitor. You can import the CSV file into Excel, where it will be easy to graph the average CPU utilization on a line chart.<\/p>\n<p>Note, the above command prints the 1-minute, 5-minute, and 15-minute averages. If you\u2019re running it every minute, it\u2019s not really necessary to print the 5- and 15-minute averages, because you can figure that out computationally.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Install_sysstat\"><\/span>Install sysstat<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>sar<\/code>\u00a0utility is great for monitoring system performance. It\u2019s included as part of <code>sysstat<\/code>, which is probably not installed by default on your system. You\u2019ll have to get it from your distro\u2019s package manager. For Debian-based systems like Ubuntu, it would be:<\/p>\n<pre>sudo apt-get install sysstat<\/pre>\n<p>Next, enable it by editing <code>\/etc\/default\/sysstat<\/code>\u00a0and setting \u201cENABLED\u201d to true.<\/p>\n<p>Doing so monitors your system and generates a report every 10 minutes, rotating them out after a week. You can modify this behavior by editing the sysstat crontab at <code>\/etc\/cron.d\/sysstat<\/code>, or by changing rotation settings in the <code>sysstat<\/code>\u00a0settings at <code>\/etc\/sysstat\/sysstat<\/code>.<\/p>\n<p>You can generate a real-time report with the following command:<\/p>\n<pre>sar -u 1 3<\/pre>\n<p><code>sysstat<\/code>\u00a0will collect background CPU usage data every minute, saving it to <code>\/var\/log\/sysstat\/<\/code>. You can then import this data for analysis, using either a spreadsheet program or a custom tool like <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/sargraph\/sargraph.github.io\">sargraph<\/a>, which displays a nice chart:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignnone wp-image-2122 size-full\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/09\/e6cf6d42.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"sargraph displays a chart of CPU usage.\" width=\"700\" height=\"322\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can also use command line utilities for plotting graphs like this, such as <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/tenox7\/ttyplot\"><code>ttyplot<\/code><\/a>, but none of them come close to being as easy to use (and as nice looking) as a GUI. The command line is beat on this one\u2014charts are nicer.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Monit_Can_Alarm_You_If_CPU_Usage_Is_Too_High\"><\/span>Monit Can Alarm You If CPU Usage Is Too High<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" alignnone wp-image-1232 size-full\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/07\/6a91c397.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Monit logo\" width=\"700\" height=\"299\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Monit is a open source monitoring suite for Unix that checks the health of your server and can be configured to send you notifications if your server\u2019s CPU usage becomes dangerously high. Read our guide to setting it up to learn more.<\/p>\n<p>Note that CloudWatch achieves the same thing out of the box with alarms, and it can operate on multiple different metrics, not just CPU usage.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/2117\/how-to-monitor-cpu-usage-in-linux-over-time\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Monitor CPU Usage in Linux Over Time \u2013 CloudSavvy IT&#8221; The top\u00a0utility displays current CPU usage for each running process, but what if you wanted to monitor this over time and display it on a graph? There are a few utilities for this if your cloud provider doesn\u2019t have one already. As a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":261364,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/09\/c64942de.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-261363","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/261363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=261363"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/261363\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/261364"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=261363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=261363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=261363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}