Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- #
- # TH - 03/02/2012
- # v0.2 working
- require 'rubygems'
- require 'snmp'
- require 'csv'
- require 'fileutils'
- require 'net/ping/icmp'
- $serversfile = '/usr/local/share/bgfl-monitor/proxy-servers.txt'
- class Proxy
- def initialize(host, port, community, cluster, shortName, type, pollTime, rpsTotal, clientTotal, serverTotal)
- @host = host
- @port = port
- @community = community
- @cluster = cluster
- @shortName = shortName
- @type = type
- @old_pollTime = pollTime.to_f
- @old_rpsTotal = rpsTotal.to_i
- @old_clientTotal = clientTotal.to_i
- @old_serverTotal = serverTotal.to_i
- end
- def proxy_poll
- begin
- if @type=='mwg7'
- SNMP::Manager.open(:host => @host, :port => @port, :Community => @community) do |pollServer|
- @snmpVar = pollServer.get_value(["1.3.6.1.4.1.1230.2.7.2.2.1.0", "1.3.6.1.4.1.1230.2.7.2.3.1.0", "1.3.6.1.4.1.1230.2.7.2.2.5.0", "1.3.6.1.4.1.1230.2.7.2.3.5.0", "1.3.6.1.4.1.1230.2.7.2.2.4.0", "1.3.6.1.4.1.1230.2.7.2.3.4.0"])
- @snmpVar.push(Time.now.to_f)
- end
- elsif @type=='bc'
- SNMP::Manager.open(:host => @host, :port => @port, :Community => @community) do |pollServer|
- @snmpVar = pollServer.get_value(["1.3.6.1.4.1.3417.2.11.3.1.1.1.0", "1.3.6.1.4.1.3417.2.11.3.1.1.10.0", "1.3.6.1.4.1.3417.2.11.3.1.2.3.0" ])
- @snmpVar.push(Time.now.to_f)
- end
- elsif @type=='ww6'
- SNMP::Manager.open(:host => @host, :port => @port, :Community => @community) do |pollServer|
- @snmpVar = pollServer.get_value(["1.3.6.1.4.1.1457.2.2.1.1.1.1.2.4", "1.3.6.1.4.1.1457.2.2.1.1.1.1.3.4", "1.3.6.1.4.1.1457.2.2.1.1.2.1.3.4",])
- end
- self.poll_good
- else
- # We should never get here...
- puts 'Poll fail on Proxy type'
- end
- self.poll_good
- #$stats.store(@shortName, Hash.new())
- #$stats[@shortName].store('status', 'up')
- # Catches exceptons thrown by SNMP when a host is pingable, but not responding in some other way
- rescue
- self.poll_fail
- puts 'Some sort of issue with the SNMP poll...'
- end
- end
- def poll_good
- $stats.store(@shortName, Hash.new())
- $stats[@shortName].store('status', 'up')
- $status = 'up'
- end
- def poll_fail
- $stats.store(@shortName, Hash.new())
- $stats[@shortName].store('status', 's_down')
- $status = 'down'
- end
- def ping_fail
- $stats.store(@shortName, Hash.new())
- $stats[@shortName].store('status', 'p_down')
- $status = 'down'
- end
- def stats_calc
- # Take data gathard from SNMP and do math with it using info from last poll
- if @type =='mwg7'
- diffTime = @snmpVar[6].to_f-@old_pollTime
- new_pollTime = @snmpVar[6]
- new_rpsTotal = @snmpVar[0].to_i+@snmpVar[1].to_i
- new_clientTotal = @snmpVar[2].to_i+@snmpVar[3].to_i
- new_serverTotal = @snmpVar[4].to_i+@snmpVar[5].to_i
- diffRPS = new_rpsTotal-@old_rpsTotal
- diffClient = new_clientTotal-@old_clientTotal
- diffServer = new_serverTotal-@old_serverTotal
- elsif @type =='bc'
- new_pollTime = @snmpVar[3]
- new_rpsTotal = @snmpVar[0].to_i
- new_clientTotal = @snmpVar[1].to_i
- new_serverTotal = @snmpVar[2].to_i
- diffTime = @snmpVar[3].to_f-@old_pollTime
- diffRPS = @snmpVar[0].to_i-@old_rpsTotal
- diffClient = @snmpVar[1].to_i-@old_clientTotal
- diffServer = @snmpVar[2].to_i-@old_serverTotal
- elsif @type !='ww6'
- # We should never get here...
- puts "Calc fail on Proxy type #{@type}"
- end
- # This is due to dead clients not having these values set, we cant process the loop, so dont try
- if $stats[@shortName].has_value?("up")
- # Update server var with new data, and add to stats hash
- if @type !='ww6' then
- $stats[@shortName].store('rps', diffRPS/diffTime)
- $stats[@shortName].store('client', (diffClient/diffTime)/1048576)
- $stats[@shortName].store('server', (diffServer/diffTime)/1048576)
- $server[@host].store("pollTime", new_pollTime)
- $server[@host].store("rpsTotal", new_rpsTotal)
- $server[@host].store("clientTotal", new_clientTotal)
- $server[@host].store("serverTotal", new_serverTotal)
- else
- $stats[@shortName].store('rps', @snmpVar[0].to_i)
- $stats[@shortName].store('client', (@snmpVar[1].to_f)/1024)
- $stats[@shortName].store('server', (@snmpVar[2].to_f)/1024)
- end
- end
- end
- def write_stats
- # Open temp file to write new rcorded data back to and create header row
- File.open("/tmp/servers.txt.tmp", "w") do |row|
- row.puts "host,port,community,cluster,shortName,type,pollTime,rpsTotal,clientTotal,serverTotal"
- $server.each_key do |host|
- #Pull server data out of var and assign to a short name, write back to temp file
- t = $server[host]
- row.puts [t["host"], t["port"], t["community"], t["cluster"], t["shortName"], t["type"], t["pollTime"], t["rpsTotal"], t["clientTotal"], t["serverTotal"]].to_a.join(',')
- end
- end
- # overwite old file. This is done this way as Ito reduce the chance of amending the file at the same time as the file being written back.
- #I plan to deamonise the process and ditch writing back in the long term
- FileUtils.mv("/tmp/servers.txt.tmp", $serversfile)
- end
- def write_html
- #Make my new stats html page
- File.open("/var/www/html/misc/proxy-table.html", "w") do |row|
- row.print "<html>\r\n<META HTTP-EQUIV='REFRESH' CONTENT='300'>\r\n<META HTTP-EQUIV='PRAGMA' CONTENT='NO-CACHE'>\r\n"
- row.print "<LINK REL='stylesheet' TYPE='text/css' HREF='https://nagios.bgfl.org/nagios/stylesheets/bgfl-noc.css'>\r\n"
- row.print "<body>\r\n"
- row.print "<table border=1 CLASS='status'>\r\n"
- row.print "<tr>\r\n"
- row.print "<th CLASS='status'>Server</th>\r\n"
- row.print "<th CLASS='status'>Client RPS</th>\r\n"
- row.print "<th CLASS='status'>Client MB/s</th>\r\n"
- row.print "<th CLASS='status'>Proxy MB/s</th>\r\n"
- row.print "</tr>\r\n"
- # This is to Zebra Stripe the table rows
- line = ['statusOdd', 'statusEven']
- i = 0
- # This takes the hash keys and puits them into an array where we can sort them. It then using var outputs rows into a file writer to build our HTML page
- a = Array.new
- $stats.each_key do |shortName|
- a.push(shortName)
- end
- a.sort.each do |var|
- t = $stats[var]
- row.print "<tr>\r\n"
- j = i.modulo(2)
- # This is to flag over loaded servers
- if t["status"] =='up' then
- if t["rps"] > 650 and t["rps"] < 700 then row.print "<tr CLASS='statusMed'>\r\n"
- elsif t["rps"] > 700 then row.print "<tr CLASS='statusMax'>\r\n"
- elsif t["client"] > 30 and t["client"] < 25 then row.print "<tr CLASS='statusMed'>\r\n"
- elsif t["client"] > 45 then row.print "<tr CLASS='statusMax'>\r\n"
- elsif t["server"] > 30 and t["server"] < 25 then row.print "<tr CLASS='statusMed'>\r\n"
- elsif t["server"] > 45 then row.print "<tr CLASS='statusMax'>\r\n"
- else row.print "<tr CLASS='#{line[j]}'>\r\n"
- end
- row.print "<td CLASS='status'>#{var}</td>\r\n"
- row.print "<td CLASS='statusStat'>"
- row.printf("%.0f", t["rps"])
- row.print "</td>\r\n"
- row.print "<td CLASS='statusStat'>"
- row.printf("%.02f", t["client"])
- row.print "</td>\r\n"
- row.print "<td CLASS='statusStat'>"
- row.printf("%.02f", t["server"])
- row.print "</td>\r\n"
- elsif t["status"] =='s_down' then
- #this is to handle down servers
- row.print "<tr CLASS='statusMax'>\r\n"
- row.print "<td CLASS='status'>#{var}</td>\r\n"
- row.print "<td CLASS='statusStat'>SNMP</td>\r\n"
- row.print "<td CLASS='statusStat'>DOWN</td>\r\n"
- row.print "<td CLASS='statusStat'>DOWN</td>\r\n"
- else
- #this is to handle down servers
- row.print "<tr CLASS='statusMax'>\r\n"
- row.print "<td CLASS='status'>#{var}</td>\r\n"
- row.print "<td CLASS='statusStat'>PING</td>\r\n"
- row.print "<td CLASS='statusStat'>DOWN</td>\r\n"
- row.print "<td CLASS='statusStat'>DOWN</td>\r\n"
- end
- row.print "</tr>\r\n"
- i = i + 1
- end
- row.print "<tr><td CLASS='statusFooter' colspan='4'>Last Updated #{Time.now.strftime("%T - %d/%m/%y")}</td></tr>\r\n"
- row.print "</body></html>\r\n"
- end
- end
- end
- head = nil
- $server = {}
- $stats = {}
- CSV.foreach($serversfile) do |row|
- if head
- tmp = {}
- head.zip row do |h,v|
- tmp[h] = v
- end
- $server[row.first] = tmp
- else
- head = row.each &:freeze
- end
- end
- $server.each_key do |host|
- t = $server[host]
- $proxy = Proxy.new(t["host"], t["port"], t["community"], t["cluster"], t["shortName"], t["type"], t["pollTime"], t["rpsTotal"], t["clientTotal"], t["serverTotal"])
- if Net::Ping::ICMP.new(host).ping? then
- $proxy.proxy_poll
- if $status !='down' then
- $proxy.stats_calc
- end
- else
- $proxy.ping_fail
- puts 'We cant ping the server'
- end
- end
- $proxy.write_html
- $proxy.write_stats
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement