Post

Grafana bootstrap script for AWS

Bootstrap script for Amazon-Linux AMI. This script installs docker and docker-compose. Runs Grafana, InfluxDB, and Telegraf with custom configuration files.

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
#!/bin/bash

yum update -y
yum install wget curl git docker -y
curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
usermod -a -G docker ec2-user
newgrp docker
chown -R ec2-user:docker /home/ec2-user/docker
systemctl enable docker.service
systemctl start docker.service
mkdir -p /home/ec2-user/docker/grafana/{provisioning,dashboards}
mkdir -p /home/ec2-user/docker/grafana/provisioning/{datasources,dashboards}
mkdir -p /home/ec2-user/docker/telegraf/etc
cat <<EOF > /home/ec2-user/docker/configuration.env
# Grafana options
GF_SECURITY_ADMIN_USER=admin
GF_SECURITY_ADMIN_PASSWORD=admin
GF_INSTALL_PLUGINS=

# InfluxDB options
INFLUXDB_DB=influx
INFLUXDB_ADMIN_USER=$(date +%s | sha256sum | base64 | head -c 8 ; echo)
INFLUXDB_ADMIN_PASSWORD=$(date +%s | sha256sum | base64 | head -c 12 ; echo)
EOF

cat <<EOF > /home/ec2-user/docker/telegraf/etc/telegraf.conf
[global_tags]

[agent]
  interval = "30s"
  round_interval = true
  metric_buffer_limit = 10000
  flush_buffer_when_full = true
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  debug = false
  quiet = false
  hostname = ""

[[outputs.influxdb]]
  urls = ["http://influxdb:8086"] # required
  database = "influx" # required
  precision = "s"
  timeout = "5s"

[[inputs.statsd]]
  protocol = "udp"
  max_tcp_connections = 250
  tcp_keep_alive = false
  service_address = ":8125"
  delete_gauges = true
  delete_counters = true
  delete_sets = true
  delete_timings = true
  percentiles = [90]
  metric_separator = "_"
  parse_data_dog_tags = false
  allowed_pending_messages = 10000
  percentile_limit = 1000

[[inputs.cpu]]
  percpu = true
  totalcpu = true
  fielddrop = ["time_*"]
  collect_cpu_time = false
  report_active = false

[[inputs.disk]]
  ignore_fs = ["tmpfs", "devtmpfs", "devfs", "iso9660", "overlay", "aufs", "squashfs"]

[[inputs.diskio]]

[[inputs.kernel]]

[[inputs.mem]]

[[inputs.processes]]

[[inputs.swap]]

[[inputs.system]]

[[inputs.net]]

[[inputs.netstat]]

[[inputs.interrupts]]

[[inputs.linux_sysctl_fs]]

[[inputs.ping]]
  urls = ["${DNSName1}", "${DNSName2}", "${DNSName3}"]
  interval = "30s"
  count = 4
  ping_interval = 1.0
  timeout = 2.0
EOF

cat <<EOF > /home/ec2-user/docker/grafana/provisioning/datasources/datasource.yml
apiVersion: 1

deleteDatasources:
  - name: Influxdb
    orgId: 1
datasources:
  - name: InfluxDB
    type: influxdb
    access: proxy
    orgId: 1
    url: http://influxdb:8086
    password: "admin"
    user: "admin"
    database: "influx"
    basicAuth: false
    isDefault: true
    jsonData:
      timeInterval: "30s"
    version: 1
    editable: false
  - name: CloudWatch
    type: cloudwatch
    jsonData:
      authType: default
      defaultRegion: ${AWS::Region}
EOF

cat <<EOF > /home/ec2-user/docker/grafana/provisioning/dashboards/dashboard.yml
apiVersion: 1

providers:
- name: 'dash'
  orgId: 1
  folder: ''
  type: file
  disableDeletion: false
  updateIntervalSeconds: 30
  options:
    path: /var/lib/grafana/dashboards/
    foldersFromFilesStructure: true
EOF

git clone https://<github-key>@github.com/senad-dizdarevic/grafana-dash.git /home/ec2-user/temp
mv /home/ec2-user/temp/AWS/AWS-dash.json /home/ec2-user/docker/grafana/dashboards/dash.json
mv /home/ec2-user/temp/AWS/grafana-conf /home/ec2-user/docker/grafana/defaults.ini
rm -rf /home/ec2-user/temp

cat <<EOF > /home/ec2-user/docker/docker-compose.yml
version: '3.6'
services:
  telegraf:
    image: telegraf:1.18-alpine
    volumes:
      - ./telegraf/etc/telegraf.conf:/etc/telegraf/telegraf.conf:ro
    depends_on:
      - influxdb
    links:
      - influxdb
    ports:
      - '8125:8125/udp'

  influxdb:
    image: influxdb:1.8-alpine
    env_file: configuration.env
    ports:
      - '8086:8086'
    volumes:
      - ./:/imports
      - influxdb_data:/var/lib/influxdb

  grafana:
    image: grafana/grafana:9.1.8
    depends_on:
      - influxdb
    env_file: configuration.env
    links:
      - influxdb
    ports:
      - '80:3000'
    volumes:
      - grafana_data:/var/lib/grafana
      - ./grafana/provisioning/:/etc/grafana/provisioning/
      - ./grafana/dashboards/:/var/lib/grafana/dashboards/
      - ./grafana/defaults.ini:/usr/share/grafana/conf/defaults.ini

volumes:
  grafana_data: {}
  influxdb_data: {}
EOF

docker-compose -f /home/ec2-user/docker/docker-compose.yml up -d

cat <<EOF > ~/mycron
0 0 * * * yum -y update --security
EOF
crontab ~/mycron
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource myASG --region ${AWS::Region}
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.