zabbix通过API创建交换机模板,ifAdminStatus;ifOperStatus;ifInUcastPkts;ifAlias

最终效果:




目的:
        通过zabbix的Latest data查看主机就可以看到其监控结果。

监控项:
        # 管理状态 
        IF-MIB::ifAdminStatus.
        # 操作状态 
        IF-MIB::ifOperStatus.
        # 接收 单播包 
        IF-MIB::ifInUcastPkts.
        # 发送 单播包
        IF-MIB::ifOutUcastPkts.
        # 接收 错误包 
        IF-MIB::ifInErrors.
        # 发送 错误包
        IF-MIB::ifOutErrors.
        # 接收 列队 
        IF-MIB::ifInQLen.
        # 发送 列队
        IF-MIB::ifOutQLen.
        # 接收 多播包 
        IF-MIB::ifInMulticastPkts.
        # 发送 多播包
        IF-MIB::ifOutMulticastPkts.
        # 接收 广播包 
        IF-MIB::ifInBroadcastPkts.
        # 发送 广播包
        IF-MIB::ifOutBroadcastPkts.
       # 接收 非单播包
       IF-MIB::ifInNUcastPkts.
       # 发送 非单播包
       IF-MIB::ifOutNUcastPkts
        # 端口描述 
        IF-MIB::ifAlias.

基础环境:
       centos6.5
       xampp集成环境
       snmpwalk

流程原理:
       通过shell截取snmpwalk出来的index对应端口信息存入MYSQL表内,PHP脚本读取存入MYSQL表内的信息,通过给予的OID名字进行字符串截取生成item名字,KEY,OID创建items,一单items被创建成功,会返回itmeids,脚本讲把这个ID与其他信息存入另一个MYSQL表内。

目录结构:
       snmpwalk.sh               # SNMP信息截取  调用
       insert_snmp_oid.php    # SNMP信息写入  使用
       zbx.inc.php                  #  ZABBIX API     调用
       zbx.templates.class.php   # 创建模板    调用
       usage.php                      # 创建模板    使用

MYSQL表内容

点击(此处)折叠或打开

  1. --
  2. -- 表的结构 `snmp_oid`
  3. --

  4. CREATE TABLE IF NOT EXISTS `snmp_oid` (
  5.   `ID` int(11) NOT NULL AUTO_INCREMENT,
  6.   `IP` varchar(255) NOT NULL,
  7.   `COMMUNITY` varchar(255) NOT NULL,
  8.   `VERSION` varchar(10) DEFAULT NULL,
  9.   `OID_index` varchar(255) NOT NULL,
  10.   `OID_port` varchar(255) NOT NULL,
  11.   `OID_in` varchar(255) NOT NULL,
  12.   `OID_out` varchar(255) NOT NULL,
  13.   PRIMARY KEY (`ID`)
  14. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;

点击(此处)折叠或打开

  1. --
  2. -- 表的结构 `items_plus`
  3. --

  4. CREATE TABLE IF NOT EXISTS `items_plus` (
  5.   `id` int(11) NOT NULL AUTO_INCREMENT,
  6.   `hostid` int(50) NOT NULL,
  7.   `snmp_index` varchar(50) DEFAULT NULL,
  8.   `itemids` int(50) NOT NULL,
  9.   `items_name` varchar(50) DEFAULT NULL,
  10.   `items_key` varchar(50) DEFAULT NULL,
  11.   `items_oid` varchar(50) DEFAULT NULL,
  12.   `ApplicationID` int(10) NOT NULL,
  13.   PRIMARY KEY (`id`)
  14. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;


脚本内容:
 snmpwalk.sh    

点击(此处)折叠或打开

  1. #!/bin/bash

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

  5. # 函数

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

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

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

  28. # 生成端口流入文件
  29. function _in(){
  30.     if [ -f "index.txt" ];then
  31.         cp ./index.txt in.txt
  32.         
  33.         #SNMP v1
  34.         if [ $version == "v1" ];then
  35.             sed -i "s/IF-MIB::ifDescr/IF-MIB::ifInOctets/g" `grep IF-MIB::ifDescr -rl in.txt `
  36.         fi

  37.         #SNMP v2c
  38.         if [ $version == "v2c" ];then
  39.             sed -i "s/IF-MIB::ifDescr/IF-MIB::ifHCInOctets/g" `grep IF-MIB::ifDescr -rl in.txt `
  40.         fi
  41.  
  42.     else
  43.         echo "not index.txt"
  44.     fi
  45. }

  46. # 生出端口流出文件
  47. function _out(){
  48.     if [ -f "index.txt" ];then
  49.         cp ./index.txt out.txt
  50.        
  51.         #SNMP v1
  52.         if [ $version == "v1" ];then
  53.             sed -i "s/IF-MIB::ifDescr/IF-MIB::ifOutOctets/g" `grep IF-MIB::ifDescr -rl out.txt `
  54.         fi
  55.       
  56.         #SNMP v2c
  57.         if [ $version == "v2c" ];then
  58.             sed -i "s/IF-MIB::ifDescr/IF-MIB::ifHCOutOctets/g" `grep IF-MIB::ifDescr -rl out.txt `
  59.         fi

  60.     else
  61.         echo "not index.txt"
  62.     fi

  63. }

  64. # 外部参数
  65. parameter=$1
  66. community=$3
  67. version=$2
  68. ip=$4

  69. # 参数使用
  70. case $parameter in
  71.     "index")
  72.          index $2 $3 $4 $a
  73.     ;;
  74.     "port")
  75.          port $2 $3 $4 $a
  76.     ;;
  77.     "del")
  78.          del
  79.     ;;
  80.     "in")
  81.          _in $2
  82.     ;;
  83.     "out")
  84.          _out $2
  85.     ;;
  86.     *)
  87.         echo ""
  88.         echo "Usage: {$0 Verison Community Parameter Ip}"
  89.         echo "Parameter: {index|port|del|in|out}"
  90.         echo "Community: {Public}"
  91.         echo "Example: {$0 index v1 Public 202.206.33.37}"
  92.         echo ""
  93.     ;;
  94. esac

insert_snmp_oid.php

点击(此处)折叠或打开

  1. #!/opt/lampp/bin/php
  2. <?php
  3. /*
  4.     使用方法
  5.     chmo +x ./inster_snmp_oid.php
  6.     ./inster_snmp_oid.php v1 stdu.edu.cn_nmc 202.206.32.42
  7. */

  8. /* var */
  9. @@$Version = $argv[1];
  10. @@$Community = $argv[2];
  11. @@$IP = $argv[3];
  12. @@$Factory = $argv[4];

  13. if(isset($Version) && isset($Community) && isset($IP)){
  14.     echo "生成中\n";
  15. }else{
  16.     echo "参数格式:版本,社区名,IP地址\n";
  17.         echo "例子:./inster_snmp_oid.php v1 stdu.edu.cn_nmc 202.206.32.42\n";
  18.     exit;
  19. }

  20. /* PDO mysql */
  21. $pdo = new PDO('mysql:host=202.206.32.218;dbname=test1', 'root', 'zsdsywr.');
  22. $pdo->exec('set names utf8');
  23. $pdo->exec('TRUNCATE TABLE `snmp_oid`');

  24. /* Config */
  25. $Parameter_1 = "index";
  26. $Parameter_2 = "port";
  27. $Parameter_3 = "in";
  28. $Parameter_4 = "out";

  29. /* Shell Script */
  30. function shell($Parameter_1,$Parameter_2,$Parameter_3,$Parameter_4,$Version,$Community,$IP){
  31.    exec("./snmpwalk.sh $Parameter_1 $Version $Community $IP");
  32.    exec("./snmpwalk.sh $Parameter_2 $Version $Community $IP");
  33.    exec("./snmpwalk.sh $Parameter_3 $Version $Community $IP");
  34.    exec("./snmpwalk.sh $Parameter_4 $Version $Community $IP");
  35. }

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

  38. /* Shell Add Files */
  39. $file_index = 'index.txt';
  40. $file_port ='port.txt';
  41. $file_in = 'in.txt';
  42. $file_out = 'out.txt';

  43. /* File Array */
  44. $index = file($file_index);
  45. $port = file($file_port);
  46. $in = file($file_in);
  47. $out = file($file_out);

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

  49. foreach ($index as $value1){
  50.    $value2 = current($port);
  51.    $value3 = current($in);
  52.    $value4 = current($out);
  53.  
  54.    $new[] = array("$value1","$value2","$value3","$value4");
  55.    next($port);
  56.    next($in);
  57.    next($out);
  58. }

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

  63. $SQL = rtrim($sql,',');
  64. $new_sql =
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值