Class: PingdomToGraphite::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/pingdom-to-graphite/cli.rb

Instance Method Summary collapse

Instance Method Details

#adviceObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pingdom-to-graphite/cli.rb', line 78

def advice
  load_config!
  total_checks = @config["pingdom"]["checks"].count
  calls_per_check = 2 + (total_checks)
  puts "You have #{total_checks} monitored checks. Given a 48000/day API limit:"
  every_minutes = 5
  begin
    daily_calls = 60*24 / every_minutes * calls_per_check
    puts "Every #{every_minutes} Minutes: #{daily_calls}/day - #{daily_calls < 48000 ? "WORKS" : "won't work"}"
    every_minutes += 5
  end until (daily_calls < 48000)
end

#backfill(check_id) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/pingdom-to-graphite/cli.rb', line 164

def backfill(check_id)
  load_config!
  load_state!
  # Check the state file
  if @state.has_key?(check_id) && @state[check_id].has_key?("earliest_ts")
    earliest_ts = @state[check_id.to_s]["earliest_ts"]
  else
    error("You can't backfill a check you've never run an update on.")
  end
  load_probe_list!
  load_check_list!
  datapull = get_datapull
  chunk = 10
  unless limit = options.limit
    limit = ask("You have #{datapull.effective_limit} API calls remaining. How many would you like to use?").to_i
  end
  created_ts = datapull.check(check_id).created

  # Keep within the API limits
  working_towards = (earliest_ts - created_ts) > 2678400 ? 31.days.ago.to_i : created_ts
  puts "Backfilling from #{Time.at(earliest_ts)} working towards #{Time.at(working_towards)}. Check began on #{Time.at(created_ts)}"
  # Break it into chunks
  additions = 0
  (limit.to_i.div(chunk)+1).times do
    batch_count = pull_and_push(check_id, working_towards, earliest_ts, chunk)
    puts "#{batch_count} metrics pushed in this batch." if options.verbose
    additions += batch_count
  end
  puts "#{additions} metrics sent to graphite for check #{check_id}."
end

#initObject



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
# File 'lib/pingdom-to-graphite/cli.rb', line 31

def init
  config_path = File.expand_path(options.config)

  if File.exists?(config_path)
    error("Config file already exists. (#{options.config})")
  else

    # Make sure we have a directory to put the config in
    unless File.directory?(File.dirname(config_path))
      FileUtils.mkdir_p(File.dirname(config_path), :mode => 0700)
    end

    # A nice little defaults file.
    settings = {
      "pingdom"   => {
        "username"  => "YOUR_USERNAME",
        "password"  => "YOUR_PASSWORD",
        "key"       => "YOUR_API_KEY",
        "checks"    => ["CHECK_ID_1","CHECK_ID_2"]
      },
      "graphite"  => {
        "host"  => "YOUR_SERVER",
        "port"    => "2003",
        "prefix"  => "pingdom"
      }
    }
    File.open(File.expand_path(options.config),"w",0600) do |f|
      f.write(JSON.pretty_generate(settings))
    end

  end

end

#init_checks(check_regex = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/pingdom-to-graphite/cli.rb', line 66

def init_checks(check_regex=nil)
  @check_regex = check_regex
  load_config!
  load_check_list!
  @config["pingdom"]["checks"] = @checks.keys
  File.open(File.expand_path(options.config),"w",0600) do |f|
    f.write(JSON.pretty_generate(@config))
  end
  puts "Added #{@checks.count} checks to #{options.config}"
end

#listObject



92
93
94
95
96
97
# File 'lib/pingdom-to-graphite/cli.rb', line 92

def list
  load_check_list!
  @checks.each do |check_id, check|
    puts "#{check.name} (#{check.id}) - #{check.status}"
  end
end

#probesObject



100
101
102
103
104
105
# File 'lib/pingdom-to-graphite/cli.rb', line 100

def probes
  load_probe_list!
  @probes.each do |probe_id, probe|
    puts "#{probe.countryiso} - #{probe.city}"
  end
end

#results(check_id) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/pingdom-to-graphite/cli.rb', line 118

def results(check_id)
  load_config!
  load_probe_list!
  start_time = (options.start_time) ? DateTime.parse(options.start_time).to_i : Time.now.to_i - 3600
  end_time = (options.end_time) ? DateTime.parse(options.end_time).to_i : Time.now.to_i
  if start_time - end_time > 2764800
    error("Date range must be less then 32 days.")
  end
  datapull = get_datapull
  datapull.results(check_id, start_time, end_time).each do |result|
    #<Pingdom::Result probeid: 33 time: 1343945109 status: "up" responsetime: 1103 statusdesc: "OK" statusdesclong: "OK">
    puts "#{Time.at(result.time)}: #{result.status} - #{result.responsetime}ms (#{@probes[result.probeid].name})"
  end
  puts datapull.friendly_limit
end

#updateObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pingdom-to-graphite/cli.rb', line 136

def update
  load_config!
  load_state!
  load_probe_list!
  load_check_list!
  datapull = get_datapull

  @config["pingdom"]["checks"].each do |check_id|
    puts "Check #{check_id}: " if options.verbose
    # Check the state file
    check_state = @state.has_key?(check_id.to_s) ? @state[check_id.to_s] : Hash.new
    latest_ts = check_state.has_key?("latest_ts") ? check_state["latest_ts"] : 1.hour.ago.to_i
    # API limits to 2764800 seconds, so we'll use that (minutes 30 seconds)
    limit_ts = 2764770.seconds.ago.to_i
    latest_ts = (latest_ts.to_i < limit_ts) ? limit_ts : latest_ts
    new_records = pull_and_push(check_id, latest_ts)
    puts "#{new_records} metrics sent to graphite for check #{check_id}."
  end
  puts datapull.friendly_limit
end