-
Notifications
You must be signed in to change notification settings - Fork 2
/
confiparse.py
159 lines (144 loc) · 7.27 KB
/
confiparse.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import logg
import ConfigParser
log = logg.Loggable(alog_name=__name__)
# Todo make file opener handler and log the message for No option 'radius_server' in section: 'globals'
# Default location of Asterisk ini files extensions.con and managers.conf
confdir = '/etc/asterisk/'
extensionsconf = confdir + 'extensions.conf'
managersconf = confdir + 'manager.conf'
celconf = confdir + 'cel.conf'
cdrconf = confdir + 'cdr_manager.conf'
asterisk_amihost = "127.0.0.1"
class ConfigOpener:
"""Class which parse configs"""
global confdir, asterisk_amihost, log
def __init__(self):
"""Initializing the following attributes"""
self.config = ConfigParser.ConfigParser()
self.configfiles = None
self.radius_addr = None
self.radius_secret = ""
self.radius_acct_port = None
self.rnas_addr = None
self.raddict = {}
# Ami Manager settings:
self.amiport = None
self.amienables = ''
self.amihost = None
self.amimanager = ''
self.amisecret = ''
self.amimdict = {}
self.cdr_enabled = None
self.amirw = ''
# CEL settings:
self.cel_enable = ''
self.cel_answer = ''
self.ami_backend = ''
def radius_config(self, afconfig=extensionsconf):
self.configfiles = self.config.readfp(open(afconfig))
log.info(" -- Start reading config settings from file %s " % afconfig)
try:
self.radius_addr = self.config.get('globals', 'RADIUS_Server')
except ConfigParser.NoOptionError:
log.critical('Please set RADIUS_Server in [general] section of %s ini file' % afconfig)
sys.stderr.write('Please set RADIUS_Server in [general] section of %s ini file' % afconfig)
sys.exit(1)
try:
self.radius_acct_port = self.config.get('globals', 'RADIUS_Acct_Port')
except ConfigParser.NoOptionError:
log.critical('Please set RADIUS_Acct_Port in [general] section of %s ini file' % afconfig)
sys.stderr.write('Please set RADIUS_Acct_Port in [general] section of %s ini file' % afconfig)
sys.exit(1)
try:
self.rnas_addr = self.config.get('globals', 'NAS_IP_Address')
except ConfigParser.NoOptionError:
log.critical('Please set NAS_IP_Address in [general] section of %s ini file' % afconfig)
sys.stderr.write('Please set NAS_IP_Address in [general] section of %s ini file' % afconfig)
sys.exit(1)
log.info(' -- Radius client settings are Radius address %s and Radius Accounting Ports %s'
% (self.radius_addr, self.radius_acct_port))
try:
self.radius_secret = self.config.get('globals', 'RADIUS_Secret')
except ConfigParser.NoOptionError:
log.critical('Please set RADIUS_Secret in [general] section of %s ini file' % afconfig)
sys.stderr.write('Please set RADIUS_Secret in [general] section of %s ini file' % afconfig)
sys.exit(1)
log.info(' -- NAS IP address %s ' % self.rnas_addr)
self.raddict = {'radius_addr': self.radius_addr, 'radius_acct_port': self.radius_acct_port,
'rnas_addr': self.rnas_addr, 'radius_secret': self.radius_secret}
return self.raddict
def ami_config(self, afconfig=managersconf, ):
log.info(" -- Start reading config settings from files %s " % afconfig)
self.configfiles = self.config.readfp(open(afconfig))
self.amiport = self.config.get('general', 'port')
self.amienables = self.config.get('general', 'enabled')
self.amihost = asterisk_amihost
log.info("Reading AMI manager config")
for managers in ['test', 'lbamimanager']:
if managers in self.config.sections():
# logger.setLevel(lg.DEBUG)
log.debug("-managers- %s" % managers)
self.amimanager = managers
self.amisecret = self.config.get(managers, 'secret')
log.info("AMI manager login credentials:%s " % managers)
try:
self.amirw = self.config.get(managers, 'read')
except ConfigParser.NoOptionError:
log.critical('Please set read = system,log,cdr,agent,call,user '
' in %s file ' % afconfig)
sys.exit(1)
if self.amirw.find('cdr') == -1:
log.critical('Please set read = system,log,cdr,agent,call,user '
' in %s file ' % afconfig)
sys.exit(1)
else:
log.info('AMI manager read settings are %s' % self.amirw)
break
else:
log.critical('AMI manager couldn\'t be found')
log.critical('Please set one in /etc/asterisk/manager.conf')
sys.exit(1)
self.amimdict = {'ami_port': self.amiport, 'ami_enabled': self.amienables, 'ami_host': self.amihost,
'ami_manager': self.amimanager, 'ami_secret': self.amisecret}
return self.amimdict
def cdr_config(self, afconfig=cdrconf):
log.info(' --Start reading CDR config from ini file : %s' % afconfig)
self.configfiles = self.config.readfp(open(afconfig))
self.cdr_enabled = self.config.get('general', 'enabled')
if self.cdr_enabled == 'yes':
return True
else:
log.critical('Please set enabled=yes in [general] section of %s ini file' % afconfig)
log.critical('Now value is set to %s' % self.cdr_enabled)
sys.stderr.write('Please set enabled=yes in [general] section of %s ini file' % afconfig)
sys.stderr.write("\n \t Exiting ...")
sys.exit(1)
def cel_config(self, afconfig=celconf):
log.info('-- Start reading config from %s file ' % afconfig)
self.configfiles = self.config.readfp(open(afconfig))
try:
self.cel_enable = self.config.get('general', 'enable')
if 'yes' != self.cel_enable:
log.critical('Please set enable=yes in [general] section of %s ini file' % afconfig)
sys.exit(1)
self.cel_answer = self.config.get('general', 'events')
if self.cel_answer.find('ANSWER') == -1:
sys.stderr.write('Please set events=ANSWER in [general] section of %s ini file' % afconfig)
log.critical('Please set events=ANSWER in [general] section of %s ini file' % afconfig)
sys.exit(1)
except ConfigParser.NoOptionError:
sys.stderr.write('Please set enable=yes in [general] section of %s ini file' % afconfig)
sys.exit(1)
# print(afconfig+"Please set enable=yes in [general] section of ini file", file=sys.stderr)
try:
self.ami_backend = self.config.get('manager', 'enabled')
if 'yes' != self.ami_backend:
log.critical('Please set enabled=yes in [manager] section of %s ini file' % afconfig)
sys.exit(1)
except ConfigParser.NoOptionError:
sys.stderr.write('Please set enabled=yes in [manager] section of %s ini file' % afconfig)
sys.exit(1)
return True