您的位置:首页 > 理论基础 > 计算机网络

nagios网络流量监控(附pnp模板)

2016-04-13 14:05 876 查看
步骤:

1、下载网络流量监控脚本:
https://raw.githubusercontent.com/June-Wang/NagiosPlugins/master/check_net_traffic.sh
点击(此处)折叠或打开

#!/bin/bash

#set nagios status

STATE_OK=0

STATE_WARNING=1

STATE_CRITICAL=2

STATE_UNKNOWN=3

usage (){

        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

        exit ${STATE_WARNING}

}

check_input () {

local str="$1"

echo "${str}"|grep
-E '[0-9]+[b|B|k|K|m|M|g|G]$'
>/dev/null 2>&1
||\

eval "echo ${str} is wrong!;usage"

}

replace_str () {

local str="$1"

output=`echo
"${str}"|sed
-r 's/[k|K]/*1024/;s/[m|M]/*1024*1024/;s/[g|G]/*1024*1024*1024/'`

echo ${output}

}

while getopts w:c:d: opt

do

        case "$opt"
in

        w)

            check_input "$OPTARG"

            warning_str=`replace_str
"$OPTARG"`

            warning=`echo
"${warning_str}"|bc`

        ;;

        c)

            check_input "$OPTARG"

            critical_str=`replace_str
"$OPTARG"`

            critical=`echo
"${critical_str}"|bc`

        ;;

        d)

            dev_id="$OPTARG"

        ;;

        *)

            usage

        ;;

        esac

done

shift $[ $OPTIND
- 1 ]

if [
-z "${dev_id}"
-o -z "${warning}"
-o -z
"${critical}" ];then

        usage

fi

source_file='/proc/net/dev'

if [
! -f "${source_file}"
];then

                echo
"${source_file} not exsit!" 1>&2

                exit ${STATE_WARNING}

fi

grep "${dev_id}" ${source_file}
>/dev/null 2>&1
|| dev_stat='not found'

if [
"${dev_stat}" =
'not found' ];then

                echo
"${dev_id} ${dev_stat}!" 1>&2

                usage

fi

time_now=`date
-d now +"%F %T"`

nagios_path='/usr/local/nagios/libexec'

test -d ${nagios_path}
|| mkdir
-p ${nagios_path} && mark="${nagios_path}/net_traffic.${dev_id}"

search_dev=`awk
-F':'
'/'${dev_id}'/{print $2}' /proc/net/dev|sed
-r 's/^[ ]+//'`

info=`echo
"${search_dev}"|awk
-v date="${time_now}"
'BEGIN{OFS=";"}{print "TIME=\""date"\"","RX="$1,"TX="$9,"DEV='${dev_id}'"}'`

#debug

#eval "${info}"

#echo $info

#echo $TIME $RX $TX && exit

marking () {

        echo "$info"
> ${mark} || exit ${STATE_WARNING}

        chown nagios.nagios ${mark}

}

if [
! -f "${mark}"
];then

                marking

                echo
"This script is First run! ${info}"

                exit ${STATE_OK}

else

                old_info=`cat ${mark}`

                eval "${old_info}"

                OLD_TIME="${TIME}";OLD_RX=${RX};OLD_TX=${TX}

                if
[ -z "${OLD_RX}"
-o -z
"${OLD_TX}" ];then

                        echo
"Data Error: ${old_info}" 1>&2

                        marking

                        exit ${STATE_WARNING}

                fi

fi

if [
-n "${info}"
];then

                eval ${info}

                sec_now=`date
-d "${TIME}"
+"%s"`

                sec_old=`date
-d "${OLD_TIME}"
+"%s"`

                sec=`echo
"${sec_now}-${sec_old}"|bc|sed
's/-//'`

                rx=`echo
"(${RX}-${OLD_RX})/${sec}"|bc|sed
's/-//'`

                tx=`echo
"(${TX}-${OLD_TX})/${sec}"|bc|sed
's/-//'`

                marking

