zabbix 监控交换机文本/日志信息

简介

  公司有台路由设备,需要每天去核对用户量,查看信息等。为了避免重复的工作量,就通过zabbix来完成这个重复性的工作。
  先说下思路,首先要创建一个脚本,能从zabbix-server 或者zabbi-agent 远程到路由/交换机,通过脚本,获取到用户量的数据并存到zabbix-server或者zabbix-agent上,然后在通过zabbix监控获取到想要的信息。
 
废话不多少 直接上干货

脚本内容

1、通过服务器获取路由的信息

此脚本是通过服务器 ssh 登录到交换机/路由上,然后打印出查询信息,存到服务器的指定路径
我要获取到的是 'dis time all','dis int gig0/1/1','display access-user' 这个三个信息
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import pexpect
import time
import sys,os
import shutil
 
def ssh_login(Host, Username, Password, Commands):
#ssh 登录到交换机/路由
ssh = pexpect.spawn('ssh %s@%s' %(Username, Host))
res = ssh.expect(['password:', 'continue connecting (yes/no)?'], timeout=5)
if res == 0:
ssh.sendline(Password)
elif res == 1:
ssh.sendline('yes')
ssh.expect('password:')
ssh.sendline(Password)
ssh.expect([finish,pexpect.TIMEOUT,pexpect.EOF])
for cmd in Commands:
print "finish....."
ssh.sendline(cmd)
for i in range(0,5):
ssh.sendline(' ')
 
res = file(filepath,'wb')
ssh.logfile_read = res
ssh.sendline('quit')
ssh.read()
ssh.close()
res.close()
 
 
def filename_change(filepath):
localtime=time.strftime('%Y%m%d%H%M',time.localtime(time.time()))
dirtime=time.strftime('%Y%m',time.localtime(time.time()))
filename=os.path.split(filepath)
filedir=filename[0]+'/'+dirtime
isExists=os.path.exists(filedir)
if not isExists:
os.makedirs(filedir)
filenewname=filename[1].split('.')
shutil.move(filepath, filedir)
os.chdir(filedir)
#os.rename(filename[1],filenewname[0]+'_'+localtime+'.'+filenewname[1])
 
 
if __name__=='__main__':
Host = '192.168.4.3'
Username = 'lenovoxj'
Password = 'Lenovo@123'
Commands = ['dis time all','dis int gig0/1/1','display access-user']
finish = '<HZ-YD-RT-LNS-1>'
filepath = '/home/data/xunjian/log/lns1hz.txt'
 
ssh_login(Host, Username, Password, Commands)
#filename_change(filepath)
运行脚本,查看获取得到的信息是否正确
[root@hz-yd-sev-zabbix-1 log]# more lns1hz.txt
dis time all
Current time is 2018-09-29 09:00:55 Saturday
 
<HZ-YD-RT-LNS-1>
<HZ-YD-RT-LNS-1>
<HZ-YD-RT-LNS-1>dis int gig0/1/1
GigabitEthernet0/1/1 current state : UP (ifindex: 9)
Line protocol current state : DOWN
Link quality grade : GOOD
Description: To:HZ-YD-RT-WW-1(0/1/1) via HZ-YD-LK-2(P1)
Route Port,The Maximum Transmit Unit is 1500
Internet protocol processing : disabled
IP Sending Frames' Format is PKTFMT_ETHNT_2, Hardware address is 506f-778c-1a9b
The Vendor PN is PT7420-81-1D-HW+
The Vendor Name is NEOPHOTONICS
Port BW: 10G, Transceiver max BW: 10G, Transceiver Mode: SingleMode
WaveLength: 1310nm, Transmission Distance: 10km
Rx Power: -2.98dBm, Warning range: [-15.003, 0.000]dBm
Tx Power: -2.92dBm, Warning range: [-5.999, 0.000]dBm
Loopback: none, LAN full-duplex mode, Pause Flowcontrol: Receive Enable and Send Enable
Last physical up time : 2018-07-04 19:13:49
Last physical down time : 2018-07-04 19:13:47
Current system time: 2018-09-29 09:00:55
Statistics last cleared:2018-07-03 17:48:46
Last 300 seconds input rate: 28785066 bits/sec, 4924 packets/sec
Last 300 seconds output rate: 8530159 bits/sec, 4894 packets/sec
Input peak rate 90891455 bits/sec, Record time: 2018-09-14 08:55:46
Output peak rate 24192822 bits/sec, Record time: 2018-09-18 16:16:26
Input: 7931483357369 bytes, 14623461505 packets
Output: 3121991957380 bytes, 16761231672 packets
Input:
Unicast: 14621812263 packets, Multicast: 1649239 packets
Broadcast: 3 packets, JumboOctets: 3391841 packets
CRC: 0 packets, Symbol: 0 packets
Overrun: 0 packets, InRangeLength: 0 packets
LongPacket: 0 packets, Jabber: 0 packets, Alignment: 0 packets
Fragment: 0 packets, Undersized Frame: 0 packets
RxPause: 0 packets
Output:
Unicast: 16759534570 packets, Multicast: 1697082 packets
Broadcast: 20 packets, JumboOctets: 1776413 packets
Lost: 0 packets, Overflow: 0 packets, Underrun: 0 packets
System: 0 packets, Overruns: 0 packets
TxPause: 0 packets
Local fault: normal, Remote fault: normal.
Last 300 seconds input utility rate: 0.28%
Last 300 seconds output utility rate: 0.08%
 
<HZ-YD-RT-LNS-1>
<HZ-YD-RT-LNS-1>
<HZ-YD-RT-LNS-1>display access-user
------------------------------------------------------------------------------
Total users : 28494
IPv4 users : 28491
IPv6 users : 0
Dual-Stack users : 0
Lac users : 0
RUI local users : 0
RUI remote users : 0
Wait authen-ack : 1
Authentication success : 28493
Accounting ready : 46
Accounting state : 28445
Wait leaving-flow-query : 0
Wait accounting-start : 0
Wait accounting-stop : 0
Wait authorization-client : 0
Wait authorization-server : 0
------------------------------------------------------------------------------
Domain-name Online-user
------------------------------------------------------------------------------
default0 : 0
default1 : 0
default_admin : 1
huawei.com : 28491
------------------------------------------------------------------------------
The used CID table are :
Write failed: Broken pipe
 

2、zabbix获取服务器上的信息

我们这个脚本是配置到zabbix server 上的,所以吧监控项也配置到zabbix server上
 
名称:随意填写
类型:客户端
键值:vfs.file.contents[/home/data/xunjian/log/lns1hz]
#一定要是 [绝对路径]
信息类型:文本
添加监控项刷新等待
 
 
原文地址:https://www.cnblogs.com/xuewenlong/p/12807402.html