Ad Widget

Collapse

Monitor Linux system CPU usage

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bbrendon
    Senior Member
    • Sep 2005
    • 870

    #1

    Monitor Linux system CPU usage

    Does anyone have a good way to monitor over-all CPU usage on a linux host?

    I have been using:
    Code:
    system.run[sar -P ALL 1 2 | grep 'Average.*all' | awk -F" " '{ print  100.0 - $ NF }']
    The problem with the above, is that on some hosts I get "Not Supported" intermittently even though running it from the command line works fine 100% of the time. I'll re-enable it and it'll work again for a day, and then stop working.
    Unofficial Zabbix Expert
    Blog, Corporate Site
  • sterno
    Junior Member
    • Jan 2006
    • 24

    #2
    You have problems because "sar -P ALL 1 2" takes 2 seconds to complete, which is too long for a userparameter.

    I use a script that looks at the performance counters under /proc/stat, calculates a percentage based on deltas from its last run, and stores the new numbers for the next run. I'm not sure how useful a measure that really is, though. It's easy enough to say that user/nice/system and interrupt handling are all "activity", but iowait is a bit of a gray area - the system isn't really doing anything, but it's not really idle either.

    Comment

    • bbrendon
      Senior Member
      • Sep 2005
      • 870

      #3
      I make a pretty drastic effort to not use extra scripts because it means I have to make sure those scripts are on all the servers and are updated, which would be almost impossible.

      It would be really nice if this was built into the agent!
      Last edited by bbrendon; 07-09-2008, 22:12.
      Unofficial Zabbix Expert
      Blog, Corporate Site

      Comment

      • mikecrowe
        Member
        • Dec 2007
        • 34

        #4
        You can also adjust the Timeout variable in the zabbix_agentd.conf. I set mine at 15s, because I'm doing a lot like this.

        Comment

        • mikecrowe
          Member
          • Dec 2007
          • 34

          #5
          Originally posted by sterno
          ... I use a script that looks at the performance counters under /proc/stat, calculates a percentage based on deltas from its last run, and stores the new numbers for the next run.
          Could you share that please?

          Comment

          • sterno
            Junior Member
            • Jan 2006
            • 24

            #6
            It's not perfect, but it seems to provide a reasonable estimate of the non-idle CPU time. Should be easy enough to customize, just check the kernel docs for the format of /proc/stat.

            Code:
            #! /bin/bash
            
            umask 022
            PATH=/usr/bin:/bin
            LD_LIBRARY_PATH=
            export PATH LD_LIBRARY_PATH
            
            if [[ ! -r /proc/stat ]] ; then
                    echo '-1'
                    exit 1
            fi
            
            file="$1"
            cpu="$2"
            
            # No CPU - default to total
            if [[ -z "${cpu}" ]] ; then
                    cpu=' '
            fi
            
            # No filename - default to /var/run/zabbix/cpucounter
            if [[ -z "${file}" ]] ; then
                    file='/var/run/zabbix/cpucounter'
            fi
            
            typeset T=0 R=0
            if [[ -f "${file}" ]] ; then
                    . "${file}"
            fi
            rm -f "${file}"
            
            exec awk "/^cpu${cpu}/ {
                    run = \$2 + \$3 + \$4;
                    tot = run + \$5;
                    printf(\"T=%f ; R=%f\\n\", tot, run) >\"${file}\";
                    tot -= ${T};
                    if (tot > 0)
                            run -= ${R};
                    printf(\"%u\\n\", int(run / tot * 100));
            }" </proc/stat

            Comment

            Working...