├── LICENSE ├── README ├── Rakefile ├── bin └── deckard ├── config └── deckard.yml └── lib ├── deckard.rb └── deckard ├── chef.rb ├── config.rb ├── ec2.rb ├── log.rb ├── monitoring.rb ├── stats.rb └── util.rb /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [2010] [Joseph Williams] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | deckard : http monitoring system 2 | 3 | deckard is a http check monitoring system built on top of CouchDB. 4 | 5 | license: apache 2 6 | 7 | Features: 8 | 9 | * Email and SMS based alerts (through email) 10 | * Designated on-call sms email address 11 | * Basic CouchDB replication latency alerts 12 | * Basic content check alerts 13 | * Content check alerts with EC2 elastic IP failover 14 | * All checks are defined in CouchDB (CRUD checks with ReST) 15 | * Alert priorities (log, email, SMS and notifo) 16 | * Simple setup via cron 17 | * Basic scheduling to silence alerts 18 | * Adjustable delay before firing check content requests 19 | * Basic Chef "tag" lookup support 20 | * Alert stats database for trending and analysis 21 | 22 | Usage: 23 | 24 | $ deckard --all ./deckard.yml 25 | 26 | You now have the option of running --all, --failover, --content, --replication if you only want to run a subset of checks 27 | 28 | Setup: 29 | 30 | * Setup and configure all appropriate databases and alert documents. 31 | * Create a crontab entry 32 | 33 | $ crontab -e 34 | */5 * * * * deckard --all /path/deckard.yml &> /dev/null 35 | 36 | 37 | Example documents: 38 | 39 | On Call document format: 40 | 41 | { 42 | "_id": "on_call_person", 43 | "sms_email": "8675309@jenny.net" 44 | "notifo_usernames" : ["jenny"] 45 | } 46 | 47 | For sms_email you will need to put in the phone number and sms to email host for your phone provider. Provide both an sms email and notifo username(s) and the sms will be only used for backup if something should go wrong with notifo. Saves you money on your text message bill! *You need the notifo application on your phone to use the notifo support. Note that notifo_usernames is an array of usernames so multiple people can get notifications. 48 | 49 | 50 | Failover check document format: 51 | 52 | { 53 | "_id": "lb01", 54 | "url": "http://somecheck.com/check.html", 55 | "secondary_instance_id": "i-1234", 56 | "priority": 2, 57 | "region": "us-east-1", 58 | "elastic_ip": "127.0.0.1", 59 | "content": "sometext", 60 | "failover": true, 61 | "primary_instance_id": "i-4321" 62 | } 63 | 64 | This document needs all the details to cause an elastic ip switch in the case the content is not found on the url. 65 | 66 | 67 | Replication check format: 68 | 69 | { 70 | "_id": "node01_node02", 71 | "name": "test", 72 | "master_url": "http://node01/db", 73 | "slave_url": "http://node02/db", 74 | "offset": 0, 75 | "priority": 1, 76 | "schedule": [ 77 | 2, 78 | 3 79 | ] 80 | } 81 | 82 | This will test the doc counts between two databases and if they become out of sync by more or less than the thresholds specified in the config an alert is triggered. 83 | 84 | 85 | HTTP content check format: 86 | 87 | { 88 | "_id": "deckard.com:5984/", 89 | "url": "http://deckard.com:5984/", 90 | "content": "couchdb", 91 | "priority": 2 92 | } 93 | 94 | 95 | For all of these priority and schedule are optional fields in these documents, priority is 0, 1 and 2. 0 is log only, 1 is log and email and 2 is log, email and sms. The schedule is an array containing integers for the hours the alert should be silent. Check out the replication check definition above. 96 | 97 | 98 | For Chef "tag" support you need to install a view in your chef database and configure the url to it in your Deckard config file. 99 | 100 | function(doc) { 101 | if (doc.chef_type != 'node') return; 102 | 103 | emit(doc.automatic.ec2 ? doc.automatic.ec2.public_hostname : doc.automatic.fqdn, doc.normal.tags[0]); 104 | } 105 | 106 | 107 | Regarding alert stats, just create a stats database and puts it's name in the config file. When alerts happen you should begin to see documents get added. By itself it isn't all that helpful, analysis is the key. Here is a basic "counts" design document to get you started, it will give you stats about the different kinds of alerts you are having. 108 | 109 | { 110 | "_id": "_design/counts", 111 | "language": "javascript", 112 | "views": { 113 | "by_type": { 114 | "map": "function(doc) {\n emit(doc.type, 1);\n}", 115 | "reduce": "_count" 116 | }, 117 | "by_url": { 118 | "map": "function(doc) {\n emit(doc.url, 1);\n}", 119 | "reduce": "_count" 120 | }, 121 | "by_error": { 122 | "map": "function(doc) {\n emit(doc.error, 1);\n}", 123 | "reduce": "_count" 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'rake/gempackagetask' 3 | require 'rake/rdoctask' 4 | 5 | spec = Gem::Specification.new do |s| 6 | s.name = "deckard" 7 | s.version = "0.5.12" 8 | s.author = "joe williams" 9 | s.email = "joe@joetify.com" 10 | s.homepage = "http://github.com/joewilliams/deckard" 11 | s.platform = Gem::Platform::RUBY 12 | s.summary = "a monitoring system built on couchdb" 13 | s.files = FileList["{bin,lib,config}/**/*"].to_a 14 | s.require_path = "lib" 15 | s.has_rdoc = true 16 | s.extra_rdoc_files = ["README"] 17 | %w{mixlib-config mixlib-log tmail json fog notifo}.each { |gem| s.add_dependency gem } 18 | s.add_dependency("rest-client", "= 1.3.0") 19 | s.bindir = "bin" 20 | s.executables = %w( deckard ) 21 | end 22 | 23 | Rake::GemPackageTask.new(spec) do |pkg| 24 | pkg.need_tar = true 25 | end 26 | 27 | Rake::RDocTask.new do |rd| 28 | rd.rdoc_files.include("lib/**/*.rb") 29 | end 30 | -------------------------------------------------------------------------------- /bin/deckard: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby 2 | require File.join(File.dirname(__FILE__), '..', 'lib', 'deckard') 3 | 4 | Deckard.main 5 | 6 | -------------------------------------------------------------------------------- /config/deckard.yml: -------------------------------------------------------------------------------- 1 | defaults: 2 | email_to: "my@email.com" 3 | email_from: "some@email.com" 4 | email_host: "localhost" 5 | db_user: 6 | db_password: 7 | db_host: "localhost" 8 | db_port: "5984" 9 | on_call_db: "monitor_on_call" 10 | on_call_doc: "on_call_person" 11 | log_file: 12 | 13 | content_check: 14 | retry_count: 1 15 | retry_interval: 5 16 | db: "monitor_content_check" 17 | delay: false 18 | delay_upper_bound: 10 19 | 20 | fo_check: 21 | retry_count: 1 22 | aws_key: "key" 23 | aws_secret: "seekrat" 24 | db: "monitor_fo_check" 25 | 26 | rep_check: 27 | doc_behind_threshold: 5000 28 | doc_ahead_threshold: -100 29 | db: "monitor_rep_check" 30 | 31 | notifo: 32 | user: "username" 33 | apikey: "password" 34 | 35 | stats: 36 | db: "monitor_stats" 37 | 38 | chef: 39 | enabled: true 40 | url:"http://localhost:5984/chef/_design/nodes/_view/hostnames" 41 | -------------------------------------------------------------------------------- /lib/deckard.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | 3 | gem 'rest-client', '1.3.0' 4 | require 'rest_client' 5 | require 'tmail' 6 | require 'json' 7 | require 'cgi' 8 | require 'net/smtp' 9 | require 'fog' 10 | require 'mixlib/log' 11 | require 'mixlib/config' 12 | require 'yaml' 13 | require 'notifo' 14 | 15 | __DIR__ = File.dirname(__FILE__) 16 | 17 | $LOAD_PATH.unshift __DIR__ unless 18 | $LOAD_PATH.include?(__DIR__) || 19 | $LOAD_PATH.include?(File.expand_path(__DIR__)) 20 | 21 | require 'deckard/config' 22 | require 'deckard/log' 23 | require 'deckard/ec2' 24 | require 'deckard/monitoring' 25 | require 'deckard/stats' 26 | require 'deckard/util' 27 | require 'deckard/chef' 28 | 29 | 30 | class Deckard 31 | 32 | def self.content_check 33 | retry_count = Deckard::Config.content_check_retry 34 | db_name = Deckard::Config.content_check_db 35 | delay = Deckard::Config.content_check_delay 36 | upper_bound = Deckard::Config.content_check_delay_upper_bound 37 | list = Array.new 38 | 39 | nodes = Deckard::Util.get_nodes(db_name) 40 | 41 | nodes.each do |node| 42 | run = Thread.new { 43 | Deckard::Monitor.content_check(node["url"], node["content"], node["priority"], retry_count, node["schedule"]) 44 | } 45 | if delay 46 | sleep(rand(upper_bound)) 47 | end 48 | list << run 49 | end 50 | 51 | list.each { |x| 52 | x.join 53 | } 54 | end 55 | 56 | def self.rep_check 57 | db_name = Deckard::Config.rep_check_db 58 | list = Array.new 59 | 60 | nodes = Deckard::Util.get_nodes(db_name) 61 | 62 | nodes.each do |node| 63 | run = Thread.new { 64 | Deckard::Monitor.rep_check(node["name"], node["master_url"], node["slave_url"], node["offset"], node["priority"], node["schedule"]) 65 | } 66 | list << run 67 | end 68 | 69 | list.each { |x| 70 | x.join 71 | } 72 | end 73 | 74 | def self.fo_check 75 | retry_count = Deckard::Config.fo_check_retry 76 | db_name = Deckard::Config.fo_check_db 77 | list = Array.new 78 | 79 | nodes = Deckard::Util.get_nodes(db_name) 80 | 81 | nodes.each do |node| 82 | run = Thread.new { 83 | check = Deckard::Monitor.content_check(node["url"], node["content"], node["priority"], retry_count, node["schedule"]) 84 | unless check 85 | Deckard::Monitor.failover(node["elastic_ip"], node["primary_instance_id"], node["secondary_instance_id"], node["priority"], node["schedule"], node["failover"], node["region"]) 86 | Deckard::Util.flip_failover(node) 87 | end 88 | } 89 | list << run 90 | end 91 | 92 | list.each { |x| 93 | x.join 94 | } 95 | end 96 | 97 | def self.main 98 | list = Array.new 99 | 100 | case ARGV[0] 101 | when "--all" 102 | list << Thread.new { content_check } 103 | list << Thread.new { rep_check } 104 | list << Thread.new { fo_check } 105 | when "--content" 106 | list << Thread.new { content_check } 107 | when "--replication" 108 | list << Thread.new { rep_check } 109 | when "--failover" 110 | list << Thread.new { fo_check } 111 | end 112 | 113 | list.each { |x| 114 | x.join 115 | } 116 | end 117 | 118 | end 119 | -------------------------------------------------------------------------------- /lib/deckard/chef.rb: -------------------------------------------------------------------------------- 1 | class Deckard 2 | class Chef 3 | 4 | def self.get_node_info(url) 5 | 6 | info = false 7 | 8 | if Deckard::Config.chef_enabled 9 | 10 | chef_url = Deckard::Config.chef_url 11 | 12 | begin 13 | chef_nodes_json = RestClient.get(chef_url) 14 | chef_nodes = JSON.parse(chef_nodes_json) 15 | 16 | chef_nodes["rows"].each do |row| 17 | if row["key"] == URI.parse(url).host 18 | info = row["value"] 19 | end 20 | end 21 | 22 | rescue Exception => e 23 | Deckard::Log.error("Could not get chef tag for alert on #{url} due to #{e}") 24 | end 25 | end 26 | 27 | info 28 | end 29 | 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /lib/deckard/config.rb: -------------------------------------------------------------------------------- 1 | class Deckard 2 | class Config 3 | 4 | if ARGV[0] 5 | monitor_config = YAML.load(File.open(ARGV[1])) 6 | else 7 | puts "No config file specified" 8 | abort 9 | end 10 | 11 | extend Mixlib::Config 12 | configure do |c| 13 | c[:email_to] = monitor_config["defaults"]["email_to"] 14 | c[:email_from] = monitor_config["defaults"]["email_from"] 15 | c[:email_host] = monitor_config["defaults"]["email_host"] 16 | c[:db_user] = monitor_config["defaults"]["db_user"] 17 | c[:db_password] = monitor_config["defaults"]["db_password"] 18 | c[:db_host] = monitor_config["defaults"]["db_host"] 19 | c[:db_port] = monitor_config["defaults"]["db_port"] 20 | c[:on_call_db] = monitor_config["defaults"]["on_call_db"] 21 | c[:on_call_doc] = monitor_config["defaults"]["on_call_doc"] 22 | c[:log_file] = monitor_config["defaults"]["log_file"] 23 | 24 | c[:content_check_retry] = monitor_config["content_check"]["retry_count"] 25 | c[:content_check_retry_interval] = monitor_config["content_check"]["retry_interval"] 26 | c[:content_check_db] = monitor_config["content_check"]["db"] 27 | c[:content_check_delay] = monitor_config["content_check"]["delay"] 28 | c[:content_check_delay_upper_bound] = monitor_config["content_check"]["delay_upper_bound"] 29 | 30 | c[:fo_check_retry] = monitor_config["fo_check"]["retry_count"] 31 | c[:aws_key] = monitor_config["fo_check"]["aws_key"] 32 | c[:aws_secret] = monitor_config["fo_check"]["aws_secret"] 33 | c[:fo_check_db] = monitor_config["fo_check"]["db"] 34 | 35 | c[:doc_behind_threshold] = monitor_config["rep_check"]["doc_behind_threshold"] 36 | c[:doc_ahead_threshold] = monitor_config["rep_check"]["doc_ahead_threshold"] 37 | c[:rep_check_db] = monitor_config["rep_check"]["db"] 38 | 39 | c[:notifo_user] = monitor_config["notifo"]["user"] 40 | c[:notifo_apikey] = monitor_config["notifo"]["apikey"] 41 | 42 | c[:stats_db] = monitor_config["stats"]["db"] 43 | 44 | c[:chef_url] = monitor_config["chef"]["url"] 45 | c[:chef_enabled] = monitor_config["chef"]["enabled"] 46 | 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/deckard/ec2.rb: -------------------------------------------------------------------------------- 1 | class Deckard 2 | class Ec2 3 | 4 | def self.get_association(region, elastic_ip) 5 | ec2 = ec2init(region) 6 | ec2.describe_addresses(elastic_ip).body["addressesSet"][0]["instanceId"] 7 | end 8 | 9 | def self.associate_address(region, instance_id, elastic_ip) 10 | ec2 = ec2init(region) 11 | ec2.associate_address(instance_id, elastic_ip).body["return"] 12 | end 13 | 14 | def self.disassociate_address(region, elastic_ip) 15 | ec2 = ec2init(region) 16 | ec2.disassociate_address(elastic_ip).body["return"] 17 | end 18 | 19 | def self.ec2init(region) 20 | aws_key = Deckard::Config.aws_key 21 | aws_secret = Deckard::Config.aws_secret 22 | 23 | Fog::AWS::Compute.new( 24 | :aws_access_key_id => aws_key, 25 | :aws_secret_access_key => aws_secret, 26 | :region => region) 27 | end 28 | 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /lib/deckard/log.rb: -------------------------------------------------------------------------------- 1 | class Deckard 2 | class Log 3 | extend Mixlib::Log 4 | log_file = Deckard::Config.log_file 5 | 6 | if log_file 7 | Deckard::Log.init(log_file) 8 | end 9 | Deckard::Log.level(:info) 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /lib/deckard/monitoring.rb: -------------------------------------------------------------------------------- 1 | class Deckard 2 | class Monitor 3 | 4 | def self.content_check(url, content, priority, retry_count, schedule) 5 | check = true 6 | begin 7 | retries = 1 unless retries 8 | result = RestClient.get(url) 9 | rescue Exception => e 10 | Deckard::Log.info("ALERT :: Could not connect to #{url}, retrying ...") 11 | sleep(Deckard::Config.content_check_retry_interval) 12 | retry if (retries += 1) < retry_count 13 | if retries >= retry_count 14 | 15 | node_info = Deckard::Chef.get_node_info(url) 16 | 17 | if node_info 18 | subject = "ALERT :: #{node_info} :: #{e}" 19 | body = "#{Time.now} :: #{url}" 20 | else 21 | subject = "ALERT :: #{url} :: #{e}" 22 | body = "#{Time.now} :: #{url} :: #{e}" 23 | end 24 | 25 | log = subject + " -- " + body 26 | Deckard::Util.alert(priority, subject, body, log, schedule, url) 27 | Deckard::Stats.alert(priority, e, url, "contentcheck") 28 | check = false 29 | end 30 | else 31 | retries = 1 32 | if result.include?(content) 33 | Deckard::Log.info("PASS :: Found text \"#{content}\" on #{url}") 34 | else 35 | 36 | node_info = Deckard::Chef.get_node_info(url) 37 | 38 | if node_info 39 | subject = "ALERT :: #{node_info} :: content missing" 40 | body = "#{Time.now} :: Could not find text \"#{content}\" at #{url}" 41 | else 42 | subject = "ALERT :: #{url} :: content missing" 43 | body = "#{Time.now} :: Could not find text \"#{content}\" at #{url}" 44 | end 45 | 46 | log = subject + " -- " + body 47 | Deckard::Util.alert(priority, subject, body, log, schedule, url) 48 | Deckard::Stats.alert(priority, "nocontent", url, "contentcheck") 49 | check = false 50 | end 51 | end 52 | check 53 | end 54 | 55 | def self.rep_check(name, master_url, slave_url, offset, priority, schedule) 56 | begin 57 | doc_behind_threshold = Deckard::Config.doc_behind_threshold 58 | doc_ahead_threshold = Deckard::Config.doc_ahead_threshold 59 | 60 | master_result = RestClient.get(master_url) 61 | slave_result = RestClient.get(slave_url) 62 | 63 | master_doc_count = JSON.parse(master_result)["doc_count"] 64 | slave_doc_count = JSON.parse(slave_result)["doc_count"] 65 | doc_count_diff = master_doc_count - slave_doc_count + offset 66 | 67 | if doc_count_diff >= doc_behind_threshold || doc_count_diff < doc_ahead_threshold 68 | subject = "ALERT :: Replication for #{name}" 69 | body = "Master: #{master_url} => Slave: #{slave_url} : off by #{doc_count_diff}" 70 | log = subject + " -- " + body 71 | Deckard::Util.alert(priority, subject, body, log, schedule, master_url) 72 | Deckard::Stats.alert(priority, "#{doc_count_diff}", url, "replication") 73 | else 74 | Deckard::Log.info("PASS :: Replication for #{name} is OK (#{doc_count_diff})") 75 | end 76 | rescue 77 | # do nothing 78 | end 79 | end 80 | 81 | def self.failover(elastic_ip, primary_instance_id, secondary_instance_id, priority, schedule, failover, region) 82 | if failover 83 | begin 84 | subject = "ALERT :: #{elastic_ip} attempting failover!" 85 | body = "#{region} : #{elastic_ip} => #{primary_instance_id} / #{secondary_instance_id} attempting failover!" 86 | log = subject + " " + body 87 | Deckard::Util.alert(priority, subject, body, log, schedule, "http://#{elastic_ip}") 88 | Deckard::Stats.alert(priority, "unknown", "http://#{elastic_ip}", "failover") 89 | 90 | instance_id = Deckard::Ec2.get_association(region, elastic_ip) 91 | 92 | if Deckard::Ec2.disassociate_address(region, elastic_ip) 93 | Deckard::Log.info("ALERT :: Disassociated #{elastic_ip}") 94 | else 95 | Deckard::Log.info("ALERT :: Could not disassociate #{elastic_ip}") 96 | Deckard::Util.alert(priority, "ALERT :: Could not disassociate #{elastic_ip}", "ALERT :: Could not disassoci 97 | ate #{elastic_ip} - #{region}", log, schedule, "http://#{elastic_ip}") 98 | end 99 | 100 | if instance_id == primary_instance_id 101 | if Deckard::Ec2.associate_address(region, secondary_instance_id, elastic_ip) 102 | info = "ALERT :: associated #{elastic_ip} to #{secondary_instance_id}" 103 | Deckard::Log.info("ALERT :: associated #{elastic_ip} to #{secondary_instance_id}") 104 | subject = "ALERT :: Failover Complete for #{elastic_ip} #{secondary_instance_id}" 105 | body = "VERIFY THINGS ARE WORKING! #{elastic_ip} => #{primary_instance_id} / #{secondary_instance_id}" 106 | Deckard::Util.alert(priority, subject, body, subject, schedule, "http://#{elastic_ip}") 107 | else 108 | info = "ALERT :: Could not associate #{elastic_ip}" 109 | Deckard::Log.info(info) 110 | Deckard::Util.alert(priority, info, info, log, schedule, "http://#{elastic_ip}") 111 | end 112 | 113 | elsif instance_id == secondary_instance_id 114 | if Deckard::Ec2.associate_address(region, primary_instance_id, elastic_ip) 115 | info = "ALERT :: associated #{elastic_ip} to #{secondary_instance_id}" 116 | Deckard::Log.info("ALERT :: associated #{elastic_ip} to #{secondary_instance_id}") 117 | subject = "ALERT :: Failover Complete for #{elastic_ip} #{secondary_instance_id}" 118 | body = "VERIFY THINGS ARE WORKING! #{elastic_ip} => #{primary_instance_id} / #{secondary_instance_id}" 119 | Deckard::Util.alert(priority, subject, body, subject, schedule, "http://#{elastic_ip}") 120 | else 121 | info = "ALERT :: Could not associate #{elastic_ip}" 122 | Deckard::Log.info(info) 123 | Deckard::Util.alert(priority, info, info, log, schedule, "http://#{elastic_ip}") 124 | end 125 | 126 | else 127 | error = "ALERT :: Could not a failover #{elastic_ip} => #{primary_instance_id} / #{secondary_instance_id}!!" 128 | log = "ALERT :: Could not a failover #{elastic_ip} => #{primary_instance_id} / #{secondary_instance_id}!! Due to instance_id != primary and secondary" 129 | Deckard::Util.alert(priority, error, error, log, schedule, "http://#{elastic_ip}") 130 | end 131 | rescue Exception => e 132 | error = "ALERT :: Could not a failover #{elastic_ip} => #{primary_instance_id} / #{secondary_instance_id}!!" 133 | log = "ALERT :: Could not a failover #{elastic_ip} => #{primary_instance_id} / #{secondary_instance_id}!!" 134 | Deckard::Log.error(e) 135 | Deckard::Util.alert(priority, error, error, log, schedule, "http://#{elastic_ip}") 136 | end 137 | else 138 | # dont failover 139 | Deckard::Log.info("ALERT :: not failing over due to failover=false") 140 | end 141 | end 142 | 143 | end 144 | end 145 | -------------------------------------------------------------------------------- /lib/deckard/stats.rb: -------------------------------------------------------------------------------- 1 | class Deckard 2 | class Stats 3 | 4 | def self.alert(priority, error, url, type) 5 | db_name = Deckard::Config.stats_db 6 | db_user = Deckard::Config.db_user 7 | db_password = Deckard::Config.db_password 8 | db_host = Deckard::Config.db_host 9 | db_port = Deckard::Config.db_port 10 | 11 | db_url = "http://#{db_user}:#{db_password}@#{db_host}:#{db_port}/#{db_name}" 12 | 13 | alert_info = {"priority" => priority, "error" => error, "url" => url, "timestamp" => Time.now.utc.iso8601, "type" => type} 14 | 15 | begin 16 | RestClient.post("#{db_url}", alert_info.to_json, "Content-Type" => "application/json") 17 | Deckard::Log.info("Added stats for alert on #{url}") 18 | rescue Exception => e 19 | Deckard::Log.error("Could not add stats for alert on #{url} due to #{e}") 20 | end 21 | end 22 | 23 | end 24 | end -------------------------------------------------------------------------------- /lib/deckard/util.rb: -------------------------------------------------------------------------------- 1 | class Deckard 2 | class Util 3 | def self.get_nodes(db_name) 4 | doc_list = [] 5 | node_list = [] 6 | 7 | db_user = Deckard::Config.db_user 8 | db_password = Deckard::Config.db_password 9 | db_host = Deckard::Config.db_host 10 | db_port = Deckard::Config.db_port 11 | 12 | if db_user && db_password 13 | db_url = "http://#{db_user}:#{db_password}@#{db_host}:#{db_port}/#{db_name}" 14 | else 15 | db_url = "http://#{db_host}:#{db_port}/#{db_name}" 16 | end 17 | 18 | all_docs = RestClient.get("#{db_url}/_all_docs") 19 | 20 | all_docs_hash = JSON.parse(all_docs) 21 | 22 | all_docs_hash["rows"].each do |doc| 23 | doc_list << doc["id"] 24 | end 25 | 26 | doc_list.each do |doc| 27 | escaped_doc = CGI.escape(doc) 28 | node_json = RestClient.get("#{db_url}/#{escaped_doc}") 29 | node = JSON.parse(node_json) 30 | node_list << node 31 | end 32 | node_list 33 | end 34 | 35 | def self.alert(priority, subject, body, log, schedule, url) 36 | email_to = Deckard::Config.email_to 37 | on_call_contacts = on_call() 38 | 39 | # if scheduled maintenance set to logging only 40 | if schedule(schedule) == true 41 | priority = 0 42 | end 43 | 44 | if priority == 0 45 | Deckard::Log.info(log) 46 | elsif priority == 1 47 | Deckard::Log.info(log) 48 | Deckard::Log.info("sending email alert to #{email_to}") 49 | send_email(email_to, subject, body) 50 | elsif priority == 2 51 | Deckard::Log.info(log) 52 | Deckard::Log.info("sending email alert to #{email_to}") 53 | send_email(email_to, subject, body) 54 | begin 55 | if on_call_contacts.has_key?("notifo_usernames") 56 | Deckard::Log.info("sending notifo alert to #{on_call_contacts["notifo_usernames"]}") 57 | send_notifo(on_call_contacts["notifo_usernames"], subject, url) 58 | else 59 | Deckard::Log.info("sending email alert to #{email_to} and sms to #{on_call_contacts["sms_email"]}") 60 | send_email(email_to, subject, body) 61 | send_email("#{on_call_contacts["sms_email"]}", subject, body) 62 | end 63 | rescue 64 | Deckard::Log.info("sending email alert to #{email_to} and sms to #{on_call_contacts["sms_email"]}") 65 | send_email(email_to, subject, body) 66 | send_email("#{on_call_contacts["sms_email"]}", subject, body) 67 | end 68 | end 69 | end 70 | 71 | def self.on_call 72 | db_user = Deckard::Config.db_user 73 | db_password = Deckard::Config.db_password 74 | db_host = Deckard::Config.db_host 75 | db_port = Deckard::Config.db_port 76 | db_name = Deckard::Config.on_call_db 77 | doc_name = Deckard::Config.on_call_doc 78 | 79 | doc_url = "http://#{db_user}:#{db_password}@#{db_host}:#{db_port}/#{db_name}/#{doc_name}" 80 | 81 | on_call_json = RestClient.get doc_url 82 | on_call = JSON.parse(on_call_json) 83 | on_call 84 | end 85 | 86 | def self.send_email(email_addr, subject, body) 87 | to = email_addr 88 | from = Deckard::Config.email_from 89 | host = Deckard::Config.email_host 90 | mail = TMail::Mail.new 91 | mail.to = to 92 | mail.from = from 93 | mail.subject = subject 94 | mail.date = Time.now 95 | mail.mime_version = '1.0' 96 | mail.body = body 97 | 98 | Net::SMTP.start( host ) do |smtp| 99 | smtp.send_message( 100 | mail.to_s, 101 | from, 102 | to 103 | ) 104 | end 105 | end 106 | 107 | def self.send_notifo(usernames, subject, url) 108 | notifo = Notifo.new(Deckard::Config.notifo_user, Deckard::Config.notifo_apikey) 109 | usernames.each do |username| 110 | response = notifo.post(username, subject, "deckard alert", url) 111 | Deckard::Log.info("Notifo response: #{response}") 112 | end 113 | end 114 | 115 | def self.schedule(schedule) 116 | if schedule.nil? 117 | false 118 | else 119 | schedule.include? Time.now.hour 120 | end 121 | end 122 | 123 | def self.flip_failover(node) 124 | db_user = Deckard::Config.db_user 125 | db_password = Deckard::Config.db_password 126 | db_host = Deckard::Config.db_host 127 | db_port = Deckard::Config.db_port 128 | db_name = Deckard::Config.fo_check_db 129 | 130 | if db_user == "" || db_password == "" 131 | db_url = "http://#{db_host}:#{db_port}/#{db_name}" 132 | else 133 | db_url = "http://#{db_user}:#{db_password}@#{db_host}:#{db_port}/#{db_name}" 134 | end 135 | 136 | doc_json = RestClient.get("#{db_url}/#{node["_id"]}") 137 | doc = JSON.parse(doc_json) 138 | doc.store("failover", false) 139 | 140 | RestClient.put("#{db_url}/#{node["_id"]}?rev=#{node["_rev"]}", doc.to_json) 141 | end 142 | 143 | end 144 | end 145 | --------------------------------------------------------------------------------