forked from akarneliuk/pygnmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygnmicli.py
93 lines (71 loc) · 3.14 KB
/
pygnmicli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
#(c)2019-2021, karneliuk.com
# Modules
import sys
import logging
import json
import os
# Own modules
from pygnmi.arg_parser import NFData
from pygnmi.client import gNMIclient, telemetryParser
from pygnmi.artefacts.messages import msg
# Variables
path_msg = 'artefacts/messages.json'
path_log = 'log/execution.log'
# Body
if __name__ == "__main__":
# Setting logger
if not os.path.exists(path_log.split('/')[0]):
os.mkdir(path_log.split('/')[0])
logging.basicConfig(filename=path_log, level=logging.INFO, format='%(asctime)s.%(msecs)03d+01:00,%(levelname)s,%(message)s', datefmt='%Y-%m-%dT%H:%M:%S')
logging.info('Starting application...')
# Collecting inputs
del sys.argv[0]
DD = NFData(sys.argv, msg)
# gNMI operation
with gNMIclient(DD.targets, username=DD.username, password=DD.password,
debug=DD.to_print, insecure=DD.insecure) as GC:
result = None
if DD.operation == 'capabilities':
print(f'Doing {DD.operation} request to {DD.targets}...')
result = GC.capabilities()
elif DD.operation == 'get':
print(f'Doing {DD.operation} request to {DD.targets}...')
result = GC.get(DD.gnmi_path, datatype='all', encoding='json')
elif DD.operation == 'set':
print(f'Doing {DD.operation} request to {DD.targets}...')
deletes = DD.gnmi_path if DD.gnmi_path else None
updates = DD.update if DD.update else None
replaces = DD.replace if DD.replace else None
encoding = 'json_ietf'
result = GC.set(delete=deletes, update=updates, replace=replaces, encoding=encoding)
elif DD.operation == 'subscribe':
# aliases = [('openconfig-interfaces:interfaces', '#interfaces'), ('openconfig-acl:acl', '#acl')]
subscribe1 = {
'subscription': [
{
'path': 'openconfig-interfaces:interfaces/interface[name=Ethernet1]',
'mode': 'sample',
'sample_interval': 10000000000,
'heartbeat_interval': 30000000000
}
],
'use_aliases': False,
'mode': 'stream',
'encoding': 'proto'}
subscribe2 = {
'subscription': [
{
'path': 'openconfig-interfaces:interfaces/interface[name=1/1/c1/1]',
'mode': 'sample',
'sample_interval': 10000000000
}
],
'use_aliases': False,
'mode': 'stream',
'encoding': 'json'}
result = GC.subscribe(subscribe=subscribe1)
for ent in result:
print(telemetryParser(ent))
if result:
print(result)