Ad Widget

Collapse

Howto: Measure %cpu of a given process (apache2/mysql for me)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mikecrowe
    Member
    • Dec 2007
    • 34

    #1

    Howto: Measure %cpu of a given process (apache2/mysql for me)

    In your zabbix_agentd.conf, enter this for your user parameters:

    Code:
    UserParameter=cpu.mysql,ps -eo pcpu,pid,user | grep mysql | sort -k 1 -r | awk -- 'BEGIN { total=0 } { total += $1 } END { print total }'
    UserParameter=cpu.apache,ps -eo pcpu,pid,user | grep www-data | sort -k 1 -r | awk -- 'BEGIN { total=0 } { total += $1 } END { print total }'
  • noxis
    Senior Member
    • Aug 2007
    • 145

    #2
    ps doesn't show the cpu% at that time slice (like top does). It shows the % of the cpu it has used since the process was started. Meaning that all the %'s you see in a ps output will not add up to 100 (or will be higher).

    This number can be highly confusing.

    However saying all that, those two lines of bash/awk will be highly useful so I thank you Although it is worth noting you are actually measuring a user there rather than a specific process/application.

    Comment

    • mikecrowe
      Member
      • Dec 2007
      • 34

      #3
      I tried top, but couldn't figure out how to summarize it. I'd love to move that direction, if you could help:

      Code:
      top -n 1 -S -b -U www-data
      top - 11:15:28 up 77 days,  4:31,  3 users,  load average: 0.65, 0.48, 0.43
      Tasks:  79 total,   1 running,  78 sleeping,   0 stopped,   0 zombie
      Cpu(s):  5.1%us,  5.4%sy,  0.0%ni, 89.0%id,  0.2%wa,  0.1%hi,  0.2%si,  0.0%st
      Mem:    520020k total,   452688k used,    67332k free,    58236k buffers
      Swap:  1036152k total,    21724k used,  1014428k free,   179612k cached
      
        PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
      10338 www-data  15   0 69212  18m 7732 S  0.0  3.6   2:13.40 apache2
      13294 www-data  15   0 66940  16m 7752 S  0.0  3.2   1:31.49 apache2
      15102 www-data  15   0 69196  18m 7536 S  0.0  3.6   0:53.08 apache2
      16241 www-data  15   0 69188  17m 6848 S  0.0  3.4   0:29.33 apache2
      16307 www-data  15   0 68340  16m 6876 S  0.0  3.3   0:29.17 apache2
      16308 www-data  15   0 68112  16m 6840 S  0.0  3.2   0:27.25 apache2
      17039 www-data  15   0 69116  17m 6492 S  0.0  3.4   0:15.36 apache2
      17166 www-data  15   0 69128  17m 6616 S  0.0  3.4   0:11.85 apache2
      17433 www-data  15   0 68660  17m 7208 S  0.0  3.4   0:08.47 apache2
      17896 www-data  15   0 69164  16m 6280 S  0.0  3.3   0:01.02 apache2
      Can I add Cpu(s) us+sy in line 5 to compute?
      How can I determine it from the above?

      Comment

      • noxis
        Senior Member
        • Aug 2007
        • 145

        #4
        Originally posted by mikecrowe
        I tried top, but couldn't figure out how to summarize it. I'd love to move that direction, if you could help:

        Code:
        top -n 1 -S -b -U www-data
        top - 11:15:28 up 77 days,  4:31,  3 users,  load average: 0.65, 0.48, 0.43
        Tasks:  79 total,   1 running,  78 sleeping,   0 stopped,   0 zombie
        Cpu(s):  5.1%us,  5.4%sy,  0.0%ni, 89.0%id,  0.2%wa,  0.1%hi,  0.2%si,  0.0%st
        Mem:    520020k total,   452688k used,    67332k free,    58236k buffers
        Swap:  1036152k total,    21724k used,  1014428k free,   179612k cached
        
          PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
        10338 www-data  15   0 69212  18m 7732 S  0.0  3.6   2:13.40 apache2
        13294 www-data  15   0 66940  16m 7752 S  0.0  3.2   1:31.49 apache2
        15102 www-data  15   0 69196  18m 7536 S  0.0  3.6   0:53.08 apache2
        16241 www-data  15   0 69188  17m 6848 S  0.0  3.4   0:29.33 apache2
        16307 www-data  15   0 68340  16m 6876 S  0.0  3.3   0:29.17 apache2
        16308 www-data  15   0 68112  16m 6840 S  0.0  3.2   0:27.25 apache2
        17039 www-data  15   0 69116  17m 6492 S  0.0  3.4   0:15.36 apache2
        17166 www-data  15   0 69128  17m 6616 S  0.0  3.4   0:11.85 apache2
        17433 www-data  15   0 68660  17m 7208 S  0.0  3.4   0:08.47 apache2
        17896 www-data  15   0 69164  16m 6280 S  0.0  3.3   0:01.02 apache2
        Can I add Cpu(s) us+sy in line 5 to compute?
        How can I determine it from the above?
        Yea I had exactly the same problem when wrestling with this one. I still dont have a better way than using PS output to figure out cpu/mem usage per process. I am sure the source of TOP would reveal where its information is coming from. Any script would need to run for a small amount of time in order to gather the date we both require. As in cpu usage over 1/5/10 seconds etc.. Undoubtedly available from somewhere inside /proc.

        Comment

        • mikecrowe
          Member
          • Dec 2007
          • 34

          #5
          OK, give this a shot:
          Code:
          UserParameter=cpu.mysql,top -b -n 1 -U mysql | awk -- 'BEGIN { i=0;total=0 } { if ( i++ > 6 ) total+= $9; } END { print total }'
          UserParameter=cpu.apache,top -b -n 1 -U www-data | awk -- 'BEGIN { i=0;total=0 } { if ( i++ > 6 ) total+= $9; } END { print total }'
          (edit -- forgot -b in top command)
          Last edited by mikecrowe; 30-01-2008, 04:38.

          Comment

          • bondbig
            Member
            • Jul 2008
            • 68

            #6
            i did as told above and get:
            Type of received value [115.4] is not sutable for [cpu.mysql@db1] having type [3]
            in zabbix

            Comment

            • txalin
              Member
              • Jan 2008
              • 31

              #7
              Originally posted by bondbig
              i did as told above and get:
              Type of received value [115.4] is not sutable for [cpu.mysql@db1] having type [3]
              in zabbix
              Change the type on zabbix server to floating point

              Comment

              • bondbig
                Member
                • Jul 2008
                • 68

                #8
                i tried, no success. It still tells the same. But the item is working!

                Comment

                • txalin
                  Member
                  • Jan 2008
                  • 31

                  #9
                  Originally posted by bondbig
                  i tried, no success. It still tells the same. But the item is working!
                  wait a few minutes, changing data type takes a fw time

                  Better explanation:

                  If you select Integer as a data type (that's type 3), zabbix server only can receive numbers ike 1,2,3, etc... Floating point allows to receive numbers like 11.3, 16.5 like the %cpu value

                  Comment

                  • bondbig
                    Member
                    • Jul 2008
                    • 68

                    #10
                    Oh, i didn't know about delay. Thanks for reply!

                    Comment

                    Working...