I have the same problem.
I noticed that Perforce Plugin is using gethostname() from Computer class that is from Jenkins Core code as Rob said.
Then, I got the method getHostName() from perforce plugin and execute it on script console in Jenkins Master.
I also got the code snippet from Computer class (jenkins/core/src/main/java/hudson/model/Computer.java) that get the hostname using InetAddress class
and execute it on script console in the Slave that have this problem.
Code snippet executed in Master:
String getHostName(Node node) {
String host = null;
try {
Computer c = node.toComputer();
if (c != null)
{
host = c.getHostName();
}
} catch (Exception ex)
{
// fallback to finally
}
finally {
if (host == null)
{
println "Could not get hostname for slave " << node.getDisplayName();
host = "UNKNOWNHOST";
}
}
if (host.contains("."))
{
host = String.valueOf(host.subSequence(0, host.indexOf('.')));
}
return host;
}
Jenkins h = Jenkins.getInstance();
for (Node n : h.getNodes()) {
println "Slave: " << n.name;
println "Hostname: " << getHostName( n);
println "--------------------------------"
}
Result
Slave: Linux_Slave_1
Hostname: server01
--------------------------------
Slave: Windows_Slave_1
Could not get hostname for slave Windows_Slave_1
Hostname: UNKNOWNHOST
Code snippet executed in Slave that the hostname is unknow (http://jenkins.xpto.net/computer/Windows_Slave_1/script):
import java.net.InetAddress;
println "Get hostname by dns"
InetAddress ia = InetAddress.getByName("server02.xpto.net");
println ia.getCanonicalHostName();
println "----------------------------------"
println "Get hostname by name"
ia = InetAddress.getByName("server02");
println ia.getCanonicalHostName();
println "----------------------------------"
println "Get hostname by ip"
ia = InetAddress.getByName("10.0.0.2");
println ia.getCanonicalHostName();
Result
Get hostname by dns
server02.xpto.net
----------------------------------
Get hostname by name
server02.xpto.net
----------------------------------
Get hostname by ip
server02.xpto.net
So, for my surprise in the slave it can resolve the hostname.
The firewall in this windows machine is disabled.
Do you thing that it could be because of the network interface ?
Do you thing that this menssage "WARNING: Could not get hostname for slave ..." is a warning log?
What do you think if we change the log level to FINE, due to it spam the jenkins log and don't bring any relevant information ?
This is a problem with core, unfortunately. Getting the hostname of a machine using java is actually quite difficult, so it doesn't work 100% of the time. Check that the hostname is resolvable in DNS, and that the slave isn't running more than one networking interface. Apart from that, there's really nothing else that can be done.