This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathview-in-grafana.sh
executable file
·146 lines (124 loc) · 4.11 KB
/
view-in-grafana.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
#!/bin/bash
usage() {
cat <<EOF
USAGE: view-in-grafana.sh <options> [directory] <retention_days>
Options: -d [influxdb|graphite] Enable InfluxDB or Graphite as the back end database. Defaults to InfluxDB
-b Build local containers from docker-compose
EOF
}
finish() {
docker-compose down --volumes
}
download_file() {
curl -O --silent -k "$1" || { echo "ERROR: Unable to download file from ${1}."; exit 1; }
}
download_dashboards() {
mkdir -p ./grafana/imports
cd ./grafana/imports || exit
download_file https://raw.githubusercontent.com/puppetlabs/puppet_metrics_dashboard/master/files/PuppetDB_Performance.json
download_file https://raw.githubusercontent.com/puppetlabs/puppet_metrics_dashboard/master/files/PuppetDB_Workload.json
download_file https://raw.githubusercontent.com/puppetlabs/puppet_metrics_dashboard/master/files/Puppetserver_Performance.json
download_file https://raw.githubusercontent.com/puppetlabs/puppet_metrics_dashboard/master/files/Archive_File_Sync.json
cd - || exit > /dev/null
}
get_latest_containers() {
if [[ $BUILDLOCAL ]]; then
echo "Building local containers"
docker-compose build &>/dev/null
else
echo "Downloading latest containers"
docker-compose pull --ignore-pull-failures &>/dev/null
fi
if [[ $DATABASE == "influxdb" ]]; then
echo "Getting the latest graphs"
download_dashboards
fi
}
BUILDLOCAL=false
DATABASE="influxdb"
CONTAINERPATH="influxdb-grafana"
NETCATARGS=(--netcat 127.0.0.1 --convert-to influxdb --influx-db archive)
# Default to 30 if $2 is empty/unset
RETENTION_DAYS="${2:-30}"
while getopts ":bd:" opt; do
case "$opt" in
b)
BUILDLOCAL=true
;;
d)
if [[ $OPTARG == "graphite" ]]; then
echo "Using Graphite"
DATABASE="graphite"
CONTAINERPATH="grafana-puppetserver"
NETCATARGS=(--netcat 127.0.0.1)
elif [[ $OPTARG == "influxdb" ]]; then
echo "Using InfluxDB"
else
echo "Invalid database: $OPTARG" >&2
usage
exit 1
fi
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
# VALIDATION
[[ ! -d $1 ]] && { echo "ERROR: First argument must be a directory."; usage; exit 1; }
type docker-compose >/dev/null 2>&1 || {
echo >&2 "ERROR: docker-compose required. Please install docker-compose."
exit 1
}
# Portable way of getting the full path to a directory
datadir="$(cd "$1" || exit; echo "$PWD")"
# Download json2timeseriesdb to the directory containing this script.
cd "${BASH_SOURCE[0]%/*}" || exit
download_file https://raw.githubusercontent.com/puppetlabs/puppetlabs-puppet_metrics_collector/master/files/json2timeseriesdb
chmod +x json2timeseriesdb
# Change to the directory containing this script.
cd "${BASH_SOURCE[0]%/*}/${CONTAINERPATH}" || exit
# MAIN SCRIPT
trap finish EXIT INT ERR
echo "Getting the latest container images"
get_latest_containers
echo "Starting Containers"
docker-compose up -d
echo "Extracting data from tarballs..."
find "$datadir" -type f -ctime -"${RETENTION_DAYS}" -name "*.bz2" -execdir tar jxf "{}" \; 2>/dev/null
find "$datadir" -type f -ctime -"${RETENTION_DAYS}" -name "*.gz" -execdir tar xf "{}" \; 2>/dev/null
echo "Waiting for database to be ready..."
type nc >/dev/null 2>&1 || {
echo >&2 "ERROR: nc required. Please install ncat/netcat."
exit 1
}
timeout=60
until nc -zv 127.0.0.1 2003 &>/dev/null || (( timeout <= 0 )); do
(( timeout-- ))
sleep 1
done
(( timeout <= 0 )) && {
echo "ERROR: Unable to connect to the database. Is docker running?"
exit 1
}
echo "ready"
echo "Deleting json files past ${RETENTION_DAYS} retention_days..."
NUM_DEL=$(find "$datadir" -type f -mtime +"${RETENTION_DAYS}" -iname "*.json" -delete -print | wc -l)
echo "Deleted $NUM_DEL files past retention_days"
echo "Loading data..."
../json2timeseriesdb --pattern "$datadir/**/*.json" "${NETCATARGS[@]}" 2>/dev/null
cat <<EOF
Metrics ready! View at http://127.0.0.1:3000
Username: admin
Password: admin
Press enter key to exit...
EOF
# Use _ as a throwaway variable
read -r _