Guest User

How to get CPU Per Core Discovery in Zabbix 2.0

a guest
Sep 10th, 2013
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.30 KB | None | 0 0
  1. [How to get per-core CPU monitoring through zabbix-aggent:
  2.  
  3. 1.1. Copy this script to host
  4. #!/bin/bash
  5. # This script detects number of cores available in Linux system
  6. CORES=$(mpstat -P ALL | sed 1,4d | awk '{ print $3}')
  7. echo "{"
  8. echo -e "\t\"data\":["
  9. for core in ${CORES}; do
  10. echo -e "\t\t{"
  11. #echo -e "\t\t\t\"{#CORENAME}\":\"Core$core\","
  12. echo -e "\t\t\t\"{#COREINDEX}\":\"$core\"},"
  13.  
  14. done | sed '$ s/,/]}/'
  15.  
  16. ####################
  17. 1.2. Copy this script to host, e.g. /etc/scripts/zabbix/cpu_perCoreStat.sh
  18. #!/bin/bash
  19. # This script gets CPU Utilization per core.
  20.  
  21. getStat() {
  22.         # $1 - type of value - user, system, idle, iowait
  23.         # $2 - core number, 0 is for all cores stat
  24.         CORE="$2"
  25.         case "${1}" in
  26.                 sys)  
  27.                         if [ "$CORE" = "0" ];then
  28.                                 mpstat -P ALL | grep all | awk '{ print $6 }'
  29.                         else  
  30.                                 mpstat -P $CORE | sed 1,3d | awk '{ print $6 }'
  31.                         fi
  32.                 ;;
  33.                 user)  
  34.                         if [ "$CORE" = "0" ];then
  35.                                 mpstat -P ALL | grep all | awk '{ print $4 }'
  36.                         else  
  37.                                 mpstat -P $CORE | sed 1,3d | awk '{ print $4 }'
  38.                         fi
  39.                 ;;
  40.                 idle)  
  41.                         if [ "$CORE" = "0" ];then
  42.                                 mpstat -P ALL | grep all | awk '{ print $12 }'
  43.                         else  
  44.                                 mpstat -P $CORE | sed 1,3d | awk '{ print $12 }'
  45.                         fi
  46.                 ;;
  47.                 iowait)
  48.                         if [ "$CORE" = "0" ];then
  49.                                 mpstat -P ALL | grep all | awk '{ print $7 }'
  50.                         else  
  51.                                 mpstat -P $CORE | sed 1,3d | awk '{ print $7 }'
  52.                         fi
  53.                 ;;
  54.         esac
  55. }
  56. while getopts ":u:s:i:w:" opt;do
  57.         # $OPTARG - argument given after parameter, e.g. if we type $(script -s 25), $OPTARG will be "25"
  58.         case $opt in
  59.                 s)
  60.                         getStat sys $OPTARG
  61.                 ;;
  62.                 u)
  63.                         getStat user $OPTARG
  64.                 ;;
  65.                 i)
  66.                         getStat idle $OPTARG
  67.                 ;;
  68.                 w)
  69.                         getStat iowait $OPTARG
  70.                 ;;
  71.                 \?)
  72.                         echo "Invalid argument : -$OPTARG"
  73.                 ;;
  74.         esac
  75.         shift
  76. done
  77. #####################################
  78. 1.3. zabbix_agentd.conf:
  79. UserParameter=cpu.cores.discovery,sudo /etc/scripts/zabbix/cpu.cores.discovery
  80. UserParameter=system.cpu.idle[*],sudo /etc/scripts/zabbix/cpu_perCoreStat -i $1
  81. UserParameter=system.cpu.iowait[*],sudo /etc/scripts/zabbix/cpu_perCoreStat -w $1
  82. UserParameter=system.cpu.sys[*],sudo /etc/scripts/zabbix/cpu_perCoreStat -s $1
  83. UserParameter=system.cpu.user[*],sudo /etc/scripts/zabbix/cpu_perCoreStat -u $1
  84.  
  85. 1.4. Modify sudo parameters in /etc/sudoers:
  86. zabbix ALL=(ALL) NOPASSWD: /etc/scripts/zabbix/
  87. Defaults:zabbix !requiretty
  88.  
  89. 1.5. Don't forget to chmod +x all of your scripts :)
  90.  
  91. 2.1 Import template, and add them to host
Advertisement
Add Comment
Please, Sign In to add comment