Sha256: 580b8efca2449050cd6b4fdfcaba7e4fd74fa1430ef44811a6231ce4a8d868e5

Contents?: true

Size: 1.7 KB

Versions: 26

Compression:

Stored size: 1.7 KB

Contents

#!/usr/bin/env ruby
#
# Copyright 2014 Paulo Miguel Almeida Rodenas (paulo.ubuntu@gmail.com)
#
# Depends on mysql gem
# gem install mysql
#
# This handler sends metrics to a MySQL database for later user such as
# historic comparisons, charts and so on
#
# =========== Initial DDL ===========
# MySQL initial can be found in mysql-metrics.sql
#
# =========== Config ===========
# MySQL 'hostname', 'username', and 'password' must be
# specified in a config file in /etc/sensu/conf.d.
# See mysql-metrics.json for an example.
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.

require 'sensu-handler'
require 'json'
require 'mysql'

class MysqlMetric < Sensu::Handler
  # override filters from Sensu::Handler. not appropriate for metric handlers
  def filter; end

  def handle
    # mysql settings
    mysql_hostname = settings['mysql']['hostname']
    mysql_username = settings['mysql']['username']
    mysql_password = settings['mysql']['password']

    # event values
    client_id = @event['client']['name']
    check_name = @event['check']['name']
    check_issued = @event['check']['issued']
    check_output = @event['check']['output']
    check_status = @event['check']['status']

    begin
      con = Mysql.new mysql_hostname, mysql_username, mysql_password
      con.query('INSERT INTO '\
                'sensumetrics.sensu_historic_metrics('\
                'client_id, check_name, issue_time, '\
                'output, status) '\
                "VALUES ('#{client_id}', '#{check_name}', "\
                "#{check_issued}, '#{check_output}', #{check_status})")
    rescue Mysql::Error => e
      puts e.errno
      puts e.error
    ensure
      con.close if con
    end
  end
end

Version data entries

26 entries across 26 versions & 3 rubygems

Version Path
sensu-plugins-mysql-1.1.0 bin/metrics-mysql.rb
sensu-plugins-mysql-1.0.0 bin/metrics-mysql.rb
sensu-plugins-mysql-0.0.4 bin/metrics-mysql.rb
sensu-plugins-mysql-0.0.3 bin/metrics-mysql.rb
sensu-plugins-mysql-0.0.2 bin/metrics-mysql.rb
sensu-plugins-mysql-0.0.1 bin/metrics-mysql.rb