Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1211406
  • 博文数量: 259
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2518
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-13 16:12
个人简介

科技改变世界,技术改变人生。

文章分类

全部博文(259)

分类: 系统运维

2015-06-12 11:19:05

步骤:
1、下载网络流量监控脚本:


点击(此处)折叠或打开

  1. #!/bin/bash

  2. #set nagios status
  3. STATE_OK=0
  4. STATE_WARNING=1
  5. STATE_CRITICAL=2
  6. STATE_UNKNOWN=3

  7. usage (){
  8.         echo -en "Usage: $0 -d [ eth|bond ]\nFor example:\t$0 -d bond0 -w 100[B|K|M|G] -c 200[B|K|M|G]\n" 1>&2
  9.         exit ${STATE_WARNING}
  10. }

  11. check_input () {
  12. local str="$1"
  13. echo "${str}"|grep -E '[0-9]+[b|B|k|K|m|M|g|G]$' >/dev/null 2>&1 ||\
  14. eval "echo ${str} is wrong!;usage"
  15. }

  16. replace_str () {
  17. local str="$1"
  18. output=`echo "${str}"|sed -r 's/[k|K]/*1024/;s/[m|M]/*1024*1024/;s/[g|G]/*1024*1024*1024/'`
  19. echo ${output}
  20. }

  21. while getopts w:c:d: opt
  22. do
  23.         case "$opt" in
  24.         w)
  25.             check_input "$OPTARG"
  26.             warning_str=`replace_str "$OPTARG"`
  27.             warning=`echo "${warning_str}"|bc`
  28.         ;;
  29.         c)
  30.             check_input "$OPTARG"
  31.             critical_str=`replace_str "$OPTARG"`
  32.             critical=`echo "${critical_str}"|bc`
  33.         ;;
  34.         d)
  35.             dev_id="$OPTARG"
  36.         ;;
  37.         *)
  38.             usage
  39.         ;;
  40.         esac
  41. done

  42. shift $[ $OPTIND - 1 ]

  43. if [ -z "${dev_id}" -o -z "${warning}" -o -z "${critical}" ];then
  44.         usage
  45. fi

  46. source_file='/proc/net/dev'
  47. if [ ! -f "${source_file}" ];then
  48.                 echo "${source_file} not exsit!" 1>&2
  49.                 exit ${STATE_WARNING}
  50. fi

  51. grep "${dev_id}" ${source_file} >/dev/null 2>&1 || dev_stat='not found'
  52. if [ "${dev_stat}" = 'not found' ];then
  53.                 echo "${dev_id} ${dev_stat}!" 1>&2
  54.                 usage
  55. fi

  56. time_now=`date -d now +"%F %T"`
  57. nagios_path='/usr/local/nagios/libexec'
  58. test -d ${nagios_path} || mkdir -p ${nagios_path} && mark="${nagios_path}/net_traffic.${dev_id}"
  59. search_dev=`awk -F':' '/'${dev_id}'/{print $2}' /proc/net/dev|sed -r 's/^[ ]+//'`
  60. info=`echo "${search_dev}"|awk -v date="${time_now}" 'BEGIN{OFS=";"}{print "TIME=\""date"\"","RX="$1,"TX="$9,"DEV='${dev_id}'"}'`

  61. #debug
  62. #eval "${info}"
  63. #echo $info
  64. #echo $TIME $RX $TX && exit

  65. marking () {
  66.         echo "$info" > ${mark} || exit ${STATE_WARNING}
  67.         chown nagios.nagios ${mark}
  68. }

  69. if [ ! -f "${mark}" ];then
  70.                 marking
  71.                 echo "This script is First run! ${info}"
  72.                 exit ${STATE_OK}
  73. else
  74.                 old_info=`cat ${mark}`
  75.                 eval "${old_info}"
  76.                 OLD_TIME="${TIME}";OLD_RX=${RX};OLD_TX=${TX}
  77.                 if [ -z "${OLD_RX}" -o -z "${OLD_TX}" ];then
  78.                         echo "Data Error: ${old_info}" 1>&2
  79.                         marking
  80.                         exit ${STATE_WARNING}
  81.                 fi
  82. fi

  83. if [ -n "${info}" ];then
  84.                 eval ${info}
  85.                 sec_now=`date -d "${TIME}" +"%s"`
  86.                 sec_old=`date -d "${OLD_TIME}" +"%s"`
  87.                 sec=`echo "${sec_now}-${sec_old}"|bc|sed 's/-//'`
  88.                 rx=`echo "(${RX}-${OLD_RX})/${sec}"|bc|sed 's/-//'`
  89.                 tx=`echo "(${TX}-${OLD_TX})/${sec}"|bc|sed 's/-//'`
  90.                 marking
  91. #debug
  92. # echo $sec $rx $tx
  93. else
  94.                 echo "Can not read ${source_file}" 1>&2
  95.                 exit ${STATE_WARNING}
  96. fi

  97. human_read () {
  98. local number="$1"
  99. if [ `echo "(${number}-1073741824) > 0"|bc` -eq 1 ];then
  100.         output="`echo "scale=2;${number}/1024/1024/1024"|bc` GB/s"
  101. elif [ `echo "(${number}-1048576) > 0"|bc` -eq 1 ];then
  102.         output="`echo "scale=2;${number}/1024/1024"|bc` MB/s"
  103. elif [ `echo "(${number}-1024) > 0"|bc` -eq 1 ];then
  104.         output="`echo "scale=2;${number}/1024"|bc` KB/s"
  105. else
  106.         output="${number} B/s"
  107. fi
  108. echo "${output}"
  109. }

  110. rx_human_read=`human_read "${rx}"`
  111. tx_human_read=`human_read "${tx}"`

  112. message () {
  113.     local stat="$1"
  114.     echo "${DEV} Traffic is ${stat} - In: ${rx_human_read} Out: ${tx_human_read} interval: ${sec}s |in=${rx};${warning};${critical};${min};${max} out=${tx};${warning};${critical};${min};${max}"
  115. }

  116. #pnp4nagios setting
  117. min=0
  118. max=1073741824

  119. total_int=`echo "${rx}+${tx}"|bc`

  120. [ `echo "(${total_int}-${warning}) < 0"|bc` -eq 1 ] && message "OK" && exit ${STATE_OK}
  121. [ `echo "(${total_int}-${critical}) >= 0"|bc` -eq 1 ] && message "Critical" && exit ${STATE_CRITICAL}
  122. [ `echo "(${total_int}-${warning}) >= 0"|bc` -eq 1 ] && message "Warning" && exit ${STATE_WARNING}




