0%

使用脚本一键安装zabbix和grafana

此脚本为zabbix4.4和grafana6.7.4的一键部署脚本,脚本运行完成即可通过web页面进行管理配置,只适用于RHEL7.X版本,可用于最小化安装的版本,脚本中使用了case语句做了菜单,可自行选择安装zabbix或grafana,脚本中yum源使用的是清华源,可自行更改为阿里源或者其它yum源,脚本中已将zabbix和grafana所需要的端口放通,未关闭防火墙的情况下也可以正常访问。

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
#!/bin/bash
#此脚本用于RHCL7安装Zabbix4.4和Grafana6.7.4

cat << EOF
********请选择需要安装的组件:********
(1) Install Zabbix 4.4
(2) Install Grafana 6.7.4
EOF

read -p "请选择需要安装的组件:" digit

case $digit in
"1" )
echo "Install Zabbix 4.4"

#关闭SELinux
if [ $(getenforce) = "Enforcing" ]; then
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
echo "Selinux已关闭!"
fi

#安装zabbix的repo源
if [ $(curl -sL -w "%{http_code}" "https://mirror.tuna.tsinghua.edu.cn/" -o /dev/null) -eq 200 ];then
echo "地址通畅,继续安装!!!"
rpm -ivh https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm
sed -i -e 's/repo.zabbix.com/mirror.tuna.tsinghua.edu.cn\/zabbix/g' /etc/yum.repos.d/zabbix.repo
yum clean all
else
echo "地址不通畅,请检查网络!!!"
exit
fi

#安装zabbix和mariadb
yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent mariadb mariadb-server
systemctl start mariadb.service
systemctl enable mariadb.service
mysql -e "create database zabbix character set utf8 collate utf8_bin;"
mysql -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';"
mysql -e "flush privileges;"
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -pzabbix zabbix

#修改zabbix-server配置文件
cp /etc/zabbix/zabbix_server.conf /etc/zabbix/zabbix_server.conf.bak
sed -i 's/# DBHost=localhost/DBHost=localhost/' /etc/zabbix/zabbix_server.conf
sed -i 's/# DBPassword=/DBPassword=zabbix/' /etc/zabbix/zabbix_server.conf
sed -i 's/# StartPollers=5/StartPollers=40/' /etc/zabbix/zabbix_server.conf
sed -i 's/# StartPreprocessors=3/StartPreprocessors=20/' /etc/zabbix/zabbix_server.conf
sed -i 's/# StartPollersUnreachable=1/StartPollersUnreachable=10/' /etc/zabbix/zabbix_server.conf
sed -i 's/# StartPingers=1/StartPingers=15/' /etc/zabbix/zabbix_server.conf
sed -i 's/# StartDiscoverers=1/StartDiscoverers=5/' /etc/zabbix/zabbix_server.conf
sed -i 's/# HousekeepingFrequency=1/HousekeepingFrequency=24/' /etc/zabbix/zabbix_server.conf
sed -i 's/# MaxHousekeeperDelete=5000/MaxHousekeeperDelete=10000/' /etc/zabbix/zabbix_server.conf
sed -i 's/# CacheSize=8M/CacheSize=4G/' /etc/zabbix/zabbix_server.conf
sed -i 's/# ValueCacheSize=8M/ValueCacheSize=2G/' /etc/zabbix/zabbix_server.conf

#修改zabbix-web配置文件
cp /etc/httpd/conf.d/zabbix.conf /etc/httpd/conf.d/zabbix.conf.bak
sed -i 's/memory_limit 128M/memory_limit 512M/' /etc/httpd/conf.d/zabbix.conf
sed -i 's/# php_value date.timezone Europe\/Riga/php_value date.timezone Asia\/Shanghai/' /etc/httpd/conf.d/zabbix.conf
systemctl restart zabbix-server zabbix-agent httpd
systemctl enable zabbix-server zabbix-agent httpd

#开放端口
firewall-cmd --zone=public --add-port=10050/tcp --permanent
firewall-cmd --zone=public --add-port=10051/tcp --permanent
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

#判断zabbix网页是否正常打开
if [ $(curl -sL -w "%{http_code}" "http://127.0.0.1/zabbix" -o /dev/null) -eq 200 ]; then
echo "Zabbix安装成功!!!"
else
echo "Zabbix异常,请检查!!!"
fi
;;
"2" )
echo "Install Grafana 6.7.4"

#判断wget是否安装
command -v wget
if [ $? -ne 0 ]; then
yum -y install wget
echo "wget安装成功!!!"
else
echo "wget已存在!!!"
fi

#判断地址是否通畅,通畅则下载rpm包
if [ $(curl -sL -w "%{http_code}" "https://mirror.tuna.tsinghua.edu.cn/" -o /dev/null) -eq 200 ];then
echo "地址通畅,继续安装!!!"
wget https://mirror.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-6.7.4-1.x86_64.rpm
else
echo "地址不通畅,请检查网络!!!"
exit
fi

#安装Grafana
yum -y install fontconfig freetype* urw-fonts
rpm -ivh /root/grafana-6.7.4-1.x86_64.rpm
grafana-cli plugins install alexanderzobnin-zabbix-app
systemctl enable grafana-server
systemctl start grafana-server

#开放端口
firewall-cmd --zone=public --add-port=3000/tcp --permanent
firewall-cmd --reload

#判断grafana网页是否正常打开
if [ $(curl -sL -w "%{http_code}" "http://127.0.0.1:3000" -o /dev/null) -eq 200 ]; then
echo "Grafana安装成功!!!"
else
echo "Grafana异常,请检查!!!"
fi
;;
"*" )
echo "Error"
;;
esac