forked from monasca/monasca-agents-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure_metrics_agent.sh
executable file
·236 lines (188 loc) · 6.16 KB
/
configure_metrics_agent.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
#!/bin/bash -e
log() { echo -e "$(date --iso-8601=seconds)" "$1"; }
error() { log "ERROR: $1"; }
warn() { log "WARNING: $1"; }
inf() { log "INFO: $1"; }
BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
INSTALL_DIR=$( cd "$BIN_DIR/.." && pwd )
MON_SUDOERS_FILE="/etc/sudoers.d/mon-agent"
MON_AGENT_DIR="/etc/monasca/agent"
MON_SYSTEMD_DIR="/etc/systemd/system"
MON_DEFAULT_AGENT_LOG_DIR="/var/log/monasca-agent"
inf "Installation directory: ${INSTALL_DIR}"
inf "Configuring metrics agent..."
if [ ! -e "${MON_SUDOERS_FILE}" ]; then
echo "mon-agent ALL=(ALL) NOPASSWD:ALL" | sudo tee "${MON_SUDOERS_FILE}" >> /dev/null
fi
sudo mkdir -p /etc/monasca
# Check if file exist, if yes and OVERWRITE_CONF set to false, create backup
# in same folder
function protect_overwrite() {
local protected_files=("$@")
if [ "${OVERWRITE_CONF}" = "true" ]; then
inf "Following files will be overwritten: ${protected_files[*]}"
return
fi
for protected_file in "${protected_files[@]}"; do
if [ -f "${protected_file}" ]; then
warn "${protected_file} already exists"
warn "If you want to overwrite it you need to use '--overwrite_conf'"
# Create backup with preserving permissions
\cp -f --preserve "${protected_file}" "${protected_file}.backup"
fi
done
}
# Restore file to original location from backup copy if OVERWRITE_CONF is false
function protect_restore() {
local protected_files=("$@")
for protected_file in "${protected_files[@]}"; do
if [ "${OVERWRITE_CONF}" = "false" ]; then
if [ ! -f "${protected_file}.backup" ]; then
warn "No backup file ${protected_file}.backup found, skipping"
return
fi
warn "Restoring original ${protected_file}"
warn "If you want to overwrite it you need to use '--overwrite_conf'"
\mv -f "${protected_file}.backup" "${protected_file}"
return
fi
done
}
function run_monasca_setup() {
local all_args=("$@")
# All this files will be uncoditionaly overwritten by monasca-setup
# so we are creating they backups if OVERWRITE_CONF is set to false
conf_files=(
"${MON_AGENT_DIR}/agent.yaml"
"${MON_AGENT_DIR}/supervisor.conf"
"${MON_SYSTEMD_DIR}/monasca-agent.service"
)
protect_overwrite "${conf_files[@]}"
inf "Running monasca-setup..."
sudo "${BIN_DIR}/python" "${BIN_DIR}/monasca-setup" "${all_args[@]}" \
--log_dir "${MON_AGENT_LOG_DIR}"
protect_restore "${MON_AGENT_DIR}/agent.yaml"
}
function generate_supervisor_config() {
if [ "${OVERWRITE_CONF}" = "false" ]; then
protect_restore "${MON_AGENT_DIR}/supervisor.conf"
return
fi
local tmp_conf_file="/tmp/supervisor.conf"
local supervisor_file="${MON_AGENT_DIR}/supervisor.conf"
echo "[supervisorctl]
serverurl = unix:///var/tmp/monasca-agent-supervisor.sock
[unix_http_server]
file=/var/tmp/monasca-agent-supervisor.sock
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisord]
minfds = 1024
minprocs = 200
loglevel = info
logfile = ${MON_AGENT_LOG_DIR}/supervisord.log
logfile_maxbytes = 50MB
nodaemon = false
pidfile = /var/run/monasca-agent-supervisord.pid
logfile_backups = 10
[program:collector]
command=${BIN_DIR}/monasca-collector foreground
stdout_logfile=NONE
stderr_logfile=NONE
priority=999
startsecs=2
user=mon-agent
autorestart=true
[program:forwarder]
command=${BIN_DIR}/monasca-forwarder
stdout_logfile=NONE
stderr_logfile=NONE
startsecs=3
priority=998
user=mon-agent
autorestart=true
[program:statsd]
command=${BIN_DIR}/monasca-statsd
stdout_logfile=NONE
stderr_logfile=NONE
startsecs=3
priority=998
user=mon-agent
autorestart=true
[group:monasca-agent]
programs=forwarder,collector,statsd" > "${tmp_conf_file}"
sudo cp -f "${tmp_conf_file}" "${supervisor_file}"
sudo chown mon-agent:mon-agent "${supervisor_file}"
sudo chmod 0640 "${supervisor_file}"
sudo systemctl daemon-reload
rm -rf "${tmp_conf_file}"
inf "${supervisor_file} created"
}
# Creates monasca-metrics-agent.service file in /etc/systemd/system/
function create_system_service_file() {
if [ "${OVERWRITE_CONF}" = "false" ]; then
protect_restore "${MON_SYSTEMD_DIR}/monasca-agent.service"
return
fi
local tmp_service_file="/tmp/monasca-agent.service"
local systemd_file="${MON_SYSTEMD_DIR}/monasca-agent.service"
echo -e "[Unit]
Description=Monasca Agent
[Service]
Type=simple
User=mon-agent
Group=mon-agent
Restart=on-failure
ExecStart=${BIN_DIR}/supervisord -c /etc/monasca/agent/supervisor.conf -n
[Install]
WantedBy=multi-user.target" > "${tmp_service_file}"
sudo cp -f "${tmp_service_file}" "${systemd_file}"
sudo chmod 0644 "${systemd_file}"
sudo systemctl daemon-reload
rm -rf "${tmp_service_file}"
inf "${systemd_file} created"
}
function set_attributes() {
# Set proper attributes of files
METRIC_DIRS=("${INSTALL_DIR}" "/etc/monasca" "${MON_AGENT_LOG_DIR}")
for directory in "${METRIC_DIRS[@]}"
do
sudo find "${directory}" -type d -exec chmod 750 {} +
sudo find "${directory}" -type d -exec chown mon-agent:mon-agent {} +
done
inf "Set proper attributes successfully"
}
OVERWRITE_CONF=false
CUSTOM_LOG_DIR=""
MONASCA_SETUP_VARS=()
# check for additional arguments in call to overwrite default values (above)
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-o|--overwrite_conf)
OVERWRITE_CONF=true
shift
;;
--log_dir)
# We change default to `/var/log/monasca-agent` but allow user
# to overwrite
CUSTOM_LOG_DIR="$2"
shift 2
;;
*) # other options
MONASCA_SETUP_VARS+=("${key}")
shift
;;
esac
done
MON_AGENT_LOG_DIR="${CUSTOM_LOG_DIR:-$MON_DEFAULT_AGENT_LOG_DIR}"
run_monasca_setup "${MONASCA_SETUP_VARS[@]}"
generate_supervisor_config
create_system_service_file
set_attributes
inf "Start Monasca Agent daemon"
sudo systemctl daemon-reload
sudo systemctl stop monasca-agent || true
sudo systemctl enable monasca-agent
sudo systemctl start monasca-agent