Do we have a method in jenkins API to find the total disk space of a slave

2.4k Views Asked by At

I am working on a automation script to find the %age space left or utilized on a slave node. I did find the function to get the remaining space (http://javadoc.jenkins-ci.org/hudson/node_monitors/DiskSpaceMonitorDescriptor.DiskSpace.html#toHtml%28%29), but couldn't find anything to get the total space or the %age.

Just wanted to know if there is a method in jenkins API that can server my requirement.

1

There are 1 best solutions below

0
forbagghioffa On

I've had the same problem and resolved it using the FilePath class (http://javadoc.jenkins.io/hudson/FilePath.html).

With this example you can iterate on all the slave nodes to get the free space percentage:

    for (slave in hudson.model.Hudson.instance.slaves) {

      FilePath slaveRootPath = slave.getRootPath();

      // Total space on the slave filesystem
      total = slaveRootPath.getTotalDiskSpace();
      // Free space on the slave filesystem
      free = slaveRootPath.getUsableDiskSpace();

      available = (100 * free)/total;

      System.out.println(slave.getDisplayName() + " free space percentage: " + available + "%");
    }

You will get something like:

SLAVE_NAME free space percentage: N%

Hope it helps.