Modify Perf Data

Hi, i m searching how to add “perfdata” to my plugins.

With other plugins like “ping” i have this :
image

I want to add the same thing for my own check command but i didn’t find the .conf file.
I found a diretory with +1000 file named xxxxperfdata but they are genereated every 15 seconds.

Do you know how can i modify perfdata and then modify warning/critical level ?
thanks

Hi,

can you share the code of your check plugin? Then we may give you hints on where to add the values. I assume you already have read the plugin API specification, right?

Cheers,
Michael

yes, ok no problem :

#!/usr/bin/ruby

require 'net/imap'
require 'optparse'
require 'time'

#############
# Variables #
#############
opts = {
  host: '',
  user: '',
  pass: '',
  port: 993,
  folder: 'INBOX',
  usessl: false,
  time: 3600
}

#############
# Arguments #
#############
OptionParser.new do |o|
  o.banner = "Usage: #{$PROGRAM_NAME} -s <server> -u <user> -p <pass> [opts]"
  o.on('-h', '--help', 'Prints this help.') { puts o && exit }
  o.on('-sSERVER', '--server=SERVER', 'IMAP server.') { |s| opts[:server] = s }
  o.on('-uUSER', '--user=USER', 'Login user.') { |u| opts[:user] = u }
  o.on('-wPASS', '--password=PASS', 'Login password.') { |w| opts[:pass] = w }
  o.on('-pPORT', '--port=PORT', 'IMAP server port.') { |p| opts[:port] = p }
  o.on('-fFOLDER', '--folder=FOLDER', 'Mailbox.') { |f| opts[:folder] = f }
  o.on('-l', '--use-ssl', 'Use SSL.') { opts[:usessl] = true }
  o.on('-tPERIOD', '--time=PERIOD', 'Period.') { |t| opts[:time] = t.to_i }
end.parse!

########
# Main #
########
imap = Net::IMAP.new(opts[:server], opts[:port], opts[:usessl])
imap.login(opts[:user], opts[:pass])
imap.examine(opts[:folder])

arr = []
target = Net::IMAP.format_date(Time.now - opts[:time])
imap.search(['SINCE', target]).each do |id|
  arr.push Time.parse(imap.fetch(id, 'ENVELOPE')[0].attr['ENVELOPE'].date).to_i
end
latest = arr.max
boxes = ["INBOX"]  # list all the IMAP folders to include in the calculations


# loop through all boxes, adding all mail-ids to the list
mail_ids = boxes.inject([]) do |sum, box|
  imap.select("INBOX")
  sum += imap.search(["ALL"])
end
mail_count=mail_ids.count
puts "Vous avez un total de  #{mail_count} mail(s)"

Since you already have a counting variable with mail_count, you can do that by modifying the output a bit with the performance data format.

-puts "Vous avez un total de  #{mail_count} mail(s)"
+puts "Vous avez un total de  #{mail_count} mail(s) | 'count'=#{mail_count}"

I’m not sure if that’s 100% correct Ruby though with the hash variable access after the equal operator.

Cheers,
Michael

It works you right ! But i still doesn’t know where I have to define the “warning value” , i must define it in the ruby script ? or in an other .conf file ?

Thank you so much for your help.

As shown in the documentation the performance data can contain additional values:
'label'=value[UOM];[warn];[crit];[min];[max]

In case you want to pass thresholds to the script, you’ll need to modify the argument parser above.

Unfortunately -w is already taken, so let’s use -W instead.

o.on('-WWARN', '--warn=WARN', 'Warning count.') { |W| opts[:warn] = W }
o.on('-CCRIT', '--crit=CRIT', 'Critical count.') { |C| opts[:crit] = C }

The value is now available as opts[:warn] and you can use that logic to compare mail_count against it.

I haven’t coded Ruby for while, so the code below might not be syntactically correct.

mail_count=mail_ids.count
warn = opts[:warn] # TODO: default values?
crit = opts[:crit]
output = "Vous avez un total de  #{mail_count} mail(s) | 'count'=#{mail_count};#{warn};#{crit}"

if mail_count > crit {
  puts "CRITICAL - " + output
  exit(2) # 2.. critical
}
else if mail_count > warn {
  puts "WARNING - " + output
  exit(1) # 1.. warning
}
else {
  puts "OK - " + output
  exit(0) # 0 .. ok
}

Cheers,
Michael

I’ve modified syntax and now it works ! Thank you for your help and your advice :slight_smile:

Can you share the complete working script, please? Others can benefit from this as well :slight_smile:

i’ve modified arguments, i had conflict with previous arguments, i have also modified IF structure and i had to convert string into numeric value for each if. Maybe some things or not really usefull but it work now.

#!/usr/bin/ruby

require 'net/imap'
require 'optparse'
require 'time'

#############
# Variables #
#############
opts = {
  host: '',
  user: '',
  pass: '',
  port: 993,
  folder: 'INBOX',
  usessl: false,
  time: 3600
}

#############
# Arguments #
#############
OptionParser.new do |o|
  o.banner = "Usage: #{$PROGRAM_NAME} -s <server> -u <user> -p <pass> [opts]"
  o.on('-h', '--help', 'Prints this help.') { puts o && exit }
  o.on('-sSERVER', '--server=SERVER', 'IMAP server.') { |s| opts[:server] = s }
  o.on('-uUSER', '--user=USER', 'Login user.') { |u| opts[:user] = u }
  o.on('-wPASS', '--password=PASS', 'Login password.') { |w| opts[:pass] = w }
  o.on('-pPORT', '--port=PORT', 'IMAP server port.') { |p| opts[:port] = p }
  o.on('-fFOLDER', '--folder=FOLDER', 'Mailbox.') { |f| opts[:folder] = f }
  o.on('-l', '--use-ssl', 'Use SSL.') { opts[:usessl] = true }
  o.on('-tPERIOD', '--time=PERIOD', 'Period.') { |t| opts[:time] = t.to_i }
  o.on('-yWARN', '--warn=WARN', 'Warning count.') { |y| opts[:warn] = y }
  o.on('-cCRIT', '--crit=CRIT', 'Critical count.') { |c| opts[:crit] = c }

end.parse!

########
# Main #
########
imap = Net::IMAP.new(opts[:server], opts[:port], opts[:usessl])
imap.login(opts[:user], opts[:pass])
imap.examine(opts[:folder])

arr = []
target = Net::IMAP.format_date(Time.now - opts[:time])
imap.search(['SINCE', target]).each do |id|
  arr.push Time.parse(imap.fetch(id, 'ENVELOPE')[0].attr['ENVELOPE'].date).to_i
end
latest = arr.max
boxes = ["INBOX"]  # list all the IMAP folders to include in the calculations


# loop through all boxes, adding all mail-ids to the list
mail_ids = boxes.inject([]) do |sum, box|
  imap.select("INBOX")
  sum += imap.search(["ALL"])
end

mail_count=mail_ids.count
warn = opts[:warn] # TODO: default values?
crit = opts[:crit]
output = "Vous avez un total de  #{mail_count} mail(s) | 'count'=#{mail_count};#{warn};#{crit}"

if mail_count > crit.to_i
  puts "CRITICAL - " + output
  exit(2) # 2.. critical


elsif mail_count > warn.to_i
  puts "WARNING - " + output
  exit(1) # 1.. warning


else
  puts "OK - " + output
  exit(0) # 0.. ok
end
1 Like

Cool, thanks. Just a tip for future postings - wrap code blocks with 3 back ticks before and after, for better highlighting. You can see that with my edits :slight_smile:

Cheers,
Michael

1 Like