hglo 发表于 2015-12-24 12:47:13

zabbix监控交换机端口流量_脚本,自动出图

通过使用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.shinster_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','$key','$key','$key'),";
[*]}
[*]
[*]$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]
查看完整版本: zabbix监控交换机端口流量_脚本,自动出图