Zabbix дээр top тушаалаар хамгийн их нөөц хэрэглэж байгаа сервисийг лог хийх

Дусал нэвтэрхий толь-с
21:40, 4 Долоодугаар сар 2015-ий байдлаарх Almas (Яриа | оруулсан хувь нэмэр) хэрэглэгчийн хийсэн залруулга

<img title="Default" class="inlineimg" src="images/icons/icon1.gif" alt="Default" border="0"> Finding CPU consumers </div>



I wrote a little script to grab the output of the linux top-command into a Zabbix item. This was inspired by <a href="http://www.zabbix.com/forum/member.php?u=23021" target="_blank">terataz</a>, asking how to find out, e. g. when zabbix reports high CPU load, what process may be causing it.

My solution is not very sophisticated, but maybe somebody finds it useful, or feels like improving it.
By now, it reports the names of top cpu-time-consuming processes, if their CPU% exceeds a given value. With small modifications and an adjusted toprc configuration, one could use it for RAM consumers or anything else top is able to report.

Code:
#!/bin/bash
#####################################################
# topcpu.sh
# returns names of most CPU time consuming processes
# as reported by 'top'
#####################################################
# 05-07-2010 by Jerry Lenk
# Use at your own risk!
#####################################################

# set limit to 1st argument, or 2% if not specified
lim=$1
test -z $lim && lim=2

# run 2 iterations of top in batch mode with 1 s delay
top -b -d1 -n2 |\
gawk --assign lim=$lim  'BEGIN { reply=""}
        END { print reply, "." }
        # if reply is empty, at least a period is returned

        # in 2nd iteration, first 3 lines
        # add columns 9 (%cpu) and 12 (process name)
        # to reply string, if cpu at least lim%
        itr == 2 && NR <= 3 && $9 >= lim { reply=reply " " $9 "%" $12 }

        # count iterations by header lines beginning with "PID"
        # reset linenumber
        $1 == "PID" { NR=0 ; itr +=1 }
       '
# Only 2nd iteration of top is of interest because
# load values are calculated since previous iteration
I save it as "topcpu.sh" to my scripts directory on the monitored machine (/etc/zabbix/userscript in my case)

and add it as a UserParameter to zabbix-agentd.conf:

Code:
UserParameter=system.topcpu[*],<u>/etc/zabbix/userscript/</u>topcpu.sh $1
If you prefer to place the script somewhere else, change the underlined path accordingly.


Now all I need to do is restart zabbix_agentd and create an item for it:
Description: CPU top consumer

Type: Zabbix agent
key: system.topcpu[5] (5 being the minimum %CPU load I want reported)
Type of information: text
Interval: 30 (I tried 60 first, but wasn't very satisfied.)

I have yet to try, if it makes sense to put the content of this item into an alert mail triggered by high CPU load. Probably the relevant process name shows up half a minute after the trigger turns "on".


Have fun,
Jerry