Permalink
Browse files

updates to mysql slow queries to handle utc, added initial asterisk p…

…lugin
  • Loading branch information...
1 parent 9c2b320 commit 28a56eefa4a6f9035a02a95b9a065cbaae4a3b0e @itsderek23 itsderek23 committed Jun 1, 2011
Showing with 41 additions and 8 deletions.
  1. +26 −0 asterisk_monitoring/asterisk_monitoring.rb
  2. +15 −8 mysql_slow_queries/mysql_slow_queries.rb
@@ -0,0 +1,26 @@
+class MonitorAsterisk < Scout::Plugin
+
+ def get_core_channels
+ lines = `/usr/sbin/asterisk -rx 'core show channels'`.split("\n")
+ channel_lines = lines.select {|l| l =~ /active channels/}
+ call_lines = lines.select {|l| l =~ /active calls/}
+ call_lines[0].squeeze(" ").split(" ")
+ @report_hash[:active_channels] = channel_lines[0].squeeze(" ").split(" ")[0].to_i
+ @report_hash[:active_calls] = call_lines[0].squeeze(" ").split(" ")[0].to_i
+ end
+
+ def get_sip_peers
+ lines = `/usr/sbin/asterisk -rx 'sip show peers'`.split("\n")
+ sip_online_peers = lines.select {|l| l =~ /OK/}
+ @report_hash[:sip_agents] = sip_online_peers.select {|l| l =~ /^\d/}.count
+ @report_hash[:sip_trunks] = sip_online_peers.select {|l| l =~ /^\D/}.count
+ end
+
+ def build_report
+ @report_hash={}
+ get_core_channels
+ get_sip_peers
+ report(@report_hash)
+ end
+
+end
@@ -45,27 +45,33 @@ def build_report
last_run = memory(:last_run) || Time.now
minimum_query_time = option(:minimum_query_time).to_f
current_time = Time.now
-
+ last_run_entry_timestamp = memory(:last_run_entry_timestamp)
+ temp_timestamp=nil
+ latest_entry_timestamp=nil
+
# starts at the bottom of the log file, moving up
Elif.foreach(log_file_path) do |line|
if line =~ /^# Query_time: ([\d\.]+) .+$/
query_time = $1.to_f
all_queries << {:query_time => query_time, :sql => sql.reverse}
sql = []
elsif line =~ /^\# Time: \d+ (.*)$/
- t = Time.parse($1.split(' ')[0]) {|y| y < 100 ? y + 2000 : y}
-
- t2 = last_run
- if t < t2
+ # We now have a complete entry. capture its timestamp:
+ temp_timestamp = Time.parse($1.split(' ')[0]) {|y| y < 100 ? y + 2000 : y}
+ # if there was a last_run_entry_timestamp, we can quit based on comparing it to the current_entry_timestamp we just parsed.
+ if last_run_entry_timestamp && temp_timestamp <= last_run_entry_timestamp
break
elsif all_queries.any?
sq = all_queries.last
if sq[:query_time] >= minimum_query_time
# this query occurred after the last time this plugin ran and should be counted.
- slow_queries << sq.merge({:time_of_query => t})
+ slow_queries << sq.merge({:time_of_query => temp_timestamp})
end
end
- elsif line !~ /^\#/ # an SQL query
+ latest_entry_timestamp ||= temp_timestamp # latest_entry_timestamp will be the bottom timestamp in the log. We'll use it as the watermark for next run
+ # if there wasn't a last_entry_timestamp, we should break now that we have one complete log entry
+ break if last_run_entry_timestamp == nil
+ elsif line !~ /^(\#|use |SET timestamp)/ # an SQL query
sql << line
end
end
@@ -78,6 +84,7 @@ def build_report
alert( build_alert(slow_queries,log_file_path) )
end
remember(:last_run,Time.now)
+ remember(:last_run_entry_timestamp, latest_entry_timestamp || last_run_entry_timestamp)
rescue Errno::ENOENT => error
error("Unable to find the MySQL slow queries log file", "Could not find a MySQL slow queries log file at: #{option(:mysql_slow_log)}. Please ensure the path is correct.")
end
@@ -97,4 +104,4 @@ def build_alert(slow_queries,log_file_path)
end
{:subject => subj, :body => body}
end # build_alert
-end
+end

0 comments on commit 28a56ee

Please sign in to comment.