最终效果:
目的:
通过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 =