-
Notifications
You must be signed in to change notification settings - Fork 50
/
CONTAINER_UPDATE.sh
405 lines (345 loc) · 16.3 KB
/
CONTAINER_UPDATE.sh
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/bin/bash
if [[ $1 == "--help" ]]; then
echo "Supported parameters:"
echo "--rights Reset file permissions"
echo "--cc Clear model cache"
exit 0
fi
# Enable debug mode so that CakePHP will create missing folders
# https://github.com/it-novum/openITCOCKPIT/issues/1446
# https://github.com/cakephp/migrations/issues/565
export OITC_DEBUG=1
APPDIR="/opt/openitc/frontend"
INIFILE=/opt/openitc/etc/mysql/mysql.cnf
DUMPINIFILE=/opt/openitc/etc/mysql/dump.cnf
BASHCONF=/opt/openitc/etc/mysql/bash.conf
OSVERSION=$(grep VERSION_CODENAME /etc/os-release | cut -d= -f2)
OS_BASE="debian"
if [[ ! -f "$BASHCONF" ]]; then
MYSQL_USER=openitcockpit
MYSQL_DATABASE=openitcockpit
MYSQL_PASSWORD=
MYSQL_HOST=localhost
MYSQL_PORT=3306
eval $(php -r "require '$APPDIR/src/itnovum/openITCOCKPIT/Database/MysqlConfigFileParserForCli.php'; \$mcp = new MysqlConfigFileParserForCli(); \$r = \$mcp->parse_mysql_cnf('/opt/openitc/etc/mysql/mysql.cnf'); echo \$r['shell'];")
echo "dbc_dbuser='${MYSQL_USER}'" >$BASHCONF
echo "dbc_dbpass='${MYSQL_PASSWORD}'" >>$BASHCONF
echo "dbc_dbserver='${MYSQL_HOST}'" >>$BASHCONF
echo "dbc_dbport='${MYSQL_PORT}'" >>$BASHCONF
echo "dbc_dbname='${MYSQL_DATABASE}'" >>$BASHCONF
fi
. /opt/openitc/etc/mysql/bash.conf
echo "Copy required system files"
rsync -K -a ${APPDIR}/system/etc/. /etc/ # we use rsync because the destination can be a symlink on RHEL
chown root:root /etc
cp -r ${APPDIR}/system/usr/. /usr/
cp ${APPDIR}/system/nginx/ssl_options_$OSVERSION /etc/nginx/openitc/ssl_options.conf
cp ${APPDIR}/system/nginx/ssl_cert.conf /etc/nginx/openitc/ssl_cert.conf
chmod +x /usr/bin/oitc
echo "Create mysqldump of your current database"
BACKUP_TIMESTAMP=$(date '+%Y-%m-%d_%H-%M-%S')
BACKUP_DIR='/opt/openitc/nagios/backup'
mkdir -p $BACKUP_DIR
#If you have mysql binlog enabled uses this command:
#mysqldump --defaults-extra-file=${DUMPINIFILE} --databases $dbc_dbname --flush-privileges --single-transaction --master-data=1 --flush-logs --triggers --routines --events --hex-blob \
# ITC-2921
# MySQL Bug: https://bugs.mysql.com/bug.php?id=109685
# As with mysqldump 8.0.32 --single-transaction requires RELOAD or FLUSH_TABLES privilege(s) which the openitcockpit user does not have
# So for now the workaround is to remove "--single-transaction"
mysqldump --defaults-extra-file=${DUMPINIFILE} --databases $dbc_dbname --flush-privileges --triggers --routines --no-tablespaces --events --hex-blob \
--ignore-table=$dbc_dbname.nagios_acknowledgements \
--ignore-table=$dbc_dbname.nagios_commands \
--ignore-table=$dbc_dbname.nagios_commenthistory \
--ignore-table=$dbc_dbname.nagios_comments \
--ignore-table=$dbc_dbname.nagios_configfiles \
--ignore-table=$dbc_dbname.nagios_configfilevariables \
--ignore-table=$dbc_dbname.nagios_conninfo \
--ignore-table=$dbc_dbname.nagios_contact_addresses \
--ignore-table=$dbc_dbname.nagios_contact_notificationcommands \
--ignore-table=$dbc_dbname.nagios_contactgroup_members \
--ignore-table=$dbc_dbname.nagios_contactgroups \
--ignore-table=$dbc_dbname.nagios_contactnotificationmethods \
--ignore-table=$dbc_dbname.nagios_contactnotifications \
--ignore-table=$dbc_dbname.nagios_contacts \
--ignore-table=$dbc_dbname.nagios_contactstatus \
--ignore-table=$dbc_dbname.nagios_customvariables \
--ignore-table=$dbc_dbname.nagios_customvariablestatus \
--ignore-table=$dbc_dbname.nagios_dbversion \
--ignore-table=$dbc_dbname.nagios_downtimehistory \
--ignore-table=$dbc_dbname.nagios_eventhandlers \
--ignore-table=$dbc_dbname.nagios_externalcommands \
--ignore-table=$dbc_dbname.nagios_flappinghistory \
--ignore-table=$dbc_dbname.nagios_host_contactgroups \
--ignore-table=$dbc_dbname.nagios_host_contacts \
--ignore-table=$dbc_dbname.nagios_host_parenthosts \
--ignore-table=$dbc_dbname.nagios_hostchecks \
--ignore-table=$dbc_dbname.nagios_hostdependencies \
--ignore-table=$dbc_dbname.nagios_hostescalation_contactgroups \
--ignore-table=$dbc_dbname.nagios_hostescalation_contacts \
--ignore-table=$dbc_dbname.nagios_hostescalations \
--ignore-table=$dbc_dbname.nagios_hostgroup_members \
--ignore-table=$dbc_dbname.nagios_hostgroups \
--ignore-table=$dbc_dbname.nagios_hosts \
--ignore-table=$dbc_dbname.nagios_hoststatus \
--ignore-table=$dbc_dbname.nagios_instances \
--ignore-table=$dbc_dbname.nagios_logentries \
--ignore-table=$dbc_dbname.nagios_notifications \
--ignore-table=$dbc_dbname.nagios_processevents \
--ignore-table=$dbc_dbname.nagios_programstatus \
--ignore-table=$dbc_dbname.nagios_runtimevariables \
--ignore-table=$dbc_dbname.nagios_scheduleddowntime \
--ignore-table=$dbc_dbname.nagios_service_contactgroups \
--ignore-table=$dbc_dbname.nagios_service_contacts \
--ignore-table=$dbc_dbname.nagios_service_parentservices \
--ignore-table=$dbc_dbname.nagios_servicechecks \
--ignore-table=$dbc_dbname.nagios_servicedependencies \
--ignore-table=$dbc_dbname.nagios_serviceescalation_contactgroups \
--ignore-table=$dbc_dbname.nagios_serviceescalation_contacts \
--ignore-table=$dbc_dbname.nagios_serviceescalations \
--ignore-table=$dbc_dbname.nagios_servicegroup_members \
--ignore-table=$dbc_dbname.nagios_servicegroups \
--ignore-table=$dbc_dbname.nagios_services \
--ignore-table=$dbc_dbname.nagios_servicestatus \
--ignore-table=$dbc_dbname.nagios_statehistory \
--ignore-table=$dbc_dbname.nagios_systemcommands \
--ignore-table=$dbc_dbname.nagios_timedeventqueue \
--ignore-table=$dbc_dbname.nagios_timedevents \
--ignore-table=$dbc_dbname.nagios_timeperiod_timeranges \
--ignore-table=$dbc_dbname.nagios_timeperiods \
--ignore-table=$dbc_dbname.statusengine_dbversion \
--ignore-table=$dbc_dbname.statusengine_host_acknowledgements \
--ignore-table=$dbc_dbname.statusengine_host_downtimehistory \
--ignore-table=$dbc_dbname.statusengine_host_notifications \
--ignore-table=$dbc_dbname.statusengine_host_scheduleddowntimes \
--ignore-table=$dbc_dbname.statusengine_host_statehistory \
--ignore-table=$dbc_dbname.statusengine_hostchecks \
--ignore-table=$dbc_dbname.statusengine_hoststatus \
--ignore-table=$dbc_dbname.statusengine_logentries \
--ignore-table=$dbc_dbname.statusengine_nodes \
--ignore-table=$dbc_dbname.statusengine_perfdata \
--ignore-table=$dbc_dbname.statusengine_service_acknowledgements \
--ignore-table=$dbc_dbname.statusengine_service_downtimehistory \
--ignore-table=$dbc_dbname.statusengine_service_notifications \
--ignore-table=$dbc_dbname.statusengine_service_scheduleddowntimes \
--ignore-table=$dbc_dbname.statusengine_service_statehistory \
--ignore-table=$dbc_dbname.statusengine_servicechecks \
--ignore-table=$dbc_dbname.statusengine_servicestatus \
--ignore-table=$dbc_dbname.statusengine_tasks \
--ignore-table=$dbc_dbname.statusengine_users \
--ignore-table=$dbc_dbname.customalerts \
--ignore-table=$dbc_dbname.customalert_statehistory \
--ignore-table=$dbc_dbname.sla_availability_status_hosts_log \
--ignore-table=$dbc_dbname.sla_availability_status_services_log \
--ignore-table=$dbc_dbname.sla_host_outages \
--ignore-table=$dbc_dbname.sla_service_outages \
>$BACKUP_DIR/openitcockpit_dump_$BACKUP_TIMESTAMP.sql
echo "---------------------------------------------------------------"
echo "Convert MySQL Tables from utf8_general_ci to utf8mb4_general_ci..."
# Disabled - this takes ages!
#mysql --defaults-extra-file=${INIFILE} -e "ALTER DATABASE ${dbc_dbname} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"
mysql --defaults-extra-file=${INIFILE} --batch --skip-column-names -e "SELECT TABLE_NAME FROM \`information_schema\`.\`TABLES\` WHERE \`TABLE_SCHEMA\`='${dbc_dbname}' AND \`TABLE_NAME\` NOT LIKE 'nagios_%' AND \`TABLE_COLLATION\`='utf8_general_ci'" | while read TABLE_NAME; do
echo "ALTER TABLE \`${TABLE_NAME}\` CONVERT TO CHARACTER SET utf8mb4; ✔"
mysql --defaults-extra-file=${INIFILE} -e "ALTER TABLE \`${TABLE_NAME}\` CONVERT TO CHARACTER SET utf8mb4;"
done
echo "Running openITCOCKPIT Core database migration"
oitc migrations migrate
oitc migrations seed
echo "Running openITCOCKPIT Module database migration/s"
for PLUGIN in $(ls -1 "${APPDIR}/plugins"); do
if [[ "$PLUGIN" == *Module ]]; then
if [[ -d "${APPDIR}/plugins/${PLUGIN}/config/Migrations" ]]; then
echo "Running openITCOCKPIT ${PLUGIN} database migration"
oitc migrations migrate -p "${PLUGIN}"
fi
if [[ -d "${APPDIR}/plugins/${PLUGIN}/config/Seeds" ]]; then
num_files=$(find "${APPDIR}/plugins/${PLUGIN}/config/Seeds" -mindepth 1 -iname "*.php" -type f | wc -l)
if [[ "$num_files" -gt 0 ]]; then
echo "Importing default records for ${PLUGIN} into database"
oitc migrations seed -p "${PLUGIN}"
fi
fi
fi
done
echo "---------------------------------------------------------------"
echo "Import extended openitcockpit-update module scripts ..."
MODULE_SCRIPS_LOCK_DIR="/opt/openitc/var/updatesh_module_locks/"
MODULES_DIR="${APPDIR}/plugins/"
DEDICATED_MODULE_SCRIPTS_DIR_NAME="updateScripts"
LOADED_MODULE_SCRIPTS=()
# include module scripts class header
echo "Import ${APPDIR}/bin/updatesh_module_scripts/system.h.sh"
. ${APPDIR}/bin/updatesh_module_scripts/system.h.sh ${APPDIR}/bin/updatesh_module_scripts/
moduleList=($(ls -A1 $MODULES_DIR*/$DEDICATED_MODULE_SCRIPTS_DIR_NAME/*))
for entry in "${moduleList[@]}"
do
if [[ $entry =~ \.h.sh$ ]]; then
echo "Import $entry"
shortName=`basename $entry`
realName=${shortName/".h.sh"/""}
path=${entry/$shortName/""}
LOADED_MODULE_SCRIPTS+=($realName)
. $entry $path
fi
done
if type "system.property" &> /dev/null; then
system.property lockDir = $MODULE_SCRIPS_LOCK_DIR
system.property modulesDir = $MODULES_DIR
system.initialize
fi
for Module in "${LOADED_MODULE_SCRIPTS[@]}"; do
if [ "$Module" != "system" ]; then
if type "${Module}" &> /dev/null; then
# create class object
${Module} module
# set required variables
module.property name = "${Module}"
module.property dbIniFile = "${Module}"
module.initialize
fi
fi
done
echo "---------------------------------------------------------------"
echo "Convert MySQL Tables from utf8_general_ci to utf8mb4_general_ci..."
# Disabled - this takes ages!
#mysql --defaults-extra-file=${INIFILE} -e "ALTER DATABASE ${dbc_dbname} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"
mysql --defaults-extra-file=${INIFILE} --batch --skip-column-names -e "SELECT TABLE_NAME FROM \`information_schema\`.\`TABLES\` WHERE \`TABLE_SCHEMA\`='${dbc_dbname}' AND \`TABLE_NAME\` NOT LIKE 'nagios_%' AND \`TABLE_COLLATION\`='utf8_general_ci'" | while read TABLE_NAME; do
echo "ALTER TABLE \`${TABLE_NAME}\` CONVERT TO CHARACTER SET utf8mb4; ✔"
mysql --defaults-extra-file=${INIFILE} -e "ALTER TABLE \`${TABLE_NAME}\` CONVERT TO CHARACTER SET utf8mb4;"
done
#Compress and minify javascript files
oitc compress
#Acc ALC dependencies config for itc core
echo "---------------------------------------------------------------"
echo "Scan for new user permissions. This will take a while..."
oitc Acl.acl_extras aco_sync
#Set default permissions, check for always allowed permissions and dependencies
oitc roles --enable-defaults --admin
echo "Checking that a server certificate for the openITCOCKPIT Monitoring Agent exists"
oitc agent --generate-server-ca
# ITC-1911
echo "Cleanup for invalid parent hosts on satellite instance"
oitc ParentHostsVisibilityCleaning
for i in "$@"; do
case $i in
--cc)
echo "Clear out Model Cache /opt/openitc/frontend/tmp/cache/models/"
rm -rf /opt/openitc/frontend/tmp/cache/models/*
echo "Clear out CLI Model Cache /opt/openitc/frontend/tmp/cli/cache/cli/models/"
rm -rf /opt/openitc/frontend/tmp/cli/cache/cli/models/*
;;
--rights)
oitc rights
;;
*)
#No default at the moment
;;
esac
done
echo "Flush redis cache"
redis-cli -h $OITC_REDIS_HOST -p $OITC_REDIS_PORT FLUSHALL
echo ""
echo "Copy required system files"
rsync -K -a ${APPDIR}/system/etc/. /etc/
chown root:root /etc
cp -r ${APPDIR}/system/usr/. /usr/
cp ${APPDIR}/system/nginx/ssl_options_$OSVERSION /etc/nginx/openitc/ssl_options.conf
# only ensure that the files exist
touch /etc/nginx/openitc/ssl_cert.conf
touch /etc/nginx/openitc/custom.conf
chmod +x /usr/bin/oitc
echo "Create required system folders"
mkdir -p /opt/openitc/etc/{mysql,grafana,carbon,frontend,nagios,nsta}
mkdir -p /opt/openitc/nagios/etc/config
mkdir -p /opt/openitc/etc/nagios/nagios.cfg.d
chown www-data:www-data /opt/openitc/logs/frontend
chown nagios:www-data /opt/openitc/nagios/etc/config
chown nagios:www-data /opt/openitc/etc/nagios/nagios.cfg.d
chmod 775 /opt/openitc/logs/frontend
chown www-data:www-data /opt/openitc/frontend/tmp
mkdir -p /opt/openitc/frontend/webroot/img/charts
chown www-data:www-data /opt/openitc/frontend/webroot/img/charts
if [[ -d /opt/openitc/frontend/plugins/MapModule/webroot/img/ ]]; then
chown -R www-data:www-data /opt/openitc/frontend/plugins/MapModule/webroot/img/
fi
if getent group ssl-cert &>/dev/null; then
if [ -z "$(groups nagios | grep ssl-cert)" ]; then
usermod -aG ssl-cert nagios
fi
fi
oitc config_generator_shell --generate-container
oitc nagios_export --resource
if [[ -d "/opt/openitc/nagios/rollout" ]]; then
if [[ ! -f "/opt/openitc/nagios/rollout/resource.cfg" ]]; then
ln -s /opt/openitc/nagios/etc/resource.cfg /opt/openitc/nagios/rollout/resource.cfg
fi
fi
echo $OITC_GRAFANA_ADMIN_PASSWORD > /opt/openitc/etc/grafana/admin_password
if [ -f /opt/openitc/etc/grafana/api_key ]; then
echo "Check if Grafana is reachable"
COUNTER=0
set +e
while [ "$COUNTER" -lt 30 ]; do
echo "Try to connect to Grafana API..."
#Is Grafana Server Online?
STATUSCODE=$(curl --noproxy "$OITC_GRAFANA_HOSTNAME" "$OITC_GRAFANA_URL/api/admin/stats" -XGET -uadmin:$OITC_GRAFANA_ADMIN_PASSWORD -H 'Content-Type: application/json' -I 2>/dev/null | head -n 1 | cut -d$' ' -f2)
if [ "$STATUSCODE" == "200" ]; then
echo "Check if Prometheus/VictoriaMetrics Datasource exists in Grafana"
DS_STATUSCODE=$(curl --noproxy "$OITC_GRAFANA_HOSTNAME" "$OITC_GRAFANA_URL/api/datasources/name/Prometheus" -XGET -uadmin:$OITC_GRAFANA_ADMIN_PASSWORD -H 'Content-Type: application/json' -I 2>/dev/null | head -n 1 | cut -d$' ' -f2)
if [ "$DS_STATUSCODE" == "404" ]; then
echo "Create Prometheus/VictoriaMetrics Datasource for Grafana"
RESPONSE=$(curl --noproxy "$OITC_GRAFANA_HOSTNAME" "$OITC_GRAFANA_URL/api/datasources" -XPOST -uadmin:$OITC_GRAFANA_ADMIN_PASSWORD -H 'Content-Type: application/json' -d '{
"name":"Prometheus",
"type":"prometheus",
"url":"http://'$VICTORIA_METRICS_HOST':'$VICTORIA_METRICS_PORT'",
"access":"proxy",
"basicAuth":false,
"isDefault": false,
"jsonData": {}
}')
echo $RESPONSE | jq .
fi
echo "Ok: Prometheus/VictoriaMetrics datasource exists."
COUNTER=9999 # break while loop bc bash where break does not brake
break
fi
COUNTER=$((COUNTER + 1))
sleep 1
done
if [ ! -f /opt/openitc/etc/grafana/api_key ]; then
echo "ERROR!"
echo "Could not connect to Grafana"
fi
set -e
fi
echo "Restart monitoring engine"
oitc supervisor restart naemon
echo "Enabling webserver configuration"
if [[ ! -f "/etc/nginx/sites-enabled/openitc" ]]; then
ln -s /etc/nginx/sites-available/openitc /etc/nginx/sites-enabled/openitc
fi
rm -f /etc/nginx/sites-enabled/default
set +e
# todo fix this
#for Module in "${LOADED_MODULE_SCRIPTS[@]}"; do
# if [ "$Module" != "system" ]; then
# if type "${Module}" &> /dev/null; then
# # create class object
# ${Module} module
#
# # set required variables
# module.property name = "${Module}"
# module.property dbIniFile = "${Module}"
#
# module.finish
# fi
# fi
#done
# Set filesystem permissions after all is done - again
#todo fix if realy required
#chown www-data:www-data /opt/openitc/logs/frontend
#oitc rights
#chown nagios:nagios /opt/openitc/logs/frontend/nagios
#chown www-data:www-data /opt/openitc/frontend/
#chmod 775 /opt/openitc/logs/frontend
#chmod 775 /opt/openitc/logs/frontend/nagios
#chown www-data:www-data /opt/openitc/frontend/tmp