Class: Roebe::ParseApacheLog

Inherits:
Base show all
Defined in:
lib/roebe/classes/parse_apache_log/constants.rb,
lib/roebe/classes/parse_apache_log/parse_apache_log.rb

Overview

Roebe::ParseApacheLog

Constant Summary collapse

T =
#

T

#
"\t"
PC =
#

PC

#
ENV['PC'].to_s
PROGRAMS_DIR =
#

PROGRAMS_DIR

#
ENV['MY_PROGRAMS'].to_s
SYSBIN =
#

SYSBIN

#
ENV['MY_SYSBIN'].to_s
USR_ERROR_LOG =
#

USR_ERROR_LOG

#
'/usr/logs/error_log'
APACHE_LOG_FILE =
#

APACHE_LOG_FILE

#
'/opt/lampp/logs/error_log'
LIGHTTPD_LOG_FILE =

LIGHTTPD_LOG_FILE = ‘/Depot/Temp/lighty/lighttpd_breakage.log’

#

LIGHTTPD_LOG_FILE

#
PROGRAMS_DIR+'/Lighttpd/Current/logs/lighttpd_breakage.log'
PROGRAMS_LOG_FILE =
'/Programs/Httpd/Current/logs/error_log'
REQUESTED_LOCATION_OF_LOG_FILE =
%w( file? file f? ? )
HTTPD_CONF =
#

HTTPD_CONF

#
PC+'/APACHE/httpd.conf'

Constants inherited from Base

Base::COLOURS, Base::HOME_DIRECTORY_OF_USER_X, Base::N, Base::NAMESPACE

Instance Method Summary collapse

Methods inherited from Base

#actions, #append_this_onto_that_file, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #burlywood, #call_next, #chdir, #cheerful_guy, #cliner, #complex_esystem, #convert_global_env, #copy_directory, #cornflowerblue, #create_directory, #crimson, #current_month?, #current_time?, #current_year?, #cyan, #darkcyan, #darkgreen, #darkkhaki, #darkslateblue, #deeppink, #delete_symlink, #do_not_use_the_base_colours, #do_use_the_base_colours, #e, #ecomment, #editor_to_use?, #efancy, #eimp, #emphasis, #ensure_main_encoding, #ensure_utf_encoding, #eparse, #erev, #esteelblue, #etomato, #ewarn, #firebrick, #get_current_day, #get_current_month, #get_files_and_directories, #get_files_from, #get_german_name_for_this_weekday, #gold, #green, #grey, #hh_mm_ss, #hh_mm_ss_day_month_year, #home_dir?, #home_directory_of_user_x?, #infer_the_namespace, #internal_hash?, #is_a_directory?, #is_a_file?, #is_a_jpg_file?, #is_an_image_file?, #is_archive?, #is_audio_file?, #is_in_studium_dir?, #is_multimedia_file?, #is_on_roebe?, #is_on_windows?, #is_studium_available?, #is_symlink?, #is_this_a_ruby_file?, #is_video_file?, #iso_encoding?, #le, #left_colour, #lightblue, #lightgreen, #lightseagreen, #lightsteelblue, #lime, #limegreen, #localhost_to_data, #log_directory?, #main_encoding?, #mediumorchid, #mediumpurple, #mediumseagreen, #mediumslateblue, #mediumspringgreen, #mediumturquoise, #mkdir_p, #mv, #n_days_in_this_month, #n_pages_in_this_pdf_file?, #namespace?, #no_file_exists_at, #olivedrab, #open_in_browser, #opne, #opnesystem, #opnn, #orange, #orchid, #palegoldenrod, #palevioletred, #pink, #powderblue, #programs_dir?, #project_base_dir?, #random_html_colour, #rds, #read_file_in_iso_encoding, #read_file_via_the_default_encoding, #read_lines_via_iso_encoding, #readlines_with_main_encoding, #red, #register_sigint, #remove_directory, #remove_file, #rename_kde_konsole_tab, #replace_localhost_with_data, #report_pwd, #require_rescue, #reset_the_internal_hash, #return_all_directories_from_this_directory, #return_all_files_from_this_directory, #return_current_directory, #return_dd_mm_yyyy, #return_file_or_directory_of, #return_files_from_pwd, #return_last_part_of_the_current_directory, #return_utc, #rev, #right_arrow?, #right_colour, #roebe_log_directory?, #rosybrown, #royalblue, #ruby_base_directory?, #run_in_background, #run_rcfiles_then_run_ata_via_qdbus, #sandybrown, #sdir, #seagreen, #set_be_verbose, #set_xorg_buffer, #sfancy, #sfile, #silent_redirection?, #simp, #simple_esystem, #skyblue, #slateblue, #slategray, #springgreen, #steelblue, #swarn, #symlink, #teal, #temp_dir?, #to_camelcase, #to_counted_hash, #tomato, #touch, #try_to_require_the_beautiful_url_gem, #try_to_require_the_html_template, #try_to_require_the_open_gem, #try_to_require_the_program_information_gem, #try_to_require_the_xorg_buffer, #use_colours?, #verbose_truth, #weekday?, #word_wrap, #write_what_into, #yellow

