-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
132 lines (111 loc) · 4.3 KB
/
__main__.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
#
# This file is part of the OpenSIPS Python Package
# (see https://github.com/OpenSIPS/python-opensips).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
""" Script to run OpenSIPS MI commands """
import sys
import json
import argparse
from opensips.mi import OpenSIPSMI, OpenSIPSMIException
parser = argparse.ArgumentParser()
communication = parser.add_argument_group('communication')
communication.add_argument('-t', '--type',
type=str,
default='fifo',
choices=['fifo', 'http', 'datagram'],
help='OpenSIPS MI Communication Type')
communication.add_argument('-i', '--ip',
type=str,
help='OpenSIPS MI IP Address',
default='127.0.0.1')
communication.add_argument('-p', '--port',
type=int,
help='OpenSIPS MI Port',
default=8888)
communication.add_argument('-f', '--fifo-file',
type=str,
help='OpenSIPS MI FIFO File')
communication.add_argument('-fb', '--fifo-fallback',
type=str,
help='OpenSIPS MI Fallback FIFO File')
communication.add_argument('-fd', '--fifo-reply-dir',
type=str,
help='OpenSIPS MI FIFO Reply Directory')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-s', '--stats',
nargs='+',
default=[],
help='statistics')
group.add_argument('command',
nargs='?',
type=str,
help='command')
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument('-j', '--json',
type=str,
help='json',
required=False)
group.add_argument('parameters',
nargs='*',
default=[],
help='cmd args')
def main():
""" Main function of the opensips-mi script """
args = parser.parse_args()
if args.stats:
print('Using get_statistics! Be careful not to use '
'command after -s/--stats.')
args.command = 'get_statistics'
if args.json:
print('Cannot use -s/--stats with -j/--json!')
sys.exit(1)
args.parameters = {'statistics': args.stats}
else:
if args.json:
try:
args.parameters = json.loads(args.json)
print(args.parameters)
except json.JSONDecodeError as e:
print('Invalid JSON: ', e)
sys.exit(1)
if args.type == 'fifo':
fifo_args = {}
if args.fifo_file:
fifo_args['fifo_file'] = args.fifo_file
if args.fifo_fallback:
fifo_args['fifo_file_fallback'] = args.fifo_fallback
if args.fifo_reply_dir:
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
mi = OpenSIPSMI('fifo', **fifo_args)
elif args.type == 'http':
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
elif args.type == 'datagram':
mi = OpenSIPSMI('datagram',
datagram_ip=args.ip,
datagram_port=args.port)
else:
print(f'Unknownt type: {args.type}')
sys.exit(1)
try:
response = mi.execute(args.command, args.parameters)
print(json.dumps(response, indent=4))
except OpenSIPSMIException as e:
print('Error: ', e)
sys.exit(1)
if __name__ == "__main__":
main()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4