grafana-rb 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/grafana-rb/cli.rb +44 -39
- data/lib/grafana-rb/version.rb +1 -1
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 0e6c02cb82164ba72d54f357f7666261006ce9f8
         | 
| 4 | 
            +
              data.tar.gz: 777d42766e7c1fcce465b5f019cc5a8afe61825f
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 64a23476e8a9ea937fe249474a9773a448347e3c222adf5b39a2ff99a88be5bb5fecaebc07fa3ab19e05ed61bfccf100e0cc23b710405334339270767071ba95
         | 
| 7 | 
            +
              data.tar.gz: 89c5539c887a303761e40868c8f9577d0b0cb8cfa939277631b0afbdb7ebe1b62d57ca4af392b4c18df05a1ea223d3cbfc0158636bd79a1fe200a94e8231d6f5
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    
    
        data/lib/grafana-rb/cli.rb
    CHANGED
    
    | @@ -79,13 +79,11 @@ module GrafanaRb | |
| 79 79 | 
             
                    puts "---"
         | 
| 80 80 | 
             
                    puts "tool_version: #{GrafanaRb::VERSION}"
         | 
| 81 81 | 
             
                    puts "grafana_url: monitoring.your-domain.com:3000     # required"
         | 
| 82 | 
            -
                    puts "prometheus_url: monitoring.your-domain.com:9090  # required"
         | 
| 83 82 | 
             
                    puts "grafana_user: user                  # 'admin' if missed"
         | 
| 84 83 | 
             
                    puts "grafana_password: secret            # 'admin' if missed"
         | 
| 85 | 
            -
                    puts "slack_channel: #alerts              # no slack notifications if missed"
         | 
| 86 | 
            -
                    puts "slack_url: https://hooks.slack.com/services/... # no slack notifications if missed"
         | 
| 87 | 
            -
                    puts "slack_mention: @john, @michelle     # optional"
         | 
| 88 84 | 
             
                    puts "require: [main.yml, dash*.yml]      # optional, [*.yml] by default"
         | 
| 85 | 
            +
                    puts "datasources: ...                    # datasources"
         | 
| 86 | 
            +
                    puts "notifications: ...                  # notifications"
         | 
| 89 87 | 
             
                    puts ""
         | 
| 90 88 | 
             
                    die
         | 
| 91 89 | 
             
                  end
         | 
| @@ -117,39 +115,35 @@ module GrafanaRb | |
| 117 115 | 
             
                  }
         | 
| 118 116 | 
             
                end
         | 
| 119 117 |  | 
| 120 | 
            -
                def  | 
| 121 | 
            -
                   | 
| 122 | 
            -
             | 
| 123 | 
            -
                     | 
| 124 | 
            -
             | 
| 125 | 
            -
             | 
| 126 | 
            -
             | 
| 127 | 
            -
             | 
| 128 | 
            -
             | 
| 129 | 
            -
                     | 
| 130 | 
            -
                  else
         | 
| 131 | 
            -
                    puts "create datasource"
         | 
| 132 | 
            -
                    @datasource_id = request(:post, "/api/datasources", payload)["id"]
         | 
| 118 | 
            +
                def create_or_update_datasources
         | 
| 119 | 
            +
                  @datasource_ids = {}
         | 
| 120 | 
            +
                  (config["datasources"] || []).each do |payload|
         | 
| 121 | 
            +
                    if @datasource_ids[payload["name"]] = request(:get, "/api/datasources/id/#{payload["name"]}")["id"]
         | 
| 122 | 
            +
                      puts "update datasource: #{payload["name"]}"
         | 
| 123 | 
            +
                      request(:put, "/api/datasources/#{@datasource_ids[payload["name"]]}", payload)
         | 
| 124 | 
            +
                    else
         | 
| 125 | 
            +
                      puts "create datasource: #{payload["name"]}"
         | 
| 126 | 
            +
                      @datasource_ids[payload["name"]] = request(:post, "/api/datasources", payload)["id"]
         | 
| 127 | 
            +
                    end
         | 
| 133 128 | 
             
                  end
         | 
| 134 129 | 
             
                end
         | 
