diff --git a/.classpath b/.classpath index 4482a70..c8ee058 100644 --- a/.classpath +++ b/.classpath @@ -16,6 +16,7 @@ - + + diff --git a/lib/commons-io-2.5-javadoc.jar b/lib/commons-io-2.5-javadoc.jar new file mode 100644 index 0000000..b957529 Binary files /dev/null and b/lib/commons-io-2.5-javadoc.jar differ diff --git a/lib/commons-io-2.5-src.jar b/lib/commons-io-2.5-src.jar new file mode 100644 index 0000000..1ef5feb Binary files /dev/null and b/lib/commons-io-2.5-src.jar differ diff --git a/lib/commons-io-2.5.jar b/lib/commons-io-2.5.jar new file mode 100644 index 0000000..1234918 Binary files /dev/null and b/lib/commons-io-2.5.jar differ diff --git a/lib/devinsy-cmdexec-0.4.0-sources.zip b/lib/devinsy-cmdexec-0.4.0-sources.zip deleted file mode 100644 index 1f65023..0000000 Binary files a/lib/devinsy-cmdexec-0.4.0-sources.zip and /dev/null differ diff --git a/lib/devinsy-cmdexec-0.4.0.jar b/lib/devinsy-cmdexec-0.4.0.jar deleted file mode 100644 index a76fa4c..0000000 Binary files a/lib/devinsy-cmdexec-0.4.0.jar and /dev/null differ diff --git a/lib/devinsy-cmdexec-0.4.1-sources.zip b/lib/devinsy-cmdexec-0.4.1-sources.zip new file mode 100644 index 0000000..46049ab Binary files /dev/null and b/lib/devinsy-cmdexec-0.4.1-sources.zip differ diff --git a/lib/devinsy-cmdexec-0.4.1.jar b/lib/devinsy-cmdexec-0.4.1.jar new file mode 100644 index 0000000..86a7bed Binary files /dev/null and b/lib/devinsy-cmdexec-0.4.1.jar differ diff --git a/src/fr/devinsy/util/Property.java b/src/fr/devinsy/util/Property.java new file mode 100644 index 0000000..6b36697 --- /dev/null +++ b/src/fr/devinsy/util/Property.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2017 Christian Pierre MOMON + * + * This file is part of Devinsy-unix. + * + * Devinsy-unix is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Devinsy-unix is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Devinsy-unix. If not, see + */ +package fr.devinsy.util; + +/** + * The Class Property. + */ +public class Property +{ + private String name; + private String value; + + /** + * Utility class has not to have a constructor. + */ + public Property(final String name, final String value) + { + this.name = name; + this.value = value; + } + + public String getName() + { + return this.name; + } + + public String getValue() + { + return this.value; + } + + public void setName(final String name) + { + this.name = name; + } + + public void setValue(final String value) + { + this.value = value; + } +} diff --git a/src/fr/devinsy/util/PropertyList.java b/src/fr/devinsy/util/PropertyList.java new file mode 100644 index 0000000..2f3e255 --- /dev/null +++ b/src/fr/devinsy/util/PropertyList.java @@ -0,0 +1,169 @@ +/* + * Copyright (C) 2017 Christian Pierre MOMON + * + * This file is part of Devinsy-unix. + * + * Devinsy-unix is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Devinsy-unix is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Devinsy-unix. If not, see + */ +package fr.devinsy.util; + +import java.util.ArrayList; +import java.util.Collections; + +/** + * The Class PropertyBeanList. + */ +public class PropertyList extends ArrayList +{ + + private static final long serialVersionUID = 90604210351214011L; + + /** + * Instantiates a new work import beans. + */ + public PropertyList() + { + super(); + } + + /** + * Instantiates a new work import beans. + * + * @param initialCapacity + * the initial capacity + */ + public PropertyList(final int initialCapacity) + { + super(initialCapacity); + } + + /** + * Adds the. + * + * @param key + * the key + * @param value + * the value + * @return true, if successful + */ + public boolean add(final String key, final String value) + { + boolean result; + + result = add(new Property(key, value)); + + // + return result; + } + + /** + * Put. + * + * @param name + * the name + * @param value + * the value + * @return true, if successful + */ + public boolean put(final String name, final int value) + { + boolean result; + + result = add(new Property(name, String.valueOf(value))); + + // + return result; + } + + /** + * Put. + * + * @param name + * the name + * @param value + * the value + * @return true, if successful + */ + public boolean put(final String name, final String value) + { + boolean result; + + result = add(new Property(name, value)); + + // + return result; + } + + /** + * Reverse. + * + * @return the work import beans + */ + public PropertyList reverse() + { + PropertyList result; + + Collections.reverse(this); + + result = this; + + // + return result; + + } + + /* (non-Javadoc) + * @see java.util.AbstractCollection#toString() + */ + @Override + public String toString() + { + String result; + + StringBuffer buffer = new StringBuffer(100000); + + for (Property property : this) + { + buffer.append("[").append(property.getName()).append(",").append(property.getValue()).append("]\n"); + } + + result = buffer.toString(); + + // + return result; + } + + /** + * Webify value of the property list item. + * + * @return the property list + */ + public PropertyList webify() + { + PropertyList result; + + for (Property property : this) + { + if (property.getValue() != null) + { + property.setValue(property.getValue().replaceAll("\n", "
")); + } + } + + result = this; + + // + return result; + } +} diff --git a/src/fr/devinsy/util/unix/Unix.java b/src/fr/devinsy/util/unix/Unix.java index cb69b4f..5a77f71 100644 --- a/src/fr/devinsy/util/unix/Unix.java +++ b/src/fr/devinsy/util/unix/Unix.java @@ -21,11 +21,13 @@ package fr.devinsy.util.unix; import java.io.File; import java.util.Vector; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.SystemUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import fr.devinsy.util.cmdexec.CmdExec; import fr.devinsy.util.cmdexec.CmdExecException; +import fr.devinsy.util.cmdexec.CmdExecUtils; import fr.devinsy.util.strings.StringListUtils; import fr.devinsy.util.unix.acl.Acl; import fr.devinsy.util.unix.acl.AclManager; @@ -37,7 +39,9 @@ import fr.devinsy.util.unix.acl.AclManager; */ public class Unix { - private static final Logger logger = LoggerFactory.getLogger(Unix.class); + private static Logger logger = LoggerFactory.getLogger(Unix.class); + + public static final String BASH = "/bin/bash"; public static final String SUDO = "/usr/bin/sudo"; /** @@ -60,7 +64,7 @@ public class Unix { try { - CmdExec.run(SUDO, "bash", "-c", "echo \"" + text + "\" >> " + path); + sudo("bash", "-c", "echo \"" + text + "\" >> " + path); } catch (CmdExecException exception) { @@ -69,6 +73,49 @@ public class Unix } } + /** + * Bash. + * + * @param command + * the command + * @return the string + * @throws CmdExecException + * the cmd exec exception + */ + public static String bash(final String command) throws CmdExecException + { + String result; + + if (SystemUtils.IS_OS_LINUX) + { + result = CmdExecUtils.run(BASH + " -c " + command); + } + else + { + throw new CmdExecException("bash call requires GNU/Linux operating system."); + } + + // + return result; + } + + public static String bash(final String... command) throws CmdExecException + { + String result; + + if (SystemUtils.IS_OS_LINUX) + { + result = CmdExecUtils.run(BASH + " -c " + command); + } + else + { + throw new CmdExecException("bash call requires GNU/Linux operating system."); + } + + // + return result; + } + /** * Chmod. * @@ -93,7 +140,8 @@ public class Unix { try { - CmdExec.run(SUDO, "chmod", changes, path); + // CmdExec.run(SUDO, "chmod", changes, path); + sudo("chmod", changes, path); } catch (CmdExecException exception) { @@ -223,9 +271,11 @@ public class Unix { try { - logger.info("Creating user account for [" + login + "]."); - CmdExec.run(SUDO, "/usr/sbin/useradd", "-m", "-c", name, "-d", home, login); - logger.info("User account created for [" + login + "]."); + logger.info("Creating user account for [{}].", login); + // CmdExec.run(SUDO, "/usr/sbin/useradd", "-m", "-c", name, + // "-d", home, login); + sudo("/usr/sbin/useradd", "-m", "-c", name, "-d", home, login); + logger.info("User account created for [{}].", login); } catch (CmdExecException exception) { @@ -283,9 +333,10 @@ public class Unix { try { - logger.info("Deleting group for [" + group + "]."); - CmdExec.run(SUDO + " groupdel " + group); - logger.info("Group deleted for [" + group + "]."); + logger.info("Deleting group for [{}].", group); + // CmdExec.run(SUDO + " groupdel " + group); + sudo("groupdel " + group); + logger.info("Group deleted for [{}].", group); } catch (CmdExecException exception) { @@ -307,9 +358,10 @@ public class Unix { try { - logger.info("Deleting user account for [" + login + "]."); - CmdExec.run(SUDO + " /usr/sbin/userdel " + login); - logger.info("User account delted for [" + login + "]."); + logger.info("Deleting user account for [{}].", login); + // CmdExec.run(SUDO + " /usr/sbin/userdel " + login); + sudo("/usr/sbin/userdel " + login); + logger.info("User account delted for [{}].", login); } catch (CmdExecException exception) { @@ -318,6 +370,32 @@ public class Unix } } + /** + * Sudo. + * + * @param command + * the command + * @return the string + * @throws CmdExecException + * the cmd exec exception + */ + public static String exec(final String command) throws CmdExecException + { + String result; + + if (SystemUtils.IS_OS_LINUX) + { + result = CmdExecUtils.run(SUDO + " " + command); + } + else + { + throw new CmdExecException("sudo call requires GNU/Linux operating system."); + } + + // + return result; + } + /** * Gets the acl data. * @@ -426,7 +504,7 @@ public class Unix { try { - CmdExec.run(SUDO, "ln", "-s", sourcePath, targetPath); + CmdExecUtils.run(SUDO, "ln", "-s", sourcePath, targetPath); } catch (CmdExecException exception) { @@ -450,7 +528,8 @@ public class Unix */ public static void modifyLogin(final String sourceLogin, final String targetLogin, final String sourceHomeDirectory) throws UnixException { - logger.info("Starting login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]"); + logger.info("Starting login modifying: [{}] -> [{}]", sourceLogin, targetLogin); + if ((sourceLogin == null) || (sourceLogin.length() == 0)) { throw new IllegalArgumentException("Original login parameters undefined"); @@ -477,8 +556,11 @@ public class Unix } else { - String targetHomeDirectory = new File(sourceHomeDirectory).getParent().toString() + "/" + targetLogin; - if (new File(targetHomeDirectory).exists()) + File targetHomeDirectory = new File(new File(sourceHomeDirectory).getParent(), targetLogin); + // String targetHomeDirectory = new + // File(sourceHomeDirectory).getParent().toString() + "/" + + // targetLogin; + if (targetHomeDirectory.exists()) { throw new IllegalArgumentException("Target home directory already exists: [" + targetHomeDirectory + "]."); } @@ -487,7 +569,7 @@ public class Unix try { logger.info("Login modifying: [" + sourceLogin + "] -> [" + targetLogin + "]"); - CmdExec.run(SUDO + " usermod -l " + targetLogin + " " + sourceLogin); + sudo("usermod -l " + targetLogin + " " + sourceLogin); logger.info("Login modified: [" + sourceLogin + "] -> [" + targetLogin + "]"); } catch (CmdExecException exception) @@ -498,9 +580,9 @@ public class Unix // TOOD problem; try { - logger.info("Renaming home directory: [" + sourceHomeDirectory + "] -> [" + targetLogin + "]"); - CmdExec.run(SUDO + " mv " + sourceHomeDirectory + " " + targetHomeDirectory); - logger.info("Home directory renamed: [" + sourceHomeDirectory + "] -> [" + targetLogin + "]"); + logger.info("Renaming home directory: [{}] -> [{}]", sourceHomeDirectory, targetLogin); + sudo(" mv " + sourceHomeDirectory + " " + targetHomeDirectory); + logger.info("Home directory renamed: [{}] -> [{}]", sourceHomeDirectory, targetLogin); } catch (CmdExecException exception) { @@ -508,7 +590,7 @@ public class Unix } } } - logger.info("Login modifying done: [" + sourceLogin + "] -> [" + targetLogin + "]"); + logger.info("Login modifying done: [{}] -> [{}]", sourceLogin, targetLogin); } /** @@ -535,7 +617,7 @@ public class Unix { try { - CmdExec.run(SUDO, "chmod", "-R", changes, path); + sudo("chmod", "-R", changes, path); } catch (CmdExecException exception) { @@ -577,16 +659,16 @@ public class Unix { try { - logger.info("Login modifying: [" + sourceGroup + "] -> [" + targetGroup + "]"); - CmdExec.run(SUDO + " groupmod -n " + targetGroup + " " + sourceGroup); - logger.info("Login modified: [" + sourceGroup + "] -> [" + targetGroup + "]"); + logger.info("Login modifying: [{}] -> [{}]", sourceGroup, targetGroup); + sudo("groupmod -n " + targetGroup + " " + sourceGroup); + logger.info("Login modified: [{}] -> [{}]", sourceGroup, targetGroup); } catch (CmdExecException exception) { throw new UnixException("Group renaming failed for [" + sourceGroup + "].", exception); } } - logger.info("Group renaming done: [" + sourceGroup + "] -> [" + targetGroup + "]"); + logger.info("Group renaming done: [{}] -> [{}]", sourceGroup, targetGroup); } /** @@ -652,7 +734,8 @@ public class Unix { try { - CmdExec.run(SUDO, "setfacl", args, 1, 6); + String[] command = ArrayUtils.add(args, 0, "setfacl"); + sudo(command); } catch (CmdExecException exception) { @@ -685,9 +768,9 @@ public class Unix { try { - logger.info("Password setting for [" + login + "]."); - CmdExec.run(SUDO, "bash", "-c", "echo \"" + login + ":" + newPassword + "\"| chpasswd "); - logger.info("Password set for [" + login + "]."); + logger.info("Password setting for [{}].", login); + sudoBash("echo \"" + login + ":" + newPassword + "\"| chpasswd "); + logger.info("Password set for [{}].", login); } catch (CmdExecException exception) { @@ -720,7 +803,7 @@ public class Unix try { logger.info("Real name changing for user [{}]", login); - CmdExec.run(SUDO, "chfn", "-f", newRealName, login); + CmdExecUtils.run(SUDO, "chfn", "-f", newRealName, login); } catch (CmdExecException exception) { @@ -729,6 +812,85 @@ public class Unix } } + /** + * Sudo. + * + * @param command + * the command + * @return the string + * @throws CmdExecException + * the cmd exec exception + */ + public static String sudo(final String command) throws CmdExecException + { + String result; + + if (SystemUtils.IS_OS_LINUX) + { + result = CmdExecUtils.run(SUDO + " " + command); + } + else + { + throw new CmdExecException("sudo call requires GNU/Linux operating system."); + } + + // + return result; + } + + /** + * Sudo. + * + * @param command + * the command + * @return the string + * @throws CmdExecException + * the cmd exec exception + */ + public static String sudo(final String... command) throws CmdExecException + { + String result; + + if (SystemUtils.IS_OS_LINUX) + { + String[] newCommand = ArrayUtils.add(command, 0, SUDO); + result = CmdExecUtils.run(newCommand); + } + else + { + throw new CmdExecException("sudo call requires GNU/Linux operating system."); + } + + // + return result; + } + + /** + * Sudo. + * + * @param command + * the command + * @return the string + * @throws CmdExecException + * the cmd exec exception + */ + public static String sudoBash(final String command) throws CmdExecException + { + String result; + + if (SystemUtils.IS_OS_LINUX) + { + result = CmdExecUtils.run(BASH + " -c " + command); + } + else + { + throw new CmdExecException("sudo call requires GNU/Linux operating system."); + } + + // + return result; + } + /** * Unlink. * @@ -737,7 +899,7 @@ public class Unix */ public static void unlink(final String filePathName) { - logger.info("[{}]", filePathName); + logger.info("unlink [filePathName=s{}]", filePathName); if (filePathName == null) { throw new IllegalArgumentException("Parameter undefined: [" + filePathName + "]."); diff --git a/src/fr/devinsy/util/unix/acl/AclManager.java b/src/fr/devinsy/util/unix/acl/AclManager.java index ccd2cdc..f3c50a6 100644 --- a/src/fr/devinsy/util/unix/acl/AclManager.java +++ b/src/fr/devinsy/util/unix/acl/AclManager.java @@ -22,10 +22,10 @@ import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.commons.lang3.ArrayUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import fr.devinsy.util.cmdexec.CmdExec; import fr.devinsy.util.cmdexec.CmdExecException; import fr.devinsy.util.strings.StringListUtils; import fr.devinsy.util.unix.Unix; @@ -39,7 +39,6 @@ import fr.devinsy.util.unix.UnixException; public class AclManager { private static final Logger logger = LoggerFactory.getLogger(AclManager.class); - public static final String SUDO = "/usr/bin/sudo"; final static public Pattern USER_PATTERN = Pattern.compile("^user:(.*):(.*)$"); final static public Pattern GROUP_PATTERN = Pattern.compile("^group:(.*):(.*)$"); @@ -228,7 +227,7 @@ public class AclManager try { logger.info("Getting Acl data for [" + filePathName + "]."); - result = CmdExec.run(SUDO, "/usr/bin/getfacl", "--no-effective", filePathName); + result = Unix.sudo("/usr/bin/getfacl", "--no-effective", filePathName); logger.info("Acl data got for [" + filePathName + "]."); } catch (CmdExecException exception) @@ -517,7 +516,8 @@ public class AclManager { try { - CmdExec.run(SUDO, "setfacl", args, 1, 5); + String[] newArgs = ArrayUtils.add(args, 0, "setfacl"); + Unix.sudo(newArgs); } catch (CmdExecException exception) { diff --git a/src/fr/devinsy/util/unix/linux/Linux.java b/src/fr/devinsy/util/unix/linux/Linux.java new file mode 100644 index 0000000..0a9bf06 --- /dev/null +++ b/src/fr/devinsy/util/unix/linux/Linux.java @@ -0,0 +1,340 @@ +/* + * Copyright (C) 2017 Christian Pierre MOMON + * + * This file is part of Devinsy-unix. + * + * Devinsy-unix is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Devinsy-unix is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Devinsy-unix. If not, see + */ +package fr.devinsy.util.unix.linux; + +import java.io.File; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DurationFormatUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.devinsy.util.Property; +import fr.devinsy.util.PropertyList; + +/** + * The Class Linux. + * + * @author Christian Pierre MOMON (christian.momon@devinsy.fr) + */ +public class Linux +{ + private static Logger logger = LoggerFactory.getLogger(Linux.class); + + private static final Pattern PROC_MEMINFO_PATTERN = Pattern.compile("^(\\S+):\\s+(\\d+)(\\s\\w{2})?$"); + + private static final String REDHAT_RELEASE_FILE = "/etc/redhat-release"; + private static final String DEBIAN_RELEASE_FILE = "/etc/debian_version"; + private static final String SUSE_RELEASE_FILE = "/etc/suse-foo"; + + /** + * Instantiates a new linux. + */ + private Linux() + { + } + + /** + * Read /proc/meminfo properties. + * + * @return the property list + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static PropertyList getProcMeminfoProperties() throws IOException + { + PropertyList result; + + result = new PropertyList(50); + + String buffer = readProcMeminfoFile(); + String[] lines = buffer.split("\n"); + for (String line : lines) + { + Matcher matcher = PROC_MEMINFO_PATTERN.matcher(line); + + if ((matcher.matches()) && ((matcher.groupCount() == 2) || (matcher.groupCount() == 3))) + { + result.add(matcher.group(1), matcher.group(2)); + } + else + { + throw new IOException("Unknown line format in /proc/meminfo file."); + } + } + + // + return result; + } + + /** + * Get a /proc/meminfo summary. + * + * @return the string + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static PropertyList getProcMeminfoSummaryProperties() throws IOException + { + PropertyList result; + + PropertyList meminfo = getProcMeminfoProperties(); + + result = new PropertyList(10); + + result.add(meminfo.get(0)); + result.add(meminfo.get(1)); + result.add(meminfo.get(2)); + result.add(meminfo.get(3)); + result.add(meminfo.get(4)); + result.add(meminfo.get(5)); + result.add(meminfo.get(6)); + result.add(meminfo.get(7)); + result.add(meminfo.get(13)); + result.add(meminfo.get(14)); + + // + return result; + } + + /** + * Gets the /proc/uptime duration. + * + * @return the /proc/uptime duration + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static long getProcUptimeDuration() throws IOException + { + long result; + + String procUptime = readProcUptimeFile(); + + result = (long) Double.parseDouble(procUptime.split("\\s")[0]); + + // + return result; + } + + /** + * Gets the /proc/uptime duration properties. + * + * @return the /proc/uptime duration properties + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static Property getProcUptimeDurationProperties() throws IOException + { + Property result; + + String procUptime = readProcUptimeFile(); + + result = new Property("uptime.duration", procUptime.split("\\s")[0]); + + // + return result; + } + + /** + * Gets the /proc/uptime duration properties. + * + * @return the /proc/uptime duration properties + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static PropertyList getProcUptimeProperties() throws IOException + { + PropertyList result; + + result = new PropertyList(); + + String procUptime = readProcUptimeFile(); + String[] data = procUptime.split("\\s"); + + result.add("uptime.duration", data[0]); + + long duration = Long.parseLong(data[0]); + String humanDuration = DurationFormatUtils.formatDuration(duration * 1000, "dd'd'HH':'mm':'ss"); + result.add("uptime.duration.human", humanDuration); + + result.add("uptime.idle", data[1]); + + // + return result; + } + + /** + * Gets the release distribution properties. + * + * @return the release distribution properties + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static Property getReleaseDistributionProperties() throws IOException + { + Property result; + + String buffer = readReleaseDistributionFile(); + + result = new Property("distribution.release", buffer); + + // + return result; + } + + /** + * Read /proc/cpu processor 0. + * + * @return the string + * @throws IOException + */ + public static String readProcCpuinfo0() throws IOException + { + String result; + + result = readProcCpuinfoFile(); + + int separator = StringUtils.indexOf(result, "\n\n"); + + if (separator != -1) + { + result = result.substring(0, separator); + } + + // + return result; + } + + /** + * Read /proc/cpuinfo file. + * + * @return the string + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static String readProcCpuinfoFile() throws IOException + { + String result; + + result = FileUtils.readFileToString(new File("/proc/cpuinfo")); + + // + return result; + } + + /** + * Read /proc/meminfo file. + * + * @return the string + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static String readProcMeminfoFile() throws IOException + { + String result; + + result = FileUtils.readFileToString(new File("/proc/meminfo")); + + // + return result; + } + + /** + * Read /proc/uptime file. + * + * @return the string + * @throws IOException + */ + public static String readProcUptimeFile() throws IOException + { + String result; + + result = FileUtils.readFileToString(new File("/proc/uptime")); + + // + return result; + } + + /** + * Read /proc/version file. + * + * @return the string + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static String readProcVersionFile() throws IOException + { + String result; + + result = FileUtils.readFileToString(new File("/proc/version")); + + // + return result; + } + + /** + * Read release distribution file. + * + * @return the string + */ + public static String readReleaseDistributionFile() + { + String result; + + try + { + File redhatFile = new File(REDHAT_RELEASE_FILE); + File debianFile = new File(DEBIAN_RELEASE_FILE); + File suseFile = new File(SUSE_RELEASE_FILE); + + if (debianFile.exists()) + { + result = "Debian " + FileUtils.readFileToString(debianFile); + } + else if (redhatFile.exists()) + { + result = FileUtils.readFileToString(redhatFile); + } + else if (suseFile.exists()) + { + // TODO + result = "SUSE " + FileUtils.readFileToString(debianFile); + } + else + { + result = null; + } + + } + catch (IOException exception) + { + logger.warn("Failed to read distribution infos.", exception); + result = null; + } + + // + return result; + } +} + +// //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/test/fr/devinsy/util/unix/LinuxTest.java b/test/fr/devinsy/util/unix/LinuxTest.java new file mode 100644 index 0000000..10d237d --- /dev/null +++ b/test/fr/devinsy/util/unix/LinuxTest.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2017 Christian Pierre MOMON + * + * This file is part of Devinsy-unix. + * + * Devinsy-unix is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Devinsy-unix is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Devinsy-unix. If not, see + */ +package fr.devinsy.util.unix; + +import java.io.IOException; + +import org.junit.Test; + +import fr.devinsy.util.unix.linux.Linux; + +/** + * The Class LinuxTest. + */ +public class LinuxTest +{ + /** + * Test 01. + * + * @throws IOException + */ + @Test + public void test01() throws IOException + { + System.out.println(Linux.readProcMeminfoFile()); + } + +}