Skip to content

Commit

Permalink
Add support for multilevel configuration [Issue: unioslo#8]
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelma committed Oct 18, 2016
1 parent c479e38 commit a467b22
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bin/zabbix-cli
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ if __name__ == '__main__':
input_file = args.input_file

conf = configuration(config_file)

#
# If logging is activated, start logging to the file defined
# with log_file in the config file.
Expand Down
21 changes: 18 additions & 3 deletions etc/zabbix-cli.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@
; You should have received a copy of the GNU General Public License
; along with Zabbix-CLI. If not, see <http://www.gnu.org/licenses/>.
;
; Zabbix-cli uses a multilevel configuration approach to implement a
; flexible configuration system.
;
; Zabbix-cli will check these configuration files if they exist and
; will merge the information to get the configuration to use.
;
; 1. /usr/share/zabbix-cli/zabbix-cli.fixed.conf
; 2. /etc/zabbix-cli/zabbix-cli.fixed.conf
; 3. Configuration file defined with the parameter -c / --config when executing zabbix-cli
; 4. $HOME/.zabbix-cli/zabbix-cli.conf
; 5. /etc/zabbix-cli/zabbix-cli.conf
; 6. /usr/share/zabbix-cli/zabbix-cli.conf
;
; Check the documentation for full details on how to configurate the
; system.
;

; ######################
; Zabbix API section
Expand All @@ -34,7 +50,6 @@
; ############################
; zabbix_config section
; ############################

[zabbix_config]

; system ID. This ID will be use in the zabbix-cli prompt
Expand Down Expand Up @@ -64,7 +79,7 @@ default_notification_users_usergroup=All-notification-users

; Default directory to save exported configuration files.
; Default: $HOME/zabbix_exports
;default_directory_exports=
; default_directory_exports=

; Default export format.
; default_export_format: JSON, XML
Expand Down Expand Up @@ -111,5 +126,5 @@ log_level=INFO

; Log file used by zabbix-cli
; Default: /var/log/zabbix-cli/zabbix-cli.log
log_file=/var/log/zabbix-cli/zabbix-cli.log
;log_file=/var/log/zabbix-cli/zabbix-cli.log

10 changes: 6 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
if sys.version_info < (2, 6):
raise SystemExit('ERROR: zabbix-cli needs at least python 2.6 to work')
else:
install_requires = ['psycopg2','argparse']
install_requires = ['argparse','requests']


#
Expand All @@ -50,13 +50,15 @@

setup(name='zabbix_cli',
version=zabbix_cli['__version__'],
description='ZABBIX-CLI - PostgreSQL Backup Manager',
description='ZABBIX-CLI - Zabbix terminal client',
author='Rafael Martinez Guerrero',
author_email='[email protected]',
url='http://www.zabbix-cli.org/',
url='https://github.com/usit-gd/zabbix-cli',
packages=['zabbix_cli',],
scripts=['bin/zabbix-cli','bin/zabbix-cli-bulk-execution','bin/zabbix-cli-init'],
data_files=[('/etc/zabbix-cli', ['etc/zabbix-cli.conf'])],
data_files=[('/etc/zabbix-cli', ['etc/zabbix-cli.conf']),
('/usr/share/zabbix-cli', ['etc/zabbix-cli.conf'])],
install_requires=install_requires,
platforms=['Linux'],
classifiers=[
'Environment :: Console',
Expand Down
38 changes: 27 additions & 11 deletions zabbix_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Authors:
# [email protected] / http://www.postgresql.org.es/
#
# Copyright (c) 2014-2015 USIT-University of Oslo
# Copyright (c) 2014-2016 USIT-University of Oslo
#
# This file is part of Zabbix-CLI
# https://github.com/rafaelma/zabbix-cli
Expand Down Expand Up @@ -32,10 +32,11 @@ class configuration():
# Constructor
# ############################################

def __init__(self,config_file):
def __init__(self,config_file_from_parameter):
""" The Constructor."""

self.config_file = config_file
self.config_file_from_parameter = config_file_from_parameter
self.config_file_list = []

# Zabbix API section
self.zabbix_api_url = ''
Expand Down Expand Up @@ -68,14 +69,30 @@ def __init__(self,config_file):
def set_configuration_file(self):
"""Set the zabbix-cli configuration file"""

config_file_list = [self.config_file] + [os.getenv('HOME') + '/.zabbix-cli/zabbix-cli.conf','/etc/zabbix-cli/zabbix-cli.conf','/etc/zabbix-cli.conf']
# This list defines the priority list of configuration files
# that can exist in the system. Files close to the top of the
# list will have priority to define configuration parameters
# in the system.
#
# 1. /usr/share/zabbix-cli/zabbix-cli.fixed.conf
# 2. /etc/zabbix-cli/zabbix-cli.fixed.conf
# 3. Configuration file defined with the parameter -c / --config when executing zabbix-cli
# 4. $HOME/.zabbix-cli/zabbix-cli.conf
# 5. /etc/zabbix-cli/zabbix-cli.conf
# 6. /usr/share/zabbix-cli/zabbix-cli.conf
#

config_file_priority_list = ['/usr/share/zabbix-cli/zabbix-cli.conf', '/etc/zabbix-cli/zabbix-cli.conf', os.getenv('HOME') + '/.zabbix-cli/zabbix-cli.conf'] + [self.config_file_from_parameter] + ['/etc/zabbix-cli/zabbix-cli.fixed.conf','/usr/share/zabbix-cli/zabbix-cli.fixed.conf']

# We check if the configuration files defined in
# config_file_priority_list exist before we start reading
# them.

for file in config_file_list:
for file in config_file_priority_list:
if os.path.isfile(file):
self.config_file = file
break
self.config_file_list.append(file)

if self.config_file == '':
if not self.config_file_list:

print '\n[ERROR]: No config file found. Exiting.\n'
sys.exit(1)
Expand All @@ -88,10 +105,10 @@ def set_configuration_file(self):
def set_configuration_parameters(self):
"""Set configuration parameters"""

if self.config_file:
for config_file in self.config_file_list:

config = ConfigParser.RawConfigParser()
config.read(self.config_file)
config.read(config_file)

#
# Zabbix APIsection
Expand Down Expand Up @@ -152,4 +169,3 @@ def set_configuration_parameters(self):
if config.has_option('logging','log_file'):
self.log_file = config.get('logging','log_file')


0 comments on commit a467b22

Please sign in to comment.