-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresynthesize
executable file
·390 lines (336 loc) · 14.1 KB
/
resynthesize
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/bash -e
# Graphite-Grafana installation script for CentOS 8~
# By @deividgdt
# Forked from: Jason Dixon <jason@dixongroup.net> -- thanks Jason;)!
SCRIPT_VERSION='1.2.1'
export SYNTHESIZE_HOME=$( cd "$( dirname "$0" )" && pwd )
export SYNTHESIZE_LOG="${SYNTHESIZE_HOME}/installation.log"
export GRAPHITE_HOME='/opt/graphite'
export GRAPHITE_CONF="${GRAPHITE_HOME}/conf"
export GRAPHITE_STORAGE="${GRAPHITE_HOME}/storage"
export GRAPHITE_RELEASE='1.1.7'
export STATSITE_RELEASE='master'
export CENTOS_RELEASE=$(cat /etc/redhat-release | grep -E "CentOS.*8.*")
# The followings vars are used in order to give some color to
# the different outputs of the script.
COLOR_R="\e[31m"
COLOR_Y="\e[33m"
COLOR_G="\e[32m"
COLOR_CL="\e[0m"
# The followings vars are used to point out the different
# results of the process executed by the script
TAGINFO=$(echo -e "[i]") # [i] Information
TAGWARN=$(echo -e "[${COLOR_Y}w${COLOR_CL}]") # [w] Warning
TAGERR=$(echo -e "[${COLOR_R}✗${COLOR_CL}]") # [✗] Error
TAGOK=$(echo -e "[${COLOR_G}✓${COLOR_CL}]") # [✓] Ok
# The followings vars get the network device (NIC), the IP address and the MAC address
# of the server where script is being executed.
MAINDEV=$(ip route get 8.8.8.8 | head -n1 | awk '{print $5}')
GRAPHIPADD=$(ip -oneline -family inet address show | grep $MAINDEV | awk '{print $4}'| cut -f1 -d"/")
# Just a beautiful banner
function BeautifulBanner() {
echo -e "-----------------------------------------------------"
echo -e "${COLOR_R} __ ___ __ ___ ___ __ __ ___ "
echo -e '|__) |__ /__` \ / |\ | | |__| |__ /__` | / |__ '
echo -e "| \ |___ .__/ | | \| | | | |___ .__/ | /_ |___${COLOR_CL}"
echo -e ""
echo -e "-----------------------------------------------------"
echo -e " ${SCRIPT_VERSION}- -by @deividgdt "
}
# Information
function info() {
case "$1" in
usage)
echo "[i] Usage $0 [ -i || -u || -d ]"
echo ""
echo " -i (install)"
echo " -u (upgrade) - WARNING: Not tested yet"
echo " -d (uninstall)"
echo " -v (version)"
;;
version)
echo "[v] REsynthesize by @deividgdt https://github.com/deividgdt - version: ${SCRIPT_VERSION}"
;;
esac
}
# Check if root is executing the installation/upgrade/removal
function checkUser() {
if [[ "$(id -u $(whoami))" != "0" ]]; then
echo -e "${TAGERR} $(whoami) is not a valid user. The installation must be executed by the user: root."
exit 1;
else
echo -e "${TAGOK} $(whoami) is a valid user."
fi
}
# Disable firewalld and selinux
function secEnforceZero() {
local SELINUXSTATUS="$( grep -v "#" /etc/selinux/config | grep "SELINUX=" )"
if [ $(echo "$SELINUXSTATUS" | cut -f2 -d"=") != "disabled" ]; then
echo -e "${TAGINFO} SELinux is enabled. It will be disabled."
# We set up SELinux in the permissive mode while the actual session is running. SELinux will be disabled in the next restart.
setenforce 0
sed -i "s,$SELINUXSTATUS,SELINUX=disabled,g" /etc/selinux/config && echo -e "${TAGOK} SELinux disabled."
else
echo -e "${TAGINFO} SELinux is disabled."
fi
echo -e "${TAGINFO} Disabling the Firewall"
systemctl disable --now firewalld 1>> ${SYNTHESIZE_LOG} 2>&1
echo -e "${TAGOK} The Firewall has been disabled"
}
# Install package dependencies from yum
function packagesInstallation() {
echo -e "${TAGINFO} Installing some packages..."
dnf --quiet groupinstall -y 'Development Tools' 1>> ${SYNTHESIZE_LOG} 2>&1
dnf --quiet install -y epel-release 1>> ${SYNTHESIZE_LOG} 2>&1
dnf --quiet --enablerepo=powertools install -y git wget curl collectd memcached \
automake libtool libffi-devel cairo cairo-devel libffi \
python3-devel python3-pip fontconfig httpd python3-mod_wsgi \
python3-setuptools openldap-devel collectd-utils collectd-apache \
collectd-disk rrdtool-devel 1>> ${SYNTHESIZE_LOG} 2>&1
# Just some optionals dependencies needed by graphite-web
pip3 install rrdtool python-ldap pyhash 1>> ${SYNTHESIZE_LOG} 2>&1
}
# Download source repositories for Graphite/Carbon/Whisper and Statsite
function gitDownload() {
echo -e "${TAGINFO} Downloading Graphite, Carbon, Whisper and Statsite..."
cd /usr/local/src
git clone https://github.com/graphite-project/graphite-web.git 1>> ${SYNTHESIZE_LOG} 2>&1
git clone https://github.com/graphite-project/carbon.git 1>> ${SYNTHESIZE_LOG} 2>&1
git clone https://github.com/graphite-project/whisper.git 1>> ${SYNTHESIZE_LOG} 2>&1
git clone https://github.com/armon/statsite.git 1>> ${SYNTHESIZE_LOG} 2>&1
}
# Build and install Graphite/Carbon/Whisper/Statsite and Grafana
function buildEnv() {
mkdir -p /opt/graphite
mkdir -p /usr/local/lib/python3.6/site-packages/
# Install whisper
echo -e "${TAGINFO} Building whisper"
cd whisper
git checkout ${GRAPHITE_RELEASE} 1>> ${SYNTHESIZE_LOG} 2>&1
python3 setup.py install 1>> ${SYNTHESIZE_LOG} 2>&1
# Install carbon
echo -e "${TAGINFO} Building carbon"
cd ../carbon
git checkout ${GRAPHITE_RELEASE} 1>> ${SYNTHESIZE_LOG} 2>&1
pip3 install -r requirements.txt 1>> ${SYNTHESIZE_LOG} 2>&1
python3 setup.py install 1>> ${SYNTHESIZE_LOG} 2>&1
# Install graphite-web
echo -e "${TAGINFO} Building graphite-web"
cd ../graphite-web
pip3 install django==2.2.9 1>> ${SYNTHESIZE_LOG} 2>&1
git checkout ${GRAPHITE_RELEASE} 1>> ${SYNTHESIZE_LOG} 2>&1
pip3 install -r requirements.txt 1>> ${SYNTHESIZE_LOG} 2>&1
python3 check-dependencies.py 1>> ${SYNTHESIZE_LOG} 2>&1
python3 setup.py install 1>> ${SYNTHESIZE_LOG} 2>&1
# Install Statsite
echo -e "${TAGINFO} Building statsite"
cd ../statsite
git checkout ${STATSITE_RELEASE} 1>> ${SYNTHESIZE_LOG} 2>&1
./autogen.sh 1>> ${SYNTHESIZE_LOG} 2>&1
./configure 1>> ${SYNTHESIZE_LOG} 2>&1
make 1>> ${SYNTHESIZE_LOG} 2>&1
cp statsite /usr/local/sbin/
cp sinks/graphite.py /usr/local/sbin/statsite-sink-graphite.py
# Install Grafana
echo -e "${TAGINFO} Downloading and installing Grafana"
cd $SYNTHESIZE_HOME && wget https://dl.grafana.com/oss/release/grafana-7.1.3-1.x86_64.rpm 1>> ${SYNTHESIZE_LOG} 2>&1
dnf --quiet install -y grafana-7.1.3-1.x86_64.rpm 1>> ${SYNTHESIZE_LOG} 2>&1
}
function confGraEnv() {
# Install configuration files for Graphite/Carbon and Apache
cp ${SYNTHESIZE_HOME}/templates/statsite/statsite.conf /etc/statsite.conf
mkdir ${GRAPHITE_CONF}/examples
mv ${GRAPHITE_CONF}/*.example ${GRAPHITE_CONF}/examples/
cp ${SYNTHESIZE_HOME}/templates/graphite/conf/* ${GRAPHITE_CONF}/
if [ -f /etc/collectd.conf ]; then mv /etc/collectd.conf /etc/collectd.conf-backup; fi
cp ${SYNTHESIZE_HOME}/templates/collectd/collectd.conf /etc/
cp ${SYNTHESIZE_HOME}/templates/httpd/graphite.conf /etc/httpd/conf.d/
sed -i 's/^\([^#].*\)/# \1/g' /etc/httpd/conf.d/welcome.conf
# using systemd units instead of init and upstart files
cp ${SYNTHESIZE_HOME}/templates/systemd/* /etc/systemd/system/
# installing scripts for carbon-cache instances control script
cp ${SYNTHESIZE_HOME}/templates/scripts/* /usr/local/bin/
# Enabling ssl mod for Apache
dnf install -y mod_ssl 1>> ${SYNTHESIZE_LOG} 2>&1
# Bootstrap Grafana datasources and dashboards
mkdir -p /etc/grafana/provisioning/datasources/
mkdir -p /etc/grafana/provisioning/dashboards/
cp ${SYNTHESIZE_HOME}/templates/grafana/provisioning/datasources/* /etc/grafana/provisioning/datasources/
cp ${SYNTHESIZE_HOME}/templates/grafana/provisioning/dashboards/* /etc/grafana/provisioning/dashboards/
}
# Install configuration files for Django
function confDjango() {
cd ${GRAPHITE_HOME}/webapp/graphite
cp ${SYNTHESIZE_HOME}/templates/graphite/webapp/* .
sed -i -e "s/UNSAFE_DEFAULT/`date | md5sum | cut -d ' ' -f 1`/" local_settings.py
# Setup the Django database
PYTHONPATH=${GRAPHITE_HOME}/webapp django-admin.py migrate --noinput --settings=graphite.settings --run-syncdb 1>> ${SYNTHESIZE_LOG} 2>&1
PYTHONPATH=${GRAPHITE_HOME}/webapp django-admin.py collectstatic --noinput --settings=graphite.settings 1>> ${SYNTHESIZE_LOG} 2>&1
}
# Add carbon system user and set permissions
function confSysEnv() {
echo -e "${TAGINFO} Configuring the system environment"
groupadd -g 1998 carbon
useradd -c "carbon user" -g 1998 -u 1998 -s /bin/false carbon
chmod 775 ${GRAPHITE_STORAGE}
chown apache:carbon ${GRAPHITE_STORAGE}
chown apache: ${GRAPHITE_STORAGE}/graphite.db
chown -R carbon ${GRAPHITE_STORAGE}/whisper
mkdir -p ${GRAPHITE_STORAGE}/log/carbon-cache/
chown -R carbon ${GRAPHITE_STORAGE}/log
chown -R apache ${GRAPHITE_STORAGE}/log/webapp
# Giving execution permissions to Carbon and Graphite binaries
chmod a+x /usr/local/bin/carbon-cache
chmod a+x /usr/local/bin/graphite-build-index
}
# Start and enable services
function startProc() {
echo -e "${TAGINFO} Starting and enabling the services"
systemctl daemon-reload 1>> ${SYNTHESIZE_LOG} 2>&1
# Enable the services
systemctl enable graphite-build-index.timer 1>> ${SYNTHESIZE_LOG} 2>&1
systemctl enable grafana-server 1>> ${SYNTHESIZE_LOG} 2>&1
/usr/local/bin/carbon-cache enable 1>> ${SYNTHESIZE_LOG} 2>&1
systemctl enable statsite 1>> ${SYNTHESIZE_LOG} 2>&1
systemctl enable memcached 1>> ${SYNTHESIZE_LOG} 2>&1
systemctl enable collectd 1>> ${SYNTHESIZE_LOG} 2>&1
systemctl enable httpd 1>> ${SYNTHESIZE_LOG} 2>&1
# Start the services
systemctl start graphite-build-index 1>> ${SYNTHESIZE_LOG} 2>&1
systemctl restart grafana-server 1>> ${SYNTHESIZE_LOG} 2>&1
/usr/local/bin/carbon-cache start 1>> ${SYNTHESIZE_LOG} 2>&1
systemctl restart statsite 1>> ${SYNTHESIZE_LOG} 2>&1
systemctl restart memcached 1>> ${SYNTHESIZE_LOG} 2>&1
systemctl restart collectd 1>> ${SYNTHESIZE_LOG} 2>&1
systemctl restart httpd 1>> ${SYNTHESIZE_LOG} 2>&1
}
# Function to install the services
function install() {
if [ ! "$CENTOS_RELEASE" ]; then
echo "Sorry, this is only supported for CentOS 8.*"
exit 1
fi
if [[ -d $GRAPHITE_HOME ]]; then
echo "Looks like you already have a Graphite installation in ${GRAPHITE_HOME}, aborting."
exit 1
fi
secEnforceZero
packagesInstallation
gitDownload
buildEnv
confGraEnv
confDjango
confSysEnv
startProc
echo -e "${TAGOK} Installation finished."
echo -e "${TAGINFO} For Graphite go to: https://${GRAPHIPADD}"
echo -e "${TAGINFO} For Grafana go to: http://${GRAPHIPADD}:3000"
echo -e "${TAGWARN} User and Password for Grafana: user:admin password: admin"
}
# Function to uninstall the services
function uninstall() {
/usr/local/bin/carbon-cache stop
/usr/local/bin/carbon-cache disable
systemctl disable --now memcached
systemctl disable --now collectd
systemctl disable --now httpd
systemctl disable --now statsite
systemctl disable --now grafana-server
# Remove various binaries, configuration files, and source directories
rm /usr/local/sbin/statsite*
rm /etc/statsite.conf
pip3 uninstall -y -r /usr/local/src/graphite-web/requirements.txt
pip3 uninstall -y -r /usr/local/src/carbon/requirements.txt
rm -r /opt/graphite
rm -r /usr/local/src/graphite-web
rm -r /usr/local/src/carbon
rm -r /usr/local/src/whisper
rm -r /usr/local/src/statsite
rm /usr/local/bin/*whisper*.py
rm /usr/local/lib/python2.7/dist-packages/whisper*
rm /etc/httpd/sites-*/graphite.conf
rm -rf /etc/systemd/system/carbon*
rm -rf /etc/systemd/system/statsite*
systemctl daemon-reload
rm /etc/cron.hourly/graphite-build-index
# Uninstall our package dependencies and purge configurations
dnf remove git collectd memcached \
automake libtool libffi-devel cairo cairo-devel libffi \
python3-devel python3-pip fontconfig httpd python3-mod_wsgi \
python3-setuptools openldap-devel collectd-utils collectd-apache \
collectd-disk rrdtool-devel
# Brute force cleanup on the collectd configuration directory
rm -r /etc/collectd
# Remove our system user for Carbon
pkill -9 -f carbon-cache
userdel carbon
rm /usr/local/bin/carbon-cache
}
# Function to upgrade the services
function upgrade() {
if [[ ! $DANGER_ZONE =~ 'TRUE' ]]; then
echo "This upgrade script has not been thoroughly tested. There is a non-zero"
echo "chance that running this script will break your Synthesize-Graphite"
echo "installation. If you accept this warning and wish to continue anyways,"
echo "run this script again with the DANGER_ZONE environment variable set to TRUE."
echo
exit 1
fi
# Update, Build and install Graphite/Carbon/Whisper and Statsite
cd /usr/local/src
cd whisper; git fetch origin; \
git checkout ${GRAPHITE_RELEASE}; \
python3 setup.py install
cd ../carbon; git fetch origin; \
git checkout ${GRAPHITE_RELEASE}; \
pip3 install -r requirements.txt; \
python3 setup.py install
cd ../graphite-web; git fetch origin; \
git checkout ${GRAPHITE_RELEASE}; \
pip3 install -r requirements.txt; \
python3 check-dependencies.py; \
python3 setup.py install
# Install configuration files for Graphite/Carbon and Apache
cp -b ${GRAPHITE_CONF}/*.example ${GRAPHITE_CONF}/examples/
cp -b ${SYNTHESIZE_HOME}/templates/graphite/conf/* ${GRAPHITE_CONF}/
cp -b ${SYNTHESIZE_HOME}/templates/collectd/collectd.conf /etc/collectd/
cp -b ${SYNTHESIZE_HOME}/templates/apache/graphite.conf /etc/apache2/sites-available/
# Install configuration files for Django
cd ${GRAPHITE_HOME}/webapp/graphite
cp -b ${SYNTHESIZE_HOME}/templates/graphite/webapp/* .
sed -i -e "s/UNSAFE_DEFAULT/`date | md5sum | cut -d ' ' -f 1`/" local_settings.py
# Upgrade Django and migrate the webapp database
service httpd stop
BACKUP=`date +%Y%m%d_%H%M%S`
cp -fv ${GRAPHITE_STORAGE}/graphite.db ${GRAPHITE_STORAGE}/graphite.db.backup-${BACKUP}
PYTHONPATH=${GRAPHITE_HOME}/webapp django-admin.py dumpdata --indent 4 --settings=graphite.settings > dump_${BACKUP}_data.json
PYTHONPATH=${GRAPHITE_HOME}/webapp django-admin.py makemigrations --noinput --settings=graphite.settings
PYTHONPATH=${GRAPHITE_HOME}/webapp django-admin.py migrate --noinput --settings=graphite.settings --run-syncdb
PYTHONPATH=${GRAPHITE_HOME}/webapp django-admin.py loaddata --settings=graphite.settings dump_${BACKUP}_data.json
# Restart our processes
/usr/local/bin/carbon-cache restart
service memcached restart
service httpd restart
}
BeautifulBanner
checkUser
if [ $# -eq 0 ]; then
info "usage"
exit 1;
fi
while getopts "iudv" opt; do
case "$opt" in
i )
install ;;
u )
upgrade ;;
d )
uninstall ;;
v )
info "version"
exit 0 ;;
\?)
echo "[e] Invalid option. -$OPTARG"
exit 1 ;;
esac
done