| 135 130 |  | 
| 136 131 | 
             
                def create_or_update_slack_notifications
         | 
| 137 | 
            -
                   | 
| 138 | 
            -
                   | 
| 139 | 
            -
                     | 
| 140 | 
            -
                     | 
| 141 | 
            -
             | 
| 142 | 
            -
             | 
| 143 | 
            -
             | 
| 144 | 
            -
                     | 
| 145 | 
            -
             | 
| 132 | 
            +
                  @notif_ids = {}
         | 
| 133 | 
            +
                  (config["notifications"] || []).each do |payload|
         | 
| 134 | 
            +
                    notif = request(:get, "/api/alert-notifications").find{ |s| s["name"] == payload["name"] }
         | 
| 135 | 
            +
                    if notif
         | 
| 136 | 
            +
                      method = :put
         | 
| 137 | 
            +
                      url = "/api/alert-notifications/#{notif["id"]}"
         | 
| 138 | 
            +
                      opts = {id: notif["id"]}
         | 
| 139 | 
            +
                    else
         | 
| 140 | 
            +
                      method = :post
         | 
| 141 | 
            +
                      url = "/api/alert-notifications"
         | 
| 142 | 
            +
                      opts = {}
         | 
| 143 | 
            +
                    end
         | 
| 144 | 
            +
                    puts "create/update slack notifications: #{payload["name"]}"
         | 
| 145 | 
            +
                    @notif_ids[payload["name"]] = request(method, url, opts.merge(payload))["id"]
         | 
| 146 146 | 
             
                  end
         | 
| 147 | 
            -
                  puts "create/update slack notifications"
         | 
| 148 | 
            -
                  request(method, url, opts.merge(type: "slack", name: "slack", isDefault: true, settings: {
         | 
| 149 | 
            -
                    url: config["slack_url"],
         | 
| 150 | 
            -
                    recipient: config["slack_channel"],
         | 
| 151 | 
            -
                    mention: config["slack_mention"]
         | 
| 152 | 
            -
                  }))
         | 
| 153 147 | 
             
                end
         | 
| 154 148 |  | 
| 155 149 | 
             
                def gen_panel_custom(id, datasource_id, desc)
         | 
| @@ -157,7 +151,7 @@ module GrafanaRb | |
| 157 151 | 
             
                  i = -1
         | 
| 158 152 | 
             
                  exprs = desc.key?("exprs") ? desc["exprs"] : [{"expr" => desc["expr"]}]
         | 
| 159 153 | 
             
                  {
         | 
| 160 | 
            -
                    datasource:  | 
| 154 | 
            +
                    datasource: @datasource_ids.invert[datasource_id],
         | 
| 161 155 | 
             
                    id: id,
         | 
| 162 156 | 
             
                    span: desc["span"] || 2,
         | 
| 163 157 | 
             
                    stack: desc["stack"],
         | 
| @@ -181,7 +175,7 @@ module GrafanaRb | |
| 181 175 | 
             
                  }.merge(desc.key?("alert") ? {alert: desc["alert"], thresholds: desc["thresholds"]} : {})
         | 
| 182 176 | 
             
                end
         | 
| 183 177 |  | 
| 184 | 
            -
                def gen_panel(id, datasource_id, desc)
         | 
| 178 | 
            +
                def gen_panel(id, datasource_id, notif_id, desc)
         | 
| 185 179 | 
             
                  if desc["type"] == "text"
         | 
| 186 180 | 
             
                    {
         | 
| 187 181 | 
             
                      "content": desc["content"].to_s,
         | 
| @@ -258,6 +252,10 @@ module GrafanaRb | |
| 258 252 | 
             
                      raise "wrong expr"
         | 
| 259 253 | 
             
                    end
         | 
| 260 254 |  | 
| 255 | 
            +
                    notification = desc.delete('notification')
         | 
| 256 | 
            +
                    if notification
         | 
| 257 | 
            +
                      notif_id = @notif_ids[notification]
         | 
| 258 | 
            +
                    end
         | 
| 261 259 | 
             
                    gen_panel_custom(id, datasource_id, desc.merge(JSON.load(JSON.dump({
         | 
| 262 260 | 
             
                      exprs: [{expr: expr, title: nil}],
         | 
| 263 261 | 
             
                      fill: 0,
         | 
| @@ -279,7 +277,8 @@ module GrafanaRb | |
| 279 277 | 
             
                        ],
         | 
| 280 278 | 
             
                        executionErrorState: "alerting",
         | 
| 281 279 | 
             
                        frequency: "30s",
         | 
| 282 | 
            -
                        name: desc["expr"]
         | 
| 280 | 
            +
                        name: desc["expr"],
         | 
| 281 | 
            +
                        notifications: (notif_id ? [{"id": notif_id}] : [])
         | 
| 283 282 | 
             
                      }
         | 
| 284 283 | 
             
                    }))))
         | 
