Skip to content

Instantly share code, notes, and snippets.

@qmx
Forked from anonymous/spcmon-web.rb
Created February 26, 2010 14:31
Show Gist options
  • Save qmx/315748 to your computer and use it in GitHub Desktop.
Save qmx/315748 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'haml'
configure :development do
set :rra_path, ""
end
configure :production do
set :rra_path, "/app/cacti/spcmon/rra/"
end
get '/' do
haml :index
end
get '/agentes' do
gera './gera_grafico.sh -'
end
get '/agentes/:agente/:intervalo/:unidade/:cor/?' do |agente, intervalo, unidade, cor|
grafico agente, intervalo, unidade, cor
end
get '/agentes/hora' do
gera './gera_grafico_hora.sh -'
end
def gera(script)
content_type 'image/png'
IO.popen(script).read
end
def grafico agente, intervalo, unidade, cor
cmd = "rrdtool graph - --start \"now - #{intervalo} #{unidade}\" --end \"now\" DEF:#{options.rra_path}#{agente}=#{agente}.rrd:#{agente}:AVERAGE AREA:#{agente}##{cor} VDEF:#{agente}avg=#{agente},AVERAGE GPRINT:#{agente}avg:\"média #{agente} %5.3lfs\" HRULE:1#000000 HRULE:0.5#000000 -o --units=si -v \"segundos\" --title \"#{agente}\" -w 300 --x-grid SECOND:10:MINUTE:1:MINUTE:2:0:%H:%M --step 1"
gera cmd
end
require 'rubygems'
require 'net/ssh'
require 'file/tail'
require 'time'
trap "INT" do
STDERR.puts "sub pid #{$$} ^c"
exit 2
end
ativo = true
class SPCMon
@@bah = /(.+)\|(.+)\|(.+)/
@@re = /\[(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2}):(\d{2})\] exec_time=(\d+.\d+)s/
def self.parse(line)
md = @@re.match line
raise "error" if md.nil?
day = md[1]
month = md[2]
year = md[3]
hour = md[4]
min = md[5]
sec = md[6]
ts = Time.local(year, month, day, hour, min, sec).to_i
value = md[7].to_f
{ts => value}
end
def self.full_parse(line)
md = @@bah.match line
raise "err" if md.nil?
{md[2] => self.parse(md[3])}
end
end
re = /\[(\d{2})-(\d{2})-(\d{4}) (\d{2}):(\d{2}):(\d{2})\] exec_time=(\d+.\d+)s/
pipename = "/tmp/spcmon"
user = 'dcampos'
logfiles = ['serasa','SRF','SCPC','USECHEQUE']
`mkfifo #{pipename}`
def sei_la(items)
now = Time.new.to_i
items.select { |k,v| k > (now - 120) && k < (now - 60) }.to_a.each do |x|
timestamp = x[0]
agentes = x[1]
agentes.keys.each do |agente|
values = agentes[agente]
agentes.keys.each do |agente|
values = agentes[agente]
value = 0
if values.size == 1
value = values[0]
else
values.each do |v|
value = value + v.to_f
end
value = value.to_f / values.size
end
current = `rrdtool info #{agente.downcase}.rrd | grep last_update | cut -f 2 -d= `.to_i
cmd = "rrdtool update #{agente.downcase}.rrd #{timestamp}:#{value}"
if ativo && (timestamp > current)
system cmd
items.delete timestamp
end
end
end
end
class Repeat
def self.every seconds, *args, &block
begin
while true
t = Thread.new(args, &block)
hold = Thread.new { sleep seconds }
hold.join
end
rescue Interrupt
puts "interrupted"
end
end
end
fork do
items = {}
t = Thread.new do
File.open(pipename, 'r+') do |f|
f.extend(File::Tail)
f.interval = 1
f.tail do |line|
begin
item = SPCMon.full_parse(line)
key = item.keys[0]
ts = item[key].keys[0]
items[ts] = {} if items[ts].nil?
value = item[key][ts]
items[ts][key] = [] if items[ts][key].nil?
items[ts][key] << value
items[ts][key] = [] if items[ts][key].nil?
items[ts][key] << value
p items[ts][key]
rescue
end
end
end
end
Repeat.every(1, items) do |args|
items = args[0]
now = Time.now.to_i
items.keys.sort.select { |key| (key > now - 90) && (key < now - 30) }.each do |ts|
items[ts].keys.each do |agente|
p agente
value = 0.0
items[ts][agente].each do |v|
value = value + v
end
value = value / items[ts][agente].size
current = `rrdtool info #{agente.downcase}.rrd | grep last_update | cut -f 2 -d= `.to_i
cmd = "rrdtool update #{agente.downcase}.rrd #{ts}:#{value}"
system cmd if ts > current
items[ts].delete agente
end
end
end
t.join
end
['10.198.14.58','10.198.14.59','10.198.14.60'].each do |host|
fork do
out = open(pipename, 'a+')
Net::SSH.start host, user do |ssh|
logfiles.each do |logfile|
ssh.open_channel do |channel|
channel.exec "tail -f /u01/app/lsnrptr/logs/com#{logfile}.log" do |ch, sucess|
raise "impossivel executar comando" unless sucess
ch.on_data do |c, data|
md = re.match data
if not md.nil?
out.puts "#{host}|#{logfile}|#{md}"
out.flush
end
end
end
end
channel.wait
end
end
end
end
end
pid = Process.wait
puts "#{pid}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment