-
Notifications
You must be signed in to change notification settings - Fork 0
/
installzabbix.py
137 lines (120 loc) · 5.29 KB
/
installzabbix.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
#!/usr/bin/python
#zabbix-agent installer
import sys
import os
import socket
import re
import subprocess
# Getting Host Name
def get_host_name():
hostname=""
print("1. Machine's Hostname\n2. Enter Hostname Manually\n")
select_host_name = input("Select the hostname (1, 2): ")
if select_host_name == "1":
hostname = socket.gethostname()
elif select_host_name == "2":
hostname = input("Enter the hostname, please: ")
return hostname
# Check Hostname Validation
def valid_host_name(hostname):
if hostname != "" and hostname != None and hostname != "localhost":
return True
else:
return False
return hostname
# Remove Existing Zabbix Agent
def remove_zabbix_agent():
try:
os.system('sudo yum remove zabbix-release')
os.system('sudo yum remove zabbix-agent')
run = subprocess.getstatusoutput('sudo ps -ef | grep zabbix_agentd | grep -v grep')
file_ = os.path.exists('/etc/zabbix')
if run:
os.system('sudo service zabbix-agent stop')
print("Zabbix agent service stopped")
if file_:
os.system('sudo rm -rf /etc/zabbix')
print("Zabbix removed")
except Exception as e:
print(e)
# Asking Zabbix Server IP Address
def zabbix_server_IP():
server_IP = input("Enter the zabbix server IP, please (exm: 192.168.X.X ): ")
return server_IP
# Check IP validation
def valid_IP(server_IP):
IP_add = re.match(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', server_IP)
if IP_add:
return True
else:
return False
# Install Zabbix Agent
def install_zabbix_agent():
try:
installed = False
remove_zabbix_agent()
print("-----------------Disabling SELINUX------------------")
os.system("sudo setenforce 0")
print("-----------------Installation Started, Updating----------------\n")
os.system("sudo yum update")
print("----------------Installing repo----------------\n")
version = input("Enter the version of zabbix to install(3, 4, 5): ")
centos_version = input("Enter the version of CentOs(6, 7): ")
try:
repo = 'https://repo.zabbix.com/zabbix/%s.0/rhel/%s/x86_64/zabbix-release-%s.0-1.el%s.noarch.rpm'%(version, centos_version, version, centos_version)
os.system('sudo yum install %s'%(repo))
except Exception as e:
print(e)
system.exit()
print("--------------------Getting zabbix repo-------------------\n")
get_repo = subprocess.getstatusoutput('sudo yum list installed | grep zabbix-release')
if get_repo:
print("---------------------Repo downloaded---------------------\n")
os.system('sudo yum install zabbix-agent')
os.system('systemctl start zabbix-agent')
print("---------------------Server/Server Active Configuration--------------------\n")
while True:
server_IP = zabbix_server_IP()
if valid_IP(server_IP):
print("---------------------Server IP Confirmed---------------------\n")
os.system("sudo sed -i 's/^Server=.*/Server="+server_IP+"/g' /etc/zabbix/zabbix_agentd.conf")
os.system("sudo sed -i 's/^ServerActive=.*/ServerActive="+server_IP+"/g' /etc/zabbix/zabbix_agentd.conf")
print("Zabbix server set at: "+server_IP)
break
else:
print("Server IP was not confirmed, try again...\n")
continue
print('--------------------Hostname Configuration-------------------\n')
while True:
user_host_name = get_host_name()
if valid_host_name(user_host_name):
os.system("sudo sed -i 's/Hostname=.*/Hostname="+user_host_name+"/g' /etc/zabbix/zabbix_agentd.conf")
print("Hostname Set: "+user_host_name)
break
else:
print("Hostname was not confirmed, try again...\n")
continue
print('-------------------Firewall Configuration------------------\n')
os.system('sudo firewall-cmd --add-port=10050/tcp --permanent')
os.system('sudo firewall-cmd --reload')
print('--------------------Starting zabbix, Checking status-------------------\n')
os.system('systemctl restart zabbix-agent')
status = subprocess.getstatusoutput('chkconfig zabbix-agent on')
run = subprocess.getstatusoutput('sudo ps -ef | grep zabbix_agentd | grep -v grep')
if run:
print("--------------------Installation completed successfully!------------------\n")
installed = True
os.system('service zabbix-agent status')
else:
print(status)
else:
print("------------------Could not find zabbix-agent and install zabbix------------------")
except Exception as e:
print(e)
finally:
return installed
# Run Script
if install_zabbix_agent():
print('------------------Zabbix agent was installed successfully!-----------------')
else:
print('\n-------------------There was an error while installing zabbix------------------')