Skip to content

Commit

Permalink
WLM: Adding Auto Discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
v-anishrao committed Dec 11, 2018
1 parent 1d7c660 commit 2057549
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Providers/Modules/WLI/conf/baseOS/wlm_ad.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<source>
type wlm_input
tag oms.wlm.ad
interval 14400s
config_path /etc/opt/microsoft/omsagent/conf/omsagent.d/wlm_ad_pe_config.json
source "AutoDiscover::ProcessEnumeration"
data_type "WLM_LINUX_VM_METADATA_BLOB"
ip "INFRASTRUCTUREINSIGHTS"
</source>
18 changes: 18 additions & 0 deletions Providers/Modules/WLI/conf/baseOS/wlm_ad_pe_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"ServiceName" : "Apache HttpServer",
"PossibleDaemons": ["httpd", "apache2"]
},
{
"ServiceName" : "MongoDB",
"PossibleDaemons" : ["mongodb", "mongod"]
},
{
"ServiceName" : "Tomcat",
"PossibleDaemons" : ["tomcat", "tomcat6", "tomcat7", "tomcat8"]
},
{
"ServiceName" : "MySql",
"PossibleDaemons" : ["mysql"]
}
]
82 changes: 82 additions & 0 deletions Providers/Modules/WLI/plugins/common/in_wlm_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/local/bin/ruby

module Fluent

class WlmInput < Input
Plugin.register_input('wlm_input', self)

def initialize
super
require_relative 'wlm_source_lib'
end

config_param :source
config_param :interval, :time, :default => nil
config_param :tag, :string, :default => "oms.wlm.ad"
config_param :config_path, :string, :default => "/etc/opt/microsoft/omsagent/conf/omsagent.d/wlm_ad_pe_config.json"
config_param :data_type, :string, :default => "WLM_AUTO_DISCOVER"
config_param :ip, :string, :default => "INFRASTRUCTURE_INSIGHTS"

def configure (conf)
super
end

def start
# The WlmAutoDiscoverLib is a factory containing a list of strategies that
begin
@wlm_source_lib = WLM::WlmSourceLib.new(@source, @config_path)
if @interval
@finished = false
@condition = ConditionVariable.new
@mutex = Mutex.new
@thread = Thread.new(&method(:run_periodic))
else
get_emit_data
end #if

rescue => e
$log.error "Error while configuring the data source. #{e}."
end #begin

end #start

def shutdown
if @interval
@mutex.synchronize {
@finished = true
@condition.signal
}
@thread.join
end
end #shutdown

def get_emit_data
wrapper = nil
time = Time.now.to_s
begin
wrapper = @wlm_source_lib.get_data(time, @data_type, @ip)
rescue => e
$log.error "Error while executing get_data. #{e}"
end
$log.info wrapper
router.emit(@tag, time, wrapper) if wrapper
end #get_emit_data

def run_periodic
@mutex.lock
done = @finished
until done
@condition.wait(@mutex, @interval)
done = @finished
@mutex.unlock
if !done
get_emit_data
end
@mutex.lock
end
@mutex.unlock
end #run_periodic

end #WlmAutoDiscoverInput

end # module
116 changes: 116 additions & 0 deletions Providers/Modules/WLI/plugins/common/wlm_ad_pe_lib.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/usr/local/bin/ruby
require 'open3'
require 'json'
require 'base64'

module WLM
class CommandHelper

attr_reader :command
attr_reader :command_name

def initialize(command, command_name)
@command = command
@command_name = command_name
@executed = false
@is_success = false
end #def

def execute_command(params)
Open3.popen3(command % params) { |stdin, stdout, stderr, wait_thr|
@is_success = wait_thr.value.success?
@stderr = stderr.read
@stdout = stdout.read
@executed = true
}
end #def

def is_success?
return @is_success if @executed
raise "Command not executed"
end #def
end #class

class SystemCtlCommandHelper < CommandHelper
def initialize
super("systemctl status %s", "SystemCtl")
end #def
end #class

class PsCommandHelper < CommandHelper

def initialize
super("ps -ef | grep %s", "PS")
end #def

def is_success?
if(super)
if(@stdout.lines.count > 2)
return true
end #if
end #if
return false
end #def

end #class

class WlmProcessEnumeration

attr_reader :config
attr_reader :commands

def initialize(config, common=nil, commands=nil)
require 'base64'
require_relative 'oms_common'

@common = common
@common = OMS::Common unless @common
@config = config
@commands = commands
@commands = [SystemCtlCommandHelper.new, PsCommandHelper.new] unless @commands
@data_items = {}

end #initialize

def get_data(time, data_type, ip)
@config.each do |service_config|
execute_parse(service_config)
end #each

if(!@data_items.empty?)
@data_items["TimeStamp"] = time
@data_items["Host"] = @common.get_hostname
@data_items["OSType"] = "Linux"
auto_discovery_data = {
"EncodedVMMetadata" => Base64.strict_encode64(@data_items.to_s)
}
return {
"DataType" => data_type,
"IPName" => ip,
"DataItems"=> [auto_discovery_data]
}
end #if
end #discover

private
def execute_parse(service_config)
@commands.each do |command|
raise "Invalid command" if !(command.is_a? CommandHelper)
begin
service_config["PossibleDaemons"].each do |daemon|
command.execute_command(daemon)
if(command.is_success?)
@data_items["CommandName"] = command.command_name if @data_items["CommandName"].nil?
@data_items[service_config["ServiceName"]] = "1"
return
end #if
end #each service_config
rescue => e
$log.warn "Command: #{command.command_name} execution failed with Error: #{e}"
end #begin
end #each
end #execute_parse

end #class

end #module
39 changes: 39 additions & 0 deletions Providers/Modules/WLI/plugins/common/wlm_source_lib.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/local/bin/ruby
require 'json'

module WLM
class WlmSourceLib

attr_reader :method
attr_reader :config

def initialize(method, config_file_path, wli_source_lib = nil)
#read the method and the config json
@method = method

config_file = File.read(config_file_path)
@config = JSON.parse(config_file)

@wli_source_lib = get_source_lib unless wli_source_lib

end #initialize

def get_data(time, data_type, ip)
@wli_source_lib.get_data(time, data_type, ip)
end

private
def get_source_lib
wlm_source = nil
case @method
when "AutoDiscover::ProcessEnumeration"
require_relative 'wlm_ad_pe_lib'
source_lib = WLM::WlmProcessEnumeration.new(@config)
else
raise "Not a valid method #{@method}"
end
return source_lib
end #get_discovery_strategy

end #class
end #module

0 comments on commit 2057549

Please sign in to comment.