#debug

# echo $sec $rx $tx

else

                echo
"Can not read ${source_file}" 1>&2

                exit ${STATE_WARNING}

fi

human_read () {

local number="$1"

if [ `echo
"(${number}-1073741824) > 0"|bc`
-eq 1
];then

        output="`echo "scale=2;${number}/1024/1024/1024"|bc`
GB/s"

elif [ `echo
"(${number}-1048576) > 0"|bc`
-eq 1
];then

        output="`echo "scale=2;${number}/1024/1024"|bc`
MB/s"

elif [ `echo
"(${number}-1024) > 0"|bc`
-eq 1
];then

        output="`echo "scale=2;${number}/1024"|bc`
KB/s"

else

        output="${number} B/s"

fi

echo "${output}"

}

rx_human_read=`human_read
"${rx}"`

tx_human_read=`human_read
"${tx}"`

message () {

    local stat="$1"

    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}"

}

#pnp4nagios setting

min=0

max=1073741824

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

[ `echo
"(${total_int}-${warning}) < 0"|bc`
-eq 1
] && message "OK" && exit ${STATE_OK}

[ `echo
"(${total_int}-${critical}) >= 0"|bc`
-eq 1
] && message "Critical" && exit ${STATE_CRITICAL}

[ `echo
"(${total_int}-${warning}) >= 0"|bc`
-eq 1
] && message "Warning" && exit ${STATE_WARNING}

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

2、pnp模板:
https://raw.githubusercontent.com/June-Wang/NagiosPlugins/master/check_net_traffic.php
上传到/usr/local/pnp4nagios/share/templates.dist/(根据自己的环境修改)

点击(此处)折叠或打开

<?php

/*

* Copyright (c) 2012 Jason Hancock
<jsnbyh@gmail.com>

*

* Permission is hereby granted, free
of charge, to any person obtaining a copy

* of
this software and associated documentation files
(the "Software"), to deal

* in the Software without restriction, including without limitation the rights

* to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell

* copies of the Software,
and to permit persons to whom the Software is furnished

* to do so, subject to the following conditions:

*

* The above copyright notice
and this permission notice shall be included
in all

* copies or substantial portions
of the Software.

*

* THE SOFTWARE IS PROVIDED
"AS IS", WITHOUT WARRANTY
OF ANY KIND, EXPRESS
OR

* IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY,

* FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT.
IN NO EVENT SHALL THE

* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES
OR OTHER

* LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT
OR OTHERWISE, ARISING FROM,

* OUT
OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS
IN

* THE SOFTWARE.

*

* This file is part
of the nagios-puppet bundle that can be found

* at https://github.com/jasonhancock/nagios-memory
*/

$alpha = 'CC';

$colors = array(

    '#00FF00'
. $alpha,

    '#000066'
. $alpha,

    '#25345C'
. $alpha,

    '#88008A'
. $alpha,

    '#4F7774'
. $alpha,

);

$opt[1]
= sprintf('-T 55 -l 0 --vertical-label "Bytes" --title "%s / Network Traffic"', $hostname);

$def[1]
= '';

$count = 0;

foreach ($DS as $i) {

    $def[1]
.= rrd::def("var$i",
$rrdfile, $DS[$i],
'AVERAGE');

    if ($i
==
'1') {

        $def[1]
.= rrd::area
("var$i", $colors[$count],
rrd::cut(ucfirst($NAME[$i]),
15));

    } else {

        $def[1]
.= rrd::line1
("var$i", $colors[$count],
rrd::cut(ucfirst($NAME[$i]),
15),
'STACK');

    }

    $def[1]
.= rrd::gprint
("var$i", array('LAST','MAX','AVERAGE'),
"%4.2lf %s\t");

    $count++;

}

用法:

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轴的单位是上行和下行的总和,累加关系,不是表示输出比输入高。(未核实)

以下是效果图:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: