设为首页 收藏本站
查看: 1897|回复: 0

[经验分享] zabbix监控交换机端口流量_脚本,自动出图

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-12-24 12:47:13 | 显示全部楼层 |阅读模式
通过使用snmpwalk命令得到端口流量信息,写入数据库,创建创建XML文件API导入Zbx,实现自动创建主机并创建items,自动出图。

目录结构
./
├── run.sh                       # 1.执行程序
├── snmpwalk.sh              # 2.snmpwalk 主要 执行snmpwalk命令得到索引,端口,流入,流出信息保存成txt文件
├── insert_snmp_oid.php  # 2.读取txt文件将文件内容写入MYSQL
├── create_host.php         # 3.读取MYSQL,将OID信息拼成XML文件,在通过api导入
├── xml/                         # 5. XML保存在这
└── zbx.inc.php

使用方法
    添加可执行权限
    chmod +x star.sh snmpwalk.sh  inster_snmp_oid.php create_host.php
    执行./star.sh 程序会有提示,写入什么参数。

MYSQL表
创建 test1 数据库



  • CREATE TABLE IF NOT EXISTS `snmp_oid` (

  •   `ID` int(11) NOT NULL AUTO_INCREMENT,
  •   `IP` varchar(255) NOT NULL,
  •   `COMMUNITY` varchar(255) NOT NULL,
  •   `VERSION` varchar(10) DEFAULT NULL,
  •   `OID_index` varchar(255) NOT NULL,
  •   `OID_port` varchar(255) NOT NULL,
  •   `OID_in` varchar(255) NOT NULL,
  •   `OID_out` varchar(255) NOT NULL,
  •   PRIMARY KEY (`ID`)
  • ) ENGINE=InnoDB DEFAULT CHARSET=utf8;