Methods included from Base::CommandlineArguments

#append_onto_the_commandline_arguments, #clear_commandline_arguments, #commandline_arguments?, #commandline_arguments_as_string?, #commandline_arguments_without_leading_hyphens?, #first_argument?, #first_argument_without_leading_hyphens?, #has_an_argument_been_passed?, #remove_hyphened_arguments_from_the_commandline_arguments, #return_commandline_arguments_with_leading_hyphens, #second_argument?, #set_commandline_arguments

Constructor Details

#initialize(input = :lighty, run_already = true) ⇒ ParseApacheLog

#

initialize

#


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 55

def initialize(
    input       = :lighty,
    run_already = true
  )
  reset
  set_commandline_arguments(
    input
  )
  set_log_file(input)
  run if run_already
end

Instance Method Details

#array_possible_log_locations?Boolean

#

array_possible_log_locations?

#

Returns:

  • (Boolean)


127
128
129
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 127

def array_possible_log_locations?
  @array_possible_log_locations
end

#copy_file(original, target) ⇒ Object

#

copy_file (copy tag)

Copy a file with this method.

#


151
152
153
154
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 151

def copy_file(original, target)
  remove(target) if File.exist? target
  FileUtils.cp(original, target)
end

#determine_which_log_file_to_use(i = LIGHTTPD_LOG_FILE) ⇒ Object

#

determine_which_log_file_to_use

#


159
160
161
162
163
164
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 159

def determine_which_log_file_to_use(
    i = LIGHTTPD_LOG_FILE
  )
  i = USR_ERROR_LOG if i.nil?
  return i
end

#display_content(shall_we_replace = true) ⇒ Object

#

display_content

This will display the value of @data.

#


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 204

def display_content(shall_we_replace = true)
  if @data
    e 'Content of `'+sfile(@log_file)+'`.'
    @data.each {|log_entry|
      if shall_we_replace
        next if log_entry.include? 'mod_mono'
        # ================================================================= #
        # === Tap into class FilterApacheLog next.
        # ================================================================= #
        log_entry = Roebe::FilterApacheLog.filter(log_entry) # bl $RUBY_TOOLS/filter_apache_log.rb
      end
      begin
        log_entry = log_entry.tr(T,'') if remove_tabs?
      rescue ArgumentError => error
        pp error
      end
      e '  '+log_entry
    }
  end
end

#lighty?Boolean

#

lighty?

#

Returns:

  • (Boolean)


195
196
197
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 195

def lighty?
  LIGHTTPD_LOG_FILE
end

#log?Boolean

#

log_file?

#

log?

Returns:

  • (Boolean)


281
282
283
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 281

def log_file?
  @log_file
end

#log_file?Boolean Also known as: log_file

#

log_file?

#

Returns:

  • (Boolean)


279
280
281
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 279

def log_file?
  @log_file
end

#logfile?Boolean

#

log_file?

#

logfile?

Returns:

  • (Boolean)


282
283
284
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 282

def log_file?
  @log_file
end
#

menu (menu tag)

#


294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 294

