|
1 | 1 | #!/bin/sh
|
2 | 2 |
|
3 |
| -if [ "$#" -ne 1 ]; |
4 |
| -then |
| 3 | +set -e |
| 4 | + |
| 5 | +usage() { |
5 | 6 | echo
|
6 | 7 | echo "Usage: $0 http(s)://target_address:port"
|
7 | 8 | echo
|
8 | 9 | echo "target_address is the hostname or IP address of the system that runs pcm-sensor-server"
|
9 | 10 | exit 1
|
10 |
| -fi |
| 11 | +} |
| 12 | + |
| 13 | +# Validate the URL format and reject localhost or 127.0.0.1 |
| 14 | +validate_url() { |
| 15 | + local url=$1 |
| 16 | + local regex='^https?://([a-zA-Z0-9.-]+):[0-9]+$' |
| 17 | + local localhost_regex='^(https?://)?(localhost|127\.0\.0\.1):[0-9]+$' |
| 18 | + |
| 19 | + if ! [[ $url =~ $regex ]]; then |
| 20 | + echo "Error: The URL provided is not in the correct format." |
| 21 | + usage |
| 22 | + fi |
11 | 23 |
|
| 24 | + if [[ $url =~ $localhost_regex ]]; then |
| 25 | + echo "Error: The target_address cannot be localhost or 127.0.0.1." |
| 26 | + usage |
| 27 | + fi |
| 28 | +} |
12 | 29 |
|
13 |
| -mkdir -p grafana_volume/dashboards |
14 |
| -mkdir -p influxdb_volume |
| 30 | +if [ "$#" -ne 1 ]; then |
| 31 | + usage |
| 32 | +fi |
| 33 | + |
| 34 | +validate_url "$1" |
15 | 35 |
|
16 |
| -chmod -R 777 *_volume |
| 36 | +mkdir -p grafana_volume/dashboards || { echo "Error creating grafana_volume/dashboards directory"; exit 1; } |
| 37 | +mkdir -p influxdb_volume || { echo "Error creating influxdb_volume directory"; exit 1; } |
17 | 38 |
|
18 |
| -mkdir -p provisioning/datasources |
19 |
| -cp automatic_influxdb.yml provisioning/datasources/automatic.yml |
| 39 | +chmod -R 777 *_volume || { echo "Error setting permissions on volume directories"; exit 1; } |
20 | 40 |
|
| 41 | +mkdir -p provisioning/datasources || { echo "Error creating provisioning/datasources directory"; exit 1; } |
| 42 | +cp automatic_influxdb.yml provisioning/datasources/automatic.yml || { echo "Error copying automatic_influxdb.yml"; exit 1; } |
21 | 43 |
|
22 | 44 | CTR_RUN=${CTR_RUN:-docker}
|
23 | 45 |
|
24 | 46 | # check if argument is file, create the telegraf.conf accordingly
|
25 | 47 | if [ -f "$1" ]; then
|
26 | 48 | echo "creating telegraf.conf for hosts in targets file";
|
27 |
| - head -n -7 "telegraf.conf.template" > telegraf.conf |
| 49 | + head -n -7 "telegraf.conf.template" > telegraf.conf || { echo "Error creating telegraf.conf"; exit 1; } |
28 | 50 | while IFS='' read -r line || [[ -n "$line" ]]; do
|
29 | 51 | # Split the line at the : character to get the IP and port
|
30 | 52 | ip=$(echo "$line" | cut -d ':' -f 1)
|
31 | 53 | port=$(echo "$line" | cut -d ':' -f 2)
|
32 | 54 | # Append the transformed line to the output file, separated by a comma
|
33 | 55 | echo -n "\"http://$ip:$port/persecond/\"," >> telegraf.conf
|
34 |
| - done < $1 |
35 |
| - sed -i '$ s/,$//' telegraf.conf |
36 |
| - tail -n -6 "telegraf.conf.template" >> telegraf.conf |
| 56 | + done < "$1" |
| 57 | + sed -i '$ s/,$//' telegraf.conf || { echo "Error editing telegraf.conf"; exit 1; } |
| 58 | + tail -n -6 "telegraf.conf.template" >> telegraf.conf || { echo "Error appending to telegraf.conf"; exit 1; } |
37 | 59 | echo Downloading PCM dashboard
|
38 |
| - curl -o grafana_volume/dashboards/pcm-dashboard.json $(head -1 $1)/dashboard |
39 |
| - |
| 60 | + curl -o grafana_volume/dashboards/pcm-dashboard.json $(head -1 "$1")/dashboard || { echo "Error downloading PCM dashboard"; exit 1; } |
40 | 61 | else
|
41 | 62 | echo "creating telegraf.conf for $1 ";
|
42 |
| - sed "s#PCMSENSORSERVER#$1#g" telegraf.conf.template > telegraf.conf |
| 63 | + sed "s#PCMSENSORSERVER#$1#g" telegraf.conf.template > telegraf.conf || { echo "Error creating telegraf.conf"; exit 1; } |
43 | 64 | echo Downloading PCM dashboard
|
44 |
| - curl -o grafana_volume/dashboards/pcm-dashboard.json $1/dashboard |
| 65 | + curl -o grafana_volume/dashboards/pcm-dashboard.json "$1"/dashboard || { echo "Error downloading PCM dashboard"; exit 1; } |
45 | 66 | fi
|
46 | 67 |
|
47 | 68 | echo "Creating influxdb network"
|
48 |
| -${CTR_RUN} network create influxdb-network |
| 69 | +${CTR_RUN} network create influxdb-network || { echo "Error creating influxdb network"; exit 1; } |
49 | 70 | echo Starting influxdb
|
50 |
| -${CTR_RUN} run -d --name influxdb -p 8083:8083 -p 8086:8086 --network=influxdb-network -v $PWD/influxdb_volume:/var/lib/influxdb influxdb:1.8.0-alpine |
| 71 | +${CTR_RUN} run -d --name influxdb -p 8083:8083 -p 8086:8086 --network=influxdb-network -v "$PWD"/influxdb_volume:/var/lib/influxdb influxdb:1.8.0-alpine || { echo "Error starting influxdb"; exit 1; } |
51 | 72 | echo Starting telegraf
|
52 |
| -${CTR_RUN} run -d --name telegraf --network=influxdb-network -v $PWD/telegraf.conf:/etc/telegraf/telegraf.conf:ro telegraf |
| 73 | +${CTR_RUN} run -d --name telegraf --network=influxdb-network -v "$PWD"/telegraf.conf:/etc/telegraf/telegraf.conf:ro telegraf || { echo "Error starting telegraf"; exit 1; } |
53 | 74 | echo Starting grafana
|
54 |
| -${CTR_RUN} run -d --network=influxdb-network --name grafana -p 3000:3000 -v $PWD/provisioning:/etc/grafana/provisioning -v $PWD/grafana_volume:/var/lib/grafana grafana/grafana |
55 |
| - |
56 |
| -echo Start browser at http://localhost:3000/ and login with admin user, password admin |
| 75 | +${CTR_RUN} run -d --network=influxdb-network --name grafana -p 3000:3000 -v "$PWD"/provisioning:/etc/grafana/provisioning -v "$PWD"/grafana_volume:/var/lib/grafana grafana/grafana || { echo "Error starting grafana"; exit 1; } |
57 | 76 |
|
| 77 | +echo "Start browser at http://"`hostname`":3000/ or http://localhost:3000/ and login with admin user, password admin" |
0 commit comments