Module: PcaprLocal

Defined in:
lib/pcapr_local/config.rb,
lib/environment.rb,
lib/pcapr_local.rb,
lib/pcapr_local/db.rb,
lib/pcapr_local/server.rb,
lib/pcapr_local/scanner.rb,
lib/pcapr_local/xtractr.rb,
lib/pcapr_local/xtractr/instance.rb

Overview

Defined Under Namespace

Modules: Config Classes: DB, Scanner, Server, Xtractr

Constant Summary collapse

ROOT =
File.expand_path(File.dirname(File.dirname(__FILE__)))
START_SCRIPT =
File.join(ROOT, 'bin/pcapr_start.rb')
Logger =

We share a single Logger across all of pcapr.Local.

Logger.new(STDOUT)
LOGFILE =

Recreate logger using configured log location.

"server.log"

Class Method Summary collapse

Class Method Details

.check_pid_file(file) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/pcapr_local.rb', line 120

def self.check_pid_file file
    if File.exist? file
        # If we get Errno::ESRCH then process does not exist and
        # we can safely cleanup the pid file.
        pid = File.read(file).to_i
        begin 
            Process.kill(0, pid)
        rescue Errno::ESRCH
            stale_pid = true
        rescue 
        end

        unless stale_pid 
            puts "Server is already running (pid=#{pid})"
            exit
        end
    end
end

.create_pid_file(file) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pcapr_local.rb', line 139

def self.create_pid_file file
    File.open(file, "w") { |f| f.puts Process.pid }

    # Remove pid file during shutdown
    at_exit do 
        Logger.info "Shutting down." rescue nil
        if File.exist? file
            File.unlink file
        end
    end
end

.get_db(config) ⇒ Object



69
70
71
# File 'lib/pcapr_local.rb', line 69

def self.get_db config
    PcaprLocal::DB.get_db config.fetch("couch")
end

.set_tshark_options(config) ⇒ Object



73
74
75
76
77
78
# File 'lib/pcapr_local.rb', line 73

def self.set_tshark_options config
    options = config['tshark']['options']
    return unless options
    
    Mu::Scenario::Pcap.reset_options(options)
end

.start(config = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pcapr_local.rb', line 80

def self.start config=nil
    config ||= PcaprLocal::Config.config
    
    # Check that server is not already running.
    check_pid_file config['pidfile']

    # Start logging.
    if config["log_dir"]
        start_logging config['log_dir']
    end
    start_msg = "Starting server at #{config['app']['host']}:#{config['app']['port']}"
    Logger.info start_msg
    puts start_msg
    puts "Log is at #{config['log_dir']}/#{LOGFILE}"

    # Deamonize
    unless config['debug_mode']
        puts "Moving server process to the background. Run 'stoppcapr' to stop the server."
        Process.daemon
    end

    # Create pid file that will be deleted when we shutdown.
    create_pid_file config['pidfile']
    
    # Override the default tshark options
    set_tshark_options(config)

    # Get database instance
    db = get_db config

    # Xtractr manager
    xtractr = start_xtractr config

    # Start scanner thread
    scanner = start_scanner config, db, xtractr

    # Start application server
    start_app config, db, scanner, xtractr
end

.start_app(config, db, scanner, xtractr) ⇒ Object

Start webserver UI/API



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pcapr_local.rb', line 54

def self.start_app config, db, scanner, xtractr
    app_config = config.fetch "app"
    root = File.expand_path(File.dirname(__FILE__))
    app_file = File.join(root, "pcapr_local/server.rb")
    PcaprLocal::Server.run! \
        :app_file => app_file,
        :dump_errors => true,
        :logging => true,
        :port    => app_config.fetch("port"), 
        :bind    => app_config.fetch("host"),
        :db      => db,
        :scanner => scanner,
        :xtractr => xtractr
end

.start_logging(log_dir) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pcapr_local.rb', line 21

def self.start_logging log_dir
    if log_dir and not log_dir.empty?
        if const_defined? :Logger
            remove_const :Logger
        end

        logfile = File.join(log_dir, LOGFILE)
        FileUtils.mkdir_p log_dir
        const_set :Logger, Logger.new(logfile, 5)
    end
end

.start_scanner(config, db, xtractr) ⇒ Object

Start file system scanner.



43
44
45
46
47
48
49
50
51
# File 'lib/pcapr_local.rb', line 43

def self.start_scanner config, db, xtractr
    scanner_config = config.fetch('scanner').merge( 
        "index_dir" => config.fetch("index_dir"),
        "pcap_dir" => config.fetch("pcap_dir"),
        "db" => db,
        "xtractr" => xtractr
    )
    PcaprLocal::Scanner.start scanner_config
end

.start_xtractr(config) ⇒ Object

Start xtractr instance manager.



34
35
36
37
38
39
40
# File 'lib/pcapr_local.rb', line 34

def self.start_xtractr config
    xtractr_config = config['xtractr'].merge(
        "index_dir" => config.fetch("index_dir"),
        "pcap_dir"  => config.fetch("pcap_dir")
    )
    Xtractr.new xtractr_config
end

.stop(config = nil) ⇒ Object

Sends SIGTERM to process in pidfile. Server should trap this and shutdown cleanly.



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/pcapr_local.rb', line 153

def self.stop config=nil
    user_config = Config.user_config_path
    if File.exist?(user_config)
        config = PcaprLocal::Config.config user_config
        pid_file = config["pidfile"]
        if pid_file and File.exist? pid_file
            pid = Integer(File.read(pid_file))
            Process.kill -15, -pid
        end
    end
end