Ruby require 'gem' issues

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Locked
bellrd07
Posts: 2
Joined: Thu Oct 26, 2017 12:05 pm

Ruby require 'gem' issues

Post by bellrd07 »

When I write a check in Ruby and must require or load a gem the require statement returns true or false which causes nagios to ignore the rest of the script. Is there a way to get around this as it is causing nagios to exit with warning status and not read any of the actual output?
Any tips would be appreciated.
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Ruby require 'gem' issues

Post by dwhitfield »

I'm not sure how you would execute this in your instance, but it is possible to write ruby plugins without require or load statements, such as the one below:

Code: Select all

#!/usr/bin/ruby
###################################################
# Written by Florian Seidel
# v 0.2
###################################################
# Get Error Level, 255 is good
#First Command-Line Argument should be the IP of the IMM
IMM_IP = ARGV[0]
SNMPv1_community = ARGV[1]

# Getting Error Level
# 255 = Normal, 0 = Critical, 2 = Non-critical Error, 4 = System-level Error
error_level=`snmpget -v 1 -c #{SNMPv1_community} #{IMM_IP} .1.3.6.1.4.1.2.3.51.3.1.4.1.0 -O q -O v`

#What is the Problem?!
case error_level.chomp.to_i
when 255
	puts "WLC Hardware Status: OK"
	puts `snmpwalk -v1 -c #{SNMPv1_community} #{IMM_IP} .1.3.6.1.4.1.2.3.51.3.1.4.2.1.3 -O q -O v`
	exit 0
when 0
 	puts "WLC Hardware Status: Critical"
	puts `snmpwalk -v1 -c #{SNMPv1_community} #{IMM_IP} .1.3.6.1.4.1.2.3.51.3.1.4.2.1.3 -O q -O v`
	exit 2
when 2
 	puts "WLC Hardware Status: Warning"
	puts `snmpwalk -v1 -c #{SNMPv1_community} #{IMM_IP} .1.3.6.1.4.1.2.3.51.3.1.4.2.1.3 -O q -O v`
	exit 1
when 4
 	puts "WLC Hardware Status: "
	puts `snmpwalk -v1 -c #{SNMPv1_community} #{IMM_IP} .1.3.6.1.4.1.2.3.51.3.1.4.2.1.3 -O q -O v`
	exit 2
else
 	puts "Unknown"
	 puts `snmpwalk -v1 -c #{SNMPv1_community} #{IMM_IP} .1.3.6.1.4.1.2.3.51.3.1.4.2.1.3 -O q -O v`
	exit 3
end
bellrd07
Posts: 2
Joined: Thu Oct 26, 2017 12:05 pm

Re: Ruby require 'gem' issues

Post by bellrd07 »

I am requiring the mysql2 gem. How would I run the script without requiring that gem? This is my simplified code. However, Nagios gets the return value from require 'mysql2' and ignores the rest of the output.

Code: Select all

#!/usr/bin/env ruby
    require 'mysql2'
    client = Mysql2::Client.new(:host => "host", :username => "dbuser", :password => 'dbpw', :database => 'db')
    items = client.query('SELECT vj.jobid, vj.accno, vj.fee, count(vd.docid), sum(vd.pages)
        FROM vpo_jobs AS vj
        left join vpo_documents AS vd ON (vd.jobid = vj.jobid)
        WHERE vj.datesubmitted >= "2017-10-17" AND vj.datesubmitted < "2017-10-18"
        GROUP BY vj.accno')
    results = Array.new
    graph = Array.new
    items.each do |h|
       results.push("#{h['accno'].upcase} Fee:#{h['fee']} Envelopes:#{h['count(vd.docid)']} Pages:#{h['sum(vd.pages)']}")
       graph.push("#{h['accno'].upcase} envelopes = #{h['count(vd.docid)']} #{h['accno'].upcase} pages = #{h['sum(vd.pages)']}")
    end
    graph = graph.join(" ").strip
    results = results.join("; ").strip
    puts "#{results} | #{graph}"
    exit 0
    
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Ruby require 'gem' issues

Post by mcapra »

Here's a very popular AWS plugin written in Ruby:
https://exchange.nagios.org/directory/P ... us/details

Glancing at the source, you may notice that the actual execution of things exists in a begin end block:

Code: Select all

begin
  opts = {
    :removes => [ "Amazon", "AWS", "(Ireland)" ],
    :services => [ "Elastic Compute Cloud", "Simple Storage Service" ],
    :region => "EU_block",
  }

  check = Nagios::AWS::DashboardCheck.new(opts)
  exit check.run()
rescue => e
  warn e.message
  warn e.backtrace.join("\n")
  exit Nagios::EXIT_UNKNOWN
end
You might try something like that.
Former Nagios employee
https://www.mcapra.com/
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Ruby require 'gem' issues

Post by dwhitfield »

Thanks @mcapra! OP, did you have any additional questions?
Locked