Writing awk to extract cpu utilization from sar command












0















I am trying to extract the values of the %user,%nice etc from the output of the sar command.



sar -P ALL 1 1


Output of this:



Linux 2.6.32-358.el6.x86_64 (ftizsldapp009.ftiz.cummins.com)    09/28/2015      _x86_64_        (4 CPU)

02:49:40 PM CPU %user %nice %system %iowait %steal %idle
02:49:41 PM all 3.01 0.00 2.51 0.00 0.00 94.49
02:49:41 PM 0 1.98 0.00 3.96 0.00 0.00 94.06
02:49:41 PM 1 6.00 0.00 4.00 0.00 0.00 90.00
02:49:41 PM 2 2.00 0.00 1.00 0.00 0.00 97.00
02:49:41 PM 3 1.98 0.00 2.97 0.00 0.00 95.05

Average: CPU %user %nice %system %iowait %steal %idle
Average: all 3.01 0.00 2.51 0.00 0.00 94.49
Average: 0 1.98 0.00 3.96 0.00 0.00 94.06
Average: 1 6.00 0.00 4.00 0.00 0.00 90.00
Average: 2 2.00 0.00 1.00 0.00 0.00 97.00
Average: 3 1.98 0.00 2.97 0.00 0.00 95.05


I have written following string to get the values but this doesn't seen to work.



My Command :



sar -P ALL 1 1 | 
awk '{cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF}'
'{printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem,
pctIowait, pctIdle}'









