Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit 2057549

Browse files
committed
WLM: Adding Auto Discovery
1 parent 1d7c660 commit 2057549

File tree

5 files changed

+264
-0
lines changed

5 files changed

+264
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<source>
2+
type wlm_input
3+
tag oms.wlm.ad
4+
interval 14400s
5+
config_path /etc/opt/microsoft/omsagent/conf/omsagent.d/wlm_ad_pe_config.json
6+
source "AutoDiscover::ProcessEnumeration"
7+
data_type "WLM_LINUX_VM_METADATA_BLOB"
8+
ip "INFRASTRUCTUREINSIGHTS"
9+
</source>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"ServiceName" : "Apache HttpServer",
4+
"PossibleDaemons": ["httpd", "apache2"]
5+
},
6+
{
7+
"ServiceName" : "MongoDB",
8+
"PossibleDaemons" : ["mongodb", "mongod"]
9+
},
10+
{
11+
"ServiceName" : "Tomcat",
12+
"PossibleDaemons" : ["tomcat", "tomcat6", "tomcat7", "tomcat8"]
13+
},
14+
{
15+
"ServiceName" : "MySql",
16+
"PossibleDaemons" : ["mysql"]
17+
}
18+
]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/local/bin/ruby
2+
3+
module Fluent
4+
5+
class WlmInput < Input
6+
Plugin.register_input('wlm_input', self)
7+
8+
def initialize
9+
super
10+
require_relative 'wlm_source_lib'
11+
end
12+
13+
config_param :source
14+
config_param :interval, :time, :default => nil
15+
config_param :tag, :string, :default => "oms.wlm.ad"
16+
config_param :config_path, :string, :default => "/etc/opt/microsoft/omsagent/conf/omsagent.d/wlm_ad_pe_config.json"
17+
config_param :data_type, :string, :default => "WLM_AUTO_DISCOVER"
18+
config_param :ip, :string, :default => "INFRASTRUCTURE_INSIGHTS"
19+
20+
def configure (conf)
21+
super
22+
end
23+
24+
def start
25+
# The WlmAutoDiscoverLib is a factory containing a list of strategies that
26+
begin
27+
@wlm_source_lib = WLM::WlmSourceLib.new(@source, @config_path)
28+
if @interval
29+
@finished = false
30+
@condition = ConditionVariable.new
31+
@mutex = Mutex.new
32+
@thread = Thread.new(&method(:run_periodic))
33+
else
34+
get_emit_data
35+
end #if
36+
37+
rescue => e
38+
$log.error "Error while configuring the data source. #{e}."
39+
end #begin
40+
41+
end #start
42+
43+
def shutdown
44+
if @interval
45+
@mutex.synchronize {
46+
@finished = true
47+
@condition.signal
48+
}
49+
@thread.join
50+
end
51+
end #shutdown
52+
53+
def get_emit_data
54+
wrapper = nil
55+
time = Time.now.to_s
56+
begin
57+
wrapper = @wlm_source_lib.get_data(time, @data_type, @ip)
58+
rescue => e
59+
$log.error "Error while executing get_data. #{e}"
60+
end
61+
$log.info wrapper
62+
router.emit(@tag, time, wrapper) if wrapper
63+
end #get_emit_data
64+
65+
def run_periodic
66+
@mutex.lock
67+
done = @finished
68+
until done
69+
@condition.wait(@mutex, @interval)
70+
done = @finished
71+
@mutex.unlock
72+
if !done
73+
get_emit_data
74+
end
75+
@mutex.lock
76+
end
77+
@mutex.unlock
78+
end #run_periodic
79+
80+
end #WlmAutoDiscoverInput
81+
82+
end # module
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/local/bin/ruby
2+
require 'open3'
3+
require 'json'
4+
require 'base64'
5+
6+
module WLM
7+
class CommandHelper
8+
9+
attr_reader :command
10+
attr_reader :command_name
11+
12+
def initialize(command, command_name)
13+
@command = command
14+
@command_name = command_name
15+
@executed = false
16+
@is_success = false
17+
end #def
18+
19+
def execute_command(params)
20+
Open3.popen3(command % params) { |stdin, stdout, stderr, wait_thr|
21+
@is_success = wait_thr.value.success?
22+
@stderr = stderr.read
23+
@stdout = stdout.read
24+
@executed = true
25+
}
26+
end #def
27+
28+
def is_success?
29+
return @is_success if @executed
30+
raise "Command not executed"
31+
end #def
32+
end #class
33+
34+
class SystemCtlCommandHelper < CommandHelper
35+
def initialize
36+
super("systemctl status %s", "SystemCtl")
37+
end #def
38+
end #class
39+
40+
class PsCommandHelper < CommandHelper
41+
42+
def initialize
43+
super("ps -ef | grep %s", "PS")
44+
end #def
45+
46+
def is_success?
47+
if(super)
48+
if(@stdout.lines.count > 2)
49+
return true
50+
end #if
51+
end #if
52+
return false
53+
end #def
54+
55+
end #class
56+
57+
class WlmProcessEnumeration
58+
59+
attr_reader :config
60+
attr_reader :commands
61+
62+
def initialize(config, common=nil, commands=nil)
63+
require 'base64'
64+
require_relative 'oms_common'
65+
66+
@common = common
67+
@common = OMS::Common unless @common
68+
@config = config
69+
@commands = commands
70+
@commands = [SystemCtlCommandHelper.new, PsCommandHelper.new] unless @commands
71+
@data_items = {}
72+
73+
end #initialize
74+
75+
def get_data(time, data_type, ip)
76+
@config.each do |service_config|
77+
execute_parse(service_config)
78+
end #each
79+
80+
if(!@data_items.empty?)
81+
@data_items["TimeStamp"] = time
82+
@data_items["Host"] = @common.get_hostname
83+
@data_items["OSType"] = "Linux"
84+
auto_discovery_data = {
85+
"EncodedVMMetadata" => Base64.strict_encode64(@data_items.to_s)
86+
}
87+
return {
88+
"DataType" => data_type,
89+
"IPName" => ip,
90+
"DataItems"=> [auto_discovery_data]
91+
}
92+
end #if
93+
end #discover
94+
95+
private
96+
def execute_parse(service_config)
97+
@commands.each do |command|
98+
raise "Invalid command" if !(command.is_a? CommandHelper)
99+
begin
100+
service_config["PossibleDaemons"].each do |daemon|
101+
command.execute_command(daemon)
102+
if(command.is_success?)
103+
@data_items["CommandName"] = command.command_name if @data_items["CommandName"].nil?
104+
@data_items[service_config["ServiceName"]] = "1"
105+
return
106+
end #if
107+
end #each service_config
108+
rescue => e
109+
$log.warn "Command: #{command.command_name} execution failed with Error: #{e}"
110+
end #begin
111+
end #each
112+
end #execute_parse
113+
114+
end #class
115+
116+
end #module
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/local/bin/ruby
2+
require 'json'
3+
4+
module WLM
5+
class WlmSourceLib
6+
7+
attr_reader :method
8+
attr_reader :config
9+
10+
def initialize(method, config_file_path, wli_source_lib = nil)
11+
#read the method and the config json
12+
@method = method
13+
14+
config_file = File.read(config_file_path)
15+
@config = JSON.parse(config_file)
16+
17+
@wli_source_lib = get_source_lib unless wli_source_lib
18+
19+
end #initialize
20+
21+
def get_data(time, data_type, ip)
22+
@wli_source_lib.get_data(time, data_type, ip)
23+
end
24+
25+
private
26+
def get_source_lib
27+
wlm_source = nil
28+
case @method
29+
when "AutoDiscover::ProcessEnumeration"
30+
require_relative 'wlm_ad_pe_lib'
31+
source_lib = WLM::WlmProcessEnumeration.new(@config)
32+
else
33+
raise "Not a valid method #{@method}"
34+
end
35+
return source_lib
36+
end #get_discovery_strategy
37+
38+
end #class
39+
end #module

0 commit comments

Comments
 (0)