Created
March 25, 2013 11:27
-
-
Save holysugar/5236512 to your computer and use it in GitHub Desktop.
datacounter でステータスごとに分かれたグラフを重ねたグラフを自動的に生成(ついでに色も調整)するスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # | |
| #= このスクリプトの用途 | |
| # | |
| # Stacked グラフが生成されていないステータスコード別のグラフ( /\dxx_count/ )を見つけて | |
| # - 色を修正し | |
| # - 指定した場所に stacked グラフを作成する | |
| # | |
| # - 今回は生成されるグラフは 同じサービス名.accesses.同じパス名_all_status という名前にする | |
| # | |
| # require Ruby 2.0.0 or later | |
| require 'growthforecast' | |
| require 'logger' | |
| class ConfigAccessChart | |
| DEFAULT_COLORS = { | |
| '2xx' => '#88e02e', # green | |
| '3xx' => '#33ccff', # skyblue | |
| '4xx' => '#333333', # gray | |
| '5xx' => '#cc0000', # red | |
| } | |
| def initialize(url, port: 80, colors: nil, service_name: nil, section_name: nil, graph_name_suffix: nil, logger: nil) | |
| @cli = GrowthForecast.new(url, port) | |
| @colors = colors || DEFAULT_COLORS.dup | |
| @service_name = service_name | |
| @section_name = section_name | |
| @graph_name_suffix = graph_name_suffix | |
| @logger = logger # FIXME give me NullLogger | |
| end | |
| def search_graphs_having_status_code | |
| @cli.graphs.find_all{|g| g.graph_name =~ /2xx_count/ } | |
| end | |
| def complax_graph_name(graph, suffix) | |
| graph.graph_name.sub(/\dxx_.*\z/, suffix || 'all_status') | |
| end | |
| def having_same_graph_of_all_status_count_graph?(graph, service_name: graph.service_name, section_name: graph.section_name, graph_name_suffix: 'all_status') | |
| graph_name = complax_graph_name(graph, graph_name_suffix) | |
| @cli.complexes.any?{|c| c.service_name == service_name && c.section_name == section_name && c.graph_name == graph_name } | |
| end | |
| def graphs_to_process | |
| search_graphs_having_status_code.find_all do |g| | |
| service_name = @service_name || g.service_name | |
| section_name = @section_name || g.section_name | |
| ! having_same_graph_of_all_status_count_graph?(g, service_name: service_name, section_name: section_name, graph_name_suffix: @graph_name_suffix) | |
| end | |
| end | |
| def status_graph_table(status_graph) | |
| service_name = status_graph.service_name | |
| section_name = status_graph.section_name | |
| graph_name_template = status_graph.graph_name | |
| @colors.map do |status, color| | |
| graph_name = graph_name_template.sub(/\dxx/, status) | |
| graph = @cli.by_name(service_name, section_name, graph_name) | |
| [graph, status, color] | |
| end.compact | |
| end | |
| def set_color(graph) | |
| status_graph_table(graph).each do |graph, status, color| | |
| graph.color = color | |
| result = @cli.edit(graph) | |
| @logger.info "graph #{graph.service_name}.#{graph.section_name}.#{graph.graph_name} color is changed to #{color}" if @logger | |
| end | |
| end | |
| def create_complex_graph(graph) | |
| # add_complex(service, section, graph_name, description, sumup, sort, type, gmode, stack, data_graph_ids) | |
| service_name = @service_name || graph.service_name | |
| section_name = @section_name || graph.section_name | |
| graph_name = complax_graph_name(graph, @graph_name_suffix) | |
| @cli.add_complex(service_name, section_name, graph_name, 'all status complex graph', true, 0, 'AREA', 'gauge', true, status_graph_table(graph).map{|list| list[0].id }) | |
| @logger.info "add complex graph #{service_name}.#{section_name}.#{graph_name}." if @logger | |
| end | |
| end | |
| if __FILE__ == $0 | |
| hostname = ARGV[0] || ($stderr.puts "usage: #{$0} hostname"; exit 1) | |
| logger = Logger.new($stdout) | |
| logger.level = Logger::DEBUG | |
| c = ConfigAccessChart.new(ARGV[0], section_name: 'accesses', logger: logger) | |
| c.graphs_to_process.each do |g| | |
| c.set_color(g) | |
| c.create_complex_graph(g) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment