Chinaunix首页 | 论坛 | 博客
  • 博客访问: 244404
  • 博文数量: 32
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 414
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-22 11:22
个人简介

.....

文章分类

全部博文(32)

文章存档

2016年(1)

2015年(12)

2014年(19)

我的朋友

分类: 系统运维

2015-04-16 00:49:55

通过使用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 数据库 

点击(此处)折叠或打开

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

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

点击(此处)折叠或打开

  1. #!/bin/bash
  2. ./insert_snmp_oid.php $1 $2 $3
  3. echo "-------------------------------------------"
  4. ./create_host.php $3
    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
    inster_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 "厂家:hw,rj\n";
  18.         echo "例子:./inster_snmp_oid.php v1 stdu.edu.cn_nmc 202.206.32.42 hw\n";
  19.     exit;
  20. }

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

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

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

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

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

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

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

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

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

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

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

  67. exec("./snmpwalk.sh del");
  68. if($inster == true){
  69.    echo "insert success\n";
  70. }else{
  71.     echo "inster error\n";
  72. }
    create_host.php

点击(此处)折叠或打开

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

  8. /* Zabbix API */
  9. require __DIR__ . '/zbx.inc.php';
  10. include ("/opt/wwwroot/db.php");
  11. $zbx = new zbx;

  12. /* 设置变量 */
  13. @@$ip = $argv[1];
  14. if(isset($ip)){
  15.     echo "生成中\n";
  16. }else{
  17.     #echo "参数格式:IP地址\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. /* Zabbix组 */
  24. $group = "办公区楼宇 核心交换机";

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

  27. /* Zabbix token */
  28. $zbx->url = "";
  29. $zbx->method = 'user.login';
  30. $zbx->query['user'] = 'api';
  31. $zbx->query['password'] = 'zabbix';
  32. $zbx->access_token = $zbx->call()['result'];

  33. /* 生成XML文件函数 */
  34. function XML($pdo,$ip,$group,$save){
  35.     
  36.     // 查询 执行
  37.     $sql = "SELECT * FROM `snmp_oid` WHERE `IP` = '$ip'";
  38.     $result = $pdo -> query($sql);
  39.     $result_2 = $pdo -> query($sql);

  40.     // XML 头
  41.             $xml = "";
  42.             $xml.= "";
  43.             $xml.= "2.0";
  44.             $xml.= "2013-11-25T02:13:46Z";
  45.             $xml.= "".$group."";
  46.             $xml.="";
  47.             $xml.="".$ip."";
  48.             $xml.="".$ip."";
  49.             $xml.=<<<EOF
  50.                         <proxy>
  51.                              <name>Zabbix proxy 32.236</name>
  52.                         </proxy>
  53.                         <status>0</status>
  54.                         <ipmi_authtype>-1</ipmi_authtype>
  55.                         <ipmi_privilege>2</ipmi_privilege>
  56.                         <ipmi_username/>
  57.                         <ipmi_password/>
  58.                         <templates/>
  59.                         <groups>
  60.                             <group>
  61. EOF;
  62.                                 $xml.="".$group."";
  63.                             $xml.=<<<EOF
  64.                             </group>
  65.                         </groups>
  66.                         <interfaces>
  67.                             <interface>
  68.                                 <default>1</default>
  69.                                 <type>2</type>
  70.                                 <useip>1</useip>
  71. EOF;
  72.                                 $xml.="".$ip."";
  73.                                 $xml.=<<<EOF
  74.                                 <dns/>
  75.                                 <port>161</port>
  76.                                 <interface_ref>if1</interface_ref>
  77.                             </interface>
  78.                         </interfaces>
  79.                         <applications/>
  80.                         <items>
  81. EOF;
  82.     /* 创建item */
  83.     foreach($result as $key => $value){
  84.         $community = $value['COMMUNITY'];
  85.         $oid_in = str_replace(array("\r\n", "\r", "\n"), "", $value['OID_in']);
  86.         $oid_out = str_replace(array("\r\n", "\r", "\n"), "", $value['OID_out']);
  87.         
  88.         if($value['VERSION'] == 'v1'){
  89.             $snmp_version = "1";
  90.         }elseif($value['VERSION'] == 'v2c'){
  91.             $snmp_version = "4";
  92.         }
  93.         
  94.         /* Items KEY */
  95.         $port = str_replace(array("\r\n", "\r", "\n"), "", $value['OID_port']);
  96.         $port_in_name = $port."_in";
  97.         $port_out_name = $port."_out";
  98.         $port_in_key = str_replace('/','_',$port_in_name);
  99.         $port_out_key = str_replace('/','_',$port_out_name);
  100.         
  101.                     $xml.="";
  102.                     $xml.="".$port_in_name."";
  103.                     $xml.="".$snmp_version."".$community."1";
  104.                     #$xml.="interfaces.ifTable.ifEntry.".$oid_in."";
  105.                     $xml.="".$oid_in."";
  106.                     $xml.="".$port_in_key."";
  107.                     $xml.=<<<EOF
  108.                                 <delay>60</delay>
  109.                                 <history>90</history>
  110.                                 <trends>365</trends>
  111.                                 <status>0</status>
  112.                                 <value_type>3</value_type>
  113.                                 <allowed_hosts/>
  114.                                 <units>bps</units>
  115.                                 <delta>1</delta>
  116.                                 <snmpv3_contextname/>
  117.                                 <snmpv3_securityname/>
  118.                                 <snmpv3_securitylevel>0</snmpv3_securitylevel>
  119.                                 <snmpv3_authprotocol>0</snmpv3_authprotocol>
  120.                                 <snmpv3_authpassphrase/>
  121.                                 <snmpv3_privprotocol>0</snmpv3_privprotocol>
  122.                                 <snmpv3_privpassphrase/>
  123.                                 <formula>8</formula>
  124.                                 <delay_flex/>
  125.                                 <params/>
  126.                                 <ipmi_sensor/>
  127.                                 <data_type>0</data_type>
  128.                                 <authtype>0</authtype>
  129.                                 <username/>
  130.                                 <password/>
  131.                                 <publickey/>
  132.                                 <privatekey/>
  133.                                 <port>161</port>
  134.                                 <description/>
  135.                                 <inventory_link>0</inventory_link>
  136.                                 <applications/>
  137.                                 <valuemap/>
  138.                                 <interface_ref>if1</interface_ref>
  139.                             </item>
  140. EOF;
  141.                     $xml.="";
  142.                     $xml.="".$port_out_name."";
  143.                     $xml.="".$snmp_version."".$community."1";
  144.                     #$xml.="interfaces.ifTable.ifEntry.".$oid_out."";
  145.                     $xml.="".$oid_out."";
  146.                     $xml.="".$port_out_key."";
  147.                     $xml.=<<<EOF
  148.                                 <delay>60</delay>
  149.                                 <history>90</history>
  150.                                 <trends>365</trends>
  151.                                 <status>0</status>
  152.                                 <value_type>3</value_type>
  153.                                 <allowed_hosts/>
  154.                                 <units>bps</units>
  155.                                 <delta>1</delta>
  156.                                 <snmpv3_contextname/>
  157.                                 <snmpv3_securityname/>
  158.                                 <snmpv3_securitylevel>0</snmpv3_securitylevel>
  159.                                 <snmpv3_authprotocol>0</snmpv3_authprotocol>
  160.                                 <snmpv3_authpassphrase/>
  161.                                 <snmpv3_privprotocol>0</snmpv3_privprotocol>
  162.                                 <snmpv3_privpassphrase/>
  163.                                 <formula>8</formula>
  164.                                 <delay_flex/>
  165.                                 <params/>
  166.                                 <ipmi_sensor/>
  167.                                 <data_type>0</data_type>
  168.                                 <authtype>0</authtype>
  169.                                 <username/>
  170.                                 <password/>
  171.                                 <publickey/>
  172.                                 <privatekey/>
  173.                                 <port>161</port>
  174.                                 <description/>
  175.                                 <inventory_link>0</inventory_link>
  176.                                 <applications/>
  177.                                 <valuemap/>
  178.                                 <interface_ref>if1</interface_ref>
  179.                             </item>
  180. EOF;
  181.     }
  182. $xml.=<<<EOF
  183.                         </items>
  184.                         <discovery_rules/>
  185.                         <macros/>
  186.                         <inventory/>
  187.                     </host>
  188.                 </hosts>
  189.                 <graphs>
  190. EOF;
  191.     /* 创建 graph */
  192.     foreach($result_2 as $key_2 => $value_2){
  193.   
  194.         /* Items KEY */
  195.         $graph_port = str_replace(array("\r\n", "\r", "\n"), "", $value_2['OID_port']);
  196.         
  197.         $graph_port_in_name = $graph_port."_in";
  198.         $graph_port_out_name = $graph_port."_out";
  199.         $graph_port_in_key = str_replace('/','_',$graph_port_in_name);
  200.         $graph_port_out_key = str_replace('/','_',$graph_port_out_name);

  201.           $xml.="";
  202.                  $xml.="$graph_port";
  203.                  $xml.=<<<EOF
  204.                         <width>900</width>
  205.                         <height>200</height>
  206.                         <yaxismin>0.0000</yaxismin>
  207.                         <yaxismax>100.0000</yaxismax>
  208.                         <show_work_period>1</show_work_period>
  209.                         <show_triggers>1</show_triggers>
  210.                         <type>0</type>
  211.                         <show_legend>1</show_legend>
  212.                         <show_3d>0</show_3d>
  213.                         <percent_left>0.0000</percent_left>
  214.                         <percent_right>0.0000</percent_right>
  215.                         <ymin_type_1>0</ymin_type_1>
  216.                         <ymax_type_1>0</ymax_type_1>
  217.                         <ymin_item_1>0</ymin_item_1>
  218.                         <ymax_item_1>0</ymax_item_1>
  219.                         <graph_items>
  220.                             <graph_item>
  221.                                 <sortorder>0</sortorder>
  222.                                 <drawtype>0</drawtype>
  223.                                 <color>00C800</color>
  224.                                 <yaxisside>0</yaxisside>
  225.                                 <calc_fnc>2</calc_fnc>
  226.                                 <type>0</type>
  227.                                 <item>
  228. EOF;
  229.                              $xml.="$ip";
  230.                              $xml.="$graph_port_in_key";
  231.                              $xml.=<<<EOF
  232.                                 </item>
  233.                             </graph_item>
  234.                             <graph_item>
  235.                                 <sortorder>1</sortorder>
  236.                                 <drawtype>0</drawtype>
  237.                                 <color>C80000</color>
  238.                                 <yaxisside>0</yaxisside>
  239.                                 <calc_fnc>2</calc_fnc>
  240.                                 <type>0</type>
  241.                                 <item>
  242. EOF;
  243.                                     $xml.="$ip";
  244.                                     $xml.="$graph_port_out_key";
  245.                                 $xml.=<<<EOF
  246.                                 </item>
  247.                             </graph_item>
  248.                         </graph_items>
  249.                     </graph>
  250. EOF;
  251.     }
  252. $xml .= <<<EOF
  253.                 </graphs>
  254.             </zabbix_export>
  255. EOF;
  256.         // 创建 XML 文件
  257.         $of = fopen($save,'w');//创建并打开文件
  258.         if($of){
  259.             fwrite($of,$xml);//把执行文件的结果写入文件
  260.             echo "Create XML success\n";
  261.         }else{
  262.             echo "XML write Error\n";
  263.         }

  264. }

  265. /* Zbx import API */
  266. function import($zbx,$save,$ip){
  267.     // 文件存入
  268.     $f = fopen ($save, "r");
  269.     $source = stream_get_contents($f);
  270.     fclose ($f);
  271.     
  272.     $zbx->method = 'configuration.import';
  273.     $zbx->query['format'] = 'xml';
  274.     $zbx->query['rules']['groups'] = array(
  275.                     "createMissing"=> true
  276.     );
  277.     $zbx->query['rules']['hosts'] = array(
  278.                     "createMissing"=> true,
  279.                     "updateExisting"=> true
  280.     );
  281.     $zbx->query['rules']['items'] = array(
  282.                     "createMissing"=> true,
  283.                     "updateExisting"=> true
  284.     );
  285.     $zbx->query['rules']['graphs'] = array(
  286.                     "createMissing"=> true,
  287.                     "updateExisting"=> true
  288.     );
  289.     $zbx->query['source'] = "$source";
  290.     $import = $zbx->call()['result'];
  291.     #print_r($import);
  292.     if($import == 1){
  293.         echo "$ip host import secuess!\n";
  294.     }else{
  295.         echo "$ip host import error!\n";
  296.     }
  297. }


  298. XML($pdo,$ip,$group,$save);
  299. import($zbx,$save,$ip);






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