diff --git a/agent/Dockerfile b/agent/Dockerfile index 53460a8..4c5adfa 100644 --- a/agent/Dockerfile +++ b/agent/Dockerfile @@ -3,7 +3,7 @@ FROM icederce/eclipse-temurin-11-jre-focal # Set environment variables -ENV TZ=Asia/Shanghai JAVA_OPTS="-Xms2028m -Xmx2048m" ServerName="" serverIpPbV4="" serverIpInV4="" serverIpPbV6="" serverIpInV6="" location="" provider="" managePort="" cpuBrand="" cpuCore="" memoryTotal="" diskTotal="" diskUsage="" osInfo="" osKernelInfo="" tcpControl="" virtualization="" ioSpeed="" +ENV TZ=Asia/Shanghai ServerName="" serverIpPbV4="" serverIpInV4="" serverIpPbV6="" serverIpInV6="" location="" provider="" managePort="" cpuBrand="" cpuCore="" memoryTotal="" diskTotal="" diskUsage="" osInfo="" osKernelInfo="" tcpControl="" virtualization="" ioSpeed="" JAVA_OPTS="-Xms2028m -Xmx2048m" # Set time zone RUN set -eux; \ diff --git a/agent/src/main/java/io/wdd/agent/initial/beans/ServerInfo.java b/agent/src/main/java/io/wdd/agent/initial/beans/ServerInfo.java index 2ad768f..ac06236 100644 --- a/agent/src/main/java/io/wdd/agent/initial/beans/ServerInfo.java +++ b/agent/src/main/java/io/wdd/agent/initial/beans/ServerInfo.java @@ -5,6 +5,8 @@ import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; import java.util.Date; @@ -12,83 +14,100 @@ import java.util.Date; @AllArgsConstructor @NoArgsConstructor @SuperBuilder(toBuilder = true) +@Configuration public class ServerInfo { + @Value("${serverName}") private String serverName; /** * server public ipv4 */ + @Value("${serverIpPbV4}") private String serverIpPbV4; /** * server inner ipv4 */ + @Value("${serverIpInV4}") private String serverIpInV4; /** * server public ipv6 */ + @Value("${serverIpPbV6}") private String serverIpPbV6; /** * server inner ipv6 */ + @Value("${serverIpInV6}") private String serverIpInV6; /** * */ + @Value("${location}") private String location; /** * */ + @Value("${provider}") private String provider; /** * split by , */ + @Value("${managePort}") private String managePort; /** * */ + @Value("${cpuCore}") private Integer cpuCore; /** * */ + @Value("${cpuBrand}") private String cpuBrand; /** * */ + @Value("${osInfo}") private String osInfo; /** * */ + @Value("${osKernelInfo}") private String osKernelInfo; - + @Value("${tcpControl}") private String tcpControl; - + @Value("${virtualization}") private String virtualization; - + @Value("${ioSpeed}") private String ioSpeed; + @Value("${memoryTotal}") private String memoryTotal; + @Value("${diskTotal}") private String diskTotal; + @Value("${diskUsage}") private String diskUsage; /** * */ + @Value("${serverIpPbV4}") private String comment; diff --git a/agent/src/main/java/io/wdd/agent/initial/bootup/AutoDeployOctopusAgent.sh b/agent/src/main/java/io/wdd/agent/initial/bootup/AutoDeployOctopusAgent.sh index db0fed9..7305449 100644 --- a/agent/src/main/java/io/wdd/agent/initial/bootup/AutoDeployOctopusAgent.sh +++ b/agent/src/main/java/io/wdd/agent/initial/bootup/AutoDeployOctopusAgent.sh @@ -727,6 +727,62 @@ print_GoIOTest() { FunctionEnd } +check_virt(){ + command_exists "dmesg" && virtualx="$(dmesg 2>/dev/null)" + if command_exists "dmidecode"; then + sys_manu="$(dmidecode -s system-manufacturer 2>/dev/null)" + sys_product="$(dmidecode -s system-product-name 2>/dev/null)" + sys_ver="$(dmidecode -s system-version 2>/dev/null)" + else + sys_manu="" + sys_product="" + sys_ver="" + fi + if grep -qa docker /proc/1/cgroup; then + virt="Docker" + elif grep -qa lxc /proc/1/cgroup; then + virt="LXC" + elif grep -qa container=lxc /proc/1/environ; then + virt="LXC" + elif [[ -f /proc/user_beancounters ]]; then + virt="OpenVZ" + elif [[ "${virtualx}" == *kvm-clock* ]]; then + virt="KVM" + elif [[ "${sys_product}" == *KVM* ]]; then + virt="KVM" + elif [[ "${cname}" == *KVM* ]]; then + virt="KVM" + elif [[ "${cname}" == *QEMU* ]]; then + virt="KVM" + elif [[ "${virtualx}" == *"VMware Virtual Platform"* ]]; then + virt="VMware" + elif [[ "${sys_product}" == *"VMware Virtual Platform"* ]]; then + virt="VMware" + elif [[ "${virtualx}" == *"Parallels Software International"* ]]; then + virt="Parallels" + elif [[ "${virtualx}" == *VirtualBox* ]]; then + virt="VirtualBox" + elif [[ -e /proc/xen ]]; then + if grep -q "control_d" "/proc/xen/capabilities" 2>/dev/null; then + virt="Xen-Dom0" + else + virt="Xen-DomU" + fi + elif [ -f "/sys/hypervisor/type" ] && grep -q "xen" "/sys/hypervisor/type"; then + virt="Xen" + elif [[ "${sys_manu}" == *"Microsoft Corporation"* ]]; then + if [[ "${sys_product}" == *"Virtual Machine"* ]]; then + if [[ "${sys_ver}" == *"7.0"* || "${sys_ver}" == *"Hyper-V" ]]; then + virt="Hyper-V" + else + virt="Microsoft Virtual Machine" + fi + fi + else + virt="Dedicated" + fi +} + GetIpv4Info() { org="$(wget -q -T10 -O- ipinfo.io/org)" city="$(wget -q -T10 -O- ipinfo.io/city)" @@ -802,6 +858,9 @@ generateSystemInfo() { FunctionSuccess GetIpv4Info FunctionSuccess + check_virt + FunctionSuccess + ServerName="$(cat /etc/hostname)" serverIpPbV4="$public_ipv4" @@ -810,7 +869,7 @@ generateSystemInfo() { serverIpInV6="" location="$city $region $country" provider="$org" - managePort="netstat -ntulp | grep sshd | grep -w tcp | awk '{print$4}' | cut -d":" -f2" + managePort="$(netstat -ntulp | grep sshd | grep -w tcp | awk '{print$4}' | cut -d":" -f2)" cpuCore="$cname" cpuBrand="$cores @ $freq MHz" memoryTotal="$tram" @@ -832,6 +891,27 @@ deployOctopusAgent() { # get the latest version of Octopus agent # poll the start up shell + echo "docker run -d \ + -e ServerName="${ServerName}" \ + -e serverIpPbV4="$serverIpPbV4" \ + -e serverIpInV4="$serverIpInV4" \ + -e serverIpPbV6="$serverIpPbV6" \ + -e serverIpInV6="$serverIpInV6" \ + -e location="$location" \ + -e provider="$provider" \ + -e managePort="$managePort" \ + -e cpuBrand="$cpuBrand" \ + -e cpuCore="$cpuCore" \ + -e memoryTotal="$memoryTotal" \ + -e diskTotal="$diskTotal" \ + -e diskUsage="$diskUsage" \ + -e osInfo="$osInfo" \ + -e osKernelInfo="$osKernelInfo" \ + -e tcpControl="$tcpControl" \ + -e virtualization="$virtualization" \ + -e ioSpeed="$ioSpeed" \ + icederce/wdd-octopus-agent:latest" + docker run -d \ -e ServerName="${ServerName}" \ -e serverIpPbV4="$serverIpPbV4" \ diff --git a/agent/src/main/java/io/wdd/agent/initial/bootup/reference/collectSystemInfo.java b/agent/src/main/java/io/wdd/agent/initial/bootup/reference/collectSystemInfo.java index d653e1b..c3c63c8 100644 --- a/agent/src/main/java/io/wdd/agent/initial/bootup/reference/collectSystemInfo.java +++ b/agent/src/main/java/io/wdd/agent/initial/bootup/reference/collectSystemInfo.java @@ -2,24 +2,29 @@ package io.wdd.agent.initial.bootup.reference; +import io.wdd.agent.initial.beans.ServerInfo; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; +import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.net.InetAddress; +import java.util.Map; import java.util.Properties; @Configuration -public class collectSystemInfo { - - - - +@Slf4j +public class collectSystemInfo implements ApplicationContextAware { @Resource Environment environment; + private ApplicationContext context; @Bean public void initialReadingEnvironment(){ @@ -33,8 +38,26 @@ public class collectSystemInfo { Properties props =System.getProperties(); + System.out.println("props = " + props); + System.out.println(); + + Map getenv = System.getenv(); + + System.out.println("getenv = " + getenv); + + System.out.println(); + + System.out.println("environment = " + environment); + + InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); + + System.out.println("loopbackAddress = " + loopbackAddress); + InetAddress ip = InetAddress.getLocalHost(); String localName = ip.getHostName(); + + System.out.println("ip = " + ip); + String osName = System.getProperty("os.name"); String userName = System.getProperty("user.name"); String osVersion = System.getProperty("os.version"); @@ -47,10 +70,27 @@ public class collectSystemInfo { System.out.println("主机系统:" + osName); System.out.println("系统版本:" + osVersion); System.out.println("系统架构:" + osArch); + } catch (Exception e) { e.printStackTrace(); } } + @PostConstruct + private void getInjectServerInfo(){ + + log.info("getInjectServerInfo"); + + ServerInfo serverInfo = (ServerInfo) context.getBean("serverInfo"); + + + System.out.println("serverInfo = " + serverInfo); + + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.context = applicationContext; + } }