通过 Cacti 监控 Centos CPU/进程

通过 Cacti 监控 Centos CPU/进程

我查找过但尚未找到任何用于绘制 Linux 设备(centos)上每个进程使用的 CPU 和内存量的模板。有人遇到过吗?

谢谢,

答案1

在 Cacti 中您可以尝试使用snmpwalk,但将其应用到您的任务中将完全是您的工作。

但是你可以尝试安装 zabbix 并使用此脚本来监控几个最耗 CPU 的进程的 CPU 使用率,这些进程的消耗超过了某个阈值

#!/bin/bash
#####################################################
# topcpu
# returns names of most CPU time consuming processes
# as reported by 'top'
#####################################################
# 05-07-2010 by Jerry Lenk
# 02-11-2010 by Frater (rewrite in bash)
#
# Use at your own risk!
#####################################################

# Add lsof to /etc/sudoers (as root) with the following command
##########################
#     echo zabbix ALL = NOPASSWD: `which lsof` >> /etc/sudoers

# Add to zabbix_agentd.conf
###########################
#     echo 'UserParameter=system.topcpu[*],/usr/local/sbin/topcpu $1' >>/etc/zabbix/zabbix_agentd.conf

# Restart Zabbix
################
#     /etc/init.d/zabbix-agent restart

# Constants
nodata='.'
deflimit=4
use_lsof=1
GREP='grep --color=never -a'
DEBUG=0

# set limit to 1st argument (given from zabbix), or deflimit if not specified
lim=`echo "$1" | tr -cd '0-9.'`
[ -z "${lim}" ] && lim=${deflimit}

toptail="`top -b -d1 -n2 | ${GREP} -A1 '^ *PID ' | tail -n1`"
cpu=`echo "${toptail}"  | awk '{print $9}'`

[ ${DEBUG} -ne 0 ] && echo "Debug: \$1=$1  limit=$lim  cpu=$cpu"

if expr ${cpu} \<= ${lim} >/dev/null ; then
  echo "${nodata}"
else

  # get PID & FULL process name (it may contain more info)
  pid2=`echo "${toptail}" | awk '{print $1}'`
  procname="`ps --pid ${pid2} -o args --no-headers 2>/dev/null`"

  if [ -z "${procname}" ] ; then
    # process is not running anymore... I might as well return nothing and quit
    echo "${nodata}"
  else

    user=`echo "${toptail}" | awk '{print $2}'`
    # return CPU usage, process owner and process name
    echo "${cpu}%   ${user}:${procname}"

    if [ ${use_lsof} -ne 0 ] ; then
      # calculate the limit when it should execute lsof
      lim=$(( 2 * ${lim} + 5 ))
      [ ${lim} -gt 50 ] && lim=50
      # Run an lsof, but exclude log files, apache modules and several runtime/library directories
      expr ${cpu} \> ${lim} >/dev/null && sudo lsof -p ${pid2} -S -b -w -n -Fftn0 | ${GREP} -v '^fDEL' | ${GREP} 'tREG'  | ${GREP} -o '/.*' | tr -d '\0' | ${GREP} -vE '(log$|\.mo$|^/var/lib|^/lib|^/var/run|^/tmp|^/usr/|^/var/log/)' | sort -u | head -n7
    fi
  fi
fi

您可以在此处找到此脚本的注释、zabbix 使用说明以及修改内容(https://www.zabbix.com/forum/showthread.php?t=17874)。

相关内容