我想从我的Jenkinsnodes打印出目录的大小
我的剧本:
import hudson.node_monitors.*
import hudson.slaves.*
import Jenkins.*
jenkins = Jenkins.instance
for (slave in jenkins.nodes) {
wsRoot = slave.getWorkspaceRoot()
println( "Nodename: " + slave.getNodeName() )
for (dir in wsRoot.list()) {
println("---------------------------------------------------------")
println(" dirname: " + dir )
println(" dirspace: " + dir.length() )
}
}
但是使用这个脚本我得到的大小为“0”(如果dir为空)或“4096”为dir为28mb。
我尝试使用File
for (dir in wsRoot.list()) {
println("-------------------------------------------------------")
println(" dirname: " + dir )
File DirFiles = new File(dir.toString())
def sampleDirSize = DirFiles.directorySize()
println(" DirSpace: " + sampleDirSize )
}
但是我在这里得到错误:
java.io.FileNotFoundException: /home/jenkins/C:\Jenkins-Slave\jars\workspace\Testprojekt
因为master是Linux服务器而某些节点是Windows服务器,它们会创建错误的Filelocation。
是否有解决方案显示所有节点的mb dir大小正确?
答案 0 :(得分:1)
<强>解决方案:强>
import hudson.node_monitors.*
import hudson.slaves.*
import Jenkins.*
nByte = 0
nByteDir = 0
jenkins = Jenkins.instance
for (slave in jenkins.nodes) {
wsRoot = slave.getWorkspaceRoot()
println( "Nodename: " + slave.getNodeName() )
for (dir in wsRoot.list()) {
nByteDir = 0
println("=====================================================================")
println(" dirname: " + dir )
showFiles( dir )
println(" Byte Dir: " + nByteDir )
}
println("=====================================================================")
println("Byte Total: " + nByte )
println("#####################################################################")
}
def showFiles( dir ) {
for ( files in dir.list() ){
if( files.isDirectory() ) {
showFiles( files );
} else {
ByteCounter( files.length() )
}
}
}
def ByteCounter ( c )
{
nByte = nByte + c
nByteDir = nByteDir + c
}