def menu(
    i = return_hyphenated_commandline_arguments
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i # case tag
    # ===================================================================== #
    # === relog --open
    # ===================================================================== #
    when /^-?-?open/
      begin
        require 'open'
      rescue LoadError; end
      _ = array_possible_log_locations?.first
      if File.exist? _
        Open.in_editor(_) {{ use_this_editor: :vim }}
      else
        no_file_exists_at(_)
      end
    end
  end
end

#read_in_datasetObject

#

read_in_dataset

This method will read in the dataset (and store it in the @data variable. As of Jul 2011 we also have basic error handling here.

#


172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 172

def read_in_dataset
  if File.exist? @log_file
    if File.size?(@log_file) == 0
      e 'The file `'+sfile(@log_file)+'` exists, but it is empty.'
      e 'That probably means that it is not the proper error-log file.'
    else
      @data = File.readlines(@log_file)
    end
  else
    opn; e swarn('The file at `'+sfile(@log_file)+'` does not exist.')
  end
end

#remove_tabs?Boolean

#

remove_tabs?

#

Returns:

  • (Boolean)


188
189
190
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 188

def remove_tabs?
  true
end

#resetObject

#

reset (reset tag)

#


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 70

def reset
  super()
  # ======================================================================= #
  # === @data
  # ======================================================================= #
  @data = nil
  # ======================================================================= #
  # === @array_possible_log_locations
  # ======================================================================= #
  @array_possible_log_locations = [
    USR_ERROR_LOG,
    APACHE_LOG_FILE,
    LIGHTTPD_LOG_FILE,
    PROGRAMS_LOG_FILE
  ]
  # ======================================================================= #
  # Next, reject all locations that can not be true.
  # ======================================================================= #
  @array_possible_log_locations.select! {|entry|
    File.exist? entry
  }
  set_log_file # Must come after @array_possible_log_location was initialized.
end

#restart_apacheObject

#

restart_apache

This will try to restart the apache webserver.

#


136
137
138
139
140
141
142
143
144
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 136

def restart_apache
  @apache_log_file = APACHE_LOG_FILE
  esystem SYSBIN+'/apachectl stop'
  if File.exist? HTTPD_CONF
    copy_file(HTTPD_CONF, PROGRAMS_DIR+'/Httpd/Current/conf/')
  end
  GenerateRewriteRules.new
  esystem SYSBIN+'/apachectl stop'
end

#runObject

#

run (run tag)

#


321
322
323
324
325
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 321

def run
  menu
  read_in_dataset
  display_content
end

#set_log_file(i = :default) ⇒ Object

#

set_log_file

Set which log file to use.

#


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 230

def set_log_file(
    i = :default
  )
  i = i.first if i.is_a? Array
  if i and i.to_s.start_with?('--')
    i = 'default' # In this case overrule it.
  end
  case i.to_s.downcase # case tag
  when 'default','usr','ulog'
    i = USR_ERROR_LOG
  when 'apache'
    i = APACHE_LOG_FILE
  # === lighty
  when 'lighty',
       'lighttpd'
    i = lighty?
  when 'programs'
    i = PROGRAMS_LOG_FILE
  when 'clear','restart','relog'
    try_to_remove_this_file(log_file?)
    restart_apache # Added this in AUG 2011.
    exit
  # ======================================================================= #
  # === relog --help
  # ======================================================================= #
  when /^-?-?help$/
    show_help
    exit
  when *REQUESTED_LOCATION_OF_LOG_FILE, 'location?'
    e 'The log file can be found at '+N+'  '+
       sfile(log_file?)
    exit
  end
  i = determine_which_log_file_to_use if i.nil?
  i = determine_which_log_file_to_use if i == :default
  i = i.to_s
  i = i.dup
  if i.empty? # Safeguard.
    i = determine_which_log_file_to_use
  end
  unless File.exist? i # Try hardcore overrule here.
    i = PROGRAMS_LOG_FILE if File.exist? PROGRAMS_LOG_FILE
  end
  @log_file = i # Finally, set the location of the log file.
end

#show_helpObject

#

show_help (help tag)

#


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 97

def show_help
  e 'We will try to show the location of the log file.'
  e
  e 'Use one of these to find it:'
  e
  e '   '+sfancy(REQUESTED_LOCATION_OF_LOG_FILE.join(', '))
  e
  e 'Furthermore the following commandline options are available:'
  e
  e '  --open # open the log in your favourit editor (internally '\
    'done via Open.in_editor())'
  e
end

#try_to_remove_this_file(f) ⇒ Object Also known as: remove

#

try_to_remove_this_file

This method will try to remove the passed file.

#


116
117
118
119
120
121
122
# File 'lib/roebe/classes/parse_apache_log/parse_apache_log.rb', line 116

def try_to_remove_this_file(f)
  if File.exist? f
    FileUtils.rm(f)
  else
    opn; e 'But the file `'+sfile(f)+'` does not exist.'
  end
end