How can I get the total CPU usage of a Linux machine with 1 or n CPU cores?












2















I am currently using the below method to extract CPU usage idle value from top command and subtracting the value from 100. Is this method correct and is there a better way to achieve the same.



Also, my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed.



CPU_IDLE="$(top -bn2 | grep -F '%Cpu' | tail -n 4 | gawk '{print $8 $9}' | tr -s 'n:,[:alpha:]' ' '| gawk '{print $2}'),"









share|improve this question





























    2















    I am currently using the below method to extract CPU usage idle value from top command and subtracting the value from 100. Is this method correct and is there a better way to achieve the same.



    Also, my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed.



    CPU_IDLE="$(top -bn2 | grep -F '%Cpu' | tail -n 4 | gawk '{print $8 $9}' | tr -s 'n:,[:alpha:]' ' '| gawk '{print $2}'),"









    share|improve this question



























      2












      2








      2


      1






      I am currently using the below method to extract CPU usage idle value from top command and subtracting the value from 100. Is this method correct and is there a better way to achieve the same.



      Also, my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed.



      CPU_IDLE="$(top -bn2 | grep -F '%Cpu' | tail -n 4 | gawk '{print $8 $9}' | tr -s 'n:,[:alpha:]' ' '| gawk '{print $2}'),"









      share|improve this question
















      I am currently using the below method to extract CPU usage idle value from top command and subtracting the value from 100. Is this method correct and is there a better way to achieve the same.



      Also, my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed.



      CPU_IDLE="$(top -bn2 | grep -F '%Cpu' | tail -n 4 | gawk '{print $8 $9}' | tr -s 'n:,[:alpha:]' ' '| gawk '{print $2}'),"






      cpu-usage






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 13 '17 at 5:14







      Bandi Sandeep

















      asked Apr 12 '17 at 6:07









      Bandi SandeepBandi Sandeep

      113




      113






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Yes, your method alright.



          But, you could use mpstat to get more details.



          It is included in sysstat package, and if you don't have it installed...



          On CentOS / RHEL:



          sudo yum install sysstat


          On Ubuntu / Debian:



          sudo apt-get install sysstat


          Use bellow command if you don't have any permission to install a package



          grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'





          share|improve this answer


























          • Sorry, I forgot to add that my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed.

            – Bandi Sandeep
            Apr 12 '17 at 9:48











          • You could extract a deb package and copy files manually, or compile from source code.

            – Michael D.
            Apr 12 '17 at 10:11











          • Hey Rakib, looks like the command is not giving actual values. Yesterday ,I ran a script to log the CPU usage for almost 8 hours and I plotted a graph from the datapoints. However, it didn't match to the CPU history shown on my hyper visor for this linux VM.

            – Bandi Sandeep
            Apr 13 '17 at 4:51













          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f358467%2fhow-can-i-get-the-total-cpu-usage-of-a-linux-machine-with-1-or-n-cpu-cores%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Yes, your method alright.



          But, you could use mpstat to get more details.



          It is included in sysstat package, and if you don't have it installed...



          On CentOS / RHEL:



          sudo yum install sysstat


          On Ubuntu / Debian:



          sudo apt-get install sysstat


          Use bellow command if you don't have any permission to install a package



          grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'





          share|improve this answer


























          • Sorry, I forgot to add that my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed.

            – Bandi Sandeep
            Apr 12 '17 at 9:48











          • You could extract a deb package and copy files manually, or compile from source code.

            – Michael D.
            Apr 12 '17 at 10:11











          • Hey Rakib, looks like the command is not giving actual values. Yesterday ,I ran a script to log the CPU usage for almost 8 hours and I plotted a graph from the datapoints. However, it didn't match to the CPU history shown on my hyper visor for this linux VM.

            – Bandi Sandeep
            Apr 13 '17 at 4:51


















          0














          Yes, your method alright.



          But, you could use mpstat to get more details.



          It is included in sysstat package, and if you don't have it installed...



          On CentOS / RHEL:



          sudo yum install sysstat


          On Ubuntu / Debian:



          sudo apt-get install sysstat


          Use bellow command if you don't have any permission to install a package



          grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'





          share|improve this answer


























          • Sorry, I forgot to add that my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed.

            – Bandi Sandeep
            Apr 12 '17 at 9:48











          • You could extract a deb package and copy files manually, or compile from source code.

            – Michael D.
            Apr 12 '17 at 10:11











          • Hey Rakib, looks like the command is not giving actual values. Yesterday ,I ran a script to log the CPU usage for almost 8 hours and I plotted a graph from the datapoints. However, it didn't match to the CPU history shown on my hyper visor for this linux VM.

            – Bandi Sandeep
            Apr 13 '17 at 4:51
















          0












          0








          0







          Yes, your method alright.



          But, you could use mpstat to get more details.



          It is included in sysstat package, and if you don't have it installed...



          On CentOS / RHEL:



          sudo yum install sysstat


          On Ubuntu / Debian:



          sudo apt-get install sysstat


          Use bellow command if you don't have any permission to install a package



          grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'





          share|improve this answer















          Yes, your method alright.



          But, you could use mpstat to get more details.



          It is included in sysstat package, and if you don't have it installed...



          On CentOS / RHEL:



          sudo yum install sysstat


          On Ubuntu / Debian:



          sudo apt-get install sysstat


          Use bellow command if you don't have any permission to install a package



          grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 12 '17 at 10:45

























          answered Apr 12 '17 at 6:24









          RakibRakib

          949812




          949812













          • Sorry, I forgot to add that my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed.

            – Bandi Sandeep
            Apr 12 '17 at 9:48











          • You could extract a deb package and copy files manually, or compile from source code.

            – Michael D.
            Apr 12 '17 at 10:11











          • Hey Rakib, looks like the command is not giving actual values. Yesterday ,I ran a script to log the CPU usage for almost 8 hours and I plotted a graph from the datapoints. However, it didn't match to the CPU history shown on my hyper visor for this linux VM.

            – Bandi Sandeep
            Apr 13 '17 at 4:51





















          • Sorry, I forgot to add that my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed.

            – Bandi Sandeep
            Apr 12 '17 at 9:48











          • You could extract a deb package and copy files manually, or compile from source code.

            – Michael D.
            Apr 12 '17 at 10:11











          • Hey Rakib, looks like the command is not giving actual values. Yesterday ,I ran a script to log the CPU usage for almost 8 hours and I plotted a graph from the datapoints. However, it didn't match to the CPU history shown on my hyper visor for this linux VM.

            – Bandi Sandeep
            Apr 13 '17 at 4:51



















          Sorry, I forgot to add that my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed.

          – Bandi Sandeep
          Apr 12 '17 at 9:48





          Sorry, I forgot to add that my linux VM is a stripped down version and has only few basic tools like top. Installing other tools is not an option as the package manager is also removed.

          – Bandi Sandeep
          Apr 12 '17 at 9:48













          You could extract a deb package and copy files manually, or compile from source code.

          – Michael D.
          Apr 12 '17 at 10:11





          You could extract a deb package and copy files manually, or compile from source code.

          – Michael D.
          Apr 12 '17 at 10:11













          Hey Rakib, looks like the command is not giving actual values. Yesterday ,I ran a script to log the CPU usage for almost 8 hours and I plotted a graph from the datapoints. However, it didn't match to the CPU history shown on my hyper visor for this linux VM.

          – Bandi Sandeep
          Apr 13 '17 at 4:51







          Hey Rakib, looks like the command is not giving actual values. Yesterday ,I ran a script to log the CPU usage for almost 8 hours and I plotted a graph from the datapoints. However, it didn't match to the CPU history shown on my hyper visor for this linux VM.

          – Bandi Sandeep
          Apr 13 '17 at 4:51




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f358467%2fhow-can-i-get-the-total-cpu-usage-of-a-linux-machine-with-1-or-n-cpu-cores%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

          is 'sed' thread safe

          How to make a Squid Proxy server?