Skip to content

Commit edc3766

Browse files
committed
add opensips-mi script
1 parent 23fe9e5 commit edc3766

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

opensips-mi.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import argparse
2+
from opensips.mi import OpenSIPSMI, OpenSIPSMIException
3+
import json
4+
5+
parser = argparse.ArgumentParser()
6+
7+
communication = parser.add_argument_group('communication')
8+
9+
communication.add_argument('-t', '--type',
10+
type=str,
11+
help='OpenSIPS MI Communication Type',
12+
default='http')
13+
communication.add_argument('-i', '--ip',
14+
type=str,
15+
help='OpenSIPS MI IP Address',
16+
default='127.0.0.1')
17+
communication.add_argument('-p', '--port',
18+
type=int,
19+
help='OpenSIPS MI Port',
20+
default=8888)
21+
22+
group = parser.add_mutually_exclusive_group(required=True)
23+
24+
group.add_argument('-s', '--stats',
25+
nargs='+',
26+
default=[],
27+
help='statistics')
28+
29+
group.add_argument('command',
30+
nargs='?',
31+
type=str,
32+
help='command')
33+
34+
group = parser.add_mutually_exclusive_group(required=False)
35+
36+
group.add_argument('-j', '--json',
37+
type=str,
38+
help='json',
39+
required=False)
40+
41+
group.add_argument('parameters',
42+
nargs='*',
43+
default=[],
44+
help='cmd args')
45+
46+
args = parser.parse_args()
47+
print(args)
48+
49+
if args.stats:
50+
print('Using get_statistics! Be careful not to use command after -s/--stats.')
51+
print(args.stats)
52+
args.command = 'get_statistics'
53+
54+
if args.json:
55+
print('Cannot use -s/--stats with -j/--json!')
56+
exit(1)
57+
58+
args.parameters = {'statistics': args.stats}
59+
else:
60+
if args.json:
61+
try:
62+
args.parameters = json.loads(args.json)
63+
print(args.parameters)
64+
except json.JSONDecodeError as e:
65+
print('Invalid JSON: ', e)
66+
exit(1)
67+
68+
if args.type == 'http':
69+
mi = OpenSIPSMI('http', url='http://{}:{}/mi'.format(args.ip, args.port))
70+
71+
if args.type == 'datagram':
72+
mi = OpenSIPSMI('datagram', datagram_ip=args.ip, datagram_port=args.port)
73+
74+
try:
75+
response = mi.execute(args.command, args.parameters)
76+
print(json.dumps(response, indent=4))
77+
except OpenSIPSMIException as e:
78+
print('Error: ', e)
79+
exit(1)

0 commit comments

Comments
 (0)