-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.bash
183 lines (158 loc) · 4.58 KB
/
install.bash
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
set -ue
set -o pipefail
PROM_VERSION=2.29.1
# see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
INSTANCE_ID="$(curl --silent http://169.254.169.254/latest/meta-data/instance-id)"
AWS_DEFAULT_REGION="$(curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document | ruby -r json -e 'puts JSON.parse(STDIN.read)["region"]')"
export AWS_DEFAULT_REGION
# fetch tag value
SERVICE_NAME="$(aws ec2 describe-tags --filters "Name=resource-id,Values=${INSTANCE_ID}" --out=text --query "Tags[?Key == 'ServiceName'].Value")"
if [ -z "$SERVICE_NAME" ]; then
echo "Can't extract ServiceName from ec2 tags"
exit 2
fi
service prometheus-relay-agent stop || true
curl -L https://github.com/prometheus/prometheus/releases/download/v${PROM_VERSION}/prometheus-${PROM_VERSION}.linux-amd64.tar.gz -o /tmp/prometheus-${PROM_VERSION}.linux-amd64.tar.gz
cd /tmp
tar zxvf prometheus-${PROM_VERSION}.linux-amd64.tar.gz
cd prometheus-${PROM_VERSION}.linux-amd64
mkdir -p /opt/prometheus
cp -p prometheus /opt/prometheus/
cat > /opt/prometheus/prometheus.yml <<_EOD_
global:
scrape_interval: 5s
evaluation_interval: 5s
external_labels:
monitor: '${SERVICE_NAME}'
scrape_configs:
- job_name: node-exporter
ec2_sd_configs:
- region: ${AWS_DEFAULT_REGION}
port: 9100
filters:
- name: "tag:ServiceName"
values: ["${SERVICE_NAME}"]
relabel_configs:
- source_labels: [__meta_ec2_tag_Name]
target_label: name
- source_labels: [__meta_ec2_tag_Env]
target_label: env
- source_labels: [__meta_ec2_tag_ServiceName]
target_label: service_name
- source_labels: [__meta_ec2_tag_Role]
target_label: role
- job_name: nginx
ec2_sd_configs:
- region: ${AWS_DEFAULT_REGION}
port: 4040
filters:
- name: "tag:ServiceName"
values: ["${SERVICE_NAME}"]
relabel_configs:
- source_labels: [__meta_ec2_tag_Name]
target_label: name
- source_labels: [__meta_ec2_tag_Env]
target_label: env
- source_labels: [__meta_ec2_tag_ServiceName]
target_label: service_name
- source_labels: [__meta_ec2_tag_Role]
target_label: role
_EOD_
#
# Install to system
#
if hash systemctl; then
# systemd
cat > /usr/lib/systemd/system/prometheus-relay-agent.service <<'_EOD_'
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target
[Service]
Type=simple
ExecStart=/opt/prometheus/prometheus \
--config.file=/opt/prometheus/prometheus.yml --storage.tsdb.retention.time=5m
Restart=always
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
_EOD_
systemctl daemon-reload
systemctl enable prometheus-relay-agent.service
systemctl start prometheus-relay-agent.service
else # hash systemctl
# SysVinit
cat > /etc/init.d/prometheus-relay-agent <<'_EOD_'
#!/bin/bash
#
# /etc/rc.d/init.d/prometheus-relay-agent
#
# chkconfig: 2345 70 30
#
# pidfile: /var/run/prometheus-relay-agent.pid
# Source function library.
. /etc/init.d/functions
RETVAL=0
ARGS="--config.file=/opt/prometheus/prometheus.yml --storage.tsdb.retention.time=5m"
PROG="prometheus-relay-agent"
DAEMON="/opt/prometheus/prometheus"
PID_FILE=/var/run/${PROG}.pid
LOG_FILE=/var/log/node_exporter.log
LOCK_FILE=/var/lock/subsys/${PROG}
GOMAXPROCS=$(grep -c ^processor /proc/cpuinfo)
start() {
if check_status > /dev/null; then
echo "node_exporter is already running"
exit 0
fi
echo -n $"Starting node_exporter: "
${DAEMON} ${ARGS} 1>>${LOG_FILE} 2>&1 &
echo $! > ${PID_FILE}
RETVAL=$?
[ $RETVAL -eq 0 ] && touch ${LOCK_FILE}
echo ""
return $RETVAL
}
stop() {
if check_status > /dev/null; then
echo -n $"Stopping node_exporter: "
kill -9 "$(cat ${PID_FILE})"
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f ${LOCK_FILE} ${PID_FILE}
echo ""
return $RETVAL
else
echo "node_exporter is not running"
rm -f ${LOCK_FILE} ${PID_FILE}
return 0
fi
}
check_status() {
status -p ${PID_FILE} ${DAEMON}
RETVAL=$?
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
N=/etc/init.d/${NAME}
echo "Usage: $N {start|stop|restart}" >&2
RETVAL=2
;;
esac
exit ${RETVAL}
_EOD_
chmod +x /etc/init.d/prometheus-relay-agent
chkconfig --add prometheus-relay-agent
chkconfig prometheus-relay-agent on
service prometheus-relay-agent start
fi # hash systemctl