作者:乐在TV | 来源:互联网 | 2023-10-17 13:25
交换机配置:intg100unshintvlani1ipadd192.168.56.177qstelseuser-iv4authaaaproinsshupl3qsshuserpyt
交换机配置:
int g1/0/0
un sh
int vlani 1
ip add 192.168.56.177
q
stel s e
user-i v 4
auth aaa
pro in ssh
u p l 3
q
ssh user python
ssh user python auth password
ssh user python ser stel
aaa
local-user python password irreversible-cipher Huawei@123
local-user python service-type ssh
local-user python user-group manage-ug
q
#进入Telemetry视图
telemetry
#配置设备推送目标,目标组Dest1,推送目标IP地址192.168.56.1,端口为20000
destination-group Dest1
ipv4-address 192.168.56.1 port 20000 protocol grpc no-tls
#配置设备采样数据
#当用户配置Telemetry静态订阅采样数据时,需要创建采样传感器组,并指定好采样路径。
#本例中创建采样组Sensor1,采样路径为CPU信息
#采样路径见:telemetry\CloudEngine 16800, 12800, 9800, 8800, 6800, 5800 V200R021C00 Telemetry性能指标集.xlsx
sensor-group Sensor1
sensor-path huawei-devm:devm/cpuInfos/cpuInfo
#创建订阅,将配置好的上送目标组和采样传感器组进行关联,完成数据上送。
#本例中关联目标组Dest1与传感器组Sensor1,并设置采样间隔为1000ms
subscription Sub1
destination-group Dest1
sensor-group Sensor1 sample-interval 1000
commit
q
q
配置步骤:
1、搭建环境,完成实验环境准备
2、配置设备Telemetry静态订阅方式想着配置,包括采集内容、推送对象和推送间隔
3、官网下载华为设备对应发布的.proto文件。
华为企业用户下载地址:http://support.huawei.com/enterprise
运营商用户下载地址:http://support.huawei.com/carrier
搜索相应的设备型号及版本号
软件——版本及补丁
采样路径见:telemetry\CloudEngine 16800, 12800, 9800, 8800, 6800, 5800 V200R021C00 Telemetry性能指标集.xlsx
编译proto文件得到服务端调用方法
本例中,使用run_codegen.py脚本编译proto文件。注意将所有.proto文件放入/protos目录
本脚本将一次编译huawei-grpc-dialout.proto、huawei-telemetry.proto和huawei-devm.proto
4、编写服务端代码,监听指定端口获取数据
5、根据上送数据的不同,选择对应的方法对数据内容进行解码
在文件目录下创建一个protos文件夹,将huawei-grpc-dialout.proto、huawei-telemetry.proto和huawei-devm.proto放到protos文件夹下
Generate_codes代码:编译.proto文件
#!/usr/bin
# _*_ coding: UTF-8 _*_
# Copyright (c) 2021 GengYu.All rights reserved
# @Create by gengyu
# @Create Time :2021/12/18
# @File Name : Generate_codes
# 打包命令 pyinstaller -F package\Generate_codes
"""
generates protocol messages and gRPC stubs.
"""
__author__ = 'Administrator'
from grpc_tools import protoc
protoc.main(
(
'',
'-I./protos',
'--python_out=.',
'--grpc_python_out=.',
'./protos/huawei-grpc-dialout.proto',#文件路径
)
)
protoc.main(
(
'',
'-I./protos',
'--python_out=.',
'--grpc_python_out=.',
'./protos/huawei-telemetry.proto',#文件路径
)
)
protoc.main(
(
'',
'-I./protos',
'--python_out=.',
'--grpc_python_out=.',
'./protos/huawei-devm.proto',#文件路径
)
)
telemetry_server.py代码:运行即可接收交换机推送的信息
#!/usr/bin
# _*_ coding: UTF-8 _*_
# Copyright (c) 2021 GengYu.All rights reserved
# @Create by gengyu
# @Create Time :2021/12/18
# @File Name : telemetry_server
# 打包命令 pyinstaller -F package\telemetry_server
"""
"""
__author__ = 'Administrator'
import doctest
from concurrent import futures
import time
import importlib
import grpc#pip 安装
import huawei_grpc_dialout_pb2_grpc#generate_codes.py生成
import huawei_telemetry_pb2#generate_codes.py生成
_ONE_DAY_IN_SECOnDS= 60 * 60 * 24
def tele_server():
#创建一个grpc server对象
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
#注册huawei的telemetry 数据监听服务
huawei_grpc_dialout_pb2_grpc.add_gRPCDataserviceServicer_to_server(
Telemetry_CPU_Info(),server)
#设置socket监听端口和PC的IP地址,与交换机中推送目标IP地址一致
server.add_insecure_port('192.168.56.1:20000')
#启动 grpc server
server.start()
#死循环监听
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
#创建类继承huawei_grpc_dialout_pb2_grpc中的Servicer方法
class Telemetry_CPU_Info(huawei_grpc_dialout_pb2_grpc.gRPCDataserviceServicer):
def __init__(self):
return
def dataPublish(self, request_iterator, context):
for i in request_iterator:
print('________________________ start ________________________')
telemetry_data = huawei_telemetry_pb2.Telemetry.FromString(i.data)
print(telemetry_data)
for row_data in telemetry_data.data_gpb.row:
print('________________________')
print('The proto path is :' + telemetry_data.proto_path)
print('________________________')
module_name = telemetry_data.proto_path.split('.')[0]
root_class = telemetry_data.proto_path.split('.')[1]
#动态加载telemetry 获取数据的对应模块,本例中为:
decode_module = importlib.import_module(module_name + '_pb2')
print(decode_module)
#定义解码方法:getattr获取动态加载的模块中的属性值,调用此属性的解码方法FromString
decode_func = getattr(decode_module,root_class).FromString
print('________________________ content is ________________________\n')
#将row_data中的content中的内容使用此方法解码,并输出
print(decode_func(row_data.content))
print('________________________ done ________________________')
if __name__ == "__main__":
tele_server()
运行结果:
________________________ start ________________________
node_id_str: "HUAWEI"
subscription_id_str: "Sub1"
sensor_path: "huawei-devm:devm/cpuInfos/cpuInfo"
collection_id: 13
collection_start_time: 1639850454743
msg_timestamp: 1639850454847
data_gpb {
row {
timestamp: 1639850454743
content: "*\022\n\020\"\0011\010\201\200\204\010(\n\030Z0K\020\010"
}
row {
timestamp: 1639850454743
content: "*\023\n\021\"\00217\010\201\200\304\010(\013\030Z0K\020\010"
}
}
collection_end_time: 1639850454743
current_period: 10000
except_desc: "OK"
product_name: "CE12800"
proto_path: "huawei_devm.Devm"
________________________
The proto path is :huawei_devm.Devm
________________________
________________________ content is ________________________
cpuInfos {
cpuInfo {
entIndex: 16842753
interval: 8
ovloadThreshold: 90
position: "1"
systemCpuUsage: 10
unovloadThreshold: 75
}
}
________________________ done ________________________
________________________
The proto path is :huawei_devm.Devm
________________________
________________________ content is ________________________
cpuInfos {
cpuInfo {
entIndex: 17891329
interval: 8
ovloadThreshold: 90
position: "17"
systemCpuUsage: 11
unovloadThreshold: 75
}
}
________________________ done ________________________