最终效果:
目的:
通过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表内容
点击(此处)折叠或打开
-
--
-
-- 表的结构 `snmp_oid`
-
--
-
-
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 AUTO_INCREMENT=0 ;
点击(此处)折叠或打开
-
--
-
-- 表的结构 `items_plus`
-
--
-
-
CREATE TABLE IF NOT EXISTS `items_plus` (
-
`id` int(11) NOT NULL AUTO_INCREMENT,
-
`hostid` int(50) NOT NULL,
-
`snmp_index` varchar(50) DEFAULT NULL,
-
`itemids` int(50) NOT NULL,
-
`items_name` varchar(50) DEFAULT NULL,
-
`items_key` varchar(50) DEFAULT NULL,
-
`items_oid` varchar(50) DEFAULT NULL,
-
`ApplicationID` int(10) NOT NULL,
-
PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
脚本内容:
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
insert_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 "生成中
";
-
}else{
-
echo "参数格式:版本,社区名,IP地址
";
-
echo "例子:./inster_snmp_oid.php v1 stdu.edu.cn_nmc 202.206.32.42
";
-
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
";
-
}else{
-
echo "inster error
";
- }
zbx.inc.php
点击(此处)折叠或打开
-
<?php
-
class jsonrpc{
-
-
protected function connect($server, $query){
-
$http = curl_init($server);
-
curl_setopt($http, CURLOPT_CUSTOMREQUEST, 'POST');
-
curl_setopt($http, CURLOPT_POSTFIELDS, $query);
-
curl_setopt($http, CURLOPT_RETURNTRANSFER, TRUE);
-
curl_setopt($http, CURLOPT_SSL_VERIFYPEER, FALSE);
-
/* curl_setopt($http, CURLOPT_PROXY, 'proxy_url');
-
curl_setopt($http, CURLOPT_PROXYPORT, '3128');
-
curl_setopt($http, CURLOPT_PROXYUSERPWD, 'login:pass'); */
-
curl_setopt($http, CURLOPT_SSL_VERIFYHOST, FALSE);
-
curl_setopt($http, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
-
$response = curl_exec($http);
-
return json_decode($response, true);
-
curl_close($http);
-
}
-
-
}
-
-
-
class zbx extends jsonrpc{
-
-
public $method;
-
public $access_token;
-
public $url;
-
public $query;
-
-
function call(){
-
$data['jsonrpc'] = '2.0';
-
$data['method'] = $this->method;
-
$data['params'] = $this->query;
-
$this->query = '';
-
if(!empty($this->access_token))
-
$data['auth'] = $this->access_token;
-
$data['id'] = rand(1,100);
-
$data = json_encode($data, JSON_PRETTY_PRINT);
-
return $this->connect($this->url, $data);
-
}
-
- }
zbx.templates.class.php
点击(此处)折叠或打开
-
#!/opt/lampp/bin/php
-
<?php
-
/**
-
*交换机snmp模板创建
-
*
-
*用于创建zabbix模板的items trigger
-
* @author kingsh2012@163.com
-
*/
-
-
/* Import Zabbix API */
-
require __DIR__ . '/zbx.inc.php';
-
-
-
class ZabbixTemplates{
-
-
/**
-
* 构造函数
-
* 负责zabbix API调用,与32.218数据库的PDO方式连接。
-
*
-
* @return $this->zbx;
-
* @return $this->pdo;
-
*/
-
function __construct() {
-
// zabbix API import
-
$this->zbx = new zbx;
-
$this->zbx->url = "http://202.206.32.203/api_jsonrpc.php"; //网页地址
-
$this->zbx->method = 'user.login';
-
$this->zbx->query['user'] = 'api'; //用户名管理员
-
$this->zbx->query['password'] = 'zabbix'; //密码
-
$this->zbx->access_token = $this->zbx->call()['result'];
-
-
// connect 202.206.32.218 databases
-
try {
-
$this->pdo = new PDO("mysql:host=202.206.32.218;dbname=test1", "root", "zsdsywr."); //数据库连接
-
$this->pdo->exec('set names utf8');
-
$sql = "SELECT * FROM `snmp_oid` ";
-
// $sql = "SELECT * FROM `items_plus` ";
-
$this->snmp = $this->pdo->query($sql);
-
}catch (PDOException $e) {
-
exit($e->getMessage());
-
}
-
}
-
-
-
/**
-
* GetTemplateID
-
* 获取模板ID
-
*
-
* @access public
-
* @param string $name 模板名称
-
* @return integer
-
*/
-
function GetTemplateID ($name) {
-
$this->zbx->method = 'template.get';
-
$this->zbx->query['output'] = 'extend';
-
$this->zbx->query['filter']['host'] = "$name";
-
@$templateid = $this->zbx->call()['result'][0]['templateid'];
-
if(!empty($templateid)){
-
return $templateid;
-
}else{
-
echo "输入主机错误";
-
}
-
}
-
-
/**
-
* GetHostID
-
* 获取主机ID
-
*
-
* @access public
-
* @param string $host 主机名称
-
* @return integer
-
*/
-
function GetHostID ($host) {
-
$this->zbx->method = 'host.get';
-
$this->zbx->query['output'] = 'extend';
-
$this->zbx->query['filter']['host'] = "$host";
-
$hostid = $this->zbx->call();
-
return $hostid;
-
}
-
-
/**
-
* GetApplicationID
-
* 获取应用ID
-
*
-
* @access 调试
-
*/
-
function GetApplicationID(){
-
$this->zbx->method = 'application.get';
-
$this->zbx->query['output'] = 'extend';
-
$this->zbx->query['hostids']['host'] = "11562";
-
$result = $this->zbx->call();
-
print_r($result);
-
}
-
-
/**
-
* DeltetItem
-
* 删除items
-
*
-
* @access 调试
-
*/
-
function DeltetItem(){
-
// $i = 0;
-
// foreach($this->snmp as $key => $value ){
-
// $itmsids = $value["itemids"];
-
// echo $itmsids;
-
// exit;
-
$this->zbx->method = 'item.delete';
-
// $this->zbx->query = ["$itmsids"];
-
$this->zbx->query = ['85668'];
-
$result = $this->zbx->call();
-
print_r($result);
-
// echo $i++."
";
-
// print_r($value);
-
// echo $value["itemids"];
-
// }
-
}
-
-
/**
-
* GetHostID
-
* 获取主机ID
-
*
-
* @access public
-
* @param string $host 主机名称
-
* @return integer
-
*/
-
function GetGroupID($GroupNmae){
-
$this->zbx->method = 'hostgroup.get';
-
$this->zbx->query['output'] = 'extend';
-
$this->zbx->query['filter']['name'] = "$GroupNmae";
-
@$groupid = $this->zbx->call()['result']['0']['groupid'];
-
if(isset($groupid)){
-
return $groupid;
-
}else{
-
echo $GroupNmae . "你输入的组不存在
";
-
exit;
-
}
-
}
-
-
/**
-
* CreateTemplate
-
* 创建模板
-
*
-
* @access public
-
* @param string $GroupNmae 以存在的 组名称
-
* @param string $TemplateNmae 不存在的 模板名称
-
* @return integer
-
*/
-
function CreateTemplate($GroupNmae,$TemplateNmae){
-
-
$groupid = $this->GetGroupID($GroupNmae);
-
// echo $groupid;
-
// exit;
-
$this->zbx->method = 'template.create';
-
$this->zbx->query['host'] = "$TemplateNmae";
-
$this->zbx->query['groups']['groupid'] = "$groupid";
-
@$templateids = $this->zbx->call()['result']['templateids']['0'];
-
if(isset($templateids)){
-
return $templateids;
-
}else{
-
echo $TemplateNmae . "模板已经存在
";
-
exit;
-
}
-
}
-
-
/**
-
* CreateApplication
-
* 创建应用
-
* 这个很重要,在 Zabbix Latest data里面相当于items的分组
-
*
-
* @access public
-
* @param integer $hostid Application创建在哪个主机下面.主机ID
-
* @param integer $number 不同的数字对应着不同的Application名字
-
* @return integer
-
*/
-
function CreateApplication($hostid,$number){
-
if($number == 1){
-
$name = '端口列队';
-
}elseif($number == 2){
-
$name = '端口单播包';
-
}elseif($number == 3){
-
$name = '端口多播包';
-
}elseif($number == 4){
-
$name = '端口广播包';
-
}elseif($number == 5){
-
$name = '端口描述';
-
}elseif($number == 6){
-
$name = '端口操作状态';
-
}elseif($number == 7){
-
$name = '端口流量';
-
}elseif($number == 8){
-
$name = '端口管理状态';
-
}elseif($number == 9){
-
$name = '端口错误包';
-
}elseif($number == 10){
-
$name = '端口非单播包';
-
}else{
-
echo "应用ID输入错误
";
-
exit;
-
}
-
$this->zbx->method = 'application.create';
-
$this->zbx->query['name'] = "$name";
-
$this->zbx->query['hostid'] = "$hostid";
-
$applicationids = $this->zbx->call()['result']['applicationids']['0'];
-
return $applicationids;
-
}
-
-
/**
-
* CreateItem
-
* 创建itme
-
*
-
* @access public
-
* @param integer $type 各种item的选择
-
* @param integer $hostid 主机ID
-
* @param string $items_name 名字
-
* @param string $snmp_version snmp版本
-
* @param string $items_key KEY
-
* @param string $items_oid OID
-
* @param string $snmp_community 社区名
-
* @param string $applications 应用ID
-
* @param string $description 备注
-
* @return integer
-
*/
-
function CreateItem($type, $hostid, $items_name, $snmp_version, $items_key, $items_oid, $snmp_community, $applications,$description){
-
// 数据包 双向OID
-
if($type == 0){
-
$this->zbx->method = 'item.create';
-
$this->zbx->query['hostid'] = "$hostid"; //主机号
-
$this->zbx->query['name'] = "$items_name"; //items名字
-
$this->zbx->query['type'] = "$snmp_version"; //snmp版本号
-
$this->zbx->query['key_'] = "$items_key"; //itmes值
-
$this->zbx->query['snmp_oid'] = "$items_oid"; //oid
-
$this->zbx->query['snmp_community'] = "$snmp_community"; //snmp社区
-
$this->zbx->query['port'] = '161'; //端口
-
$this->zbx->query['value_type'] = '3'; //3 - numeric unsigned; 数值 特殊
-
$this->zbx->query['delay'] = '60';
-
$this->zbx->query['applications'] = ["$applications"]; //应用ID
-
$this->zbx->query['history'] = '7'; //记录时间 天
-
$this->zbx->query['description'] = "$description"; //描述
-
$this->zbx->query['delta'] = '1'; //1 - Delta, speed per second; 特殊
-
$itemids = $this->zbx->call()['result']['itemids']['0'];
-
return $itemids;
-
}elseif($type == 1){
-
// 流量 双向OID 这个被单独拿了出来是因为流量的涉及到 单位换算 显示单位 差值
-
// 注意 其实这个流量生成不建议使用这个函数 有单独对流量做过完整脚本
-
$this->zbx->method = 'item.create';
-
$this->zbx->query['hostid'] = "$hostid";
-
$this->zbx->query['name'] = "$items_name";
-
$this->zbx->query['type'] = "$snmp_version";
-
$this->zbx->query['key_'] = "$items_key";
-
$this->zbx->query['snmp_oid'] = "$items_oid";
-
$this->zbx->query['snmp_community'] = "$snmp_community";
-
$this->zbx->query['port'] = '161';
-
$this->zbx->query['value_type'] = '3';
-
$this->zbx->query['delay'] = '60';
-
$this->zbx->query['applications'] = ["$applications"];
-
$this->zbx->query['history'] = '7';
-
$this->zbx->query['description'] = "$description";
-
$this->zbx->query['delta'] = '1';
-
$this->zbx->query['formula'] = '8'; //特殊
-
$this->zbx->query['multiplier'] = '1'; //特殊
-
$this->zbx->query['units'] = 'bps'; //特殊
-
$itemids = $this->zbx->call()['result']['itemids']['0'];
-
return $itemids;
-
}elseif($type == 2){
-
// 单项 OID
-
$this->zbx->method = 'item.create';
-
$this->zbx->query['hostid'] = "$hostid";
-
$this->zbx->query['name'] = "$items_name";
-
$this->zbx->query['type'] = "$snmp_version";
-
$this->zbx->query['key_'] = "$items_key";
-
$this->zbx->query['snmp_oid'] = "$items_oid";
-
$this->zbx->query['snmp_community'] = "$snmp_community";
-
$this->zbx->query['port'] = '161';
-
$this->zbx->query['value_type'] = '1'; //特殊
-
$this->zbx->query['delay'] = '60';
-
$this->zbx->query['applications'] = ["$applications"];
-
$this->zbx->query['history'] = '7';
-
$this->zbx->query['description'] = "$description";
-
$itemids = $this->zbx->call()['result']['itemids']['0'];
-
return $itemids;
-
}elseif($type == 3){
-
// 单项OID 管理状态 这个被单独拿了出来是因为"Show value"这个项,在官方的API里面没有找到怎么取这个值. Value mapping
-
// 这个item被创建以后返回值是 1-3 (1=up,2=down,3=testing)
-
$this->zbx->method = 'item.create';
-
$this->zbx->query['hostid'] = "$hostid";
-
$this->zbx->query['name'] = "$items_name";
-
$this->zbx->query['type'] = "$snmp_version";
-
$this->zbx->query['key_'] = "$items_key";
-
$this->zbx->query['snmp_oid'] = "$items_oid";
-
$this->zbx->query['snmp_community'] = "$snmp_community";
-
$this->zbx->query['port'] = '161';
-
$this->zbx->query['value_type'] = '1';
-
$this->zbx->query['delay'] = '60';
-
$this->zbx->query['applications'] = ["$applications"];
-
$this->zbx->query['history'] = '7';
-
$this->zbx->query['description'] = "$description";
-
$this->zbx->query['valuemapid'] = '11'; //特殊
-
$itemids = $this->zbx->call()['result']['itemids']['0'];
-
return $itemids;
-
}elseif($type == 4){
-
// 单项OID 操作状态 这个被单独拿了出来是因为"Show value"这个项,在官方的API里面没有找到怎么取这个值. Value mapping
-
// 这个item被创建以后返回值是 1-7 (1=up,2=down,3=testing,4=unknown,5=dormant,6=notPresent,7=lowerLayerDown)
-
$this->zbx->method = 'item.create';
-
$this->zbx->query['hostid'] = "$hostid";
-
$this->zbx->query['name'] = "$items_name";
-
$this->zbx->query['type'] = "$snmp_version";
-
$this->zbx->query['key_'] = "$items_key";
-
$this->zbx->query['snmp_oid'] = "$items_oid";
-
$this->zbx->query['snmp_community'] = "$snmp_community";
-
$this->zbx->query['port'] = '161';
-
$this->zbx->query['value_type'] = '1';
-
$this->zbx->query['delay'] = '60';
-
$this->zbx->query['applications'] = ["$applications"];
-
$this->zbx->query['history'] = '7';
-
$this->zbx->query['description'] = "$description";
-
$this->zbx->query['valuemapid'] = '8'; //特殊
-
$itemids = $this->zbx->call()['result']['itemids']['0'];
-
return $itemids;
-
}
-
}
-
-
/**
-
* Usage_CreateTwoOID
-
* 执行函数 创建双OID
-
*
-
* @access public
-
* @param string $GroupNmae 组名称
-
* @param string $TemplateNmae 新建模板名称
-
* @param string $oid OID
-
* @param integer $applications 应用ID
-
* @param integer $type item类型选择
-
* @param string $description 描述
-
* @return integer
-
*/
-
function Usage_CreateOneOID($GroupNmae, $TemplateNmae, $oid, $applications, $type, $description){
-
-
if(!isset($GroupNmae) || !isset($TemplateNmae) || !isset($oid) || !isset($applications) || !isset($type) || !isset($description)){
-
echo "###################################################################################################
";
-
echo "# 说明: $a->Pkts('组名称','模板名称','oid','应用ID','类型ID','描述'); #
";
-
echo "# 使用: $a->Pkts('华为交换机2326模板','Switch_Quidway_S2326TP-EI','ifAlias',5,2,'端口描述'); #
";
-
echo "# 创建应用ID: 输入1-10 #
";
-
echo "# 对应关系: 5=端口描述;6=操作状态;8=管理状态 #
";
-
echo "# 创建类型ID: 输入2-4 #
";
-
echo "# 对应关系: 2=其他;3=管理状态;4=操作状态 #
";
-
echo "###################################################################################################
";
-
exit();
-
}
-
-
$TemplateID = $this->CreateTemplate($GroupNmae,$TemplateNmae);
-
$ApplicationsID = $this->CreateApplication($TemplateID,$applications);
-
$oid_name = str_replace(array('if'), "",$oid);
-
-
$insert_sql="INSERT
INTO `items_plus`(`id`, `hostid`, `snmp_index`, `itemids`,
`items_name`, `items_key`, `items_oid`, `ApplicationID`) VALUES ";
-
-
foreach($this->snmp as $key => $value ){
-
-
if($value['VERSION'] == 'v1'){
-
$snmp_version = "1";
-
}elseif($value['VERSION'] == 'v2c'){
-
$snmp_version = "4";
-
}
-
-
$items = str_replace(array("
", "
", "
"), "", $value['OID_port']);
-
$items_name = $items."_$oid_name";
-
$items_key = str_replace('/','_',$items_name);
-
$items_oid = str_replace('ifDescr',"$oid",$value['OID_index']);
-
$items_oid = str_replace(array("
", "
", "
"), "", $items_oid);
-
-
$itemids = $this->CreateItem($type, $TemplateID, $items_name, $snmp_version, $items_key, $items_oid, $value['COMMUNITY'], $ApplicationsID,$description);
-
-
$insert_sql .= "(NULL,'$TemplateID','$value[4]','$itemids','$items_name','$items_key','$items_oid','$ApplicationsID'),";
-
}
-
$insert_sql = rtrim($insert_sql,',');
-
$insert_sql = $insert_sql.";";
-
$this->pdo->exec("$insert_sql");
-
}
-
-
/**
-
* Usage_CreateTwoOID
-
* 执行函数 创建双OID
-
*
-
* @access public
-
* @param string $GroupNmae 组名称
-
* @param string $TemplateNmae 新建模板名称
-
* @param string $InPkts 接收OID
-
* @param string $OutPkts 发送OID
-
* @param integer $type 选择item
-
* @param integer $applications 应用ID
-
* @param string $in_description 接收item描述
-
* @param string $out_description 发送item描述
-
* @return integer
-
*/
-
function Usage_CreateTwoOID($GroupNmae, $TemplateNmae, $InPkts, $OutPkts, $type, $applications, $in_description,$out_description){
-
-
if(!isset($GroupNmae) || !isset($TemplateNmae) || !isset($InPkts) || !isset($OutPkts) || !isset($type) || !isset($applications) || !isset($in_description) || !isset($out_description)){
-
echo "###########################################################################################################################
";
-
echo "# 说明: $a->Pkts('组名称', '模板名称', '接收OID', '发送OID', '选择item', '应用ID', '接收OID描述', '发送OID描述'); #
";
-
echo "# 使用: $a->Pkts('华为交换机2326模板', 'Switch_Quidway_S2326TP-EI', 'ifInUcastPkts', 'ifOutUcastPkts', 0, 2, '单波包'); #
";
-
echo "# type: 输入0-1 #
";
-
echo "# 对应关系: 0=其他数据包差值,1=特指流量包; #
";
-
echo "# 创建应用ID: 输入1-10 #
";
-
echo "# 对应关系: 1=列队;2=单播包;3=多播包;4=广播包;9=错误包;10=非单播包; #
";
-
echo "###########################################################################################################################
";
-
exit();
-
}
-
-
$TemplateID = $this->CreateTemplate($GroupNmae,$TemplateNmae);
-
$ApplicationsID = $this->CreateApplication($TemplateID,$applications);
-
-
$InPkts_name = str_replace(array('ifIn'), "",$InPkts);
-
$OutPkts_name = str_replace(array('ifOut'), "",$OutPkts);
-
-
$insert_sql="INSERT
INTO `items_plus`(`id`, `hostid`, `snmp_index`, `itemids`,
`items_name`, `items_key`, `items_oid`, `ApplicationID`) VALUES ";
-
-
foreach($this->snmp as $key => $value ){
-
-
if($value['VERSION'] == 'v1'){
-
$snmp_version = "1";
-
}elseif($value['VERSION'] == 'v2c'){
-
$snmp_version = "4";
-
}
-
-
$items = str_replace(array("
", "
", "
"), "", $value['OID_port']);
-
$items_in_name = $items."_in_$InPkts_name";
-
$items_in_key = str_replace('/','_',$items_in_name);
-
$items_in_oid = str_replace('ifDescr',"$InPkts",$value['OID_index']);
-
$items_in_oid = str_replace(array("
", "
", "
"), "", $items_in_oid);
-
-
$items_out_name = $items."_out_$OutPkts_name";
-
$items_out_key = str_replace('/','_',$items_out_name);
-
$items_out_oid = str_replace('ifDescr',"$OutPkts",$value['OID_index']);
-
$items_out_oid = str_replace(array("
", "
", "
"), "", $items_out_oid);
-
-
$in_itemids = $this->CreateItem($type, $TemplateID, $items_in_name, $snmp_version, $items_in_key, $items_in_oid, $value['COMMUNITY'], $ApplicationsID,$in_description);
-
$out_itemids = $this->CreateItem($type, $TemplateID, $items_out_name, $snmp_version, $items_out_key, $items_out_oid, $value['COMMUNITY'], $ApplicationsID,$out_description);
-
-
$insert_sql .= "(NULL,'$TemplateID','$value[4]','$in_itemids','$items_in_name','$items_in_key','$items_in_oid','$ApplicationsID'),";
-
$insert_sql .= "(NULL,'$TemplateID','$value[4]','$in_itemids','$items_out_name','$items_out_key','$items_out_oid','$ApplicationsID'),";
-
}
-
$insert_sql = rtrim($insert_sql,',');
-
$insert_sql = $insert_sql.";";
-
$this->pdo->exec("$insert_sql");
-
}
-
-
/**
-
* CreateTrigger
-
* 这个函数是创建触发器,但只是创建接收NUcastPkts的触发器
-
* 预防广播风暴
-
*
-
* @access public
-
* @param string $HostName 模板名称
-
* @return
-
*/
-
function CreateTrigger($HostName){
-
$Macros1_1 = '{$NUCASTPKTS_VALUE1}';
-
$Macros1_2 = '{$NUCASTPKTS_VALUE2}';
-
$Macros1_3 = '{$NUCASTPKTS_VALUE3}';
-
$Macros2 = '{$NUCASTPKTS_NAME_HEAD}';
-
$Macros3 = '{$NUCASTPKTS_NAME_TAIL}';
-
$HostID = $this->GetTemplateID($HostName);
-
$sql = "SELECT * FROM `items_plus` WHERE `hostid` = $HostID AND `items_key` LIKE '%in%'";
-
$this->items = $this->pdo->query($sql);
-
-
$i = 1;
-
foreach($this->items as $key => $value ){
-
$description_name = str_replace(array("
", "
", "
"), "", $value['items_name']);
-
$description_name = str_replace(array('_in_NUcastPkts'), "",$description_name);
-
// print_r($description_name);
-
-
$this->zbx->method = 'trigger.create';
-
$this->zbx->query['description'] = $Macros2 . ' ' . $description_name . " > " . $Macros1_1 .' '. $Macros3;
-
$this->zbx->query['expression'] = '{' . $HostName . ':' . $value['items_key'] . ".last(#5)" . '}>' . $Macros1_1;
-
$this->zbx->query['priority'] = '1';
-
$result1 = $this->zbx->call();
-
-
$this->zbx->method = 'trigger.create';
-
$this->zbx->query['description'] = $Macros2 . ' ' . $description_name . " > " . $Macros1_2 .' '. $Macros3;
-
$this->zbx->query['expression'] = '{' . $HostName . ':' . $value['items_key'] . ".last(#5)" . '}>' . $Macros1_2;
-
$this->zbx->query['priority'] = '2';
-
$result2 = $this->zbx->call();
-
-
$this->zbx->method = 'trigger.create';
-
$this->zbx->query['description'] = $Macros2 . ' ' . $description_name . " > " . $Macros1_3 .' '. $Macros3;
-
$this->zbx->query['expression'] = '{' . $HostName . ':' . $value['items_key'] . ".last(#5)" . '}>' . $Macros1_3;
-
$this->zbx->query['priority'] = '3';
-
$result3 = $this->zbx->call();
-
echo $i++."
";
-
}
-
}
-
}
- ?>
usage.php
点击(此处)折叠或打开
-
#!/opt/lampp/bin/php
-
<?php
-
require __DIR__ . '/zbx.templates.class.php';
- $ZabbixTemplates = new ZabbixTemplates;
-
# 华为2326交换机
-
// $ZabbixTemplates->Usage_CreateOneOID('华为交换机2326模板','Switch_Quidway_S2326TP-EI_Alias','ifAlias',5,2,'端口描述');
-
// $ZabbixTemplates->Usage_CreateOneOID('华为交换机2326模板','Switch_Quidway_S2326TP-EI_AdminStatus','ifAdminStatus',8,3,'端口管理状态');
-
// $ZabbixTemplates->Usage_CreateOneOID('华为交换机2326模板','Switch_Quidway_S2326TP-EI_OperStatus','ifOperStatus',6,4,'端口操作状态');
-
//
$ZabbixTemplates->Usage_CreateTwoOID('华为交换机2326模板
','Switch_Quidway_S2326TP-
EI_NUcastPkts','ifInNUcastPkts','ifOutNUcastPkts',0,10,'接收非单波包','发送非单波包
');
-
//
$ZabbixTemplates->Usage_CreateTwoOID('华为交换机2326模板
','Switch_Quidway_S2326TP-EI_Errors','ifInErrors','ifOutErrors',0,9,'接收错
误包','发送错误包');
-
//
$ZabbixTemplates->Usage_CreateTwoOID('华为交换机2326模板
','Switch_Quidway_S2326TP-
EI_MulticastPkts','ifInMulticastPkts','ifInMulticastPkts',0,3,'接收多波包','发
送多波包');
-
//
$ZabbixTemplates->Usage_CreateTwoOID('华为交换机2326模板
','Switch_Quidway_S2326TP-
EI_UcastPkts','ifInUcastPkts','ifOutUcastPkts',0,2,'接收单波包','发送单波包');
-
//
$ZabbixTemplates->Usage_CreateTwoOID('华为交换机2326模板
','Switch_Quidway_S2326TP-
EI_BroadcastPkts','ifInBroadcastPkts','ifOutBroadcastPkts',0,4,'接收广波包','
发送广波包');
-
//
$ZabbixTemplates->Usage_CreateTwoOID('华为交换机2326模板
','Switch_Quidway_S2326TP-EI_QLen','ifInQLen','ifOutQLen',0,1,'接收列队','发送
列队');
- // $ZabbixTemplates->CreateTrigger('Switch_Quidway_S2326TP-EI_NUcastPkts');
注意:
insert_snmp_oid.php这个文件是操作文件,用来生成SNMP信息。
usage.php这个文件是导入的操作文件。这个导入只能一次一个的执行这个文件来完成。不能同时导入多个。上边有10个导入,就得执行10次这个文件。
本人是这样用的,第一开个putty的SSH客户端,打开一个可以支持sftp的编辑器notepad++,putty创建文件加入操作权限,通过notepad++连接centos进行编辑修改。
为什么要创建这么多的模板?模板关联模板自己想吧。
最后的那个itmes_plus表是干什么用的? 存itemids有了这个id,后期再写trigger 和 graphs就比较容易了。
其实那些OID都是用的OID名字,zabbix支持这种写法。
为什么TMD用PHP写?因为目前我就会点这语言,以后会重新写成PYTHON的。