脚本内容
    zbx.inc.php       #  ZBX API
   star.sh



  • #!/bin/bash

  • ./insert_snmp_oid.php $1 $2 $3
  • echo "-------------------------------------------"
  • ./create_host.php $3
    snmpwalk.sh  



  • #!/bin/bash


  • # 迈普交换机设置成 mp
  • # 锐捷或者华为 随便设定一个值就行。
  • a="cc"

  • # 函数

  • # 生成索引文件
  • function index() {
  •     if [ $a == "mp" ];then
  •         snmpwalk -$version -c $community $ip IF-MIB::ifDescr |grep 'gi' |awk -F'[ ,=]' '{print $1}' > index.txt
  •         #snmpwalk -$version -c $community $ip IF-MIB::ifDescr |grep 'port' |awk -F'[ ,=]' '{print $1}' > index.txt
  •     else
  •         snmpwalk -$version -c $community $ip IF-MIB::ifDescr |grep 'Ethernet' |awk -F'[ ,=]' '{print $1}' > index.txt
  •     fi
  • }

  • # 生成端口名称
  • function port() {
  •     if [ $a == "mp" ];then
  •         snmpwalk -$version -c $community $ip IF-MIB::ifDescr |grep 'gi' |awk -F'[ ,=]' '{print $5 $6}' > port.txt
  •         #snmpwalk -$version -c $community $ip IF-MIB::ifDescr |grep 'port' |awk -F'[ ,=]' '{print $5 $6}' > port.txt
  •     else
  •         snmpwalk -$version -c $community $ip IF-MIB::ifDescr |grep 'Ethernet' |awk -F'[ ,=]' '{print $5 $6}' > port.txt
  •     fi
  • }

  • # 删除生成txt文件
  • function del(){
  •     rm -rf ./in.txt out.txt index.txt port.txt
  • }

  • # 生成端口流入文件
  • function _in(){
  •     if [ -f "index.txt" ];then
  •         cp ./index.txt in.txt

  •         #SNMP v1
  •         if [ $version == "v1" ];then
  •             sed -i "s/IF-MIB::ifDescr/IF-MIB::ifInOctets/g" `grep IF-MIB::ifDescr -rl in.txt `
  •         fi

  •         #SNMP v2c
  •         if [ $version == "v2c" ];then
  •             sed -i "s/IF-MIB::ifDescr/IF-MIB::ifHCInOctets/g" `grep IF-MIB::ifDescr -rl in.txt `
  •         fi

  •     else
  •         echo "not index.txt"
  •     fi
  • }

  • # 生出端口流出文件
  • function _out(){
  •     if [ -f "index.txt" ];then
  •         cp ./index.txt out.txt

  •         #SNMP v1
  •         if [ $version == "v1" ];then
  •             sed -i "s/IF-MIB::ifDescr/IF-MIB::ifOutOctets/g" `grep IF-MIB::ifDescr -rl out.txt `
  •         fi

  •         #SNMP v2c
  •         if [ $version == "v2c" ];then
  •             sed -i "s/IF-MIB::ifDescr/IF-MIB::ifHCOutOctets/g" `grep IF-MIB::ifDescr -rl out.txt `
  •         fi

  •     else
  •         echo "not index.txt"
  •     fi

  • }

  • # 外部参数
  • parameter=$1
  • community=$3
  • version=$2
  • ip=$4

  • # 参数使用
  • case $parameter in
  •     "index")
  •          index $2 $3 $4 $a
  •     ;;
  •     "port")
  •          port $2 $3 $4 $a
  •     ;;
  •     "del")
  •          del
  •     ;;
  •     "in")
  •          _in $2
  •     ;;
  •     "out")
  •          _out $2
  •     ;;
  •     *)
  •         echo ""
  •         echo "Usage:     {$0 Verison Community Parameter Ip}"
  •         echo "Parameter: {index|port|del|in|out}"
  •         echo "Community: {Public}"
  •         echo "Example:   {$0 index v1 Public 202.206.33.37}"
  •         echo ""
  •     ;;
  • esac
    inster_snmp_oid.php



  • #!/opt/lampp/bin/php

  • exec('set names utf8');
  • $pdo->exec('TRUNCATE TABLE `snmp_oid`');

  • /* Config */
  • $Parameter_1 = "index";
  • $Parameter_2 = "port";
  • $Parameter_3 = "in";
  • $Parameter_4 = "out";

  • /* Shell Script */
  • function shell($Parameter_1,$Parameter_2,$Parameter_3,$Parameter_4,$Version,$Community,$IP){
  •    exec("./snmpwalk.sh $Parameter_1 $Version $Community $IP");
  •    exec("./snmpwalk.sh $Parameter_2 $Version $Community $IP");
  •    exec("./snmpwalk.sh $Parameter_3 $Version $Community $IP");
  •    exec("./snmpwalk.sh $Parameter_4 $Version $Community $IP");
  • }

  • /* Run Shell */
  • shell($Parameter_1,$Parameter_2,$Parameter_3,$Parameter_4,$Version,$Community,$IP);

  • /* Shell Add Files */
  • $file_index = 'index.txt';
  • $file_port ='port.txt';
  • $file_in = 'in.txt';
  • $file_out = 'out.txt';

  • /* File Array */
  • $index = file($file_index);
  • $port = file($file_port);
  • $in = file($file_in);
  • $out = file($file_out);

  • $sql ="INSERT INTO `snmp_oid`(`ID`,`IP`,`COMMUNITY`,`VERSION`,`OID_index`,`OID_port`,`OID_in`,`OID_out`) VALUES";

  • foreach ($index as $value1){
  •    $value2 = current($port);
  •    $value3 = current($in);
  •    $value4 = current($out);

  •    $new[] = array("$value1","$value2","$value3","$value4");
  •    next($port);
  •    next($in);
  •    next($out);
  • }

  • foreach($new as $value => $key){
  •   #print_r($key);
  •   $sql .= "(NULL, '$IP', '$Community', '$Version', '$key[0]','$key[1]','$key[2]','$key[3]'),";
  • }

  • $SQL = rtrim($sql,',');
  • $new_sql = $SQL.";";

  • $inster = $pdo->exec("$new_sql");

  • exec("./snmpwalk.sh del");
  • if($inster == true){
  •    echo "insert success\n";
  • }else{
  •     echo "inster error\n";
  • }
    create_host.php



  • #!/opt/lampp/bin/php

  • exec('set names utf8');

  • /* Zabbix组 */
  • $group = "办公区楼宇 核心交换机";

  • /* XML 文件存放位置 */
  • $save = "./xml/$ip.xml";

  • /* Zabbix token */
  • $zbx->url = "http://202.206.32.41/api_jsonrpc.php";
  • $zbx->method = 'user.login';
  • $zbx->query['user'] = 'api';
  • $zbx->query['password'] = 'zabbix';
  • $zbx->access_token = $zbx->call()['result'];

  • /* 生成XML文件函数 */
  • function XML($pdo,$ip,$group,$save){

  •     // 查询 执行
  •     $sql = "SELECT * FROM `snmp_oid` WHERE `IP` = '$ip'";
  •     $result = $pdo -> query($sql);
  •     $result_2 = $pdo -> query($sql);

  •     // XML 头
  •             $xml = "";
  •             $xml.= "";
  •             $xml.= "2.0";
  •             $xml.= "2013-11-25T02:13:46Z";
  •             $xml.= "".$group."";
  •             $xml.="";
  •             $xml.="".$ip."";
  •             $xml.="".$ip."";
  •             $xml.= true
  •     );
  •     $zbx->query['rules']['items'] = array(
  •                     "createMissing"=> true,
  •                     "updateExisting"=> true
  •     );
  •     $zbx->query['rules']['graphs'] = array(
  •                     "createMissing"=> true,
  •                     "updateExisting"=> true
  •     );
  •     $zbx->query['source'] = "$source";
  •     $import = $zbx->call()['result'];
  •     #print_r($import);
  •     if($import == 1){
  •         echo "$ip host import secuess!\n";
  •     }else{
  •         echo "$ip host import error!\n";
  •     }
  • }


  • XML($pdo,$ip,$group,$save);
  • import($zbx,$save,$ip);

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-155712-1-1.html 上篇帖子: Zabbix上IO监控 下篇帖子: zabbix中unknown状态
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表