summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/DiskStat.java
blob: 9861c71534bf6ce62c7f5886478770a6794bdfd5 (plain) (tree)



















                                                                                                                               
                                        

                                          
                                                        










































































                                                                                                                                    
package org.openslx.taskmanager.tasks;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openslx.taskmanager.api.SystemCommandTask;

public class DiskStat extends SystemCommandTask
{

	private static final Pattern dfLine = Pattern.compile( "^(.*\\S)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)%\\s+(/.*)$" );

	private final Output status = new Output();
	private final List<Output.Entry> list = new ArrayList<>();

	@Override
	protected String[] initCommandLine()
	{
		this.timeoutSeconds = 2;
		return new String[] {
				"/bin/df",
				"-P", "-B", "1024", "-a"
		};
	}

	@Override
	protected boolean processEnded( int exitCode )
	{
		if ( exitCode != 0 && list.isEmpty() ) {
			status.error = "df returned exit code " + exitCode;
			return false;
		}
		status.list = list;
		return true;
	}

	@Override
	protected void processStdOut( String line )
	{
		Matcher matcher = dfLine.matcher( line );
		while ( matcher.find() ) {
			try {
				list.add( new Output.Entry(
						matcher.group( 1 ),
						matcher.group( 6 ),
						Long.parseLong( matcher.group( 2 ) ),
						Long.parseLong( matcher.group( 3 ) ),
						Long.parseLong( matcher.group( 4 ) ),
						Integer.parseInt( matcher.group( 5 ) ) )
						);
			} catch ( Exception e ) {
				// Silently skip line, can't parse....
			}
		}
	}

	@Override
	protected void processStdErr( String line )
	{
		status.error = line;
	}

	@Override
	protected boolean initTask()
	{
		this.setStatusObject( status );
		return true;
	}

	/**
	 * Output - contains additional status data of this task
	 */
	@SuppressWarnings( "unused" )
	private static class Output
	{
		protected String error = null;
		protected List<Entry> list = null;

		public static class Entry
		{
			protected final String fileSystem, mountPoint;
			protected final long sizeKb, usedKb, freeKb;
			protected final int usedPercent;

			public Entry( String fileSystem, String mountPoint, long sizeKb, long usedKb, long freeKb, int usedPercent )
			{
				this.fileSystem = fileSystem;
				this.mountPoint = mountPoint;
				this.sizeKb = sizeKb;
				this.usedKb = usedKb;
				this.freeKb = freeKb;
				this.usedPercent = usedPercent;
			}
		}
	}

}