Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- require 'yaml'
- #File to be tested
- $filename='zabbix_ejjabard_checks.yml'
- class Main
- METRICS_COMMANDS = {
- 'transaction_commits' => '/usr/sbin/ejabberdctl mnesia_info transaction_commits',
- 'transaction_failures' => '/usr/sbin/ejabberdctl mnesia_info transaction_failures',
- 'transaction_restarts' => '/usr/sbin/ejabberdctl mnesia_info transaction_restarts',
- 'transaction_log_writes' => '/usr/sbin/ejabberdctl mnesia_info transaction_log_writes',
- 'port_count' => '/usr/sbin/ejabberdctl system_info port_count',
- 'port_limit' => '/usr/sbin/ejabberdctl system_info port_limit',
- 'process_count' => '/usr/sbin/ejabberdctl system_info process_count',
- 'process_limit' => '/usr/sbin/ejabberdctl system_info process_limit',
- 'connected_users' => '/usr/sbin/ejabberdctl connected_users_number',
- 'memory_total' => '/usr/sbin/ejabberdctl memory total',
- 'memory_processes' => '/usr/sbin/ejabberdctl memory processes',
- 'memory_processes_used' => '/usr/sbin/ejabberdctl memory processes_used',
- 'memory_system' => '/usr/sbin/ejabberdctl memory system',
- 'memory_atom' => '/usr/sbin/ejabberdctl memory atom',
- 'memory_atom_used' => '/usr/sbin/ejabberdctl memory atom_used',
- 'memory_binary' => '/usr/sbin/ejabberdctl memory binary',
- 'memory_code' => '/usr/sbin/ejabberdctl memory code',
- 'memory_ets' => '/usr/sbin/ejabberdctl memory ets',
- 'ti_muc_room_memory' => '/usr/sbin/ejabberdctl mnesia_table_info muc_room memory',
- 'ti_muc_room_size' => '/usr/sbin/ejabberdctl mnesia_table_info muc_room size',
- 'ti_muc_online_room_memory' => '/usr/sbin/ejabberdctl mnesia_table_info muc_online_room memory',
- 'ti_muc_online_room_size' => '/usr/sbin/ejabberdctl mnesia_table_info muc_online_room size',
- 'ti_session_memory' => '/usr/sbin/ejabberdctl mnesia_table_info session memory',
- 'ti_session_size' => '/usr/sbin/ejabberdctl mnesia_table_info session size',
- #'ejabberd_presences' => '/usr/sbin/ejabberdctl ejabberd presences', // does not make sense - just example for future internal calls - this one always returns 1.
- }
- def run()
- command_to_run=ARGV[0]
- file_retention=ARGV[1].to_i
- unless ARGV.length == 2 and METRICS_COMMANDS.include?(ARGV[0]) and file_retention.is_a? Integer
- puts "Two Arguments: Metric command and Minutes for retention \nUsage: #{__FILE__} [#{METRICS_COMMANDS.keys.join('|')}]"
- exit(1)
- else
- unless is_file_ok and is_file_new(file_retention)
- puts "file not ok, Lets create a new one"
- if File.exists?($filename)
- system "rm #{$filename}"
- end
- command_output = Hash.new(0)
- METRICS_COMMANDS.each {|key,each| command_output[key]=run_command(each) } # runs call commands and stores the output into a hash
- File.write($filename, command_output.to_yaml) # Writes the output hash to file called $filename
- from_file = YAML.load_file('zabbix_ejjabard_checks.yml') # loads the created file
- puts from_file[command_to_run] # outputs the value from the file
- else
- puts "the file we have is ok, lets work with it"
- from_file = YAML.load_file('zabbix_ejjabard_checks.yml') # loads the created file
- puts from_file[command_to_run] # outputs the value from the file
- end
- end
- end
- def is_file_old(minutes)
- return ((Time.now - File.stat($filename).mtime) / 60) >= minutes # True if greater than 1
- end
- def is_file_new(minutes)
- return ((Time.now - File.stat($filename).mtime) / 60) < minutes # True if greater than 1
- end
- def is_file_ok
- return File.exists?($filename) # True is file is ok
- end
- def run_command(cmd)
- return exec(cmd) #Return information to zabbix
- end
- end
- main = Main.new
- main.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement