diff --git a/.kitchen.yml b/.kitchen.yml index c5bcad4..897dbc6 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -64,6 +64,7 @@ suites: dashboard: port: 4000 monitor: + use_nagios_plugins: true metric_handlers: ["graphite"] additional_client_attributes: haproxy_services: "servers-http" diff --git a/attributes/default.rb b/attributes/default.rb index b20ab78..3c55d66 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -6,6 +6,7 @@ default["monitor"]["use_local_ipv4"] = false default["monitor"]["sensu_plugin_version"] = "0.2.1" +default["monitor"]["use_nagios_plugins"] = false default["monitor"]["additional_client_attributes"] = Mash.new diff --git a/attributes/nagios_plugins.rb b/attributes/nagios_plugins.rb new file mode 100644 index 0000000..8cde18e --- /dev/null +++ b/attributes/nagios_plugins.rb @@ -0,0 +1 @@ +default["monitor"]["nagios_plugin_packages"] = ["nagios-plugins"] diff --git a/files/default/extensions/nagios_perfdata.rb b/files/default/extensions/nagios_perfdata.rb new file mode 100644 index 0000000..c86b293 --- /dev/null +++ b/files/default/extensions/nagios_perfdata.rb @@ -0,0 +1,44 @@ +# +# Nagios performance data to Graphite plain text mutator extension. +# === +# +# Copyright 2013 Heavy Water Operations, LLC. +# +# Released under the same terms as Sensu (the MIT license); see LICENSE +# for details. + +module Sensu + module Extension + class NagiosPerfData < Mutator + def name + 'nagios_perfdata' + end + + def description + 'converts nagios performance data to graphite plain text' + end + + def run(event) + result = [] + client = event[:client] + check = event[:check] + + # https://www.nagios-plugins.org/doc/guidelines.html#AEN200 + perfdata = check[:output].split('|').last.strip + + perfdata.split(/\s+/).each do |data| + # label=value[UOM];[warn];[crit];[min];[max] + label, value = data.split('=') + + name = label.strip.gsub(/\W/, '_') + measurement = value.strip.split(';').first.gsub(/[^-\d\.]/, '') + + path = [client[:name], check[:name], name].join('.') + + result << [path, measurement, check[:executed]].join("\t") + end + yield(result.join("\n") + "\n", 0) + end + end + end +end diff --git a/recipes/_chef_node_handler.rb b/recipes/_chef_node_handler.rb index a233972..e89d874 100644 --- a/recipes/_chef_node_handler.rb +++ b/recipes/_chef_node_handler.rb @@ -44,13 +44,7 @@ ) end -sensu_filter "keepalives" do - attributes( - :check => { - :name => "keepalive" - } - ) -end +include_recipe "monitor::_filters" sensu_handler "chef_node" do type "pipe" diff --git a/recipes/_filters.rb b/recipes/_filters.rb new file mode 100644 index 0000000..293d75c --- /dev/null +++ b/recipes/_filters.rb @@ -0,0 +1,30 @@ +# +# Cookbook Name:: monitor +# Recipe:: _filters +# +# Copyright 2013, Sean Porter Consulting +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +sensu_filter "actions" do + attributes(:action => "eval: %w[create resolve].include? value") +end + +sensu_filter "keepalives" do + attributes( + :check => { + :name => "keepalive" + } + ) +end diff --git a/recipes/_graphite_handler.rb b/recipes/_graphite_handler.rb index e960101..833c866 100644 --- a/recipes/_graphite_handler.rb +++ b/recipes/_graphite_handler.rb @@ -53,12 +53,15 @@ mutator "only_check_output" end -sensu_handler "graphite_amqp" do - type "amqp" - exchange( - :type => "topic", - :name => "graphite", - :durable => true - ) - mutator "only_check_output" +if node["monitor"]["use_nagios_plugins"] + include_recipe "monitor::_nagios_perfdata" + + sensu_handler "graphite_perfdata" do + type "tcp" + socket( + :host => graphite_address, + :port => graphite_port + ) + mutator "nagios_perfdata" + end end diff --git a/recipes/_nagios_perfdata.rb b/recipes/_nagios_perfdata.rb new file mode 100644 index 0000000..4e2627d --- /dev/null +++ b/recipes/_nagios_perfdata.rb @@ -0,0 +1,24 @@ +# +# Cookbook Name:: monitor +# Recipe:: _nagios_perfdata +# +# Copyright 2013, Sean Porter Consulting +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +cookbook_file "/etc/sensu/extensions/nagios_perfdata.rb" do + source "extensions/nagios_perfdata.rb" + mode 0755 + notifies :create, "ruby_block[sensu_service_trigger]", :immediately +end diff --git a/recipes/_nagios_plugins.rb b/recipes/_nagios_plugins.rb new file mode 100644 index 0000000..0479ec6 --- /dev/null +++ b/recipes/_nagios_plugins.rb @@ -0,0 +1,22 @@ +# +# Cookbook Name:: monitor +# Recipe:: _nagios_plugins +# +# Copyright 2013, Sean Porter Consulting +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +node["monitor"]["nagios_plugin_packages"].each do |package_name| + package package_name +end diff --git a/recipes/_pagerduty_handler.rb b/recipes/_pagerduty_handler.rb index 007f8ca..8f243ce 100644 --- a/recipes/_pagerduty_handler.rb +++ b/recipes/_pagerduty_handler.rb @@ -28,7 +28,10 @@ content(:api_key => node["monitor"]["pagerduty_api_key"]) end +include_recipe "monitor::_filters" + sensu_handler "pagerduty" do type "pipe" command "pagerduty.rb" + filters ["actions"] end diff --git a/recipes/default.rb b/recipes/default.rb index 43ab5d4..e38e67f 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -39,6 +39,10 @@ version node["monitor"]["sensu_plugin_version"] end +if node["monitor"]["use_nagios_plugins"] + include_recipe "monitor::_nagios_plugins" +end + %w[ check-procs.rb check-banner.rb diff --git a/test/integration/data_bags/sensu_checks/ntp.json b/test/integration/data_bags/sensu_checks/ntp.json new file mode 100644 index 0000000..855d969 --- /dev/null +++ b/test/integration/data_bags/sensu_checks/ntp.json @@ -0,0 +1,10 @@ +{ + "id": "ntp", + "type": "metric", + "command": "/usr/lib/nagios/plugins/check_ntp -H time.nrc.ca", + "subscribers": [ + "all" + ], + "interval": 60, + "handlers": ["graphite_perfdata"] +}