Class: SkyEye::Exec
- Inherits:
-
Object
- Object
- SkyEye::Exec
- Defined in:
- lib/skyeye/exec.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #deregister_alarms_matching!(pattern) ⇒ Object
- #deregister_aws_alarms! ⇒ Object
- #deregister_instance_alarms! ⇒ Object
- #dimension_values_for_resource_type(resource_type) ⇒ Object
- #go(*args) ⇒ Object
- #instance_id ⇒ Object
- #kill!(e) ⇒ Object
- #load_or_register_topics! ⇒ Object
- #namespace_for_resource_type(resource_type) ⇒ Object
- #register_alarms_for_watches!(resource_type, dimension_values) ⇒ Object
- #register_aws_alarms! ⇒ Object
- #register_instance_alarms! ⇒ Object
- #shutdown! ⇒ Object
- #start! ⇒ Object
- #thread_for_watch(namespace, watch) ⇒ Object
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
9 10 11 |
# File 'lib/skyeye/exec.rb', line 9 def config @config end |
Instance Method Details
#deregister_alarms_matching!(pattern) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/skyeye/exec.rb', line 56 def deregister_alarms_matching!(pattern) cw = AWS::CloudWatch.new alarms = cw.client.describe_alarms to_delete = [] alarms.data[:metric_alarms].each do |alarm| if alarm[:alarm_name] =~ pattern to_delete << alarm[:alarm_name] end end if to_delete.size > 0 @logger.info "De-Register Alarms #{to_delete.inspect}" cw.alarms.delete(*to_delete) end end |
#deregister_aws_alarms! ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/skyeye/exec.rb', line 48 def deregister_aws_alarms! @config[:watches].keys.each do |resource_type| unless resource_type == :instance deregister_alarms_matching!(/^skyeye::#{resource_type}::/) end end end |
#deregister_instance_alarms! ⇒ Object
44 45 46 |
# File 'lib/skyeye/exec.rb', line 44 def deregister_instance_alarms! deregister_alarms_matching!(/^skyeye::instance::#{instance_id}::/) end |
#dimension_values_for_resource_type(resource_type) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/skyeye/exec.rb', line 81 def dimension_values_for_resource_type(resource_type) values = [] case resource_type when :elb AWS::ELB.new.load_balancers.each do |load_balancer| values << [{ name: "LoadBalancerName", value: load_balancer.name }] end when :rds AWS::RDS.new.db_instances.each do |db_instance| values << [{ name: "DBInstanceIdentifier", value: db_instance.db_instance_identifier }] end when :elasticache AWS::ElastiCache.new.client.describe_cache_clusters(show_cache_node_info: true).data[:cache_clusters].each do |cluster| cluster[:cache_nodes].each do |node| values << [{ name: "CacheClusterId", value: cluster[:cache_cluster_id] }, { name: "CacheNodeId", value: node[:cache_node_id] }] end end when :sqs AWS::SQS.new.queues.each do |queue| values << [{ name: "QueueName", value: queue.arn.split(/:/)[-1] }] end end values end |
#go(*args) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/skyeye/exec.rb', line 15 def go(*args) config_file = ENV["SKYEYE_CONFIG_FILE"] || "/etc/skyeye.yml" unless File.exists?(config_file) puts "Missing #{config_file}. You can set the path to this file by exporting SKYEYE_CONFIG_FILE." return end @config = YAML.load(File.read(config_file)) @logger = Logger.new(STDOUT) @mutex = Mutex.new if args.size == 0 start! elsif args[0] == "register:instance" load_or_register_topics! register_instance_alarms! elsif args[0] == "deregister:instance" deregister_instance_alarms! elsif args[0] == "deregister:aws" deregister_aws_alarms! elsif args[0] == "register:aws" load_or_register_topics! register_aws_alarms! else puts "valid commands: [de]register:(aws|instance)" end end |
#instance_id ⇒ Object
11 12 13 |
# File 'lib/skyeye/exec.rb', line 11 def instance_id @instance_id ||= open("http://169.254.169.254/latest/meta-data/instance-id").read end |
#kill!(e) ⇒ Object
190 191 192 193 194 |
# File 'lib/skyeye/exec.rb', line 190 def kill!(e) @mutex.synchronize do @killed_by = e end end |
#load_or_register_topics! ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/skyeye/exec.rb', line 280 def load_or_register_topics! sns = AWS::SNS.new current_topics = sns.client.list_topics arns = [] (@config[:topics] || ["skyeye-alerts"]).each do |topic_name| current_topic = current_topics[:topics].find do |topic| topic[:topic_arn].split(/:/)[-1] == topic_name end if current_topic arns << current_topic[:topic_arn] else arn = sns.topics.create(topic_name).arn arns << arn @logger.info "Created Topic #{arn}" end end @config[:arns] = arns end |
#namespace_for_resource_type(resource_type) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/skyeye/exec.rb', line 109 def namespace_for_resource_type(resource_type) case resource_type when :instance return "AWS/EC2" when :elb return "AWS/ELB" when :rds return "AWS/RDS" when :elasticache return "AWS/ElastiCache" when :sqs return "AWS/SQS" end end |
#register_alarms_for_watches!(resource_type, dimension_values) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/skyeye/exec.rb', line 133 def register_alarms_for_watches!(resource_type, dimension_values) cw = AWS::CloudWatch.new dimension_values.each do |dimension_value| @config[:watches][resource_type].each do |watch| [:warning, :critical].each do |threshold| target = dimension_value.map { |v| v[:value] }.join("_") alarm_name = "skyeye::#{resource_type}::#{target}::#{watch[:name]}-#{threshold}" alarm = cw.alarms[alarm_name] unless alarm.exists? is_command = !!watch[:command] raise "Cannot specify command for non-instance watch" if is_command && resource_type != :instance if is_command || watch[threshold] @logger.info "Register Alarm #{alarm_name}" cw.alarms.create(alarm_name, { namespace: is_command ? @config[:namespace] : namespace_for_resource_type(resource_type), metric_name: watch[:name], dimensions: dimension_value, comparison_operator: watch[:comparison_operator] || "GreaterThanThreshold", evaluation_periods: 1, period: (watch[:period] || @config[:alarm_periods][threshold] || 5) * 60, statistic: watch[:statistic] || "Maximum", threshold: is_command ? (threshold == :warning ? 0 : 1) : watch[threshold], insufficient_data_actions: (threshold == :critical && is_command ? @config[:arns] : []), actions_enabled: true, alarm_actions: @config[:arns], alarm_description: "skyeye: #{watch.to_json}", }) end end end end end end |
#register_aws_alarms! ⇒ Object
74 75 76 77 78 79 |
# File 'lib/skyeye/exec.rb', line 74 def register_aws_alarms! @config[:watches].each do |resource_type, watches| dimension_values = dimension_values_for_resource_type(resource_type) register_alarms_for_watches!(resource_type, dimension_values) if dimension_values end end |
#register_instance_alarms! ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/skyeye/exec.rb', line 124 def register_instance_alarms! cw = AWS::CloudWatch.new watches = @config[:watches][:instance] dimension_values = [[{ name: "InstanceId", value: instance_id }]] register_alarms_for_watches!(:instance, dimension_values) end |
#shutdown! ⇒ Object
274 275 276 277 278 |
# File 'lib/skyeye/exec.rb', line 274 def shutdown! @mutex.synchronize do @running = false end end |
#start! ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/skyeye/exec.rb', line 172 def start! @running = true @logger.info "SkyEye starting." namespace = @config[:namespace] || "skyeye" @threads = @config[:watches][:instance].map do |w| thread_for_watch(namespace, w) end.compact @threads.each(&:join) if @killed_by @logger.error(@killed_by.to_s + " " + @killed_by.backtrace.join("\n")) raise @killed_by end end |
#thread_for_watch(namespace, watch) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/skyeye/exec.rb', line 196 def thread_for_watch(namespace, watch) return nil if watch[:type] == "aws" raise "missing name" unless watch[:name] Thread.new do last_check = nil interval = (watch[:interval] || 5) * 60 do_check = lambda do |&block| if !last_check || (Time.now - last_check > interval) last_check = Time.now block.call end end begin loop do running = true @mutex.synchronize do running = @running && !@killed_by end break unless running do_check.call do @logger.info "[CHECK] [#{instance_id}] #{watch[:name]}::#{watch[:type]} #{watch[:command] || ""}" if watch[:command] command = watch[:command] = nil status = nil @mutex.synchronize do status = Open4::popen4(command) do |pid, stdin, stdout, stderr| = "#{watch[:name]} :: #{stdout.readlines.join(" ").gsub(/\n/, " ").chomp} :: #{stderr.readlines.join(" ").gsub(/\n/, " ").chomp}" end case status when 0 @logger.info when 1 @logger.warn when 2 @logger.error else @logger.warn end cw = AWS::CloudWatch.new.client cw.put_metric_data({ namespace: namespace, metric_data: [{ metric_name: watch[:name], dimensions: [{ :name => "InstanceId", :value => instance_id }], value: status.exitstatus, unit: "None" }] }) end else raise "Missing command or aws id for watch #{watch[:name]}" end end sleep 1 end rescue Exception => e kill!(e) end end end |