wang_rx 发表于 2018-1-1 08:34:31

zabbix 批量添加聚合图形

#!/bin/sh  
function NIC {
  #变量
  #网卡名
  NIC=`ifconfig | grep -v ':Link' | grep -E 'Link|flags' | awk '{print $1}' | grep -vE 'lo|^$'`
  #统计网卡个数
  Num=`ifconfig | grep -v ':Link' | grep -E 'Link|flags' | awk '{print $1}' | grep -vE 'lo|^$' | wc -l`
  #新布局的网卡名后面有冒号需要去掉
  sed=`ifconfig | grep -v ':Link' | grep -E 'Link|flags' | awk '{print $1}' | grep -vE 'lo|^$'| sed 's/://g'`
  #网卡总数减1
  Less=`expr $Num - 1`
  #用于修改key结尾的自增变量
  add="0"
  #存放"判断ifconfig是否为新布局"值
  judgment=''
  #判断ifconfig是否为新布局
  if [ $add == 0 ]
  then
  HWaddr=`ifconfig $i | grep -E 'HWaddr'`
  judgment=`echo $?`
  fi
  #替换NIC变量
  if [ $judgment != 0 ]
  then
  NIC=`echo $sed| sed 's/ /\n/g'`
  fi
  #写key开头参数
  echo -e "{"'\n\t''"data":['
  #收集ifconfig,网卡名,mac,IP,子网掩码,网关
  for i in $NIC;
  do
  echo -e '\t\t''{'
  

  if [ $judgment == 0 ]
  then
  #新布局
  if [ $add == $Less ]
  then
  #当为最后一块网卡,则替换结尾逗号
  ifconfig $i | grep -E 'HWaddr|inet addr|Bcast|Mask' | sed -e ':a;$!N;s/\n/ /;ta;P;D' | sed -e 's/inet addr:/inet addr /g' -e 's/Bcast:/Bcast /g' -e 's/Mask:/Mask /g' | awk '{print "\t\t\t\"{#IFNMSG}\":\""$1"\":\""$5"\":\""$8"\":\""$12"\":\""$10"\"},"}'|sed -e 's/,/]}/g'
  else
  ifconfig $i | grep -E 'HWaddr|inet addr|Bcast|Mask' | sed -e ':a;$!N;s/\n/ /;ta;P;D' | sed -e 's/inet addr:/inet addr /g' -e 's/Bcast:/Bcast /g' -e 's/Mask:/Mask /g' | awk '{print "\t\t\t\"{#IFNMSG}\":\""$1"\":\""$5"\":\""$8"\":\""$12"\":\""$10"\"},"}'
  fi
  else
  #旧布局
  if [ $add == $Less ]
  then
  #当为最后一块网卡,则替换结尾逗号
  ifconfig $i | grep -v inet6 | grep -E 'flags|inet|netmask|broadcast|ether' | sed -e ':a;$!N;s/\n/ /;ta;P;D' | sed -e 's/:://g' -e 's/inet addr:/inet addr /g' -e 's/Bcast:/Bcast /g' -e 's/Mask:/Mask /g' | awk '{print "\t\t\t\"{#IFNMSG}\":\"""'"$i"'""\":\""$12"\":\""$6"\":\""$8"\":\""$10"\"},"}'|sed -e 's/,/]}/g'
  else
  ifconfig $i | grep -v inet6 | grep -E 'flags|inet|netmask|broadcast|ether' | sed -e ':a;$!N;s/\n/ /;ta;P;D' | sed -e 's/:://g' -e 's/inet addr:/inet addr /g' -e 's/Bcast:/Bcast /g' -e 's/Mask:/Mask /g' | awk '{print "\t\t\t\"{#IFNMSG}\":\"""'"$i"'""\":\""$12"\":\""$6"\":\""$8"\":\""$10"\"},"}'
  fi
  fi
  #用于修改key结尾的自增变量
  let add+=1
  done
  
}
  
$1
页: [1]
查看完整版本: zabbix 批量添加聚合图形