<commit_before>provides "apache2"

require_plugin 'linux::lsb'

def parse_apache_output(apache_command)
  return @parsed_apache if @parsed_apache
  response = {}
  output = retrieve_apache_output(apache_command)
  output[:stdout].each do |line|
    case line
    when /-D HTTPD_ROOT=["']?(.+?)["']?$/
      response[:config_path] = $1
    when /Server version:\s(.+?)?$/
      response[:version] = $1
    when /Server MPM:\s(.+?)?$/
      response[:mpm] = $1.strip.downcase
    when /-D SERVER_CONFIG_FILE=["']?(.+?)["']?$/
      response[:config_file] = $1.strip
    end
  end

  output[:stderr].each do |line|
    case line
    when /WARNING: Require MaxClients > 0, setting to\s(.+?)?$/
      response[:max_clients] = $1.to_i
    when /Syntax OK/
      response[:syntax_ok] = true
    when /Syntax error\s(.+?)?$/
      response[:syntax_ok] = false
      errors = $1.split(": ")
      errors[0] = "Syntax error " + errors[0]
      response[:syntax_errors] = errors
    end
  end

  return @parsed_apache = response
end

def retrieve_apache_output(apache_command)
  output = {}
  popen4("#{apache_command} -V") do |pid, stdin, stdout, stderr|
    stdin.close
    output[:pid] = pid
    output[:stdout] = stdout
  end
  popen4("#{apache_command} -S") do |pid, stdin, stdout, stderr|
    stdin.close
    output[:stderr] = stderr
  end
  return output
end

def count_apache_clients(apache_command)
  command = "ps -eo euser,ruser,suser,fuser,f,cmd |grep #{apache_command}|grep -v grep|wc -l"
  status, stdout, stderr = run_command(:no_status_check => true,
                                       :command => command)
  return stdout.to_i
end

def find_apache_executable(os_name)
  if ["ubuntu", "debian"].include?(os_name)
    status, stdout, stderr = run_command(:no_status_check => true,
                                         :command => "which apache2")
    apache2_bin = stdout.strip
  elsif ["rhel", "centos"].include?(os_name)
    status, stdout, stderr = run_command(:no_status_check => true,
                                         :command => "which httpd")
    apache2_bin = stdout.strip
  else
    raise(RuntimeError, "Apache test cannot run on os type #{os_name}")
  end

  return apache2_bin unless apache2_bin.empty?
end

def find_apache_user(os_name)
  if ["ubuntu", "debian"].include?(os_name)
    status, stdout, stderr = run_command(:no_status_check => true,
                                         :command => "ps -ef|awk '/apache2/ && !/root/ {print $1}' | uniq")
    apache_user = stdout.strip
  elsif ["rhel", "centos"].include?(os_name)
    status, stdout, stderr = run_command(:no_status_check => true,
                                         :command => "ps -ef|awk '/httpd/ && !/root/ {print $1}' | uniq")
    apache_user = stdout.strip
  else
    raise(RuntimeError, "Apache test cannot run on os type #{os_name}")
  end

  return apache_user unless apache_user.empty?
end


if apache2_bin = find_apache_executable(lsb[:id].downcase)
  apache2 Mash.new
  apache2[:bin] = apache2_bin
  apache2[:clients] = count_apache_clients(apache2_bin)
  apache2[:user] = find_apache_user(lsb[:id].downcase)
  apache2.merge!(parse_apache_output(apache2_bin))

  apache2[:config_file] = apache2[:config_path] + "/" + apache2[:config_file]

  case apache2[:mpm]
  when "prefork"
    command = "grep -A8 -i mpm_prefork_module #{apache2[:config_file]}|grep MaxClients"
    status, stdout, stderr = run_command(:no_status_check => true,
                                         :command => command)
    max_clients = stdout.strip<commit_msg>Use platform instead of lsb<commit_after>provides "apache2"

require_plugin 'linux::platform'

def parse_apache_output(apache_command)
  return @parsed_apache if @parsed_apache
  response = {}
  output = retrieve_apache_output(apache_command)
  output[:stdout].each do |line|
    case line
    when /-D HTTPD_ROOT=["']?(.+?)["']?$/
      response[:config_path] = $1
    when /Server version:\s(.+?)?$/
      response[:version] = $1
    when /Server MPM:\s(.+?)?$/
      response[:mpm] = $1.strip.downcase
    when /-D SERVER_CONFIG_FILE=["']?(.+?)["']?$/
      response[:config_file] = $1.strip
    end
  end

  output[:stderr].each do |line|
    case line
    when /WARNING: Require MaxClients > 0, setting to\s(.+?)?$/
      response[:max_clients] = $1.to_i
    when /Syntax OK/
      response[:syntax_ok] = true
    when /Syntax error\s(.+?)?$/
      response[:syntax_ok] = false
      errors = $1.split(": ")
      errors[0] = "Syntax error " + errors[0]
      response[:syntax_errors] = errors
    end
  end

  return @parsed_apache = response
end

def retrieve_apache_output(apache_command)
  output = {}
  popen4("#{apache_command} -V") do |pid, stdin, stdout, stderr|
    stdin.close
    output[:pid] = pid
    output[:stdout] = stdout
  end
  popen4("#{apache_command} -S") do |pid, stdin, stdout, stderr|
    stdin.close
    output[:stderr] = stderr
  end
  return output
end

def count_apache_clients(apache_command)
  command = "ps -eo euser,ruser,suser,fuser,f,cmd |grep #{apache_command}|grep -v grep|wc -l"
  status, stdout, stderr = run_command(:no_status_check => true,
                                       :command => command)
  return stdout.to_i
end

def find_apache_executable(platform_family)
  if platform_family == "debian"
    status, stdout, stderr = run_command(:no_status_check => true,
                                         :command => "which apache2")
    apache2_bin = stdout.strip
  elsif platform_family == "rhel"
    status, stdout, stderr = run_command(:no_status_check => true,
                                         :command => "which httpd")
    apache2_bin = stdout.strip
  else
    raise(RuntimeError, "Apache test cannot run on os type #{platform_family}")
  end

  return apache2_bin unless apache2_bin.empty?
end

def find_apache_user(platform_family)
  if platform_family == "debian"
    status, stdout, stderr = run_command(:no_status_check => true,
                                         :command => "ps -ef|awk '/apache2/ && !/root/ {print $1}' | uniq")
    apache_user = stdout.strip
  elsif platform_family == "rhel"
    status, stdout, stderr = run_command(:no_status_check => true,
                                         :command => "ps -ef|awk '/httpd/ && !/root/ {print $1}' | uniq")
    apache_user = stdout.strip
  else
    raise(RuntimeError, "Apache test cannot run on os type #{platform_family}")
  end

  return apache_user unless apache_user.empty?
end


if apache2_bin = find_apache_executable(platform_family)
  apache2 Mash.new
  apache2[:bin] = apache2_bin
  apache2[:clients] = count_apache_clients(apache2_bin)
  apache2[:user] = find_apache_user(platform_family)
  apache2.merge!(parse_apache_output(apache2_bin))

  apache2[:config_file] = apache2[:config_path] + "/" + apache2[:config_file]

  case apache2[:mpm]
  when "prefork"
    command = "grep -A8 -i mpm_prefork_module #{apache2[:config_file]}|grep MaxClients"
    status, stdout, stderr = run_command(:no_status_check => true,
                                         :command => command)
    max_clients = stdout.strip
{'fraction_non_alphanumeric': 0.13443960061875967, 'fraction_numerical': 0.009843903810997047, 'mean_word_length': 2.970965940815187, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '465153', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2352, 'n_words': 531}