Skip to content

Commit

Permalink
fix nom issue when config is empty (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishiy authored Sep 1, 2020
1 parent af0f981 commit 7fc4d4c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 33 deletions.
4 changes: 2 additions & 2 deletions build/linux/installer/conf/telegraf-rs.conf
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ $AZMON_RS_PROM_PLUGINS_WITH_NAMESPACE_FILTER
[[inputs.prometheus]]
#name_prefix="container.azm.ms/"
## An array of urls to scrape metrics from.
urls = ["$AZMON_INTEGRATION_NPM_METRICS_URL_LIST_CLUSTER"]
fielddrop = ["$AZMON_INTEGRATION_NPM_METRICS_DROP_LIST_CLUSTER"]
urls = $AZMON_INTEGRATION_NPM_METRICS_URL_LIST_CLUSTER
fielddrop = $AZMON_INTEGRATION_NPM_METRICS_DROP_LIST_CLUSTER

metric_version = 2
url_tag = "scrapeUrl"
Expand Down
2 changes: 1 addition & 1 deletion build/linux/installer/conf/telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@
[[inputs.prometheus]]
#name_prefix="container.azm.ms/"
## An array of urls to scrape metrics from.
urls = ["$AZMON_INTEGRATION_NPM_METRICS_URL_LIST_NODE"]
urls = $AZMON_INTEGRATION_NPM_METRICS_URL_LIST_NODE

metric_version = 2
url_tag = "scrapeUrl"
Expand Down
83 changes: 53 additions & 30 deletions build/linux/installer/scripts/tomlparser-npm-config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
@configSchemaVersion = ""
@collect_basic_npm_metrics = false
@collect_advanced_npm_metrics = false
@npm_node_url="http://$NODE_IP:10091/node-metrics"
@npm_cluster_url="http://npm-metrics-cluster-service.kube-system:9000/cluster-metrics"
@npm_basic_drop_metrics_cluster = "npm_ipset_counts"
@npm_default_setting = "[]"
@npm_node_urls = "[\"http://$NODE_IP:10091/node-metrics\"]"
@npm_cluster_urls="[\"http://npm-metrics-cluster-service.kube-system:9000/cluster-metrics\"]"
@npm_basic_drop_metrics_cluster = "[\"npm_ipset_counts\"]"
@tgfConfigFileDS = "/etc/opt/microsoft/docker-cimprov/telegraf.conf"
@tgfConfigFileRS = "/etc/opt/microsoft/docker-cimprov/telegraf-rs.conf"
@replicaset = "replicaset"

# Use parser to parse the configmap toml file to a ruby structure
def parseConfigMap
Expand All @@ -42,72 +46,91 @@ def populateSettingValuesFromConfigMap(parsedConfig)
begin
if !parsedConfig.nil? && !parsedConfig[:integrations].nil? && !parsedConfig[:integrations][:azure_network_policy_manager].nil? && !parsedConfig[:integrations][:azure_network_policy_manager][:collect_advanced_metrics].nil?
advanced_npm_metrics = parsedConfig[:integrations][:azure_network_policy_manager][:collect_advanced_metrics].to_s
puts "got:integrations.azure_network_policy_manager.collect_advanced_metrics='#{advanced_npm_metrics}'"
puts "config::npm::got:integrations.azure_network_policy_manager.collect_advanced_metrics='#{advanced_npm_metrics}'"
if !advanced_npm_metrics.nil? && advanced_npm_metrics.strip.casecmp("true") == 0
@collect_advanced_npm_metrics = true
else
@collect_advanced_npm_metrics = false
end
puts "set:integrations.azure_network_policy_manager.collect_advanced_metrics=#{@collect_advanced_npm_metrics}"
puts "config::npm::set:integrations.azure_network_policy_manager.collect_advanced_metrics=#{@collect_advanced_npm_metrics}"
end
rescue => errorStr
puts "config::error:Exception while reading config settings for npm advanced setting - #{errorStr}, using defaults"
puts "config::npm::error:Exception while reading config settings for npm advanced setting - #{errorStr}, using defaults"
@collect_advanced_npm_metrics = false
end
begin
if !parsedConfig.nil? && !parsedConfig[:integrations].nil? && !parsedConfig[:integrations][:azure_network_policy_manager].nil? && !parsedConfig[:integrations][:azure_network_policy_manager][:collect_basic_metrics].nil?
basic_npm_metrics = parsedConfig[:integrations][:azure_network_policy_manager][:collect_basic_metrics].to_s
puts "got:integrations.azure_network_policy_manager.collect_basic_metrics='#{basic_npm_metrics}'"
puts "config::npm::got:integrations.azure_network_policy_manager.collect_basic_metrics='#{basic_npm_metrics}'"
if !basic_npm_metrics.nil? && basic_npm_metrics.strip.casecmp("true") == 0
@collect_basic_npm_metrics = true
else
@collect_basic_npm_metrics = false
end
puts "set:integrations.azure_network_policy_manager.collect_basic_metrics=#{@collect_basic_npm_metrics}"
puts "config::npm::set:integrations.azure_network_policy_manager.collect_basic_metrics=#{@collect_basic_npm_metrics}"
end
rescue => errorStr
puts "config::error:Exception while reading config settings for npm basic setting - #{errorStr}, using defaults"
puts "config::npm::error:Exception while reading config settings for npm basic setting - #{errorStr}, using defaults"
@collect_basic_npm_metrics = false
end
end

@configSchemaVersion = ENV["AZMON_AGENT_CFG_SCHEMA_VERSION"]
puts "****************Start Config Processing********************"
puts "****************Start NPM Config Processing********************"
if !@configSchemaVersion.nil? && !@configSchemaVersion.empty? && @configSchemaVersion.strip.casecmp("v1") == 0 #note v1 is the only supported schema version , so hardcoding it
configMapSettings = parseConfigMap
if !configMapSettings.nil?
populateSettingValuesFromConfigMap(configMapSettings)
end
else
if (File.file?(@configMapMountPath))
ConfigParseErrorLogger.logError("config::unsupported/missing config schema version - '#{@configSchemaVersion}' , using defaults, please use supported schema version")
ConfigParseErrorLogger.logError("config::npm::unsupported/missing config schema version - '#{@configSchemaVersion}' , using defaults, please use supported schema version")
end
@collect_basic_npm_metrics = false
@collect_advanced_npm_metrics = false
end

# Write the settings to file, so that they can be set as environment variables
file = File.open("integration_npm_config_env_var", "w")

if !file.nil?

controller = ENV["CONTROLLER_TYPE"]
tgfConfigFile = @tgfConfigFileDS

if controller.casecmp(@replicaset) == 0
tgfConfigFile = @tgfConfigFileRS
end

#replace place holders in configuration file
tgfConfig = File.read(tgfConfigFile) #read returns only after closing the file

if @collect_advanced_npm_metrics == true
tgfConfig = tgfConfig.gsub("$AZMON_INTEGRATION_NPM_METRICS_URL_LIST_NODE", @npm_node_urls)
tgfConfig = tgfConfig.gsub("$AZMON_INTEGRATION_NPM_METRICS_URL_LIST_CLUSTER", @npm_cluster_urls)
tgfConfig = tgfConfig.gsub("$AZMON_INTEGRATION_NPM_METRICS_DROP_LIST_CLUSTER", @npm_default_setting)
elsif @collect_basic_npm_metrics == true
tgfConfig = tgfConfig.gsub("$AZMON_INTEGRATION_NPM_METRICS_URL_LIST_NODE", @npm_node_urls)
tgfConfig = tgfConfig.gsub("$AZMON_INTEGRATION_NPM_METRICS_URL_LIST_CLUSTER", @npm_cluster_urls)
tgfConfig = tgfConfig.gsub("$AZMON_INTEGRATION_NPM_METRICS_DROP_LIST_CLUSTER", @npm_basic_drop_metrics_cluster)
else
tgfConfig = tgfConfig.gsub("$AZMON_INTEGRATION_NPM_METRICS_URL_LIST_NODE", @npm_default_setting)
tgfConfig = tgfConfig.gsub("$AZMON_INTEGRATION_NPM_METRICS_URL_LIST_CLUSTER", @npm_default_setting)
tgfConfig = tgfConfig.gsub("$AZMON_INTEGRATION_NPM_METRICS_DROP_LIST_CLUSTER", @npm_default_setting)
end

File.open(tgfConfigFile, "w") { |file| file.puts tgfConfig } # 'file' will be closed here after it goes out of scope
puts "config::npm::Successfully substituted the NPM placeholders into #{tgfConfigFile} file for #{controller}"

# Write the telemetry to file, so that they can be set as environment variables
telemetryFile = File.open("integration_npm_config_env_var", "w")

if !telemetryFile.nil?
if @collect_advanced_npm_metrics == true
file.write("export TELEMETRY_NPM_INTEGRATION_METRICS_ADVANCED=1\n")
file.write("export AZMON_INTEGRATION_NPM_METRICS_URL_LIST_NODE=#{@npm_node_url}\n")
file.write("export AZMON_INTEGRATION_NPM_METRICS_URL_LIST_CLUSTER=#{@npm_cluster_url}\n")
file.write("export AZMON_INTEGRATION_NPM_METRICS_DROP_LIST_CLUSTER=\n")
telemetryFile.write("export TELEMETRY_NPM_INTEGRATION_METRICS_ADVANCED=1\n")
elsif @collect_basic_npm_metrics == true
file.write("export TELEMETRY_NPM_INTEGRATION_METRICS_BASIC=1\n")
file.write("export AZMON_INTEGRATION_NPM_METRICS_URL_LIST_NODE=#{@npm_node_url}\n")
file.write("export AZMON_INTEGRATION_NPM_METRICS_URL_LIST_CLUSTER=#{@npm_cluster_url}\n")
file.write("export AZMON_INTEGRATION_NPM_METRICS_DROP_LIST_CLUSTER=#{@npm_basic_drop_metrics_cluster}\n")
else
file.write("export AZMON_INTEGRATION_NPM_METRICS_URL_LIST_NODE=\n")
file.write("export AZMON_INTEGRATION_NPM_METRICS_URL_LIST_CLUSTER=\n")
file.write("export AZMON_INTEGRATION_NPM_METRICS_DROP_LIST_CLUSTER=\n")
telemetryFile.write("export TELEMETRY_NPM_INTEGRATION_METRICS_BASIC=1\n")
end
# Close file after writing all environment variables
file.close
telemetryFile.close
else
puts "Exception while opening file for writing config environment variables"
puts "****************End Config Processing********************"
end
puts "config::npm::Exception while opening file for writing NPM telemetry environment variables"
puts "****************End NPM Config Processing********************"
end

0 comments on commit 7fc4d4c

Please sign in to comment.