tmm1 (owner)

Revisions

  • e7639c tmm1 Sun Aug 02 14:35:46 -0700 2009
gist: 160215 Download_button fork
public
Description:
ruby wrapper for collectd's collection3 perl cgi scripts
Public Clone URL: git://gist.github.com/160215.git
Embed All Files: show embed
collection3.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'rubygems'
require 'eventmachine'
require 'evma_httpserver'
 
Dir.chdir(File.dirname(__FILE__))
 
class CGIWrapper < EM::Connection
  include EM::HttpServer
 
  def process_http_request
    p [Time.now, @http_request_uri]
 
    case @http_request_uri
    when '/'
      redirect '/cgi-bin/collection3/bin/index.cgi'
 
    when /(graph|index|json)\.cgi$/
      EM.system('sh', proc{ |cmd|
        cmd.send_data "cd bin && ./#{$1}.cgi\nexit\n"
      }, proc{ |result, exit|
        if exit.exitstatus == 0
          send_data "HTTP/1.0 200\r\n"
          send_data "Connection: close\r\n"
          send_data result
          close_connection_after_writing
        else
          not_found
        end
      })
 
    when /share\/(\w+\.(js|css|png))$/
      if File.exists?("share/#{$1}")
        send_data "HTTP/1.1 200\r\n"
        send_data "Connection: close\r\n"
        send_data "Content-Type: #{$2 == 'png' ? 'image' : 'text'}/#{$2}\r\n"
        send_data "Transfer-Encoding: chunked\r\n\r\n"
        stream_file_data "share/#{$1}", :http_chunks => true
        close_connection_after_writing
      else
        not_found
      end
 
    else
      not_found
    end
  end
 
  def not_found
    response = EM::DelegatedHttpResponse.new(self)
    response.status = 404
    response.content_type 'text/html'
    response.content = '<center><h1>Not Found.</h1></center>'
    response.send_response
  end
 
  def redirect url
    response = EM::DelegatedHttpResponse.new(self)
    response.send_redirect url
  end
end
 
EM.run{
  EM.start_server '127.0.0.1', 9876, CGIWrapper
}