Advertisement
trishoar

proxy-table.rb

May 1st, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.17 KB | None | 0 0
  1. #!/usr/bin/ruby
  2. #
  3. # TH - 03/02/2012
  4. # v0.2 working
  5.  
  6. require 'rubygems'
  7. require 'snmp'
  8. require 'csv'
  9. require 'fileutils'
  10. require 'net/ping/icmp'
  11.  
  12. $serversfile = '/usr/local/share/bgfl-monitor/proxy-servers.txt'
  13.  
  14. class Proxy
  15.   def initialize(host, port, community, cluster, shortName, type, pollTime, rpsTotal, clientTotal, serverTotal)
  16.     @host = host
  17.     @port = port
  18.     @community = community
  19.     @cluster = cluster
  20.     @shortName = shortName
  21.     @type = type
  22.     @old_pollTime = pollTime.to_f
  23.     @old_rpsTotal = rpsTotal.to_i
  24.     @old_clientTotal = clientTotal.to_i
  25.     @old_serverTotal = serverTotal.to_i
  26.   end
  27.   def proxy_poll
  28.     begin
  29.       if @type=='mwg7'
  30.         SNMP::Manager.open(:host => @host, :port => @port, :Community => @community) do |pollServer|
  31.           @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"])
  32.           @snmpVar.push(Time.now.to_f)
  33.         end
  34.       elsif @type=='bc'
  35.         SNMP::Manager.open(:host => @host, :port => @port, :Community => @community) do |pollServer|
  36.           @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" ])
  37.           @snmpVar.push(Time.now.to_f)
  38.         end
  39.       elsif @type=='ww6'
  40.         SNMP::Manager.open(:host => @host, :port => @port, :Community => @community) do |pollServer|
  41.           @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",])
  42.         end
  43.         self.poll_good
  44.       else
  45.         # We should never get here...
  46.         puts 'Poll fail on Proxy type'
  47.       end
  48.       self.poll_good
  49.       #$stats.store(@shortName, Hash.new())
  50.       #$stats[@shortName].store('status', 'up')
  51.     # Catches exceptons thrown by SNMP when a host is pingable, but not responding in some other way
  52.     rescue
  53.       self.poll_fail
  54.       puts 'Some sort of issue with the SNMP poll...'
  55.     end
  56.   end
  57.   def poll_good
  58.     $stats.store(@shortName, Hash.new())
  59.     $stats[@shortName].store('status', 'up')
  60.     $status = 'up'
  61.   end
  62.   def poll_fail
  63.     $stats.store(@shortName, Hash.new())
  64.     $stats[@shortName].store('status', 's_down')
  65.     $status = 'down'
  66.   end
  67.   def ping_fail
  68.     $stats.store(@shortName, Hash.new())
  69.     $stats[@shortName].store('status', 'p_down')
  70.     $status = 'down'
  71.   end
  72.   def stats_calc
  73.     # Take data gathard from SNMP and do math with it using info from last poll
  74.     if @type =='mwg7'
  75.       diffTime = @snmpVar[6].to_f-@old_pollTime
  76.       new_pollTime = @snmpVar[6]
  77.       new_rpsTotal = @snmpVar[0].to_i+@snmpVar[1].to_i
  78.       new_clientTotal = @snmpVar[2].to_i+@snmpVar[3].to_i
  79.       new_serverTotal = @snmpVar[4].to_i+@snmpVar[5].to_i
  80.       diffRPS = new_rpsTotal-@old_rpsTotal
  81.       diffClient = new_clientTotal-@old_clientTotal
  82.       diffServer = new_serverTotal-@old_serverTotal
  83.     elsif @type =='bc'
  84.       new_pollTime = @snmpVar[3]
  85.       new_rpsTotal = @snmpVar[0].to_i
  86.       new_clientTotal = @snmpVar[1].to_i
  87.       new_serverTotal = @snmpVar[2].to_i
  88.       diffTime = @snmpVar[3].to_f-@old_pollTime
  89.       diffRPS = @snmpVar[0].to_i-@old_rpsTotal
  90.       diffClient = @snmpVar[1].to_i-@old_clientTotal
  91.       diffServer = @snmpVar[2].to_i-@old_serverTotal
  92.     elsif @type !='ww6'
  93.       # We should never get here...
  94.       puts "Calc fail on Proxy type #{@type}"
  95.     end
  96.  
  97.     # This is due to dead clients not having these values set, we cant process the loop, so dont try
  98.     if $stats[@shortName].has_value?("up")
  99.       # Update server var with new data, and add to stats hash
  100.       if @type !='ww6' then
  101.         $stats[@shortName].store('rps', diffRPS/diffTime)
  102.         $stats[@shortName].store('client', (diffClient/diffTime)/1048576)
  103.         $stats[@shortName].store('server', (diffServer/diffTime)/1048576)
  104.         $server[@host].store("pollTime", new_pollTime)
  105.         $server[@host].store("rpsTotal", new_rpsTotal)
  106.         $server[@host].store("clientTotal", new_clientTotal)
  107.         $server[@host].store("serverTotal", new_serverTotal)
  108.       else
  109.         $stats[@shortName].store('rps', @snmpVar[0].to_i)
  110.         $stats[@shortName].store('client', (@snmpVar[1].to_f)/1024)
  111.         $stats[@shortName].store('server', (@snmpVar[2].to_f)/1024)
  112.       end
  113.     end
  114.   end
  115.   def write_stats
  116.     # Open temp file to write new rcorded data back to and create header row
  117.     File.open("/tmp/servers.txt.tmp", "w") do |row|
  118.       row.puts "host,port,community,cluster,shortName,type,pollTime,rpsTotal,clientTotal,serverTotal"
  119.       $server.each_key do |host|
  120.       #Pull server data out of var and assign to a short name, write back to temp file
  121.       t  = $server[host]
  122.         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(',')
  123.       end
  124.     end
  125.     # 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.
  126.     #I plan to deamonise the process and ditch writing back in the long term
  127.     FileUtils.mv("/tmp/servers.txt.tmp", $serversfile)
  128.   end
  129.   def write_html
  130.     #Make my new stats html page
  131.     File.open("/var/www/html/misc/proxy-table.html", "w") do |row|
  132.       row.print "<html>\r\n<META HTTP-EQUIV='REFRESH' CONTENT='300'>\r\n<META HTTP-EQUIV='PRAGMA' CONTENT='NO-CACHE'>\r\n"
  133.       row.print "<LINK REL='stylesheet' TYPE='text/css' HREF='https://nagios.bgfl.org/nagios/stylesheets/bgfl-noc.css'>\r\n"
  134.       row.print "<body>\r\n"
  135.       row.print "<table border=1 CLASS='status'>\r\n"
  136.       row.print "<tr>\r\n"
  137.       row.print "<th CLASS='status'>Server</th>\r\n"
  138.       row.print "<th CLASS='status'>Client RPS</th>\r\n"
  139.       row.print "<th CLASS='status'>Client MB/s</th>\r\n"
  140.       row.print "<th CLASS='status'>Proxy MB/s</th>\r\n"
  141.       row.print "</tr>\r\n"
  142.       # This is to Zebra Stripe the table rows
  143.       line = ['statusOdd', 'statusEven']
  144.       i = 0
  145.       # 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
  146.       a = Array.new
  147.       $stats.each_key do |shortName|
  148.         a.push(shortName)
  149.       end
  150.       a.sort.each do |var|
  151.         t = $stats[var]
  152.         row.print "<tr>\r\n"
  153.         j = i.modulo(2)
  154.         # This is to flag over loaded servers
  155.         if t["status"] =='up' then
  156.           if  t["rps"] > 650 and t["rps"] < 700 then row.print "<tr CLASS='statusMed'>\r\n"
  157.             elsif t["rps"] > 700 then row.print "<tr CLASS='statusMax'>\r\n"
  158.             elsif t["client"] > 30 and t["client"] < 25 then row.print "<tr CLASS='statusMed'>\r\n"
  159.             elsif t["client"] > 45 then row.print "<tr CLASS='statusMax'>\r\n"
  160.             elsif t["server"] > 30 and t["server"] < 25 then row.print "<tr CLASS='statusMed'>\r\n"
  161.             elsif t["server"] > 45 then row.print "<tr CLASS='statusMax'>\r\n"
  162.             else row.print "<tr CLASS='#{line[j]}'>\r\n"
  163.           end
  164.           row.print "<td CLASS='status'>#{var}</td>\r\n"
  165.           row.print "<td CLASS='statusStat'>"
  166.           row.printf("%.0f", t["rps"])
  167.           row.print "</td>\r\n"
  168.           row.print "<td CLASS='statusStat'>"
  169.           row.printf("%.02f", t["client"])
  170.           row.print "</td>\r\n"
  171.           row.print "<td CLASS='statusStat'>"
  172.           row.printf("%.02f", t["server"])
  173.           row.print "</td>\r\n"
  174.     elsif t["status"] =='s_down' then
  175.         #this is to handle down servers
  176.           row.print "<tr CLASS='statusMax'>\r\n"
  177.           row.print "<td CLASS='status'>#{var}</td>\r\n"
  178.           row.print "<td CLASS='statusStat'>SNMP</td>\r\n"
  179.           row.print "<td CLASS='statusStat'>DOWN</td>\r\n"
  180.           row.print "<td CLASS='statusStat'>DOWN</td>\r\n"
  181.         else
  182.         #this is to handle down servers
  183.           row.print "<tr CLASS='statusMax'>\r\n"
  184.           row.print "<td CLASS='status'>#{var}</td>\r\n"
  185.           row.print "<td CLASS='statusStat'>PING</td>\r\n"
  186.           row.print "<td CLASS='statusStat'>DOWN</td>\r\n"
  187.           row.print "<td CLASS='statusStat'>DOWN</td>\r\n"
  188.         end
  189.         row.print "</tr>\r\n"
  190.         i = i + 1
  191.       end
  192.       row.print "<tr><td CLASS='statusFooter' colspan='4'>Last Updated #{Time.now.strftime("%T - %d/%m/%y")}</td></tr>\r\n"
  193.       row.print "</body></html>\r\n"
  194.     end
  195.   end
  196. end
  197.  
  198. head = nil
  199. $server = {}
  200. $stats = {}
  201.  
  202. CSV.foreach($serversfile) do |row|
  203.   if head
  204.     tmp = {}
  205.     head.zip row do |h,v|
  206.       tmp[h] = v
  207.     end
  208.     $server[row.first] = tmp
  209.   else
  210.     head = row.each &:freeze
  211.   end
  212. end
  213.  
  214. $server.each_key do |host|
  215.   t  = $server[host]
  216.   $proxy = Proxy.new(t["host"], t["port"], t["community"], t["cluster"], t["shortName"], t["type"], t["pollTime"], t["rpsTotal"], t["clientTotal"], t["serverTotal"])
  217.   if Net::Ping::ICMP.new(host).ping? then
  218.     $proxy.proxy_poll
  219.     if $status !='down' then
  220.       $proxy.stats_calc
  221.     end
  222.   else
  223.     $proxy.ping_fail
  224.     puts 'We cant ping the server'
  225.   end
  226. end
  227. $proxy.write_html
  228. $proxy.write_stats
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement