├── .gitignore ├── .travis.yml ├── Gemfile ├── LICENSE ├── README.md ├── bin └── metis-server ├── lib ├── metis.rb └── metis │ ├── application.rb │ ├── check_definition.rb │ ├── check_definition_list.rb │ ├── client.rb │ ├── configuration.rb │ ├── context.rb │ ├── mixin │ ├── from_file.rb │ └── params_validate.rb │ ├── nrpe_packet.rb │ ├── provider.rb │ ├── server.rb │ └── version.rb ├── metis.gemspec └── spec ├── client_spec.rb ├── context_spec.rb ├── nrpe_packet_spec.rb ├── provider_spec.rb ├── resources └── sample │ ├── checks │ ├── examples.rb │ └── others.rb │ └── metis.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | .rvmrc 2 | .ruby-version 3 | .rbenv-gemsets 4 | *.gem 5 | coverage 6 | coverage.data 7 | Gemfile.lock 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | script: rspec 3 | rvm: 4 | - 1.8.7 5 | - 1.9.2 6 | - 1.9.3 7 | - 2.0.0 8 | branches: 9 | only: 10 | - master -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gemspec 4 | 5 | group :test do 6 | gem "rspec" 7 | gem "simplecov" 8 | end 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Metis 2 | ===== 3 | 4 | Metis is an implementation of the Nagios NRPE daemon in Ruby. It provides an easy framework to write your own monitors in Ruby, have them running in a daemon, and distribute/share them with others. 5 | 6 | Goals 7 | ----- 8 | 9 | Metis is built around the idea of: 10 | 11 | * **Monitors in Ruby** 12 | Why? Ruby is a great language with a rich ecosystem of users, gems/libraries, and culture of testing. The existing ecosystem of Nagios monitors has a lot of variance in what language they're in. Some are bash, python, perl, etc. That is an awesome strength, but also means less commonality. 13 | * **Testable monitors** 14 | We test our applications don't we? Our monitors are what are supposed to be making sure our applications and servers are running correctly. They should be tested too. And they should be distributed with tests as well. You're running a monitor from a 3rd party on all yours servers... do you have full confidence it was written well and is bug free? 15 | * **Easy distribution of monitors** 16 | Nagios has a great community and tons of available monitors for you to grab. But grabbing monitors others have written can be hairy. They can have varying dependencies such as modules from CPAN in Perl, or EasyInstall in Python. If you don't know those languages, can be easily confused. They have varying requirements, such as a check in python require v2.7 while your OS release only has v2.6. Metis focuses on building in dependency handling and any framework to describe the configuration of the checks. 17 | * **Easy deployment** 18 | Metis works to cleanly separate monitor definition from configuration. It utilizes a simple ruby DSL modeled after [Chef](http://www.opscode.com/chef/) for configuration of monitor parameters (username/passwords, warning/critical thresholds) as well as the monitor definition itself. It also strives for easy integration with chef-server, so that the two can work hand-in-hand for self configuration. 19 | * **Making monitors simple** 20 | If you've ever written any of your own Nagios monitors, there can sometimes be a lot of setup. Beyond just performing the check, you might also need to parse command-line parameters, remembering exit codes, and ensuring the proper messages get propagated. Its wasted time and effort. Metis provides a quick and simple way to define the output of your monitor and returns the most important parts. 21 | 22 | Installation 23 | ------------ 24 | 25 | Installing Metis is a simple matter of running: 26 | 27 | ``` 28 | gem install metis 29 | metis-server 30 | ``` 31 | 32 | Boom, you're up and ready... though you won't have any monitors defined. 33 | 34 | 35 | Defining Monitors 36 | ----------------- 37 | 38 | Monitors are defined as `define` blocks containing configuration attributes and an `execute` block that defines what to actually do. The checks are defined in files under `checks/*.rb` from the working directory by default. 39 | 40 | A simple monitor might be: 41 | 42 | ```ruby 43 | define :simple do 44 | execute do 45 | "Hello World" 46 | end 47 | end 48 | ``` 49 | 50 | You can set the result of the monitor using `critical()`, `warn()`, or `ok()`. By default, Metis will assume the monitor is OK and if the `execute` block returns a string, set it as the message. 51 | 52 | ```ruby 53 | define :eod do 54 | execute do 55 | warn("Getting close to the end of the day") if Time.now.hour >= 21 56 | critical("Real close now!") if Time.now.hour >= 23 57 | ok("We're all good") 58 | end 59 | end 60 | ``` 61 | 62 | Monitors can define attributes that can be configured outside of the monitor logic itself using the `attribute` keyword. They are then accessible within the monitor through a `params` hash. For instance, to make a configurable warning/critical threshold: 63 | 64 | ```ruby 65 | define :eod do 66 | attribute :warning, :default => 21 67 | attribute :critical, :default => 23 68 | execute do 69 | warn("Getting close to the end of the day") if Time.now.hour >= params[:warning] 70 | critical("Real close now!") if Time.now.hour >= params[:critical] 71 | ok("We're all good") 72 | end 73 | end 74 | ``` 75 | 76 | How to set these will be covered in the next section. 77 | 78 | Monitors can also define external libraries or gems they might be dependent on using `require_gem`. These will only be required when the monitor is triggered, return a critical result and message if not found, and soon be installed as a part of the deployment process. 79 | 80 | ```ruby 81 | define :check_mysql do 82 | require_gem 'mysql' 83 | execute do 84 | # Connect to mysql and query it 85 | end 86 | end 87 | ``` 88 | 89 | Configuring Monitors 90 | -------------------- 91 | 92 | By default, Metis will look for a `config.rb` file in the working directory that should contain all the extra configuration settings for monitors. Building on the `:eod` example from the last section, you could configure its alert thresholds using the `configure` block: 93 | 94 | ```ruby 95 | configure :eod do 96 | warning 21 97 | critical 23 98 | end 99 | ``` 100 | 101 | If you defined a more advanced monitor that required username/passwords to connect to a resource, you could include all of those: 102 | 103 | ```ruby 104 | configure :check_mysql do 105 | username "foo" 106 | password "bar" 107 | port 3306 108 | end 109 | ``` 110 | 111 | Testing Monitors 112 | ---------------- 113 | 114 | Helpers for writing tests against your monitors will be coming soon. 115 | 116 | 117 | Contributing 118 | ------------ 119 | 120 | * Fork the project. 121 | * Make your feature addition or bug fix. 122 | * Add tests for it. This is important. 123 | * Commit, do not mess with Rakefile, version, or history. 124 | (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull) 125 | * Send me a pull request. Bonus points for topic branches. 126 | 127 | Copyright 128 | --------- 129 | 130 | Copyright (c) 2011 Ken Robertson. See LICENSE for details. 131 | -------------------------------------------------------------------------------- /bin/metis-server: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # -*- encoding: binary -*- 3 | 4 | require 'rubygems' 5 | $:.unshift(File.join(File.dirname(__FILE__), "..", "lib")) 6 | require 'metis' 7 | require 'metis/application' 8 | 9 | require 'optparse' 10 | 11 | options = {} 12 | 13 | opts = OptionParser.new("", 24, ' ') do |opts| 14 | cmd = File.basename($0) 15 | opts.banner = "Usage: #{cmd} [options]" 16 | opts.separator "Ruby options:" 17 | 18 | opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do 19 | $DEBUG = true 20 | end 21 | 22 | opts.on("-w", "--warn", "turn warnings on for your script") do 23 | $-w = true 24 | end 25 | 26 | opts.on("-I", "--include PATH", 27 | "specify $LOAD_PATH (may be used more than once)") do |path| 28 | $LOAD_PATH.unshift(*path.split(/:/)) 29 | end 30 | 31 | opts.on("-r", "--require LIBRARY", 32 | "require the library, before executing your script") do |library| 33 | require library 34 | end 35 | 36 | opts.separator "#{cmd} options:" 37 | 38 | opts.on("-c", "--config-file FILE", "Config file") do |f| 39 | options[:config_file] = f 40 | end 41 | 42 | opts.on("-D", "--daemonize", "Run daemonized in the background") do |d| 43 | options[:daemonize] = !!d 44 | end 45 | 46 | opts.on("-p", "--pid-file FILE", "Pid file") do |f| 47 | options[:pid_file] = f 48 | end 49 | 50 | opts.on("-l", "--log-file FILE", "Log file") do |f| 51 | options[:log_file] = f 52 | end 53 | 54 | opts.separator "Common options:" 55 | 56 | opts.on_tail("--help", "Show this message") do 57 | puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '') 58 | exit 59 | end 60 | 61 | opts.on_tail("-v", "--version", "Show version") do 62 | puts "#{cmd} v#{Metis::VERSION}" 63 | exit 64 | end 65 | 66 | opts.parse! ARGV 67 | end 68 | 69 | Metis::Application.new(options).run 70 | -------------------------------------------------------------------------------- /lib/metis.rb: -------------------------------------------------------------------------------- 1 | class Metis 2 | STATUS_OK = 0 3 | STATUS_WARNING = 1 4 | STATUS_CRITICAL = 2 5 | end 6 | 7 | require 'metis/version' 8 | require 'metis/mixin/from_file' 9 | require 'metis/mixin/params_validate' 10 | require 'metis/context' 11 | require 'metis/check_definition' 12 | require 'metis/check_definition_list' 13 | require 'metis/provider' 14 | require 'metis/nrpe_packet' 15 | require 'metis/server' 16 | require 'metis/client' 17 | require 'metis/configuration' 18 | -------------------------------------------------------------------------------- /lib/metis/application.rb: -------------------------------------------------------------------------------- 1 | class Metis::Application 2 | def initialize(opts={}) 3 | @options = opts 4 | end 5 | 6 | def run 7 | if @options[:daemonize] 8 | daemonize! 9 | else 10 | start 11 | end 12 | end 13 | 14 | def start 15 | # write pid file 16 | File.open(@options[:pid_file], 'w') { |f| f.write(Process.pid.to_s) } if @options[:pid_file] 17 | 18 | # load context 19 | context = Metis::Context.new(@options[:config_file]) 20 | context.load 21 | 22 | # fire up server 23 | server = Metis::Server.new(context) 24 | server.start 25 | server.run 26 | end 27 | 28 | def daemonize! 29 | raise 'Need to set a pid_file in order to daemonize!' unless @options[:pid_file] 30 | 31 | fork do 32 | Process.setsid 33 | exit if fork 34 | File.umask 0000 35 | STDIN.reopen "/dev/null" 36 | STDOUT.reopen @options[:log_file], "a" 37 | STDERR.reopen STDOUT 38 | start 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/metis/check_definition.rb: -------------------------------------------------------------------------------- 1 | require 'metis/mixin/params_validate' 2 | 3 | class Metis 4 | class CheckDefinition 5 | include Metis::Mixin::ParamsValidate 6 | 7 | attr_accessor :name, :params 8 | 9 | def initialize 10 | @name = nil 11 | @params = {} 12 | @requires = [] 13 | end 14 | 15 | def attribute(attr_name, validation_opts={}) 16 | shim_method=<<-SHIM 17 | def #{attr_name}(arg=nil) 18 | set_or_return(:#{attr_name.to_s}, arg, #{validation_opts.inspect}) 19 | end 20 | SHIM 21 | set_or_return(attr_name.to_sym, nil, validation_opts) 22 | self.instance_eval(shim_method) 23 | end 24 | 25 | def require_gem(*libs) 26 | @requires << libs 27 | @requires.flatten! 28 | end 29 | 30 | def execute(&block) 31 | set_or_return(:execute, block, :kind_of => [Proc]) 32 | end 33 | 34 | def _requires 35 | @requires 36 | end 37 | 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /lib/metis/check_definition_list.rb: -------------------------------------------------------------------------------- 1 | require 'metis/mixin/from_file' 2 | require 'metis/check_definition' 3 | 4 | class Metis 5 | class CheckDefinitionList 6 | include Metis::Mixin::FromFile 7 | 8 | attr_accessor :defines 9 | 10 | def initialize 11 | @defines = Hash.new 12 | end 13 | 14 | def define(check_name, &block) 15 | @defines[check_name] = CheckDefinition.new 16 | @defines[check_name].instance_eval(&block) 17 | true 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/metis/client.rb: -------------------------------------------------------------------------------- 1 | require 'timeout' 2 | 3 | class Metis::Client 4 | 5 | def initialize(socket, context) 6 | @socket = socket 7 | @context = context 8 | end 9 | 10 | def process 11 | query = Metis::NrpePacket.read(@socket) 12 | 13 | # ensure it doesn't specify any commands 14 | if query.buffer =~ /!/ 15 | send_response(Metis::STATUS_CRITICAL, "Arguments not allowed") 16 | return 17 | end 18 | 19 | # find the command 20 | check_name = query.buffer.to_sym 21 | check_definition = @context.definitions[check_name] 22 | 23 | # ensure it exists 24 | unless check_definition 25 | send_response(Metis::STATUS_WARNING, "Command #{query.buffer} not found") 26 | return 27 | end 28 | 29 | # run it 30 | provider = Metis::Provider.new(check_definition, @context) 31 | provider.run 32 | send_response(provider.response_code, provider.response_message) 33 | 34 | rescue Exception => e 35 | send_response(Metis::STATUS_CRITICAL, "Error encountered while processing: #{e.message}") 36 | 37 | ensure 38 | @socket.close 39 | end 40 | 41 | private 42 | 43 | def send_response(result_code, message) 44 | response = Metis::NrpePacket.new 45 | response.packet_type = :response 46 | response.result_code = result_code 47 | response.buffer = message 48 | @socket.write(response.to_bytes) 49 | true 50 | end 51 | 52 | end 53 | -------------------------------------------------------------------------------- /lib/metis/configuration.rb: -------------------------------------------------------------------------------- 1 | class Metis::Configuration 2 | include Metis::Mixin::FromFile 3 | include Metis::Mixin::ParamsValidate 4 | 5 | attr_reader :params, :blocks 6 | 7 | def initialize 8 | @params = {} 9 | @blocks = Hash.new { |h,k| h[k] = [] } 10 | end 11 | 12 | def checks_include_directories(*arg) 13 | set_or_return( 14 | :checks_include_directories, 15 | arg.empty? ? nil : arg, 16 | :kind_of => Array, :default => ['checks'] 17 | ) 18 | end 19 | 20 | def ignore_filename_patterns(*arg) 21 | set_or_return( 22 | :ignore_filename_patterns, 23 | arg.empty? ? nil : arg, 24 | :kind_of => Array, :default => [ /_spec.rb$/ ] 25 | ) 26 | end 27 | 28 | def timeout(seconds=nil) 29 | set_or_return( 30 | :timeout, 31 | seconds, 32 | :kind_of => Fixnum, :default => 30 33 | ) 34 | end 35 | 36 | def configure(check_name, &block) 37 | @blocks[check_name] << block 38 | end 39 | 40 | end 41 | -------------------------------------------------------------------------------- /lib/metis/context.rb: -------------------------------------------------------------------------------- 1 | class Metis 2 | class Context 3 | 4 | attr_reader :definitions, :configuration 5 | 6 | def initialize(configuration_file=nil) 7 | @definitions = Hash.new 8 | @configuration_file = configuration_file if configuration_file && File.exists?(configuration_file) 9 | 10 | @configuration = Metis::Configuration.new 11 | @configuration.from_file(configuration_file) if @configuration_file 12 | end 13 | 14 | def load 15 | load_checks 16 | load_check_config 17 | true 18 | end 19 | 20 | def load_checks 21 | @configuration.checks_include_directories.each do |dir| 22 | Dir.glob(File.join(dir, "**/*.rb")).each do |filename| 23 | next if @configuration.ignore_filename_patterns.detect { |p| filename =~ p } 24 | 25 | checklist = Metis::CheckDefinitionList.new 26 | checklist.from_file(filename) 27 | 28 | definitions.merge!(checklist.defines) do |key, oldval, newval| 29 | puts "Overriding duplicate definition #{key}, new definition found in #{filename}" 30 | newval 31 | end 32 | end 33 | end 34 | end 35 | 36 | def load_check_config 37 | @configuration.blocks.each do |check_name,blocks| 38 | raise "Unknown check definition: #{check_name}" unless @definitions[check_name] 39 | blocks.each { |block| @definitions[check_name].instance_eval(&block) } 40 | end 41 | true 42 | end 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /lib/metis/mixin/from_file.rb: -------------------------------------------------------------------------------- 1 | class Metis 2 | module Mixin 3 | module FromFile 4 | 5 | # Loads a given ruby file, and runs instance_eval against it in the context of the current 6 | # object. 7 | # 8 | # Raises an IOError if the file cannot be found, or is not readable. 9 | def from_file(filename) 10 | if File.exists?(filename) && File.readable?(filename) 11 | self.instance_eval(IO.read(filename), filename, 1) 12 | else 13 | raise IOError, "Cannot open or read #{filename}!" 14 | end 15 | end 16 | 17 | # Loads a given ruby file, and runs class_eval against it in the context of the current 18 | # object. 19 | # 20 | # Raises an IOError if the file cannot be found, or is not readable. 21 | def class_from_file(filename) 22 | if File.exists?(filename) && File.readable?(filename) 23 | self.class_eval(IO.read(filename), filename, 1) 24 | else 25 | raise IOError, "Cannot open or read #{filename}!" 26 | end 27 | end 28 | 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /lib/metis/mixin/params_validate.rb: -------------------------------------------------------------------------------- 1 | class Metis 2 | module Mixin 3 | module ParamsValidate 4 | 5 | # Takes a hash of options, along with a map to validate them. Returns the original 6 | # options hash, plus any changes that might have been made (through things like setting 7 | # default values in the validation map) 8 | # 9 | # For example: 10 | # 11 | # validate({ :one => "neat" }, { :one => { :kind_of => String }}) 12 | # 13 | # Would raise an exception if the value of :one above is not a kind_of? string. Valid 14 | # map options are: 15 | # 16 | # :default:: Sets the default value for this parameter. 17 | # :callbacks:: Takes a hash of Procs, which should return true if the argument is valid. 18 | # The key will be inserted into the error message if the Proc does not return true: 19 | # "Option #{key}'s value #{value} #{message}!" 20 | # :kind_of:: Ensure that the value is a kind_of?(Whatever). If passed an array, it will ensure 21 | # that the value is one of those types. 22 | # :respond_to:: Ensure that the value has a given method. Takes one method name or an array of 23 | # method names. 24 | # :required:: Raise an exception if this parameter is missing. Valid values are true or false, 25 | # by default, options are not required. 26 | # :regex:: Match the value of the paramater against a regular expression. 27 | # :equal_to:: Match the value of the paramater with ==. An array means it can be equal to any 28 | # of the values. 29 | def validate(opts, map) 30 | #-- 31 | # validate works by taking the keys in the validation map, assuming it's a hash, and 32 | # looking for _pv_:symbol as methods. Assuming it find them, it calls the right 33 | # one. 34 | #++ 35 | raise ArgumentError, "Options must be a hash" unless opts.kind_of?(Hash) 36 | raise ArgumentError, "Validation Map must be a hash" unless map.kind_of?(Hash) 37 | 38 | map.each do |key, validation| 39 | unless key.kind_of?(Symbol) || key.kind_of?(String) 40 | raise ArgumentError, "Validation map keys must be symbols or strings!" 41 | end 42 | case validation 43 | when true 44 | _pv_required(opts, key) 45 | when false 46 | true 47 | when Hash 48 | validation.each do |check, carg| 49 | check_method = "_pv_#{check.to_s}" 50 | if self.respond_to?(check_method, true) 51 | self.send(check_method, opts, key, carg) 52 | else 53 | raise ArgumentError, "Validation map has unknown check: #{check}" 54 | end 55 | end 56 | end 57 | end 58 | opts 59 | end 60 | 61 | def set_or_return(symbol, arg, validation) 62 | map = { 63 | symbol => validation 64 | } 65 | 66 | if arg == nil && self.params[symbol] 67 | self.params[symbol] 68 | else 69 | opts = validate({ symbol => arg }, { symbol => validation }) 70 | self.params[symbol] = opts[symbol] 71 | end 72 | end 73 | 74 | private 75 | 76 | # Return the value of a parameter, or nil if it doesn't exist. 77 | def _pv_opts_lookup(opts, key) 78 | if opts.has_key?(key.to_s) 79 | opts[key.to_s] 80 | elsif opts.has_key?(key.to_sym) 81 | opts[key.to_sym] 82 | else 83 | nil 84 | end 85 | end 86 | 87 | # Raise an exception if the parameter is not found. 88 | def _pv_required(opts, key, is_required=true) 89 | if is_required 90 | if (opts.has_key?(key.to_s) && !opts[key.to_s].nil?) || 91 | (opts.has_key?(key.to_sym) && !opts[key.to_sym].nil?) 92 | true 93 | else 94 | raise Exceptions::ValidationFailed, "Required argument #{key} is missing!" 95 | end 96 | end 97 | end 98 | 99 | def _pv_equal_to(opts, key, to_be) 100 | value = _pv_opts_lookup(opts, key) 101 | unless value.nil? 102 | passes = false 103 | Array(to_be).each do |tb| 104 | passes = true if value == tb 105 | end 106 | unless passes 107 | raise Exceptions::ValidationFailed, "Option #{key} must be equal to one of: #{to_be.join(", ")}! You passed #{value.inspect}." 108 | end 109 | end 110 | end 111 | 112 | # Raise an exception if the parameter is not a kind_of?(to_be) 113 | def _pv_kind_of(opts, key, to_be) 114 | value = _pv_opts_lookup(opts, key) 115 | unless value.nil? 116 | passes = false 117 | Array(to_be).each do |tb| 118 | passes = true if value.kind_of?(tb) 119 | end 120 | unless passes 121 | raise Exceptions::ValidationFailed, "Option #{key} must be a kind of #{to_be}! You passed #{value.inspect}." 122 | end 123 | end 124 | end 125 | 126 | # Raise an exception if the parameter does not respond to a given set of methods. 127 | def _pv_respond_to(opts, key, method_name_list) 128 | value = _pv_opts_lookup(opts, key) 129 | unless value.nil? 130 | Array(method_name_list).each do |method_name| 131 | unless value.respond_to?(method_name) 132 | raise Exceptions::ValidationFailed, "Option #{key} must have a #{method_name} method!" 133 | end 134 | end 135 | end 136 | end 137 | 138 | # Assert that parameter returns false when passed a predicate method. 139 | # For example, :cannot_be => :blank will raise a Exceptions::ValidationFailed 140 | # error value.blank? returns a 'truthy' (not nil or false) value. 141 | # 142 | # Note, this will *PASS* if the object doesn't respond to the method. 143 | # So, to make sure a value is not nil and not blank, you need to do 144 | # both :cannot_be => :blank *and* :cannot_be => :nil (or :required => true) 145 | def _pv_cannot_be(opts, key, predicate_method_base_name) 146 | value = _pv_opts_lookup(opts, key) 147 | predicate_method = (predicate_method_base_name.to_s + "?").to_sym 148 | 149 | if value.respond_to?(predicate_method) 150 | if value.send(predicate_method) 151 | raise Exceptions::ValidationFailed, "Option #{key} cannot be #{predicate_method_base_name}" 152 | end 153 | end 154 | end 155 | 156 | # Assign a default value to a parameter. 157 | def _pv_default(opts, key, default_value) 158 | value = _pv_opts_lookup(opts, key) 159 | if value == nil 160 | opts[key] = default_value 161 | end 162 | end 163 | 164 | # Check a parameter against a regular expression. 165 | def _pv_regex(opts, key, regex) 166 | value = _pv_opts_lookup(opts, key) 167 | if value != nil 168 | passes = false 169 | [ regex ].flatten.each do |r| 170 | if value != nil 171 | if r.match(value.to_s) 172 | passes = true 173 | end 174 | end 175 | end 176 | unless passes 177 | raise Exceptions::ValidationFailed, "Option #{key}'s value #{value} does not match regular expression #{regex.inspect}" 178 | end 179 | end 180 | end 181 | 182 | # Check a parameter against a hash of proc's. 183 | def _pv_callbacks(opts, key, callbacks) 184 | raise ArgumentError, "Callback list must be a hash!" unless callbacks.kind_of?(Hash) 185 | value = _pv_opts_lookup(opts, key) 186 | if value != nil 187 | callbacks.each do |message, zeproc| 188 | if zeproc.call(value) != true 189 | raise Exceptions::ValidationFailed, "Option #{key}'s value #{value} #{message}!" 190 | end 191 | end 192 | end 193 | end 194 | 195 | # Allow a parameter to default to @name 196 | def _pv_name_attribute(opts, key, is_name_attribute=true) 197 | if is_name_attribute 198 | if opts[key] == nil 199 | opts[key] = self.instance_variable_get("@name") 200 | end 201 | end 202 | end 203 | end 204 | end 205 | end 206 | -------------------------------------------------------------------------------- /lib/metis/nrpe_packet.rb: -------------------------------------------------------------------------------- 1 | require 'zlib' 2 | 3 | class Metis 4 | class NrpePacket 5 | NRPE_PACKET_VERSION_3 = 3 6 | NRPE_PACKET_VERSION_2 = 2 7 | NRPE_PACKET_VERSION_1 = 1 8 | 9 | QUERY_PACKET = 1 10 | RESPONSE_PACKET = 2 11 | 12 | MAX_PACKETBUFFER_LENGTH = 1024 13 | 14 | MAX_PACKET_SIZE = 12 + 1024 15 | 16 | attr_accessor :packet_version, :crc32, :result_code, :buffer 17 | 18 | def initialize(unpacked=nil) 19 | @packet_version = NRPE_PACKET_VERSION_2 20 | @random = 1 21 | 22 | if unpacked 23 | @packet_version = unpacked[0] 24 | @packet_type = unpacked[1] 25 | @crc32 = unpacked[2] 26 | @result_code = unpacked[3] 27 | @buffer = unpacked[4] 28 | @random = unpacked[5] 29 | end 30 | end 31 | 32 | def packet_type 33 | case @packet_type 34 | when QUERY_PACKET then :query 35 | when RESPONSE_PACKET then :response 36 | end 37 | end 38 | 39 | def packet_type=(type) 40 | case type 41 | when :query then @packet_type = QUERY_PACKET 42 | when :response then @packet_type = RESPONSE_PACKET 43 | else 44 | raise "Invalid packet type" 45 | end 46 | end 47 | 48 | def calculate_crc32 49 | Zlib::crc32(self.to_bytes(0)) 50 | end 51 | 52 | def validate_crc32 53 | raise 'Invalid CRC32' unless @crc32 == self.calculate_crc32 54 | end 55 | 56 | def to_bytes(use_crc32=self.calculate_crc32) 57 | [ @packet_version, @packet_type, use_crc32, @result_code, @buffer, @random].pack("nnNna#{MAX_PACKETBUFFER_LENGTH}n") 58 | end 59 | 60 | def self.read(io) 61 | bytes = io.read(MAX_PACKET_SIZE) 62 | values = bytes.unpack("nnNnA#{MAX_PACKETBUFFER_LENGTH}n") 63 | packet = self.new(values) 64 | packet.validate_crc32 65 | packet 66 | end 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /lib/metis/provider.rb: -------------------------------------------------------------------------------- 1 | class Metis 2 | class Provider 3 | attr_reader :definition, :response_code, :response_message 4 | 5 | def initialize(definition, context) 6 | @definition = definition 7 | @context = context 8 | @response_code = Metis::STATUS_OK 9 | end 10 | 11 | def params 12 | definition.params 13 | end 14 | 15 | def warn(msg) 16 | unless @response_code > Metis::STATUS_WARNING 17 | @response_code = Metis::STATUS_WARNING 18 | @response_message = msg 19 | end 20 | end 21 | 22 | def critical(msg) 23 | unless @response_code > Metis::STATUS_CRITICAL 24 | @response_code = Metis::STATUS_CRITICAL 25 | @response_message = msg 26 | end 27 | end 28 | 29 | def ok(msg=nil) 30 | unless @response_code > Metis::STATUS_OK 31 | @response_code = Metis::STATUS_OK 32 | @response_message = msg 33 | end 34 | end 35 | 36 | def run 37 | Timeout.timeout @context.configuration.timeout do 38 | result = self.instance_eval(&definition.execute) if prepare 39 | ok(result) if result.is_a?(String) 40 | end 41 | 42 | # catch a tmeout here... sometimes it can be raised here rather than in Client 43 | # want it caught here rather than the more general one so the message returned here 44 | # and from Client is ensured to be the same 45 | rescue Timeout::Error => e 46 | critical("The check timed out") 47 | 48 | rescue Exception => e 49 | critical("Exception raised: #{e.message}") 50 | end 51 | 52 | private 53 | 54 | def prepare 55 | f = nil 56 | definition._requires.each { |r| f = r ; require r } 57 | true 58 | rescue LoadError 59 | critical("Failed to load gem: #{f}") 60 | false 61 | end 62 | end 63 | end 64 | -------------------------------------------------------------------------------- /lib/metis/server.rb: -------------------------------------------------------------------------------- 1 | require 'socket' 2 | require 'openssl' 3 | 4 | class Metis::Server 5 | 6 | DEFAULT_OPTIONS = { 7 | :host => '0.0.0.0', 8 | :port => 5668, 9 | :enable_ssl => true 10 | } 11 | 12 | def initialize(context, options={}) 13 | @context = context 14 | @options = DEFAULT_OPTIONS.merge(options) 15 | 16 | if @options[:enable_ssl] 17 | @ssl_context = OpenSSL::SSL::SSLContext.new "SSLv23_server" 18 | @ssl_context.ciphers = 'ADH' 19 | end 20 | end 21 | 22 | def start 23 | @server = TCPServer.new(@options[:host], @options[:port]) 24 | @server = OpenSSL::SSL::SSLServer.new(@server, @ssl_context) if @options[:enable_ssl] 25 | 26 | trap(:QUIT) { 27 | self.stop 28 | exit(0) 29 | } 30 | 31 | trap(:TERM) { 32 | self.stop 33 | exit(0) 34 | } 35 | 36 | true 37 | end 38 | 39 | def stop 40 | @server.stop if @server 41 | end 42 | 43 | def run 44 | loop do 45 | client_socket = @server.accept 46 | Thread.start do 47 | client = Metis::Client.new(client_socket, @context) 48 | client.process 49 | end 50 | end 51 | end 52 | 53 | end 54 | -------------------------------------------------------------------------------- /lib/metis/version.rb: -------------------------------------------------------------------------------- 1 | class Metis 2 | VERSION = '0.1.1' 3 | end 4 | -------------------------------------------------------------------------------- /metis.gemspec: -------------------------------------------------------------------------------- 1 | $:.unshift(File.dirname(__FILE__) + '/lib') 2 | require 'metis/version' 3 | 4 | Gem::Specification.new do |s| 5 | s.name = 'metis' 6 | s.version = Metis::VERSION 7 | s.platform = Gem::Platform::RUBY 8 | s.extra_rdoc_files = ["README.md", "LICENSE" ] 9 | s.summary = "A server monitoring framework built to bring flexible and testable monitoring to your entire infrastructure." 10 | s.description = s.summary 11 | s.author = "Ken Robertson" 12 | s.email = "ken@invalidlogic.com" 13 | s.homepage = "http://github.com/krobertson/metis" 14 | 15 | s.bindir = "bin" 16 | s.executables = %w( metis-server ) 17 | s.require_path = 'lib' 18 | s.files = Dir.glob("lib/**/*") 19 | end 20 | -------------------------------------------------------------------------------- /spec/client_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Metis::Client do 4 | before do 5 | @socket = mock 6 | @context = Metis::Context.new File.dirname(__FILE__)+"/resources/sample/metis.rb" 7 | @context.load 8 | @client = Metis::Client.new @socket, @context 9 | end 10 | 11 | describe '.process' do 12 | it 'should process a valid packet, run the command, and send a response' do 13 | query = Metis::NrpePacket.new 14 | query.packet_type = :query 15 | query.buffer = "simple" 16 | 17 | Metis::NrpePacket.should_receive(:read).and_return(query) 18 | 19 | @socket.should_receive(:write).with(/we're good/) 20 | @socket.should_receive :close 21 | 22 | @client.process.should == true 23 | end 24 | 25 | it 'should not accept command arguments' do 26 | query = Metis::NrpePacket.new 27 | query.packet_type = :query 28 | query.buffer = "simple!argument" 29 | 30 | Metis::NrpePacket.should_receive(:read).and_return(query) 31 | 32 | @socket.should_receive(:write).with(/\002Arguments not allowed/) 33 | @socket.should_receive :close 34 | 35 | @client.process.should be_nil 36 | end 37 | 38 | it 'should return a message when the command doesn\'t exist' do 39 | query = Metis::NrpePacket.new 40 | query.packet_type = :query 41 | query.buffer = "nonexistant" 42 | 43 | Metis::NrpePacket.should_receive(:read).and_return(query) 44 | 45 | @socket.should_receive(:write).with(/\001Command nonexistant not found/) 46 | @socket.should_receive :close 47 | 48 | @client.process.should be_nil 49 | end 50 | 51 | it 'should handle packets that throw an invalid CRC error' do 52 | Metis::NrpePacket.should_receive(:read).and_raise("Invalid CRC32") 53 | 54 | @socket.should_receive(:write).with(/\002Error encountered while processing: Invalid CRC32/) 55 | @socket.should_receive(:close) 56 | 57 | @client.process.should == true 58 | end 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /spec/context_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Metis::Context do 4 | context "configuration" do 5 | it 'should load the specified configuration file' do 6 | config_file = File.dirname(__FILE__)+"/resources/sample/metis.rb" 7 | IO.should_receive(:read).with(config_file).and_return("") 8 | Metis::Context.new(config_file).should_not be_nil 9 | end 10 | end 11 | 12 | describe '.load' do 13 | before do 14 | @context = Metis::Context.new File.dirname(__FILE__)+"/resources/sample/metis.rb" 15 | end 16 | 17 | it 'should load the given checks' do 18 | @context.load 19 | 20 | @context.definitions[:simple].should_not be_nil 21 | @context.definitions[:simple].should be_kind_of Metis::CheckDefinition 22 | end 23 | 24 | it 'handle duplicates and have the 2nd take precedence' do 25 | @context.load 26 | 27 | @context.definitions[:duplicate].should_not be_nil 28 | @context.definitions[:duplicate].should be_kind_of Metis::CheckDefinition 29 | @context.definitions[:duplicate].params[:name].should == 'Jane' 30 | end 31 | 32 | it 'should load extra configuration' do 33 | @context.load 34 | 35 | @context.definitions[:simple_arg].params[:name].should == 'Jim' 36 | end 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /spec/nrpe_packet_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Metis::NrpePacket do 4 | describe '#read' do 5 | it 'should read a valid request packet' do 6 | socket = stub 7 | socket.should_receive(:read).and_return("\000\002\000\001e\232\350\211\000{hello world\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"+ 8 | "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\001") 9 | 10 | packet = Metis::NrpePacket.read socket 11 | 12 | packet.packet_version.should == Metis::NrpePacket::NRPE_PACKET_VERSION_2 13 | packet.packet_type.should == :query 14 | packet.buffer.should == "hello world" 15 | packet.result_code.should == 123 16 | packet.crc32.should == 1704650889 17 | end 18 | 19 | it 'should reject a packet with a bad crc32' do 20 | socket = stub 21 | # changed the last byte 22 | socket.should_receive(:read).and_return("\000\002\000\001e\232\350\211\000{hello world\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"+ 23 | "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\002") 24 | 25 | lambda { 26 | packet = Metis::NrpePacket.read(socket) 27 | }.should raise_error 'Invalid CRC32' 28 | end 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /spec/provider_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Metis::Provider do 4 | before do 5 | @context = Metis::Context.new File.dirname(__FILE__)+"/resources/sample/metis.rb" 6 | @context.load 7 | end 8 | 9 | describe '.run' do 10 | it 'should return a result' do 11 | provider = Metis::Provider.new @context.definitions[:simple], @context 12 | provider.run 13 | 14 | provider.response_code.should == Metis::STATUS_OK 15 | provider.response_message.should == "we're good" 16 | end 17 | 18 | it 'should capture exceptions' do 19 | provider = Metis::Provider.new @context.definitions[:error], @context 20 | 21 | lambda { 22 | provider.run 23 | }.should_not raise_error 24 | 25 | provider.response_code.should == Metis::STATUS_CRITICAL 26 | provider.response_message.should == "Exception raised: booboo" 27 | end 28 | 29 | it 'should handle a return of a string as the message' do 30 | provider = Metis::Provider.new @context.definitions[:just_string], @context 31 | provider.run 32 | 33 | provider.response_code.should == Metis::STATUS_OK 34 | provider.response_message.should == "hello" 35 | end 36 | 37 | it 'should handle requiring gems that aren\'t there' do 38 | provider = Metis::Provider.new @context.definitions[:bad_gem], @context 39 | 40 | lambda { 41 | provider.run 42 | }.should_not raise_error 43 | 44 | provider.response_code.should == Metis::STATUS_CRITICAL 45 | provider.response_message.should == "Failed to load gem: uhhohh" 46 | end 47 | 48 | it 'should enforce a timeout and return an error when exceeded' do 49 | provider = Metis::Provider.new @context.definitions[:sleep], @context 50 | 51 | lambda { 52 | provider.run 53 | }.should_not raise_error 54 | 55 | provider.response_code.should == Metis::STATUS_CRITICAL 56 | provider.response_message.should == "The check timed out" 57 | end 58 | end 59 | 60 | describe 'state' do 61 | it 'should set warning state' do 62 | provider = Metis::Provider.new @context.definitions[:warning], @context 63 | provider.run 64 | 65 | provider.response_code.should == Metis::STATUS_WARNING 66 | provider.response_message.should == "careful there" 67 | end 68 | 69 | it 'should set critical state' do 70 | provider = Metis::Provider.new @context.definitions[:critical], @context 71 | provider.run 72 | 73 | provider.response_code.should == Metis::STATUS_CRITICAL 74 | provider.response_message.should == "uhh ohh" 75 | end 76 | 77 | it 'should handle precedence when there are multiple state assertions' do 78 | provider = Metis::Provider.new @context.definitions[:precedence], @context 79 | provider.run 80 | 81 | provider.response_code.should == Metis::STATUS_CRITICAL 82 | provider.response_message.should == "crit" 83 | end 84 | end 85 | end 86 | -------------------------------------------------------------------------------- /spec/resources/sample/checks/examples.rb: -------------------------------------------------------------------------------- 1 | define :simple do 2 | execute do 3 | ok "we're good" 4 | end 5 | end 6 | 7 | define :warning do 8 | execute do 9 | warn "careful there" 10 | end 11 | end 12 | 13 | define :critical do 14 | execute do 15 | critical "uhh ohh" 16 | end 17 | end 18 | 19 | define :error do 20 | execute do 21 | raise 'booboo' 22 | end 23 | end 24 | 25 | 26 | define :simple_arg do 27 | attribute :name 28 | execute do 29 | params[:name] 30 | end 31 | end 32 | 33 | define :default_arg do 34 | attribute :name, :default => 'Bill' 35 | execute do 36 | params[:name] 37 | end 38 | end 39 | 40 | define :typed_arg do 41 | attribute :age, :kind_of => Fixnum 42 | execute do 43 | params[:age].to_s 44 | end 45 | end 46 | 47 | define :bad_gem do 48 | require_gem 'uhhohh' 49 | execute do 50 | '.' 51 | end 52 | end 53 | 54 | define :just_string do 55 | execute do 56 | 'hello' 57 | end 58 | end 59 | 60 | define :duplicate do 61 | attribute :name, :default => 'Joe' 62 | execute do 63 | params[:name] 64 | end 65 | end 66 | 67 | define :precedence do 68 | execute do 69 | critical 'crit' 70 | warn 'warn' 71 | ok 'not so' 72 | end 73 | end 74 | 75 | define :sleep do 76 | execute do 77 | sleep 2 78 | 'ok' 79 | end 80 | end 81 | -------------------------------------------------------------------------------- /spec/resources/sample/checks/others.rb: -------------------------------------------------------------------------------- 1 | define :duplicate do 2 | attribute :name, :default => 'Jane' 3 | execute do 4 | params[:name] 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /spec/resources/sample/metis.rb: -------------------------------------------------------------------------------- 1 | checks_include_directories File.dirname(__FILE__)+'/checks' 2 | ignore_filename_patterns /_spec.rb/ 3 | timeout 1 4 | 5 | configure :simple_arg do 6 | name 'Jim' 7 | end 8 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require "simplecov" 2 | SimpleCov.start do 3 | add_filter "spec" 4 | end 5 | 6 | # Add the root to the load path. 7 | $LOAD_PATH << File.join(File.dirname(__FILE__), "..", "lib") 8 | 9 | require "bundler" 10 | Bundler.require 11 | require "rspec" 12 | 13 | require "metis" 14 | --------------------------------------------------------------------------------