| 285 284 |  | 
| @@ -292,16 +291,23 @@ module GrafanaRb | |
| 292 291 | 
             
                  editable = config.delete("editable") { true }
         | 
| 293 292 | 
             
                  duration = config.delete("duration") { "30m" }
         | 
| 294 293 | 
             
                  refresh = config.delete("refresh") { "10s" }
         | 
| 294 | 
            +
                  datasource = config.delete("datasource") { "unknown" }
         | 
| 295 | 
            +
                  notification = config.delete("notification") { nil }
         | 
| 295 296 | 
             
                  name = config.delete("name")
         | 
| 296 297 | 
             
                  puts "create/update dashboard '#{name}'"
         | 
| 297 298 |  | 
| 299 | 
            +
                  unless @datasource_ids.key?(datasource)
         | 
| 300 | 
            +
                    puts "FATAL: unknown datasource for #{name} dashboard"
         | 
| 301 | 
            +
                    exit
         | 
| 302 | 
            +
                  end
         | 
| 303 | 
            +
             | 
| 298 304 | 
             
                  rows = config.map { |name, group| 
         | 
| 299 305 | 
             
                    group = {"panels" => group} if group.is_a?(Array)
         | 
| 300 306 | 
             
                    {
         | 
| 301 307 | 
             
                      title: group["title"] || name,
         | 
| 302 308 | 
             
                      showTitle: group.delete("showTitle") { true },
         | 
| 303 309 | 
             
                      height: group["height"] || 200,
         | 
| 304 | 
            -
                      panels: group["panels"].map { |panel| gen_panel((@panel_id += 1), @ | 
| 310 | 
            +
                      panels: group["panels"].map { |panel| gen_panel((@panel_id += 1), @datasource_ids[datasource], @notif_ids[notification], panel) }
         | 
| 305 311 | 
             
                    }
         | 
| 306 312 | 
             
                  }
         | 
| 307 313 |  | 
| @@ -335,7 +341,6 @@ module GrafanaRb | |
| 335 341 | 
             
                def apply
         | 
| 336 342 | 
             
                  check_config_existance
         | 
| 337 343 | 
             
                  die("missed grafana_url variable") unless config["grafana_url"]
         | 
| 338 | 
            -
                  die("missed prometheus_url variable") unless config["prometheus_url"]
         | 
| 339 344 |  | 
| 340 345 | 
             
                  tool_version = config["tool_version"] || GrafanaRb::VERSION
         | 
| 341 346 | 
             
                  if compare_versions(tool_version, GrafanaRb::VERSION) > 0
         | 
| @@ -343,8 +348,8 @@ module GrafanaRb | |
| 343 348 | 
             
                    exit -1
         | 
| 344 349 | 
             
                  end
         | 
| 345 350 |  | 
| 346 | 
            -
                   | 
| 347 | 
            -
                  create_or_update_slack_notifications | 
| 351 | 
            +
                  create_or_update_datasources
         | 
| 352 | 
            +
                  create_or_update_slack_notifications
         | 
| 348 353 | 
             
                  dashboards.each do |dashboard|
         | 
| 349 354 | 
             
                    create_or_update_dashboard(deep_clone(dashboard))
         | 
| 350 355 | 
             
                  end
         | 
    
        data/lib/grafana-rb/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: grafana-rb
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.15.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Alexey Vakhov
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: exe
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2017-04- | 
| 11 | 
            +
            date: 2017-04-11 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         |