commit 9487fb10aa9b68aedfa900a4f635fd0a7b850acf Author: Sebastian Blasiak Date: Sat Jun 9 10:29:15 2018 +0200 adding some base for monitoring, before adding prometheus and the rest diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e3c714 --- /dev/null +++ b/.gitignore @@ -0,0 +1,73 @@ +# swp vim edits +*.swp +*.swo +.os_release.sh +.configuration.cluster +.configuration.installs +.awscli-bundle/ + +# Python +*.pyc +**/.cache +**/.coverage + +# Eclipse related files +.classpath +.cproject +.deps +.project +.settings +.metadata/ + +# MAC DS files +.DS_Store + +# STS +.springBeans +# Maven and build related +target/ +packages.mk + +# IntelliJ IDEA related files +*.iml +*.iws +*.ipr +.idea/ + +.sonar-ide.properties + + +*.orig +*rebel*.xml +.idea/ + +/INSTALL +/Makefile +/aclocal.m4 +/autom4te.cache/ +/build/aux/ +/config.log +/config.status +/configure + +# sed related files +**/*.bak_remove + +# terraform state files +**/.terraform +**/*.tfstate +**/*.tfstate.backup +**/.terraform.tfstate.lock.info + +# Folders and files +certificates +terraform +configuration.cluster +configuration.installs +other +ec2 +monitoring.backup +monitoring/monitoring_*_dir +**/*.bak_remove +**/*.wrapped +data diff --git a/README.md b/README.md new file mode 100644 index 0000000..4db26e5 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# monitoring-grafana-influxdb-telegraf-prometheus diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ddf92ce --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,54 @@ +version: '3' + +networks: + public: {} + +volumes: + grafana_lib: {} + grafana_ds: + +services: + influxdb: + image: influxdb:alpine + container_name: influxdb + ports: + - "8086:8086" + networks: + - public + volumes: + - ./data/influxdb:/var/lib/influxdb + environment: + INFLUXDB_REPORTING_DISABLED: "true" + INFLUXDB_DB: telegraf + INFLUXDB_USER: telegraf + INFLUXDB_USER_PASSWORD: nimda + + grafana: + image: grafana/grafana:5.1.3 + container_name: grafana + ports: + - "3000:3000" + networks: + - public + volumes: + - grafana_lib:/var/lib/grafana + - grafana_ds:/var/lib/grafana/ds:rw + - ${PWD}/grafana/add_datasources.sh:/var/lib/grafana/ds/add_datasources.sh + environment: + GF_AUTH_ANONYMOUS_ENABLED: "true" + GF_AUTH_ANONYMOUS_ORG_ROLE: "Admin" + INFLUXDB_URI: "http://influxdb:8086" + INFLUXDB_DB: telegraf + INFLUXDB_USER: telegraf + INFLUXDB_USER_PASSWORD: nimda + command: ["bash", "/var/lib/grafana/ds/add_datasources.sh"] + + telegraf: + image: telegraf:latest + container_name: telegraf + network_mode: "host" + volumes: + - ./telegraf.conf:/etc/telegraf/telegraf.conf:ro + environment: + # real influx host + INFLUXDB_URI: "http://localhost:8086" diff --git a/grafana/add_dashboards.sh b/grafana/add_dashboards.sh new file mode 100644 index 0000000..fe76298 --- /dev/null +++ b/grafana/add_dashboards.sh @@ -0,0 +1,97 @@ +#!/bin/bash +set -e + +NC='\033[0m' + +RED='\033[00;31m' +GREEN='\033[00;32m' +YELLOW='\033[00;33m' +BLUE='\033[00;34m' +PURPLE='\033[00;35m' +CYAN='\033[00;36m' +LIGHTGRAY='\033[00;37m' +MAGENTA='\033[00;35m' +LRED='\033[01;31m' +LGREEN='\033[01;32m' +LYELLOW='\033[01;33m' +LBLUE='\033[01;34m' +LPURPLE='\033[01;35m' +LCYAN='\033[01;36m' +WHITE='\033[01;37m' + +GRAFANA_URL=http://admin:admin@localhost:3000 + + +grafana_api() { + local verb=$1 + local url=$2 + local params=$3 + local bodyfile=$4 + local response + local cmd + + cmd="curl -L -s --fail -H \"Accept: application/json\" -H \"Content-Type: application/json\" -X ${verb} -k ${GRAFANA_URL}${url}" + [[ -n "${params}" ]] && cmd="${cmd} -d \"${params}\"" + [[ -n "${bodyfile}" ]] && cmd="${cmd} --data @${bodyfile}" + echo -e "Running ${cmd}" + eval ${cmd} || return 1 + return 0 +} + +wait_for_api() { + echo -e "${BLUE}Waiting for Grafana to be available...${NC}" + while ! grafana_api GET /api/user/preferences + do + echo -e "${BLUE}Waiting still...${NC}" + sleep 15 + done +} + +replace_datasource() { + local dashboard_file=$1 + local datasource_name=$2 + cmd="sed -i.bak_remove \"s/\\\${DS_INFLUXDB}/${datasource_name}/g\" ${dashboard_file}" + eval ${cmd} || return 1 + return 0 +} + +install_dashboards() { + local dashboard + + for dashboard in dashboards/*.json + + do + if [[ $(grep "\"name\": \"DS_INFLUXDB\"," ${dashboard}) ]]; then + echo -e "${PURPLE}Dashboard ${dashboard} seems to be for InfluxDB datasource${NC}" + datasource_name="influxdb" + fi + if [[ $(grep "\"name\": \"DS_PROMETHEUS\"," ${dashboard}) ]]; then + echo -e "${PURPLE}Dashboard ${dashboard} seems to be for Prometheus datasource${NC}" + datasource_name="prometheus" + fi + if [[ -f "${dashboard}" ]]; then + echo -e "${LCYAN}Installing dashboard ${dashboard}${NC}" + replace_datasource ${dashboard} ${datasource_name} + # backup will be created before wrapping dashboard ^ + #echo -e "{\"dashboard\": `cat $dashboard`}" > "${dashboard}.wrapped" + cp ${dashboard} ${dashboard}.wrapped + sed -i '1s/^/{"dashboard":\n/' ${dashboard}.wrapped + echo "}" >> ${dashboard}.wrapped + + if grafana_api POST /api/dashboards/db "" "${dashboard}.wrapped"; then + echo -e "\n** ${GREEN}installed ok **${NC}" + else + echo -e "\n** ${RED}installation of: ${PURPLE}\"${dashboard}\"${RED} failed **${NC}" + fi + fi + #rm ${dashboard}.wrapped + done +} + +configure_grafana() { + wait_for_api + install_dashboards +} + +configure_grafana + diff --git a/grafana/add_datasources.sh b/grafana/add_datasources.sh new file mode 100644 index 0000000..52ae6c3 --- /dev/null +++ b/grafana/add_datasources.sh @@ -0,0 +1,31 @@ +#!/bin/bash +#set -e + +# ADD INFLUXDB DATASOURCE +curl -s -v -H "Content-Type: application/json" \ + -XPOST http://admin:admin@localhost:3000/api/datasources \ + -d @- <