share|improve this question





























    0















    I am trying to extract the values of the %user,%nice etc from the output of the sar command.



    sar -P ALL 1 1


    Output of this:



    Linux 2.6.32-358.el6.x86_64 (ftizsldapp009.ftiz.cummins.com)    09/28/2015      _x86_64_        (4 CPU)

    02:49:40 PM CPU %user %nice %system %iowait %steal %idle
    02:49:41 PM all 3.01 0.00 2.51 0.00 0.00 94.49
    02:49:41 PM 0 1.98 0.00 3.96 0.00 0.00 94.06
    02:49:41 PM 1 6.00 0.00 4.00 0.00 0.00 90.00
    02:49:41 PM 2 2.00 0.00 1.00 0.00 0.00 97.00
    02:49:41 PM 3 1.98 0.00 2.97 0.00 0.00 95.05

    Average: CPU %user %nice %system %iowait %steal %idle
    Average: all 3.01 0.00 2.51 0.00 0.00 94.49
    Average: 0 1.98 0.00 3.96 0.00 0.00 94.06
    Average: 1 6.00 0.00 4.00 0.00 0.00 90.00
    Average: 2 2.00 0.00 1.00 0.00 0.00 97.00
    Average: 3 1.98 0.00 2.97 0.00 0.00 95.05


    I have written following string to get the values but this doesn't seen to work.



    My Command :



    sar -P ALL 1 1 | 
    awk '{cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF}'
    '{printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem,
    pctIowait, pctIdle}'









    share|improve this question



























      0












      0








      0








      I am trying to extract the values of the %user,%nice etc from the output of the sar command.



      sar -P ALL 1 1


      Output of this:



      Linux 2.6.32-358.el6.x86_64 (ftizsldapp009.ftiz.cummins.com)    09/28/2015      _x86_64_        (4 CPU)

      02:49:40 PM CPU %user %nice %system %iowait %steal %idle
      02:49:41 PM all 3.01 0.00 2.51 0.00 0.00 94.49
      02:49:41 PM 0 1.98 0.00 3.96 0.00 0.00 94.06
      02:49:41 PM 1 6.00 0.00 4.00 0.00 0.00 90.00
      02:49:41 PM 2 2.00 0.00 1.00 0.00 0.00 97.00
      02:49:41 PM 3 1.98 0.00 2.97 0.00 0.00 95.05

      Average: CPU %user %nice %system %iowait %steal %idle
      Average: all 3.01 0.00 2.51 0.00 0.00 94.49
      Average: 0 1.98 0.00 3.96 0.00 0.00 94.06
      Average: 1 6.00 0.00 4.00 0.00 0.00 90.00
      Average: 2 2.00 0.00 1.00 0.00 0.00 97.00
      Average: 3 1.98 0.00 2.97 0.00 0.00 95.05


      I have written following string to get the values but this doesn't seen to work.



      My Command :



      sar -P ALL 1 1 | 
      awk '{cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF}'
      '{printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem,
      pctIowait, pctIdle}'









      share|improve this question
















      I am trying to extract the values of the %user,%nice etc from the output of the sar command.



      sar -P ALL 1 1


      Output of this:



      Linux 2.6.32-358.el6.x86_64 (ftizsldapp009.ftiz.cummins.com)    09/28/2015      _x86_64_        (4 CPU)

      02:49:40 PM CPU %user %nice %system %iowait %steal %idle
      02:49:41 PM all 3.01 0.00 2.51 0.00 0.00 94.49
      02:49:41 PM 0 1.98 0.00 3.96 0.00 0.00 94.06
      02:49:41 PM 1 6.00 0.00 4.00 0.00 0.00 90.00
      02:49:41 PM 2 2.00 0.00 1.00 0.00 0.00 97.00
      02:49:41 PM 3 1.98 0.00 2.97 0.00 0.00 95.05

      Average: CPU %user %nice %system %iowait %steal %idle
      Average: all 3.01 0.00 2.51 0.00 0.00 94.49
      Average: 0 1.98 0.00 3.96 0.00 0.00 94.06
      Average: 1 6.00 0.00 4.00 0.00 0.00 90.00
      Average: 2 2.00 0.00 1.00 0.00 0.00 97.00
      Average: 3 1.98 0.00 2.97 0.00 0.00 95.05


      I have written following string to get the values but this doesn't seen to work.



      My Command :



      sar -P ALL 1 1 | 
      awk '{cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF}'
      '{printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem,
      pctIowait, pctIdle}'






      awk cpu-usage sar






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 21 mins ago









      perror

      1,95542036




      1,95542036










      asked Sep 28 '15 at 18:56









      user133617user133617

      11




      11






















          1 Answer
          1






          active

          oldest

          votes


















          0














          try



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ;
          printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}'




          • NF == 9 you need to filter on nine filed (NF)


          • $3 != "all" skip the line that summarize cpus

          • no caracter but end-of-line after |


          a more compact version



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" { printf "%-3s %9s %9s %9s %9s %9sn",$3,$4,$5,$6,$7,$NF}'


          a more more compact version



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" { $1=$2=$8="" ; print ; }'





          share|improve this answer
























          • Thanks a lot. The following seems to give me expected view. sar -P ALL 1 1|awk '{ printf "%-3s %9s %9s %9s %9s %9s %9sn",$2,$3,$4,$5,$6,$7,$8}' I also need to put the output in format like. all CPU %user = 51.50 0 CPU %user = 97.00

            – user133617
            Sep 28 '15 at 19:38











          • remove && $3 != "all" to get the line with all cpus.

            – Archemar
            Sep 28 '15 at 19:41











          • sar -P ALL 1 1 | awk 'NF == 9 {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ; printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' This seems to give me the correct view. Is there a way in which i can iterate the values of cpu cores and their corresponding %user,%nice values. I am looking for output like 0CPU %user= 68.38 1CPU %user=57.43 0CPU %nice=0.24 1CPU %nice=4.21

            – user133617
            Sep 28 '15 at 20:04













          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%2f232601%2fwriting-awk-to-extract-cpu-utilization-from-sar-command%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














          try



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ;
          printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}'




          • NF == 9 you need to filter on nine filed (NF)


          • $3 != "all" skip the line that summarize cpus

          • no caracter but end-of-line after |


          a more compact version



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" { printf "%-3s %9s %9s %9s %9s %9sn",$3,$4,$5,$6,$7,$NF}'


          a more more compact version



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" { $1=$2=$8="" ; print ; }'





          share|improve this answer
























          • Thanks a lot. The following seems to give me expected view. sar -P ALL 1 1|awk '{ printf "%-3s %9s %9s %9s %9s %9s %9sn",$2,$3,$4,$5,$6,$7,$8}' I also need to put the output in format like. all CPU %user = 51.50 0 CPU %user = 97.00

            – user133617
            Sep 28 '15 at 19:38











          • remove && $3 != "all" to get the line with all cpus.

            – Archemar
            Sep 28 '15 at 19:41











          • sar -P ALL 1 1 | awk 'NF == 9 {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ; printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' This seems to give me the correct view. Is there a way in which i can iterate the values of cpu cores and their corresponding %user,%nice values. I am looking for output like 0CPU %user= 68.38 1CPU %user=57.43 0CPU %nice=0.24 1CPU %nice=4.21

            – user133617
            Sep 28 '15 at 20:04


















          0














          try



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ;
          printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}'




          • NF == 9 you need to filter on nine filed (NF)


          • $3 != "all" skip the line that summarize cpus

          • no caracter but end-of-line after |


          a more compact version



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" { printf "%-3s %9s %9s %9s %9s %9sn",$3,$4,$5,$6,$7,$NF}'


          a more more compact version



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" { $1=$2=$8="" ; print ; }'





          share|improve this answer
























          • Thanks a lot. The following seems to give me expected view. sar -P ALL 1 1|awk '{ printf "%-3s %9s %9s %9s %9s %9s %9sn",$2,$3,$4,$5,$6,$7,$8}' I also need to put the output in format like. all CPU %user = 51.50 0 CPU %user = 97.00

            – user133617
            Sep 28 '15 at 19:38











          • remove && $3 != "all" to get the line with all cpus.

            – Archemar
            Sep 28 '15 at 19:41











          • sar -P ALL 1 1 | awk 'NF == 9 {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ; printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' This seems to give me the correct view. Is there a way in which i can iterate the values of cpu cores and their corresponding %user,%nice values. I am looking for output like 0CPU %user= 68.38 1CPU %user=57.43 0CPU %nice=0.24 1CPU %nice=4.21

            – user133617
            Sep 28 '15 at 20:04
















          0












          0








          0







          try



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ;
          printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}'




          • NF == 9 you need to filter on nine filed (NF)


          • $3 != "all" skip the line that summarize cpus

          • no caracter but end-of-line after |


          a more compact version



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" { printf "%-3s %9s %9s %9s %9s %9sn",$3,$4,$5,$6,$7,$NF}'


          a more more compact version



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" { $1=$2=$8="" ; print ; }'





          share|improve this answer













          try



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ;
          printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}'




          • NF == 9 you need to filter on nine filed (NF)


          • $3 != "all" skip the line that summarize cpus

          • no caracter but end-of-line after |


          a more compact version



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" { printf "%-3s %9s %9s %9s %9s %9sn",$3,$4,$5,$6,$7,$NF}'


          a more more compact version



           sar -P ALL 1 1 | 
          awk 'NF == 9 && $3 != "all" { $1=$2=$8="" ; print ; }'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 28 '15 at 19:22









          ArchemarArchemar

          20.2k93973




          20.2k93973













          • Thanks a lot. The following seems to give me expected view. sar -P ALL 1 1|awk '{ printf "%-3s %9s %9s %9s %9s %9s %9sn",$2,$3,$4,$5,$6,$7,$8}' I also need to put the output in format like. all CPU %user = 51.50 0 CPU %user = 97.00

            – user133617
            Sep 28 '15 at 19:38











          • remove && $3 != "all" to get the line with all cpus.

            – Archemar
            Sep 28 '15 at 19:41











          • sar -P ALL 1 1 | awk 'NF == 9 {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ; printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' This seems to give me the correct view. Is there a way in which i can iterate the values of cpu cores and their corresponding %user,%nice values. I am looking for output like 0CPU %user= 68.38 1CPU %user=57.43 0CPU %nice=0.24 1CPU %nice=4.21

            – user133617
            Sep 28 '15 at 20:04





















          • Thanks a lot. The following seems to give me expected view. sar -P ALL 1 1|awk '{ printf "%-3s %9s %9s %9s %9s %9s %9sn",$2,$3,$4,$5,$6,$7,$8}' I also need to put the output in format like. all CPU %user = 51.50 0 CPU %user = 97.00

            – user133617
            Sep 28 '15 at 19:38











          • remove && $3 != "all" to get the line with all cpus.

            – Archemar
            Sep 28 '15 at 19:41











          • sar -P ALL 1 1 | awk 'NF == 9 {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ; printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' This seems to give me the correct view. Is there a way in which i can iterate the values of cpu cores and their corresponding %user,%nice values. I am looking for output like 0CPU %user= 68.38 1CPU %user=57.43 0CPU %nice=0.24 1CPU %nice=4.21

            – user133617
            Sep 28 '15 at 20:04



















          Thanks a lot. The following seems to give me expected view. sar -P ALL 1 1|awk '{ printf "%-3s %9s %9s %9s %9s %9s %9sn",$2,$3,$4,$5,$6,$7,$8}' I also need to put the output in format like. all CPU %user = 51.50 0 CPU %user = 97.00

          – user133617
          Sep 28 '15 at 19:38





          Thanks a lot. The following seems to give me expected view. sar -P ALL 1 1|awk '{ printf "%-3s %9s %9s %9s %9s %9s %9sn",$2,$3,$4,$5,$6,$7,$8}' I also need to put the output in format like. all CPU %user = 51.50 0 CPU %user = 97.00

          – user133617
          Sep 28 '15 at 19:38













          remove && $3 != "all" to get the line with all cpus.

          – Archemar
          Sep 28 '15 at 19:41





          remove && $3 != "all" to get the line with all cpus.

          – Archemar
          Sep 28 '15 at 19:41













          sar -P ALL 1 1 | awk 'NF == 9 {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ; printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' This seems to give me the correct view. Is there a way in which i can iterate the values of cpu cores and their corresponding %user,%nice values. I am looking for output like 0CPU %user= 68.38 1CPU %user=57.43 0CPU %nice=0.24 1CPU %nice=4.21

          – user133617
          Sep 28 '15 at 20:04







          sar -P ALL 1 1 | awk 'NF == 9 {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF ; printf "%-3s %9s %9s %9s %9s %9sn", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' This seems to give me the correct view. Is there a way in which i can iterate the values of cpu cores and their corresponding %user,%nice values. I am looking for output like 0CPU %user= 68.38 1CPU %user=57.43 0CPU %nice=0.24 1CPU %nice=4.21

          – user133617
          Sep 28 '15 at 20:04




















          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%2f232601%2fwriting-awk-to-extract-cpu-utilization-from-sar-command%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

          サソリ

          広島県道265号伴広島線

          Setup Asymptote in Texstudio