# Copyright 2017 Google, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: v1
kind: ConfigMap
metadata:
  name: grafana-config
data:
  grafana-init.sh: |+
    #!/bin/bash -e

    echo "Starting grafana..."
    /run.sh &

    upcheck='curl -sS localhost:3000'

    until $upcheck 2> /dev/null; do ((count++)) && ((count==10)) && echo "Grafana failed to start in $count seconds" && exit 1; sleep 1; done

    echo "Grafana took $count seconds to start - ready"

    echo -e "\nCreating Datasource..." 
    curl 'http://admin:admin@127.0.0.1:3000/api/datasources' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary @/opt/grafana/datasource.json 2> /dev/null
    echo -e "\nDone creating Datasource..." 

    echo -e "\nCreating Dashboard..." 
    curl 'http://admin:admin@127.0.0.1:3000/api/dashboards/db' -X POST -H 'Content-Type: application/json;charset=UTF-8' --data-binary @/opt/grafana/dashboard.json 2> /dev/null
    echo -e "\nDone creating Dashboard..." 

    echo -e "\nSetting Home Dashboard..."
    curl http://localhost:3000/api/user/preferences -X PUT -H 'Content-Type: application/json;charset=UTF-8' -d '{"theme": "", "homeDashboardId":1, "timezone":"utc"}' 2> /dev/null
    echo -e "\nDone setting Home Dashboard..."


    # don't exit - wait on Grafana
    wait %1
    
  datasource.json: |-
    {
       "name":"OpenTSDB-k8s",
       "type":"opentsdb",
       "typeLogoUrl":"public/app/plugins/datasource/opentsdb/img/opentsdb_logo.png",
       "access":"proxy",
       "url":"http://opentsdb-read:4242",
       "password":"",
       "user":"",
       "database":"",
       "basicAuth":false,
       "isDefault":true,
       "jsonData":{
          "tsdbResolution":1,
          "tsdbVersion":3
       }
    }
    
  dashboard.json: |-
    {
       "dashboard":{
          "id":null,
          "title":"OpenTSDB Cluster Metrics",
          "tags":[
             "templated"
          ],
          "timezone":"browser",
          "time": {
            "from": "now-5m",
            "to": "now"
          },
          "refresh": "3s",
          "rows":[
             {
                "collapse":false,
                "height":233,
                "panels":[
                   {
                      "aliasColors":{

                      },
                      "bars":false,
                      "dashLength":10,
                      "dashes":false,
                      "datasource":null,
                      "fill":1,
                      "id":2,
                      "legend":{
                         "avg":false,
                         "current":false,
                         "max":false,
                         "min":false,
                         "show":true,
                         "total":false,
                         "values":false
                      },
                      "lines":true,
                      "linewidth":1,
                      "links":[

                      ],
                      "nullPointMode":"null",
                      "percentage":false,
                      "pointradius":5,
                      "points":false,
                      "renderer":"flot",
                      "seriesOverrides":[

                      ],
                      "spaceLength":10,
                      "span":12,
                      "stack":false,
                      "steppedLine":false,
                      "targets":[
                         {
                            "aggregator":"max",
                            "downsampleAggregator":"avg",
                            "downsampleFillPolicy":"none",
                            "downsampleInterval":"",
                            "metric":"cpu_node_utilization_gauge",
                            "refId":"A"
                         }
                      ],
                      "thresholds":[

                      ],
                      "timeFrom":null,
                      "timeShift":null,
                      "title":"Cluster CPU Utilization",
                      "tooltip":{
                         "shared":true,
                         "sort":0,
                         "value_type":"individual"
                      },
                      "type":"graph",
                      "xaxis":{
                         "buckets":null,
                         "mode":"time",
                         "name":null,
                         "show":true,
                         "values":[

                         ]
                      },
                      "yaxes":[
                         {
                            "format":"short",
                            "label":null,
                            "logBase":1,
                            "max":null,
                            "min":null,
                            "show":true
                         },
                         {
                            "format":"short",
                            "label":null,
                            "logBase":1,
                            "max":null,
                            "min":null,
                            "show":true
                         }
                      ]
                   }
                ],
                "repeat":null,
                "repeatIteration":null,
                "repeatRowId":null,
                "showTitle":false,
                "title":"Dashboard Row",
                "titleSize":"h6"
             },
             {
                "collapse":false,
                "height":250,
                "panels":[
                   {
                      "aliasColors":{

                      },
                      "bars":false,
                      "dashLength":10,
                      "dashes":false,
                      "datasource":null,
                      "fill":1,
                      "id":1,
                      "legend":{
                         "avg":false,
                         "current":false,
                         "max":false,
                         "min":false,
                         "show":true,
                         "total":false,
                         "values":false
                      },
                      "lines":true,
                      "linewidth":1,
                      "links":[

                      ],
                      "nullPointMode":"null",
                      "percentage":false,
                      "pointradius":5,
                      "points":false,
                      "renderer":"flot",
                      "seriesOverrides":[

                      ],
                      "spaceLength":10,
                      "span":12,
                      "stack":false,
                      "steppedLine":false,
                      "targets":[
                         {
                            "aggregator":"last",
                            "downsampleAggregator":"avg",
                            "downsampleFillPolicy":"none",
                            "downsampleInterval":"",
                            "metric":"memory_usage_gauge",
                            "refId":"A"
                         }
                      ],
                      "thresholds":[

                      ],
                      "timeFrom":null,
                      "timeShift":null,
                      "title":"Cluster Memory Utilization",
                      "tooltip":{
                         "shared":true,
                         "sort":0,
                         "value_type":"individual"
                      },
                      "type":"graph",
                      "xaxis":{
                         "buckets":null,
                         "mode":"time",
                         "name":null,
                         "show":true,
                         "values":[

                         ]
                      },
                      "yaxes":[
                         {
                            "format":"short",
                            "label":null,
                            "logBase":1,
                            "max":null,
                            "min":null,
                            "show":true
                         },
                         {
                            "format":"short",
                            "label":null,
                            "logBase":1,
                            "max":null,
                            "min":null,
                            "show":true
                         }
                      ]
                   }
                ],
                "repeat":null,
                "repeatIteration":null,
                "repeatRowId":null,
                "showTitle":false,
                "title":"Dashboard Row",
                "titleSize":"h6"
             }
          ],
          "schemaVersion":6,
          "version":0
       },
       "overwrite":false
    }
