1
+ # 设置变量
2
+ variable "region" {
3
+ default = " cn-qingdao"
4
+ }
5
+
6
+ variable "instance_name" {
7
+ default = " deploying-zabbix-server"
8
+ }
9
+
10
+ variable "instance_type" {
11
+ default = " ecs.c6.large"
12
+ }
13
+
14
+ variable "image_id" {
15
+ default = " centos_7_9_x64_20G_alibase_20240628.vhd"
16
+ }
17
+
18
+ variable "internet_bandwidth" {
19
+ default = " 10"
20
+ }
21
+
22
+ variable "instance_password" {
23
+ default = " Test@12345"
24
+ }
25
+
26
+ variable "mysql_zabbix_password" {
27
+ default = " zabbix"
28
+ }
29
+
30
+ provider "alicloud" {
31
+ region = var. region
32
+ }
33
+
34
+ data "alicloud_zones" "default" {
35
+ available_disk_category = " cloud_efficiency"
36
+ available_resource_creation = " VSwitch"
37
+ available_instance_type = var. instance_type
38
+ }
39
+
40
+ resource "alicloud_vpc" "vpc" {
41
+ vpc_name = var. instance_name
42
+ cidr_block = " 172.16.0.0/12"
43
+ }
44
+
45
+ resource "alicloud_vswitch" "vsw" {
46
+ vpc_id = alicloud_vpc. vpc . id
47
+ cidr_block = " 172.16.0.0/21"
48
+ zone_id = data. alicloud_zones . default . zones . 0 . id
49
+ }
50
+
51
+ resource "alicloud_security_group" "default" {
52
+ name = var. instance_name
53
+ vpc_id = alicloud_vpc. vpc . id
54
+ }
55
+
56
+ resource "alicloud_instance" "instance" {
57
+ availability_zone = data. alicloud_zones . default . zones . 0 . id
58
+ security_groups = alicloud_security_group. default . * . id
59
+ password = var. instance_password
60
+ instance_type = var. instance_type
61
+ system_disk_category = " cloud_efficiency"
62
+ image_id = var. image_id
63
+ instance_name = var. instance_name
64
+ vswitch_id = alicloud_vswitch. vsw . id
65
+ internet_max_bandwidth_out = var. internet_bandwidth
66
+ }
67
+
68
+ resource "alicloud_security_group_rule" "allow_tcp_22" {
69
+ type = " ingress"
70
+ ip_protocol = " tcp"
71
+ nic_type = " intranet"
72
+ policy = " accept"
73
+ port_range = " 22/22"
74
+ priority = 1
75
+ security_group_id = alicloud_security_group. default . id
76
+ cidr_ip = " 0.0.0.0/0"
77
+ }
78
+
79
+ resource "alicloud_security_group_rule" "allow_tcp_80" {
80
+ type = " ingress"
81
+ ip_protocol = " tcp"
82
+ nic_type = " intranet"
83
+ policy = " accept"
84
+ port_range = " 80/80"
85
+ priority = 1
86
+ security_group_id = alicloud_security_group. default . id
87
+ cidr_ip = " 0.0.0.0/0"
88
+ }
89
+
90
+ resource "alicloud_security_group_rule" "allow_tcp_10050" {
91
+ type = " ingress"
92
+ ip_protocol = " tcp"
93
+ nic_type = " intranet"
94
+ policy = " accept"
95
+ port_range = " 10050/10050"
96
+ priority = 1
97
+ security_group_id = alicloud_security_group. default . id
98
+ cidr_ip = " 0.0.0.0/0"
99
+ }
100
+
101
+ # deploy zabbix server
102
+ resource "null_resource" "deploy" {
103
+ triggers = {
104
+ always_run = " ${ timestamp ()} "
105
+ script_hash = sha256 (" ${ local . mirrorlist_Adjust } " )
106
+ }
107
+ provisioner "remote-exec" {
108
+ connection {
109
+ type = " ssh"
110
+ user = " root"
111
+ password = var. instance_password
112
+ host = alicloud_instance. instance . public_ip
113
+ }
114
+ inline = [
115
+ # 1、关闭防火墙,修改主机名
116
+ " systemctl disable --now firewalld" ,
117
+ " setenforce 0" ,
118
+ " hostnamectl set-hostname zbx-server" ,
119
+
120
+ # 2、获取 zabbix 的下载源
121
+ " rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm" ,
122
+
123
+ # 3、更换 zabbix.repo 为阿里源,安装zabbix-server-mysql和zabbix-agent
124
+ # "cd /etc/yum.repos.d",
125
+ " sed -i.bak 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo" ,
126
+ # Centos7使用yum命令失效,报错:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误", 通过如下命令添加参数到CentOS-Base.repo中解决。
127
+ " echo \" ${ local . mirrorlist_Adjust } \" >> /etc/yum.repos.d/CentOS-Base.repo" ,
128
+
129
+ " yum clean all && yum makecache" ,
130
+ " yum install -y zabbix-server-mysql zabbix-agent" ,
131
+ # 安装SCL(Software Collections),便于后续安装高版本的 php,默认 yum 安装的 php 版本为 5.4,版本过低,zabbix 5.0 版本对 php 版本最低要 7.2.0 版本。SCL 可以使得在同一台机器上使用多个版本的软件,而又不会影响整个系统的依赖环境。软件包会安装在 /etc/opt/rh 目录下。
132
+ " yum install -y centos-release-scl" ,
133
+
134
+ # 4、修改 zabbix-front 前端源,安装 zabbix 前端环境到 scl 环境下
135
+ " sed -i.bak 's#enabled=0#enabled=1#' /etc/yum.repos.d/zabbix.repo" ,
136
+ # 安装zabbix前端环境到 scl 环境下
137
+ " yum install -y zabbix-web-mysql-scl zabbix-apache-conf-scl" ,
138
+
139
+ # 5、安装 zabbix 所需的数据库
140
+ " yum install -y mariadb-server mariadb" ,
141
+ # 将数据库设置为开机自启,并立即启动
142
+ " systemctl enable --now mariadb" ,
143
+ # 6、初始化数据库,添加数据库用户,以及 zabbix 所需的数据库信息
144
+ " echo \" ${ local . mysqlConfig } \" > /tmp/mysqlConfig.sh" ,
145
+ " source /tmp/mysqlConfig.sh" ,
146
+ # "mysqladmin -u root -p password abc123",
147
+
148
+ # 查询已安装的zabbix-server-mysql的文件列表,找到 sql.gz 文件的位置
149
+ " rpm -ql zabbix-server-mysql" ,
150
+ # 获取zabbix-server-mysql版本号
151
+ " zabbix_server_mysql_version=$(rpm -q zabbix-server-mysql --queryformat " % {VERSION}" )" ,
152
+ # 导入数据库信息,使用zcat将sql.gz文件导入数据库
153
+ " zcat /usr/share/doc/zabbix-server-mysql-$zabbix_server_mysql_version/create.sql.gz | mysql -uroot -pabc123 zabbix" ,
154
+
155
+ # 7、修改 zabbix-server 配置文件,修改数据库的密码
156
+ # 124行,取消注释,指定 zabbix 数据库的密码,DBPassword的值是数据库授权zabbix用户的密码。
157
+ " sed -i.bak 's/# DBPassword=/DBPassword=${ var . mysql_zabbix_password } /' /etc/zabbix/zabbix_server.conf" ,
158
+
159
+ # 8、修改 zabbix 的 php 配置文件,修改时区
160
+ " sed -i.bak 's/; php_value\\ [date.timezone\\ ] = Europe\\ /Riga/php_value\\ [date.timezone\\ ] = Asia\\ /Shanghai/' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf" ,
161
+
162
+ # 9、启动 zabbix 相关服务
163
+ " systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm" ,
164
+ " systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm" ,
165
+ " netstat -natp | grep zabbix" ,
166
+
167
+ # 10、服务端和客户端都配置时间同步,使用阿里云的时钟源
168
+ " yum install -y ntpdate" ,
169
+ " ntpdate -u ntp.aliyun.com" ,
170
+
171
+ # 11、服务端安装zabbix-get,方便日后排查
172
+ " yum install -y zabbix-get" ,
173
+
174
+
175
+ " sleep 2"
176
+ ]
177
+ }
178
+ }
179
+
180
+
181
+ # Centos7使用yum命令失效,报错:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误", 通过如下命令添加参数到CentOS-Base.repo中解决。
182
+ locals {
183
+ mirrorlist_Adjust = << EOF
184
+
185
+ [centos-sclo-rh]
186
+ name=CentOS-7 - SCLo rh
187
+ baseurl=http://vault.centos.org/centos/7/sclo/\$basearch/rh/
188
+ gpgcheck=1
189
+ enabled=1
190
+ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
191
+
192
+ [centos-sclo-sclo]
193
+ name=CentOS-7 - SCLo sclo
194
+ baseurl=http://vault.centos.org/centos/7/sclo/\$basearch/sclo/
195
+ gpgcheck=1
196
+ enabled=1
197
+ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
198
+ EOF
199
+ }
200
+
201
+ # 初始化数据库,添加数据库用户,以及 zabbix 所需的数据库信息
202
+ locals {
203
+ mysqlConfig = << EOF
204
+ #!/bin/bash
205
+ #6、添加数据库用户,以及 zabbix 所需的数据库信息
206
+
207
+ mysqladmin -u root password 'abc123'
208
+ #设置utf8字符集
209
+ mysql -u root -pabc123 -e \"CREATE DATABASE zabbix character set utf8 collate utf8_bin\"
210
+ #创建并授权用户,使得zabbix可以访问数据库
211
+ mysql -u root -pabc123 -e \"GRANT all ON zabbix.* TO 'zabbix'@'%' IDENTIFIED BY 'zabbix'\"
212
+ mysql -u root -pabc123 -e \"GRANT ALL ON zabbix.* TO 'zabbix'@'localhost' IDENTIFIED BY 'zabbix'\"
213
+
214
+ #刷新权限
215
+ mysql -u root -pabc123 -e \"flush privileges\"
216
+ mysql -u root -pabc123 -e \"show databases\"
217
+
218
+ EOF
219
+ }
220
+
221
+
222
+
223
+
224
+ # #############################
225
+ # 新增Zabbix被监控的Agent相关配置
226
+ # #############################
227
+
228
+ variable "agent_instance_name" {
229
+ default = " zabbix-agent-1"
230
+ }
231
+
232
+ resource "alicloud_instance" "agent_instance" {
233
+ availability_zone = data. alicloud_zones . default . zones . 0 . id
234
+ security_groups = alicloud_security_group. default . * . id
235
+ password = var. instance_password
236
+ instance_type = var. instance_type
237
+ system_disk_category = " cloud_efficiency"
238
+ image_id = var. image_id
239
+ instance_name = var. agent_instance_name
240
+ vswitch_id = alicloud_vswitch. vsw . id
241
+ internet_max_bandwidth_out = var. internet_bandwidth
242
+ }
243
+
244
+
245
+ # deploy zabbix agent
246
+ resource "null_resource" "agent_deploy" {
247
+ triggers = {
248
+ always_run = " ${ timestamp ()} "
249
+ # script_hash = sha256("${local.mirrorlist_Adjust}")
250
+ }
251
+ provisioner "remote-exec" {
252
+ connection {
253
+ type = " ssh"
254
+ user = " root"
255
+ password = var. instance_password
256
+ host = alicloud_instance. agent_instance . public_ip
257
+ }
258
+ inline = [
259
+ # 1、关闭防火墙,修改主机名
260
+ " systemctl disable --now firewalld" ,
261
+ " setenforce 0" ,
262
+ " hostnamectl set-hostname zbx-agent01" ,
263
+
264
+ # 2、服务端和客户端都配置时间同步,使用阿里云的时钟源
265
+ " yum install -y ntpdate" ,
266
+ " ntpdate -u ntp.aliyun.com" ,
267
+
268
+ # 3、客户端配置时区,与服务器保持一致
269
+ " mv /etc/localtime{,.bak}" ,
270
+ " ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime" ,
271
+ " date" ,
272
+
273
+ # 4、设置 zabbix 的下载源,安装 zabbix-agent2
274
+ " rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm" ,
275
+ " cd /etc/yum.repos.d" ,
276
+ " sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo" ,
277
+ " yum install -y zabbix-agent2" ,
278
+
279
+ # 5、修改 agent2 配置文件
280
+ " sed -i.bak 's/Server=127.0.0.1/Server=${ alicloud_instance . instance . private_ip } /' /etc/zabbix/zabbix_agent2.conf" ,
281
+ " sed -i.bak 's/ServerActive=127.0.0.1/Server=${ alicloud_instance . instance . private_ip } /' /etc/zabbix/zabbix_agent2.conf" ,
282
+ " sed -i.bak 's/Hostname=Zabbix server/Hostname=zbx-agent01/' /etc/zabbix/zabbix_agent2.conf" ,
283
+
284
+ # 6、启动 zabbix-agent2
285
+ " systemctl start zabbix-agent2" ,
286
+ " systemctl enable zabbix-agent2" ,
287
+ " netstat -natp | grep zabbix" ,
288
+
289
+ " sleep 2"
290
+ ]
291
+ }
292
+ }
293
+
294
+ output "zabbix_Server_URL" {
295
+ value = format (" http://%v/zabbix" , alicloud_instance. instance . public_ip )
296
+ }
297
+
298
+ output "zabbix_Server_Private_IP" {
299
+ value = format (" %v" , alicloud_instance. instance . private_ip )
300
+ }
301
+
302
+ output "zabbix_Agent_Private_IP" {
303
+ value = format (" %v" , alicloud_instance. agent_instance . private_ip )
304
+ }
0 commit comments