Moocar / logback-gelf

Logback plugin to send GELF messages to graylog2 server
Apache License 2.0
147 stars 59 forks source link

Wrong IP address with ${HOSTNAME} #68

Open dmitrime opened 8 years ago

dmitrime commented 8 years ago

I have the following interfaces on a remote AWS machine:

$ ifconfig

docker0   Link encap:Ethernet  HWaddr 02:42:BB:13:E8:25
          inet addr:172.17.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

eth0      Link encap:Ethernet  HWaddr 0A:8C:15:E6:AF:9D
          inet addr:172.30.2.229  Bcast:172.30.2.255  Mask:255.255.255.0
          inet6 addr: fe80::88c:15ff:fee6:af9d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:9001  Metric:1
          RX packets:2741444 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1601099 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3543604947 (3.3 GiB)  TX bytes:170417798 (162.5 MiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:51692380 errors:0 dropped:0 overruns:0 frame:0
          TX packets:51692380 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:3391639364 (3.1 GiB)  TX bytes:3391639364 (3.1 GiB)

My app however, is not using docker and is deployed by simply copying it to home directory and running from there. I set the host in logger.xml:

...
      <layout class="me.moocar.logbackgelf.GelfLayout">
        <host>${HOSTNAME} </host>
      </layout>
...

Now, I go to Graylog where I send my log messages and see docker's IP as the source: 172.17.0.1. Since all the instances that got docker running seem to have the same IP, I cannot really tell which instance is producing which messages! How can I get the eth0 IP? Or identify the host in some other way?

Moocar commented 8 years ago

Docker networking, hoorah! I'm afraid I'm not a docker user so can't help you. But, I would suggest doing some reading on how networking works in docker. It's not as straightforward as VMs.

dmitrime commented 8 years ago

@Moocar The thing is, I'm not even using docker! Like I said, the app is just copied to the home dir. It's true that the instance has docker installed, but my app is not using it, so I have no idea why it's choosing docker's IP...

Moocar commented 8 years ago

ahh, right you are. I'm not sure what the issue would be. But it's most likely something that docker is doing on that machine. In case it helps, here's the code that gets the hostname: https://github.com/Moocar/logback-gelf/blob/master/src/main/java/me/moocar/logbackgelf/InternetUtils.java#L18

dmitrime commented 8 years ago

Ah, I think I see the problem there. It tries to take the first network interface, which happens to be the docker one.

            NetworkInterface networkInterface = NetworkInterface.getNetworkInterfaces().nextElement();
// ...
            InetAddress ipAddress = networkInterface.getInetAddresses().nextElement();

Would be nice to change it to prefer eth0. However I already got around that by setting my own environment variable.

It's a bit strange that the correct $HOSTNAME variable gets shadowed by that code, which overwrites it with an incorrect value. Why not just make a separate ${LOGBACK_HOSTNAME} and keep the original $HOSTNAME.