通过使用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
inster_snmp_oid.php
-
#!/opt/lampp/bin/php
-
<?php
-
/*
-
使用方法
-
chmo +x ./inster_snmp_oid.php
-
./inster_snmp_oid.php v1 stdu.edu.cn_nmc 202.206.32.42
-
*/
-
-
/* var */
-
@@$Version = $argv[1];
-
@@$Community = $argv[2];
-
@@$IP = $argv[3];
-
@@$Factory = $argv[4];
-
-
if(isset($Version) && isset($Community) && isset($IP)){
-
echo "生成中\n";
-
}else{
-
echo "参数格式:版本,社区名,IP地址,厂家\n";
-
echo "厂家:hw,rj\n";
-
echo "例子:./inster_snmp_oid.php v1 stdu.edu.cn_nmc 202.206.32.42 hw\n";
-
exit;
-
}
-
-
/* PDO mysql */
-
$pdo = new PDO('mysql:host=202.206.32.218;dbname=test1', 'root', 'zsdsywr.');
-
$pdo->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
-
<?php
-
/*
-
使用方法
-
chmo +x ./cerate_host.php
-
./cerate_host.php 202.206.32.42
-
*/
-
-
/* Zabbix API */
-
require __DIR__ . '/zbx.inc.php';
-
include ("/opt/wwwroot/db.php");
-
$zbx = new zbx;
-
-
/* 设置变量 */
-
@@$ip = $argv[1];
-
if(isset($ip)){
-
echo "生成中\n";
-
}else{
-
#echo "参数格式:IP地址\n";
-
exit;
-
}
-
-
/* PDO mysql */
-
$pdo = new PDO('mysql:host=202.206.32.218;dbname=test1', 'root', 'zsdsywr.');
-
$pdo -> exec('set names utf8');
-
-
/* Zabbix组 */
-
$group = "办公区楼宇 核心交换机";
-
-
/* XML 文件存放位置 */
-
$save = "./xml/$ip.xml";
-
-
/* Zabbix token */
-
$zbx->url = "";
-
$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.=<<<EOF
-
<proxy>
-
<name>Zabbix proxy 32.236</name>
-
</proxy>
-
<status>0</status>
-
<ipmi_authtype>-1</ipmi_authtype>
-
<ipmi_privilege>2</ipmi_privilege>
-
<ipmi_username/>
-
<ipmi_password/>
-
<templates/>
-
<groups>
-
<group>
-
EOF;
-
$xml.="".$group."";
-
$xml.=<<<EOF
-
</group>
-
</groups>
-
<interfaces>
-
<interface>
-
<default>1</default>
-
<type>2</type>
-
<useip>1</useip>
-
EOF;
-
$xml.="".$ip."";
-
$xml.=<<<EOF
-
<dns/>
-
<port>161</port>
-
<interface_ref>if1</interface_ref>
-
</interface>
-
</interfaces>
-
<applications/>
-
<items>
-
EOF;
-
/* 创建item */
-
foreach($result as $key => $value){
-
$community = $value['COMMUNITY'];
-
$oid_in = str_replace(array("\r\n", "\r", "\n"), "", $value['OID_in']);
-
$oid_out = str_replace(array("\r\n", "\r", "\n"), "", $value['OID_out']);
-
-
if($value['VERSION'] == 'v1'){
-
$snmp_version = "1";
-
}elseif($value['VERSION'] == 'v2c'){
-
$snmp_version = "4";
-
}
-
-
/* Items KEY */
-
$port = str_replace(array("\r\n", "\r", "\n"), "", $value['OID_port']);
-
$port_in_name = $port."_in";
-
$port_out_name = $port."_out";
-
$port_in_key = str_replace('/','_',$port_in_name);
-
$port_out_key = str_replace('/','_',$port_out_name);
-
-
$xml.="
- "
;
-
$xml.="".$port_in_name."";
-
$xml.="".$snmp_version."".$community."1";
-
#$xml.="interfaces.ifTable.ifEntry.".$oid_in."";
-
$xml.="".$oid_in."";
-
$xml.="".$port_in_key."";
-
$xml.=<<<EOF
-
<delay>60</delay>
-
<history>90</history>
-
<trends>365</trends>
-
<status>0</status>
-
<value_type>3</value_type>
-
<allowed_hosts/>
-
<units>bps</units>
-
<delta>1</delta>
-
<snmpv3_contextname/>
-
<snmpv3_securityname/>
-
<snmpv3_securitylevel>0</snmpv3_securitylevel>
-
<snmpv3_authprotocol>0</snmpv3_authprotocol>
-
<snmpv3_authpassphrase/>
-
<snmpv3_privprotocol>0</snmpv3_privprotocol>
-
<snmpv3_privpassphrase/>
-
<formula>8</formula>
-
<delay_flex/>
-
<params/>
-
<ipmi_sensor/>
-
<data_type>0</data_type>
-
<authtype>0</authtype>
-
<username/>
-
<password/>
-
<publickey/>
-
<privatekey/>
-
<port>161</port>
-
<description/>
-
<inventory_link>0</inventory_link>
-
<applications/>
-
<valuemap/>
-
<interface_ref>if1</interface_ref>
-
</item>
-
EOF;
-
$xml.="
- "
;
-
$xml.="".$port_out_name."";
-
$xml.="".$snmp_version."".$community."1";
-
#$xml.="interfaces.ifTable.ifEntry.".$oid_out."";
-
$xml.="".$oid_out."";
-
$xml.="".$port_out_key."";
-
$xml.=<<<EOF
-
<delay>60</delay>
-
<history>90</history>
-
<trends>365</trends>
-
<status>0</status>
-
<value_type>3</value_type>
-
<allowed_hosts/>
-
<units>bps</units>
-
<delta>1</delta>
-
<snmpv3_contextname/>
-
<snmpv3_securityname/>
-
<snmpv3_securitylevel>0</snmpv3_securitylevel>
-
<snmpv3_authprotocol>0</snmpv3_authprotocol>
-
<snmpv3_authpassphrase/>
-
<snmpv3_privprotocol>0</snmpv3_privprotocol>
-
<snmpv3_privpassphrase/>
-
<formula>8</formula>
-
<delay_flex/>
-
<params/>
-
<ipmi_sensor/>
-
<data_type>0</data_type>
-
<authtype>0</authtype>
-
<username/>
-
<password/>
-
<publickey/>
-
<privatekey/>
-
<port>161</port>
-
<description/>
-
<inventory_link>0</inventory_link>
-
<applications/>
-
<valuemap/>
-
<interface_ref>if1</interface_ref>
-
</item>
-
EOF;
-
}
-
$xml.=<<<EOF
-
</items>
-
<discovery_rules/>
-
<macros/>
-
<inventory/>
-
</host>
-
</hosts>
-
<graphs>
-
EOF;
-
/* 创建 graph */
-
foreach($result_2 as $key_2 => $value_2){
-
-
/* Items KEY */
-
$graph_port = str_replace(array("\r\n", "\r", "\n"), "", $value_2['OID_port']);
-
-
$graph_port_in_name = $graph_port."_in";
-
$graph_port_out_name = $graph_port."_out";
-
$graph_port_in_key = str_replace('/','_',$graph_port_in_name);
-
$graph_port_out_key = str_replace('/','_',$graph_port_out_name);
-
-
$xml.="";
-
$xml.="$graph_port";
-
$xml.=<<<EOF
-
<width>900</width>
-
<height>200</height>
-
<yaxismin>0.0000</yaxismin>
-
<yaxismax>100.0000</yaxismax>
-
<show_work_period>1</show_work_period>
-
<show_triggers>1</show_triggers>
-
<type>0</type>
-
<show_legend>1</show_legend>
-
<show_3d>0</show_3d>
-
<percent_left>0.0000</percent_left>
-
<percent_right>0.0000</percent_right>
-
<ymin_type_1>0</ymin_type_1>
-
<ymax_type_1>0</ymax_type_1>
-
<ymin_item_1>0</ymin_item_1>
-
<ymax_item_1>0</ymax_item_1>
-
<graph_items>
-
<graph_item>
-
<sortorder>0</sortorder>
-
<drawtype>0</drawtype>
-
<color>00C800</color>
-
<yaxisside>0</yaxisside>
-
<calc_fnc>2</calc_fnc>
-
<type>0</type>
-
<item>
-
EOF;
-
$xml.="$ip";
-
$xml.="$graph_port_in_key";
-
$xml.=<<<EOF
-
</item>
-
</graph_item>
-
<graph_item>
-
<sortorder>1</sortorder>
-
<drawtype>0</drawtype>
-
<color>C80000</color>
-
<yaxisside>0</yaxisside>
-
<calc_fnc>2</calc_fnc>
-
<type>0</type>
-
<item>
-
EOF;
-
$xml.="$ip";
-
$xml.="$graph_port_out_key";
-
$xml.=<<<EOF
-
</item>
-
</graph_item>
-
</graph_items>
-
</graph>
-
EOF;
-
}
-
$xml .= <<<EOF
-
</graphs>
-
</zabbix_export>
-
EOF;
-
// 创建 XML 文件
-
$of = fopen($save,'w');//创建并打开文件
-
if($of){
-
fwrite($of,$xml);//把执行文件的结果写入文件
-
echo "Create XML success\n";
-
}else{
-
echo "XML write Error\n";
-
}
-
-
}
-
-
/* Zbx import API */
-
function import($zbx,$save,$ip){
-
// 文件存入
-
$f = fopen ($save, "r");
-
$source = stream_get_contents($f);
-
fclose ($f);
-
-
$zbx->method = 'configuration.import';
-
$zbx->query['format'] = 'xml';
-
$zbx->query['rules']['groups'] = array(
-
"createMissing"=> true
-
);
-
$zbx->query['rules']['hosts'] = array(
-
"createMissing"=> true,
-
"updateExisting"=> 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);
阅读(6489) | 评论(0) | 转发(0) |