SHARE
TWEET

Untitled

a guest Aug 19th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/ruby
  2. require 'yaml'
  3.  
  4. #File to be tested
  5. $filename='zabbix_ejjabard_checks.yml'
  6.  
  7. class Main
  8.   METRICS_COMMANDS = {
  9.     'transaction_commits' => '/usr/sbin/ejabberdctl mnesia_info transaction_commits',
  10.     'transaction_failures' => '/usr/sbin/ejabberdctl mnesia_info transaction_failures',
  11.     'transaction_restarts' => '/usr/sbin/ejabberdctl mnesia_info transaction_restarts',
  12.     'transaction_log_writes' => '/usr/sbin/ejabberdctl mnesia_info transaction_log_writes',
  13.     'port_count' => '/usr/sbin/ejabberdctl system_info port_count',
  14.     'port_limit' => '/usr/sbin/ejabberdctl system_info port_limit',
  15.     'process_count' => '/usr/sbin/ejabberdctl system_info process_count',
  16.     'process_limit' => '/usr/sbin/ejabberdctl system_info process_limit',
  17.     'connected_users' => '/usr/sbin/ejabberdctl connected_users_number',
  18.     'memory_total' => '/usr/sbin/ejabberdctl memory total',
  19.     'memory_processes' => '/usr/sbin/ejabberdctl memory processes',
  20.     'memory_processes_used' => '/usr/sbin/ejabberdctl memory processes_used',
  21.     'memory_system' => '/usr/sbin/ejabberdctl memory system',
  22.     'memory_atom' => '/usr/sbin/ejabberdctl memory atom',
  23.     'memory_atom_used' => '/usr/sbin/ejabberdctl memory atom_used',
  24.     'memory_binary' => '/usr/sbin/ejabberdctl memory binary',
  25.     'memory_code' => '/usr/sbin/ejabberdctl memory code',
  26.     'memory_ets' => '/usr/sbin/ejabberdctl memory ets',
  27.     'ti_muc_room_memory' => '/usr/sbin/ejabberdctl mnesia_table_info muc_room memory',
  28.     'ti_muc_room_size' => '/usr/sbin/ejabberdctl mnesia_table_info muc_room size',
  29.     'ti_muc_online_room_memory' => '/usr/sbin/ejabberdctl mnesia_table_info muc_online_room memory',
  30.     'ti_muc_online_room_size' => '/usr/sbin/ejabberdctl mnesia_table_info muc_online_room size',
  31.     'ti_session_memory' => '/usr/sbin/ejabberdctl mnesia_table_info session memory',
  32.     'ti_session_size' => '/usr/sbin/ejabberdctl mnesia_table_info session size',
  33.     #'ejabberd_presences' => '/usr/sbin/ejabberdctl ejabberd presences', // does not make sense - just example for future internal calls - this one always returns 1.
  34.   }
  35.  
  36.   def run()
  37.     command_to_run=ARGV[0]
  38.     file_retention=ARGV[1].to_i
  39.  
  40.     unless ARGV.length == 2 and METRICS_COMMANDS.include?(ARGV[0]) and file_retention.is_a? Integer
  41.       puts "Two Arguments: Metric command and Minutes for retention \nUsage: #{__FILE__} [#{METRICS_COMMANDS.keys.join('|')}]"
  42.       exit(1)
  43.     else
  44.       unless is_file_ok and is_file_new(file_retention)
  45.         puts "file not ok, Lets create a new one"
  46.         if File.exists?($filename)
  47.           system "rm #{$filename}"
  48.         end
  49.         command_output = Hash.new(0)
  50.         METRICS_COMMANDS.each {|key,each| command_output[key]=run_command(each) } # runs call commands and stores the output into a hash
  51.         File.write($filename, command_output.to_yaml) # Writes the output hash to file called $filename
  52.         from_file = YAML.load_file('zabbix_ejjabard_checks.yml') # loads the created file
  53.         puts from_file[command_to_run] # outputs the value from the file
  54.       else
  55.         puts "the file we have is ok, lets work with it"
  56.         from_file = YAML.load_file('zabbix_ejjabard_checks.yml') # loads the created file
  57.         puts from_file[command_to_run] # outputs the value from the file
  58.       end
  59.     end
  60.   end
  61.  
  62.   def is_file_old(minutes)
  63.     return ((Time.now - File.stat($filename).mtime) / 60) >= minutes # True if greater than 1
  64.   end
  65.  
  66.   def is_file_new(minutes)
  67.     return ((Time.now - File.stat($filename).mtime) / 60) < minutes # True if greater than 1
  68.   end
  69.  
  70.   def is_file_ok
  71.     return File.exists?($filename) # True is file is ok
  72.   end
  73.  
  74.   def run_command(cmd)
  75.     return exec(cmd) #Return information to zabbix
  76.   end
  77. end
  78.  
  79. main = Main.new
  80. main.run()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top