in source/plugins/ruby/kubernetes_container_inventory.rb [17:169]
def getContainerInventoryRecords(podItem, batchTime, clusterCollectEnvironmentVar, isWindows = false)
containerInventoryRecords = Array.new
begin
containersInfoMap = getContainersInfoMap(podItem, isWindows)
podContainersStatuses = []
if !podItem["status"]["containerStatuses"].nil? && !podItem["status"]["containerStatuses"].empty?
podContainersStatuses = podItem["status"]["containerStatuses"]
end
if !podItem["status"]["initContainerStatuses"].nil? && !podItem["status"]["initContainerStatuses"].empty?
podContainersStatuses = podContainersStatuses + podItem["status"]["initContainerStatuses"]
end
if !podContainersStatuses.empty?
podContainersStatuses.each do |containerStatus|
containerInventoryRecord = {}
containerInventoryRecord["CollectionTime"] = batchTime
containerName = containerStatus["name"]
containerRuntime = ""
containerId = ""
if !containerStatus["containerID"].nil?
containerRuntime = containerStatus["containerID"].split(":")[0]
containerId = containerStatus["containerID"].split("//")[1]
containerInventoryRecord["InstanceID"] = containerId
else
containerInventoryRecord["InstanceID"] = containerId
end
imageIdValue = containerStatus["imageID"]
if !imageIdValue.nil? && !imageIdValue.empty?
atLocation = imageIdValue.index("@")
if !atLocation.nil?
containerInventoryRecord["ImageId"] = imageIdValue[(atLocation + 1)..-1]
end
end
containerInventoryRecord["ExitCode"] = 0
isContainerTerminated = false
isContainerWaiting = false
if !containerStatus["state"].nil? && !containerStatus["state"].empty?
containerState = containerStatus["state"]
if containerState.key?("running")
containerInventoryRecord["State"] = "Running"
containerInventoryRecord["StartedTime"] = containerState["running"]["startedAt"]
elsif containerState.key?("terminated")
containerInventoryRecord["State"] = "Terminated"
containerInventoryRecord["StartedTime"] = containerState["terminated"]["startedAt"]
containerInventoryRecord["FinishedTime"] = containerState["terminated"]["finishedAt"]
exitCodeValue = containerState["terminated"]["exitCode"]
if exitCodeValue < 0
exitCodeValue = 128
end
containerInventoryRecord["ExitCode"] = exitCodeValue
if exitCodeValue > 0
containerInventoryRecord["State"] = "Failed"
end
isContainerTerminated = true
elsif containerState.key?("waiting")
containerInventoryRecord["State"] = "Waiting"
isContainerWaiting = true
end
end
restartCount = 0
if !containerStatus["restartCount"].nil?
restartCount = containerStatus["restartCount"]
end
containerInfoMap = containersInfoMap[containerName]
imageValue = containerInfoMap["image"]
if !imageValue.nil? && !imageValue.empty?
atLocation = imageValue.index("@")
isDigestSpecified = false
if !atLocation.nil?
imageValue = imageValue[0..(atLocation - 1)]
if containerInventoryRecord["ImageId"].nil? || containerInventoryRecord["ImageId"].empty?
containerInventoryRecord["ImageId"] = imageValue[(atLocation + 1)..-1]
end
isDigestSpecified = true
end
slashLocation = imageValue.index("/")
colonLocation = imageValue.index(":")
if !colonLocation.nil?
if slashLocation.nil?
containerInventoryRecord["Image"] = imageValue[0..(colonLocation - 1)]
else
containerInventoryRecord["Repository"] = imageValue[0..(slashLocation - 1)]
containerInventoryRecord["Image"] = imageValue[(slashLocation + 1)..(colonLocation - 1)]
end
containerInventoryRecord["ImageTag"] = imageValue[(colonLocation + 1)..-1]
else
if slashLocation.nil?
containerInventoryRecord["Image"] = imageValue
else
containerInventoryRecord["Repository"] = imageValue[0..(slashLocation - 1)]
containerInventoryRecord["Image"] = imageValue[(slashLocation + 1)..-1]
end
if isDigestSpecified == false
containerInventoryRecord["ImageTag"] = "latest"
end
end
end
podName = containerInfoMap["PodName"]
namespace = containerInfoMap["Namespace"]
containerNameInDockerFormat = "k8s_#{containerName}_#{podName}_#{namespace}_#{containerId}_#{restartCount}"
containerInventoryRecord["ElementName"] = containerNameInDockerFormat
containerInventoryRecord["Computer"] = containerInfoMap["Computer"]
containerInventoryRecord["ContainerHostname"] = podName
containerInventoryRecord["CreatedTime"] = containerInfoMap["CreatedTime"]
containerInventoryRecord["EnvironmentVar"] = containerInfoMap["EnvironmentVar"]
containerInventoryRecord["Ports"] = containerInfoMap["Ports"]
containerInventoryRecord["Command"] = containerInfoMap["Command"]
if !clusterCollectEnvironmentVar.nil? && !clusterCollectEnvironmentVar.empty? && clusterCollectEnvironmentVar.casecmp("false") == 0
containerInventoryRecord["EnvironmentVar"] = ["AZMON_CLUSTER_COLLECT_ENV_VAR=FALSE"]
elsif isWindows || isContainerTerminated || isContainerWaiting
containerInventoryRecord["EnvironmentVar"] = containerInfoMap["EnvironmentVar"]
else
if containerId.nil? || containerId.empty? || containerRuntime.nil? || containerRuntime.empty?
containerInventoryRecord["EnvironmentVar"] = ""
else
if containerRuntime.casecmp("cri-o") == 0
containerInventoryRecord["EnvironmentVar"] = obtainContainerEnvironmentVars("crio-#{containerId}")
else
containerInventoryRecord["EnvironmentVar"] = obtainContainerEnvironmentVars(containerId)
end
end
end
containerInventoryRecords.push containerInventoryRecord
end
end
rescue => error
$log.warn("KubernetesContainerInventory::getContainerInventoryRecords : Get Container Inventory Records failed: #{error}")
$log.debug_backtrace(error.backtrace)
ApplicationInsightsUtility.sendExceptionTelemetry(error)
end
return containerInventoryRecords
end