forked from it-novum/openITCOCKPIT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CONTAINER_SETUP.sh
230 lines (185 loc) · 8.76 KB
/
CONTAINER_SETUP.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
#!/bin/bash
# This SETUP Script should ONLY BE USED WHEN running inside of an container like for example Docker
if ! [ $(id -u) = 0 ]; then
echo "You need to run this script as root user or via sudo!"
exit 1
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"
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
echo "# This file will NOT be overwritten during an update" >> /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
# It is important that the statusengine-worker container is already up and running and has created all
# the Statusengine related MySQL tables, because the setup will add partitions to the tables
echo "Checking if Statusengine Tables where already created..."
COUNTER=0
MYSQL_ONLINE=0
set +e
while [ "$COUNTER" -lt 30 ]; do
#Is Grafana Server Online?
OUTPUT=$(mysql "--defaults-extra-file=$INIFILE" -e "SELECT 1 FROM statusengine_nodes;" -B -s 2>/dev/null)
if [ "$OUTPUT" ]; then
echo "Tables are present."
MYSQL_ONLINE=1
break
fi
echo "Waiting for Statusengine to create tables..."
COUNTER=$((COUNTER + 1))
sleep 1
done
if [[ "$MYSQL_ONLINE" == 0 ]]; then
echo "ERROR!"
echo "Statusengine tables are missing! ABORT!"
exit 1
fi
set -e
#mkdir -p /opt/openitc/var/prometheus
#chown nagios:nagios /opt/openitc/var/prometheus
#mkdir -p /opt/openitc/var/prometheus/victoria-metrics
echo "---------------------------------------------------------------"
echo "Import openITCOCKPIT Core database schema"
oitc migrations migrate
echo "---------------------------------------------------------------"
echo "Load default database"
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
if [ -d "${APPDIR}/plugins/CheckmkModule/src" ]; then
oitc checkmkNagiosExport --init
fi
echo "---------------------------------------------------------------"
echo "Create new WebSocket Key"
WEBSOCKET_KEY=$(php -r "echo bin2hex(openssl_random_pseudo_bytes(80, \$cstrong));")
mysql "--defaults-extra-file=${INIFILE}" -e "UPDATE systemsettings SET \`systemsettings\`.\`value\`='${WEBSOCKET_KEY}' WHERE \`key\`='SUDO_SERVER.API_KEY';"
echo $OITC_GRAFANA_ADMIN_PASSWORD > /opt/openitc/etc/grafana/admin_password
oitc config_generator_shell --generate-container
echo "---------------------------------------------------------------"
echo "Configure Grafana"
if [ ! -f /opt/openitc/etc/grafana/api_key ]; then
echo "Create new Grafana API Key for openITCOCKPIT"
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
API_KEY=$(curl --noproxy "$OITC_GRAFANA_HOSTNAME" "$OITC_GRAFANA_URL/api/auth/keys" -XPOST -uadmin:$OITC_GRAFANA_ADMIN_PASSWORD -H 'Content-Type: application/json' -d '{"role":"Editor","name":"openITCOCKPIT"}' | jq -r '.key')
echo "$API_KEY" >/opt/openitc/etc/grafana/api_key
break
fi
COUNTER=$((COUNTER + 1))
sleep 1
done
if [ ! -f /opt/openitc/etc/grafana/api_key ]; then
echo "ERROR!"
echo "Could not create API key for Grafana"
fi
set -e
fi
echo "Check if Graphite Datasource exists in Grafana"
DS_STATUSCODE=$(curl --noproxy "$OITC_GRAFANA_HOSTNAME" "$OITC_GRAFANA_URL/api/datasources/name/Graphite" -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 Graphite as default 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":"Graphite",
"type":"graphite",
"url":"http://'$OITC_GRAPHITE_WEB_ADDRESS':'$OITC_GRAPHITE_WEB_PORT'",
"access":"proxy",
"basicAuth":false,
"isDefault": true,
"jsonData": {
"graphiteVersion": 1.1
}
}')
echo $RESPONSE | jq .
fi
echo "Ok: Graphite datasource exists."
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."
if [ -f /opt/openitc/etc/grafana/api_key ]; then
echo "Check for Grafana Configuration in openITCOCKPIT database"
API_KEY=$(cat /opt/openitc/etc/grafana/api_key)
set +e
COUNT=$(mysql "--defaults-extra-file=$INIFILE" -e "SELECT COUNT(*) FROM grafana_configurations;" -B -s 2>/dev/null)
if [ "$?" == 0 ] && [ "$COUNT" == 0 ]; then
echo "Create missing default configuration."
# This is different from the default SETUP.sh!
mysql --defaults-extra-file=${INIFILE} -e "INSERT INTO grafana_configurations (api_url, api_key, graphite_prefix, use_https, use_proxy, ignore_ssl_certificate, dashboard_style, created, modified) VALUES('${OITC_GRAFANA_HOSTNAME}', '${API_KEY}', 'openitcockpit', 1, 0, 1, 'light', '2018-12-05 08:42:55', '2018-12-05 08:42:55');"
fi
set -e
fi
echo "---------------------------------------------------------------"
echo "Scan and import ACL objects. This will take a while..."
oitc Acl.acl_extras aco_sync
oitc compress
oitc setup --fast
oitc nagios_export
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 "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 default permissions, check for always allowed permissions and dependencies
oitc roles --enable-defaults --admin
date > /opt/openitc/var/.installation_done