-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtanzucfgmonitor
executable file
·144 lines (124 loc) · 4.37 KB
/
tanzucfgmonitor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env ruby
require 'yaml'
require 'optimist'
require 'sqlite3'
opts = Optimist::options do
version "Tanzu Config Monitor 0.0.1"
banner <<-EOS
Tanzu Config Monitor will parse opsman configuration files and monitor specific variables for changes
Usage:
tanzucfgmonitor [options]
Example:
tanzuconfigmonitor -f TAS_config.yaml -p "cf-config,bosh-director-config"
Process Config Options:
pivotal-container-service-config
bosh-director-config
cf-config
where [options] are:
EOS
opt :configfile, "Config file to monitor", short: "-f", type: :string
opt :monitorlist, "File with list of variables to monitor", short: "-m", type: :string, default: "tanzu-monitor.yaml"
opt :databasefile, "What file to save the results in", short: "-d", type: :string, default: "tanzucfgmonitor.db"
opt :process, "Process configs", short: "-p", type: :string, default: "pivotal-container-service-config,bosh-director-config"
opt :query, "Query database values", short: "-q"
end
Optimist::die :configfile, "must be entered" if !opts[:configfile]
Optimist::die :configfile, "must be exist" if !opts[:configfile].nil? && !File.exist?(opts[:configfile])
Optimist::die :monitorlist, "must be exist" if !opts[:monitorlist].nil? && !File.exist?(opts[:monitorlist])
def get_variable(*args)
data = args[0]
args.shift
# get section
if args[0].kind_of?(Array)
variables = args[0]
section = variables[0]
variables.shift
else
section = args[0]
args.shift
variables = args
end
# check if Config file has Section
if not data.has_key?(section)
puts "Config file does not have section information for #{section}"
exit 1
end
parsepath = "data[\"#{section}\"]"
args.each do |arg|
if arg.kind_of?(Array)
arg.each do |multiarg|
parsepath = parsepath+"[\"#{multiarg}\"]"
end
else
parsepath = parsepath+"[\"#{arg}\"]"
end
end
# Handle errors
begin
return eval(parsepath)
rescue
puts "Config file does not have information for #{parsepath}"
exit 1
end
end #EOF
def process_variables(db,config,service,data)
exitvalue = 0
config['tanzu-config-monitor'][service].each do |config_variable|
value = get_variable(data,(service+','+config_variable).split(","))
query_result = db.get_first_value "SELECT value FROM variable_values WHERE section='#{service}' AND variable='#{config_variable}' ORDER by date DESC LIMIT 1"
if query_result.nil?
exitvalue = 1
puts "#{service},#{config_variable} (#{value}) being inserted into database"
db.execute "INSERT INTO variable_values ( section, variable, value,date) VALUES(?, ?, ?,julianday())",[service,config_variable, value.to_s]
elsif query_result != value.to_s
exitvalue = 1
puts service + "," + config_variable + " has changed (#{value.to_s})"
db.execute "INSERT INTO variable_values ( section, variable, value, date) VALUES(?, ?, ?,julianday())",[service,config_variable, value.to_s]
end
end
return exitvalue
end
def query_values(db,config,service,data)
config['tanzu-config-monitor'][service].each do |config_variable|
query = db.prepare "SELECT datetime(date, 'localtime'),value FROM variable_values WHERE section='#{service}' AND variable='#{config_variable}' ORDER by date"
query_result = query.execute!
puts sprintf("%s:\n\t%s\n",service,config_variable)
query_result.each do |row|
puts sprintf("\t\t%s | %s\n",row[0],row[1])
end
query.close if query
puts
end
end
config = File.open(opts[:monitorlist]) { |f| YAML::load(f) }
data = File.open(opts[:configfile]) { |f| YAML::load(f) }
exitvalue = 0
if !File.exist?(opts[:databasefile])
db = SQLite3::Database.new(opts[:databasefile])
# Create a table
rows = db.execute <<-SQL
create table variable_values (
section varchar(50),
variable varchar(100),
value varchar(100),
date real
);
SQL
else
db = SQLite3::Database.open(opts[:databasefile])
end
db.results_as_hash = true
if opts[:query]
opts[:process].split(/,/).each do |service|
query_values(db,config,service,data)
end
elsif opts[:process]
exitvalue = 0
process_exit_value = 0
opts[:process].split(/,/).each do |service|
process_exit_value = process_variables(db,config,service,data)
end
exitvalue = process_exit_value if process_exit_value == 1
end
db.close if db
exit exitvalue