| 1 | /* |
| 2 | * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | package org.lttng.ust.agent.utils; |
| 20 | |
| 21 | import java.util.Arrays; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.lttng.tools.ILttngSession; |
| 25 | import org.lttng.tools.ILttngSession.Domain; |
| 26 | import org.lttng.tools.utils.ShellUtils; |
| 27 | |
| 28 | /** |
| 29 | * Utility methods to test the presence of certain LTTng tools or libraries in |
| 30 | * the runtime environment. |
| 31 | */ |
| 32 | public final class LttngUtils { |
| 33 | |
| 34 | private LttngUtils() {} |
| 35 | |
| 36 | /** |
| 37 | * Check that lttng-tools and babeltrace are installed on the system and |
| 38 | * working. |
| 39 | * |
| 40 | * @param domain |
| 41 | * The tracing domain to test for (we will try to setup a session |
| 42 | * with this domain) |
| 43 | * @return True if the environment should allow tracing fine, false if there |
| 44 | * was an error |
| 45 | */ |
| 46 | public static boolean checkForLttngTools(Domain domain) { |
| 47 | System.out.println("Sanity check: start"); |
| 48 | try (ILttngSession session = ILttngSession.createSession(null, domain)) { |
| 49 | boolean ret1 = session.enableAllEvents(); |
| 50 | boolean ret2 = session.start(); |
| 51 | boolean ret3 = session.stop(); |
| 52 | /* |
| 53 | * "lttng view" also tests that Babeltrace is installed and working |
| 54 | */ |
| 55 | List<String> contents = session.view(); |
| 56 | if (!contents.isEmpty()) { |
| 57 | System.out.print(String.join("\n", contents)); |
| 58 | } |
| 59 | System.out.println("Sanity check: end"); |
| 60 | return (ret1 && ret2 && ret3 && contents.isEmpty()); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Check if there is a user session daemon currently running on the system. |
| 66 | * It needs to be of the same user as the application running this program. |
| 67 | * |
| 68 | * @return If there is a user session daemon currently running |
| 69 | */ |
| 70 | public static boolean checkForUserSessiond() { |
| 71 | String userName = System.getProperty("user.name"); |
| 72 | |
| 73 | /* The user name is truncated to 7 characters in "ps" */ |
| 74 | String shortUserName = userName.substring(0, Math.min(userName.length(), 7)); |
| 75 | |
| 76 | List<String> command = Arrays.asList("ps", "-e", "u"); |
| 77 | List<String> output = ShellUtils.getOutputFromCommand(false, command); |
| 78 | return output.stream() |
| 79 | .filter(s -> s.contains("lttng-sessiond")) |
| 80 | .anyMatch(s -> s.startsWith(shortUserName)); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Check if there is a root user session daemon daemon currently running on |
| 85 | * the system. |
| 86 | * |
| 87 | * @return If there is a root session daemon currently running |
| 88 | */ |
| 89 | public static boolean checkForRootSessiond() { |
| 90 | List<String> command = Arrays.asList("ps", "-e", "u"); |
| 91 | List<String> output = ShellUtils.getOutputFromCommand(false, command); |
| 92 | return output.stream() |
| 93 | .filter(s -> s.contains("lttng-sessiond")) |
| 94 | .anyMatch(s -> s.startsWith("root")); |
| 95 | } |
| 96 | |
| 97 | } |