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表内容
点击(此处)折叠或打开
- --
- -- 表的结构 `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 "生成中\n";
- }else{
- echo "参数格式:版本,社区名,IP地址\n";
- echo "例子:./inster_snmp_oid.php v1 stdu.edu.cn_nmc 202.206.32.42\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";
- }
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++."\n";
- // 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 . "你输入的组不存在\n";
- 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 . "模板已经存在\n";
- 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输入错误\n";
- 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 "###################################################################################################\n";
- echo "# 说明: \$a->Pkts('组名称','模板名称','oid','应用ID','类型ID','描述'); #\n";
- echo "# 使用: \$a->Pkts('华为交换机2326模板','Switch_Quidway_S2326TP-EI','ifAlias',5,2,'端口描述'); #\n";
- echo "# 创建应用ID: 输入1-10 #\n";
- echo "# 对应关系: 5=端口描述;6=操作状态;8=管理状态 #\n";
- echo "# 创建类型ID: 输入2-4 #\n";
- echo "# 对应关系: 2=其他;3=管理状态;4=操作状态 #\n";
- echo "###################################################################################################\n";
- 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("\r\n", "\r", "\n"), "", $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("\r\n", "\r", "\n"), "", $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 "###########################################################################################################################\n";
- echo "# 说明: \$a->Pkts('组名称', '模板名称', '接收OID', '发送OID', '选择item', '应用ID', '接收OID描述', '发送OID描述'); #\n";
- echo "# 使用: \$a->Pkts('华为交换机2326模板', 'Switch_Quidway_S2326TP-EI', 'ifInUcastPkts', 'ifOutUcastPkts', 0, 2, '单波包'); #\n";
- echo "# type: 输入0-1 #\n";
- echo "# 对应关系: 0=其他数据包差值,1=特指流量包; #\n";
- echo "# 创建应用ID: 输入1-10 #\n";
- echo "# 对应关系: 1=列队;2=单播包;3=多播包;4=广播包;9=错误包;10=非单播包; #\n";
- echo "###########################################################################################################################\n";
- 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("\r\n", "\r", "\n"), "", $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("\r\n", "\r", "\n"), "", $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("\r\n", "\r", "\n"), "", $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("\r\n", "\r", "\n"), "", $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++."\n";
- }
- }
- }
- ?>
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的。
zabbix通过API创建交换机模板,ifAdminStatus;ifOperStatus;ifInUcastPkts;ifAlias的更多相关文章
- zabbix利用api批量添加item,并且批量配置添加graph
关于zabbix的API见,zabbixAPI 1item批量添加 我是根据我这边的具体情况来做的,本来想在模板里面添加item,但是看了看API不支持,只是支持在host里面添加,所以我先在一个ho ...
- zabbix java api
zabbix java api zabbix官方的api文档地址:https://www.zabbix.com/documentation/3.0/manual/api Zabbix功能 概观 Zab ...
- zabbix利用自带的模板监控mysql数据库
zabbix利用自带的模板监控mysql数据库 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 有些东西你不会的时候觉得它特别难,但是当你去做的时候就发现如此的简单~zabbix功能 ...
- 调用zabbix 分组api
调用zabbix 分组api,获取分组中主机host信息,并分类保存, #!/usr/bin/env python #coding:utf8 import requests import json i ...
- Web Api ——创建WebAPI
方法在Win10 + VS2017(MVC5)测试通过 1.建立 WebApi项目: 选择菜单 “文件->新建醒目->web ->ASP.NET Web 应用程序” 输入项目名称和位 ...
- [水煮 ASP.NET Web API2 方法论](1-5)ASP.NET Web API Scaffolding(模板)
问题 我们想快速启动一个 ASP.NET Web API 解决方案. 解决方案 APS.NET 模板一开始就支持 ASP.NET Web API.使用模板往我们的项目中添加 Controller,在我 ...
- (41)zabbix监控api接口性能及可用性 天气预报api为例
现在各种应用都走api,例如淘宝,天气预报等手机.pad客户端都是走api的,那么平时也得对这些api做监控了.怎么做呢?zabbix的web监控是不二选择了.今天就以天气预报api作为一个例子. 天 ...
- 创建JDBC模板简化代码、JDBC应用的事务管理以及连接池的作用
一.创建JDBC模板简化代码 一个简单的查询.要做这么一大堆事情,并且还要处理异常,我们不防来梳理一下: 1.获取connection 2.获取statement 3.获取resultset 4 ...
- 使用 .NET CORE 创建 项目模板,模板项目,Template
场景:日常工作中,你可能会碰到需要新建一个全新的解决方案的情况(如公司新起了一个新项目,需要有全新配套的后台程序),如果公司内部基础框架较多.解决方案需要DDD模式等,那么从新起项目到各种依赖引用到能 ...
随机推荐
- json转换数据后面参数要带ture,代码
强大的PHP已经提供了内置函数:json_encode() 和 json_decode().很容易理解,json_encode()就是将PHP数组转换成Json.相反,json_decode()就是将 ...
- IOS第九天(3:QQ聊天界面通知的使用)
#import <Foundation/Foundation.h> #import "Person.h" #import "XQCompany.h" ...
- JTA和JDBC事务
一般情况下,J2EE应用服务器支持JDBC事务.JTA事务.容器管理事务.这里讨论JTA和JDBC事务的区别.这2个是常用的DAO模式事务界定方式.JDBC 事务 JDBC 事务是用 Connecti ...
- Codeforces Round #376 (Div. 2) C D F
在十五楼做的cf..一会一断...比赛的时候做出了ABCF 就没有时间了 之后没看题解写出了D..E是个神奇的博弈(递推或者dp?)看了题解也没有理解..先写了CDF.. C 有n个袜子 每个袜子都有 ...
- 省略号 对单行 多行的css
.twoline{ display: -webkit-box !important;; overflow:hidden; text-overflow: ellipsis; word-break: br ...
- LoadRunner11.00入门教程出现的问题
问题1.打不开浏览器 解决办法:打开浏览器工具--Internet 选项--高级--取消启用第三方浏览器扩展. 顺带解决了,有两个浏览器问题. 两个浏览器:一个是自带的IE,一个是其他软件插件. 解决 ...
- book
http://www.ed2000.com/ShowFile.asp?FileID=61391 e-itbook.com
- SQL ORDER BY 子句
ORDER BY 语句用于对结果集进行排序. ORDER BY 语句 ORDER BY 语句用于根据指定的列对结果集进行排序. ORDER BY 语句默认按照升序对记录进行排序. 如果您希望按照降序对 ...
- block(代码块)的介绍以及使用方法和变量之间的关系
http://blog.csdn.net/menxu_work/article/details/8762848 block(代码块)的介绍以及使用方法和变量之间的关系 block(代码块)的介绍以及使 ...
- AngularJS 实现简单购物车
使用AngularJS实现一个简单的购物车,主要感受强大的双向绑定和只关注对象不关注界面特性. 先看看界面: 点击+-操作和删除: 这些全部只需要操作数据源就行,不需要关注界面. 实现过程: 一.使用 ...