View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package it.jnrpe.plugin.utils;
17  
18  import java.io.BufferedReader;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.InputStreamReader;
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  /**
28   * Utility class for running shell commands 
29   * 
30   *
31   * @author Frederico Campos
32   */
33  public class ShellUtils {
34      
35      /**
36       * Executes a system command with arguments and returns the output
37       * 
38       * @param command
39       * @param encoding
40       * @return command output
41       * @throws IOException
42       */
43      public static String executeSystemCommandAndGetOutput(String[] command, String encoding) throws IOException{
44          Process p = Runtime.getRuntime().exec(command);
45          InputStream input = p.getInputStream();
46          StringBuffer lines = new StringBuffer();
47          String line = null;
48          BufferedReader in =
49                  new BufferedReader(new InputStreamReader(input, encoding));
50          while ((line = in.readLine()) != null) {
51              lines.append(line).append("\n");
52          }
53          return lines.toString();
54      }
55   
56      /**
57       * Check if we are running in a Windows environment
58       * 
59       * @return true if windows
60       */
61      public static boolean isWindows() {
62          String os = System.getProperty("os.name").toLowerCase();
63          return os.contains("windows");
64      }
65      /**
66      
67       * Check if name of process is a windows idle process
68       * 
69       * @param proc
70       * @return true if idle process, false otherwise
71       */
72  	public static boolean isWindowsIdleProc(String proc) {
73  		proc = proc.trim().toLowerCase();
74  		if (proc.equals("system idle process") ||
75  				proc.contains("inactiv") ||
76  				proc.equals("system")) {
77  			return true;
78  		}
79  		return false;
80  	}
81  
82  }