001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.docker; 018 019import java.io.IOException; 020import java.net.URL; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.logging.log4j.Logger; 025import org.apache.logging.log4j.core.LogEvent; 026import org.apache.logging.log4j.core.config.plugins.Plugin; 027import org.apache.logging.log4j.core.lookup.AbstractLookup; 028import org.apache.logging.log4j.core.lookup.StrLookup; 029import org.apache.logging.log4j.core.util.NetUtils; 030import org.apache.logging.log4j.docker.model.Container; 031import org.apache.logging.log4j.docker.model.Network; 032import org.apache.logging.log4j.status.StatusLogger; 033import org.apache.logging.log4j.util.PropertiesUtil; 034import com.fasterxml.jackson.core.type.TypeReference; 035import com.fasterxml.jackson.databind.ObjectMapper; 036 037/** 038 * 039 */ 040@Plugin(name = "docker", category = StrLookup.CATEGORY) 041public class DockerLookup extends AbstractLookup { 042 043 private static final Logger LOGGER = StatusLogger.getLogger(); 044 private static final String DOCKER_URI = "DOCKER_URI"; 045 private static final String HTTP = "http"; 046 private final Container container; 047 048 public DockerLookup() { 049 String baseUri = System.getenv(DOCKER_URI); 050 if (baseUri == null) { 051 PropertiesUtil props = PropertiesUtil.getProperties(); 052 baseUri = props.getStringProperty(DOCKER_URI); 053 } 054 if (baseUri == null) { 055 LOGGER.warn("No Docker URI provided. Docker information is unavailble"); 056 } 057 Container current = null; 058 try { 059 URL url= new URL(baseUri + "/containers/json"); 060 String hostName = NetUtils.getLocalHostname(); 061 String macAddr = NetUtils.getMacAddressString(); 062 063 if (url.getProtocol().equals(HTTP)) { 064 ObjectMapper objectMapper = new ObjectMapper(); 065 List<Container> containerList = objectMapper.readValue(url, new TypeReference<List<Container>>(){}); 066 067 for (Container container : containerList) { 068 if (macAddr != null && container.getNetworkSettings() != null) { 069 Map<String, Network> networks = container.getNetworkSettings().getNetworks(); 070 if (networks != null) { 071 for (Network network: networks.values()) { 072 if (macAddr.equals(network.getMacAddress())) { 073 current = container; 074 break; 075 } 076 } 077 } 078 } 079 if (current != null) { 080 break; 081 } 082 } 083 } 084 if (current == null) { 085 LOGGER.warn("Unable to determine current container"); 086 } 087 } catch (IOException ioe) { 088 LOGGER.warn("Unable to read container information: " + ioe.getMessage()); 089 } 090 container = current; 091 } 092 093 @Override 094 public String lookup(LogEvent event, String key) { 095 if (container == null) { 096 return null; 097 } 098 switch (key) { 099 case "shortContainerId": { 100 return container.getId().substring(0, 12); 101 } 102 case "containerId": { 103 return container.getId(); 104 } 105 case "containerName": { 106 if (container.getNames().size() > 1) { 107 return container.getNames().toString(); 108 } 109 return container.getNames().get(0); 110 } 111 case "shortImageId": { 112 return container.getImageId().substring(0, 12); 113 } 114 case "imageId": { 115 return container.getImageId(); 116 } 117 case "imageName": { 118 return container.getImage(); 119 } 120 default: 121 return null; 122 } 123 } 124}