-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df032dc
commit b17795b
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Authors: | ||
# Mustafa Ocak | ||
# muo@uio.no | ||
# | ||
# Copyright (c) 2015 USIT-University of Oslo | ||
########################################################################################### | ||
# This script initialize zabbix-cli environment. it will copy /etc/zabbix-cli.conf to | ||
# $HOME/.zabbix-cli/ | ||
# and change log configuration in zabbix-cli.conf so that zabbix-cli logs to | ||
# $HOME/.zabbix-cli/ | ||
########################################################################################### | ||
|
||
from os import getenv,path,remove,close,makedirs | ||
from tempfile import mkstemp | ||
from shutil import move, copy2 | ||
from sys import exit | ||
|
||
def replace(file_path, pattern, subst): | ||
|
||
# | ||
# changing the line having log configuration option | ||
# replacing | ||
# log_file=/var/log/zabbix-cli/zabbix-cli.log | ||
# to log_file=~/.zabbix-cli/zabbix-cli.log | ||
|
||
try: | ||
|
||
#Create temp file | ||
fh, abs_path = mkstemp() | ||
with open(abs_path,'w') as new_file: | ||
with open(file_path) as old_file: | ||
for line in old_file: | ||
new_file.write(line.replace(pattern, subst)) | ||
close(fh) | ||
#Remove original file | ||
remove(file_path) | ||
#Move new file | ||
move(abs_path, file_path) | ||
except Exception as e: | ||
print '\n[ERROR]: %s\n',e | ||
|
||
if __name__ == "__main__": | ||
|
||
pattern="log_file=/var/log/zabbix-cli/zabbix-cli.log" | ||
subst="log_file="+getenv('HOME')+"/.zabbix-cli/zabbix-cli.log" | ||
|
||
zabbixconfdir = getenv('HOME')+"/.zabbix-cli/" | ||
defconf="/etc/zabbix-cli.conf" | ||
filename="zabbix-cli.conf" | ||
|
||
file_path=path.join(zabbixconfdir,filename) | ||
|
||
# | ||
# creating ~/.zabbix-cli folder if not exists | ||
# | ||
|
||
if not path.exists(zabbixconfdir): | ||
|
||
try: | ||
makedirs(zabbixconfdir) | ||
except Exception as e: | ||
print '\n[ERROR]: %s\n',e | ||
|
||
# | ||
# /etc/zabbix-cli.conf will be created under installation of zabbix-cli package. | ||
# copying /etc/zabbix-cli.conf file to ~/.zabbix-cli/zabbix-cli.conf | ||
# | ||
|
||
if path.isfile(defconf): | ||
try: | ||
copy2(defconf,zabbixconfdir) | ||
|
||
except Exception as e: | ||
print '\n[ERROR]: %s\n',e | ||
else: | ||
print "Cannot find /etc/zabbix-cli.conf file. ERROR with zabbix-cli installation." | ||
exit(1) | ||
|
||
# | ||
# changing the line having log configuration option | ||
# replacing log file option | ||
# log_file=/var/log/zabbix-cli/zabbix-cli.log | ||
# to log_file=~/.zabbix-cli/zabbix-cli.log | ||
# | ||
replace(file_path,pattern,subst) | ||
|