上传到/usr/local/nagios/libexec(根据自己的环境修改)

2、pnp模板:

上传到/usr/local/pnp4nagios/share/templates.dist/(根据自己的环境修改)

点击(此处)折叠或打开

  1. <?php
  2. /*
  3. * Copyright (c) 2012 Jason Hancock <jsnbyh@gmail.com>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is furnished
  10. * to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. *
  23. * This file is part of the nagios-puppet bundle that can be found
  24. * at https://github.com/jasonhancock/nagios-memory
  25. */
  26. $alpha = 'CC';
  27. $colors = array(
  28.     '#00FF00' . $alpha,
  29.     '#000066' . $alpha,
  30.     '#25345C' . $alpha,
  31.     '#88008A' . $alpha,
  32.     '#4F7774' . $alpha,
  33. );
  34. $opt[1] = sprintf('-T 55 -l 0 --vertical-label "Bytes" --title "%s / Network Traffic"', $hostname);
  35. $def[1] = '';
  36. $count = 0;
  37. foreach ($DS as $i) {
  38.     $def[1] .= rrd::def("var$i", $rrdfile, $DS[$i], 'AVERAGE');
  39.     if ($i == '1') {
  40.         $def[1] .= rrd::area ("var$i", $colors[$count], rrd::cut(ucfirst($NAME[$i]), 15));
  41.     } else {
  42.         $def[1] .= rrd::line1 ("var$i", $colors[$count], rrd::cut(ucfirst($NAME[$i]), 15), 'STACK');
  43.     }
  44.     $def[1] .= rrd::gprint ("var$i", array('LAST','MAX','AVERAGE'), "%4.2lf %s\t");
  45.     $count++;
  46. }

用法:
1、将以下命令行添加到nrpe.cfg
echo 'command[check_net_traffic]=/usr/local/nagios/libexec/check_net_traffic.sh -d eth0 -w 7m -c 10m' >> /usr/local/nagios/etc/nrpe.cfg

参数说明:
-d 是要监控的网卡名
-w -c 是设定的阀值,只能是b、k、m、g,大小写均可,单位是大B(字节),需要说明的是这个阀值是上行(上传)和下行(下载)的总和。


2、将pnp模板考到Nagios Server 的pnp文件夹:
cp check_net_traffic.php /usr/local/pnp4nagios/share/templates.dist/

3、Nagios Server主机配置文件中加入
define service{
        use                     dpmc-service,srv-pnp
        host_name               backup
        service_description     Check Net
        check_command           check_nrpe!check_net_traffic
        contact_groups          system
       }


注意:
1、脚本第一次运行时,会将当前网卡的相关数值写到临时文件中,临时文件会保存在/usr/local/nagios/libexec/下。
2、(未核实)流量图Y轴的单位是上行和下行的总和,累加关系,不是表示输出比输入高。(未核实)

以下是效果图:

阅读(2337) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~