Ad Widget

Collapse

How are you getting CPU usage of a Linux process?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tchjts1
    Senior Member
    • May 2008
    • 1605

    #1

    How are you getting CPU usage of a Linux process?

    We are trying to monitor how much CPU usage a process uses on Linux.
    There is no native Zabbix agent way of getting this.
    This is on Zabbix 2.0.9

    I have tried using this UserParameter with no luck. Anyone have a better way?
    Code:
    UserParameter=cpulog,top -b | grep caiLogA2 | awk '{print $9}'
    Regards -
    Jeff
  • jan.garaj
    Senior Member
    Zabbix Certified Specialist
    • Jan 2010
    • 506

    #2
    I'm using the same parameter convention in key as proc.num[], but not all parameters are used in code (e.g. user).
    Code:
    # 2.) CPU UTILIZATION PER PROCESS
    # CPU utilization is normalized (divided by number of cores)
    UserParameter=proc.util[*],CORES=`grep -c ^processor /proc/cpuinfo 2>/dev/null`; /bin/ps u -C $1 | /bin/grep -e '$4' | awk -v cores=$$CORES '{sum+=$$3}END{printf "%.3f",sum/cores}'
    Unfortunately the minimum utilization obtained from ps is 0,1%. If you need better precision (or more details about type of utilizations), you should to use /proc/pid/stat http://stackoverflow.com/questions/1...-proc-pid-stat
    Devops Monitoring Expert advice: Dockerize/automate/monitor all the things.
    My DevOps stack: Docker / Kubernetes / Mesos / ECS / Terraform / Elasticsearch / Zabbix / Grafana / Puppet / Ansible / Vagrant

    Comment

    • tchjts1
      Senior Member
      • May 2008
      • 1605

      #3
      jangaraj - Thanks for the info. I will definitely give that a try and let you know how it works out.

      Jeff

      Comment

      • tchjts1
        Senior Member
        • May 2008
        • 1605

        #4
        Hey Jan. I implemented that, but the ouput of TOP doesn't seem to match too well with what Zabbix sees. I have the polling interval set to 30. Do you think that the momentary spikes (75.5, 68.6, 40.0) are simply too short lived to be caught by the script?

        # top -b | grep caiLogA2
        27153 root 22 0 121m 26m 2080 S 0.0 0.1 1483:56 caiLogA2
        27153 root 22 0 121m 26m 2080 S 0.0 0.1 1483:56 caiLogA2
        27153 root 22 0 121m 26m 2080 S 0.0 0.1 1483:56 caiLogA2
        27153 root 22 0 121m 26m 2080 S 75.5 0.1 1483:58 caiLogA2
        27153 root 22 0 121m 26m 2080 S 68.6 0.1 1484:01 caiLogA2
        27153 root 22 0 121m 26m 2080 S 40.0 0.1 1484:02 caiLogA2
        27153 root 22 0 121m 26m 2080 S 0.0 0.1 1484:02 caiLogA2
        27153 root 22 0 121m 26m 2080 S 0.0 0.1 1484:02 caiLogA2
        27153 root 22 0 121m 26m 2080 S 0.0 0.1 1484:02 caiLogA2
        27153 root 22 0 121m 26m 2080 S 0.0 0.1 1484:02 caiLogA2
        27153 root 22 0 121m 26m 2080 S 0.0 0.1 1484:02 caiLogA2
        27153 root 22 0 121m 26m 2080 S 0.0 0.1 1484:02 caiLogA2
        27153 root 22 0 121m 26m 2080 S 0.0 0.1 1484:02 caiLogA2
        27153 root 22 0 121m 26m 2080 S 0.0 0.1 1484:02 caiLogA2
        27153 root 22 0 121m 26m 2080 S 0.0 0.1 1484:02 caiLogA2
        Attached Files

        Comment

        • jan.garaj
          Senior Member
          Zabbix Certified Specialist
          • Jan 2010
          • 506

          #5
          I see your problem. What do you want to see in your Zabbix graphs in this case (only 9 seconds in row is process utilized and then 51 seconds has 0 utilization)?

          And BTW top and ps have different %CPU implementation:
          ps
          Code:
          %CPU	    cpu utilization of the process in "##.#" format.
          		    Currently, it is the CPU time used divided by the time the
          		    process has been running (cputime/realtime ratio),
          		    expressed as a percentage. It will not add up to 100%
          		    unless you are lucky.
          top
          Code:
          %CPU	--  CPU usage
          	  The task's share of the elapsed  CPU	time  [B]since  the  last	screen
          	  update[/B],  expressed as a percentage of total CPU time.	 In a true SMP
          	  environment, if 'Irix mode' is Off, top  will	 operate  in  'Solaris
          	  mode'	 where	a task's cpu usage will be divided by the total number
          	  of CPUs.  You toggle 'Irix/Solaris' modes with the  'I'  interactive
          	  command.
          Devops Monitoring Expert advice: Dockerize/automate/monitor all the things.
          My DevOps stack: Docker / Kubernetes / Mesos / ECS / Terraform / Elasticsearch / Zabbix / Grafana / Puppet / Ansible / Vagrant

          Comment

          • tchjts1
            Senior Member
            • May 2008
            • 1605

            #6
            Jan - Thank you for the explanation of top vs ps. I didn't know that.
            I will talk to the application team and see what it is they want. I suspect they are wanting to see all the spikes, but scripts aren't going to be able to grab that unless executed at a 1 second interval, and I don't want to do that.

            Comment

            Working...