|
| 1 | +/** |
| 2 | + * The contents of this file are subject to the license and copyright |
| 3 | + * detailed in the LICENSE and NOTICE files at the root of the source |
| 4 | + * tree and available online at |
| 5 | + * |
| 6 | + * http://www.dspace.org/license/ |
| 7 | + */ |
| 8 | +package org.dspace.app.healthreport; |
| 9 | + |
| 10 | +import static org.apache.commons.io.IOUtils.toInputStream; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | +import java.text.SimpleDateFormat; |
| 16 | +import java.util.Date; |
| 17 | +import java.util.LinkedHashMap; |
| 18 | +import java.util.Locale; |
| 19 | +import java.util.Map; |
| 20 | +import javax.mail.MessagingException; |
| 21 | + |
| 22 | +import org.apache.commons.cli.ParseException; |
| 23 | +import org.apache.logging.log4j.LogManager; |
| 24 | +import org.apache.logging.log4j.Logger; |
| 25 | +import org.dspace.core.Context; |
| 26 | +import org.dspace.core.Email; |
| 27 | +import org.dspace.core.I18nUtil; |
| 28 | +import org.dspace.eperson.factory.EPersonServiceFactory; |
| 29 | +import org.dspace.eperson.service.EPersonService; |
| 30 | +import org.dspace.health.Check; |
| 31 | +import org.dspace.health.Report; |
| 32 | +import org.dspace.health.ReportInfo; |
| 33 | +import org.dspace.scripts.DSpaceRunnable; |
| 34 | +import org.dspace.services.ConfigurationService; |
| 35 | +import org.dspace.services.factory.DSpaceServicesFactory; |
| 36 | +import org.dspace.utils.DSpace; |
| 37 | + |
| 38 | +/** |
| 39 | + * This class is used to generate a health report of the DSpace instance. |
| 40 | + * @author Matus Kasak (dspace at dataquest.sk) |
| 41 | + * @author Milan Majchrak (dspace at dataquest.sk) |
| 42 | + */ |
| 43 | +public class HealthReport extends DSpaceRunnable<HealthReportScriptConfiguration> { |
| 44 | + ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService(); |
| 45 | + private static final Logger log = LogManager.getLogger(HealthReport.class); |
| 46 | + private EPersonService ePersonService; |
| 47 | + |
| 48 | + /** |
| 49 | + * Checks to be performed. |
| 50 | + */ |
| 51 | + private static final LinkedHashMap<String, Check> checks = Report.checks(); |
| 52 | + |
| 53 | + /** |
| 54 | + * `-i`: Info, show help information. |
| 55 | + */ |
| 56 | + private boolean info = false; |
| 57 | + |
| 58 | + /** |
| 59 | + * `-e`: Email, send report to specified email address. |
| 60 | + */ |
| 61 | + private String email; |
| 62 | + |
| 63 | + /** |
| 64 | + * `-c`: Check, perform only specific check by index (0-`getNumberOfChecks()`). |
| 65 | + */ |
| 66 | + private int specificCheck = -1; |
| 67 | + |
| 68 | + /** |
| 69 | + * `-f`: For, specify the last N days to consider. |
| 70 | + * Default value is set in dspace.cfg. |
| 71 | + */ |
| 72 | + private int forLastNDays = configurationService.getIntProperty("healthcheck.last_n_days"); |
| 73 | + |
| 74 | + /** |
| 75 | + * `-o`: Output, specify a file to save the report. |
| 76 | + */ |
| 77 | + private String fileName; |
| 78 | + |
| 79 | + @Override |
| 80 | + public HealthReportScriptConfiguration getScriptConfiguration() { |
| 81 | + return new DSpace().getServiceManager() |
| 82 | + .getServiceByName("health-report", HealthReportScriptConfiguration.class); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void setup() throws ParseException { |
| 87 | + ePersonService = EPersonServiceFactory.getInstance().getEPersonService(); |
| 88 | + // `-i`: Info, show help information. |
| 89 | + if (commandLine.hasOption('i')) { |
| 90 | + info = true; |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + // `-e`: Email, send report to specified email address. |
| 95 | + if (commandLine.hasOption('e')) { |
| 96 | + email = commandLine.getOptionValue('e'); |
| 97 | + handler.logInfo("\nReport sent to this email address: " + email); |
| 98 | + } |
| 99 | + |
| 100 | + // `-c`: Check, perform only specific check by index (0-`getNumberOfChecks()`). |
| 101 | + if (commandLine.hasOption('c')) { |
| 102 | + String checkOption = commandLine.getOptionValue('c'); |
| 103 | + try { |
| 104 | + specificCheck = Integer.parseInt(checkOption); |
| 105 | + if (specificCheck < 0 || specificCheck >= getNumberOfChecks()) { |
| 106 | + specificCheck = -1; |
| 107 | + } |
| 108 | + } catch (NumberFormatException e) { |
| 109 | + log.info("Invalid value for check. It has to be a number from the displayed range."); |
| 110 | + return; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // `-f`: For, specify the last N days to consider. |
| 115 | + if (commandLine.hasOption('f')) { |
| 116 | + String daysOption = commandLine.getOptionValue('f'); |
| 117 | + try { |
| 118 | + forLastNDays = Integer.parseInt(daysOption); |
| 119 | + } catch (NumberFormatException e) { |
| 120 | + log.info("Invalid value for last N days. Argument f has to be a number."); |
| 121 | + return; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // `-o`: Output, specify a file to save the report. |
| 126 | + if (commandLine.hasOption('o')) { |
| 127 | + fileName = commandLine.getOptionValue('o'); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void internalRun() throws Exception { |
| 133 | + if (info) { |
| 134 | + printHelp(); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + ReportInfo ri = new ReportInfo(this.forLastNDays); |
| 139 | + |
| 140 | + StringBuilder sbReport = new StringBuilder(); |
| 141 | + sbReport.append("\n\nHEALTH REPORT:\n"); |
| 142 | + |
| 143 | + int position = -1; |
| 144 | + for (Map.Entry<String, Check> check_entry : Report.checks().entrySet()) { |
| 145 | + ++position; |
| 146 | + if (specificCheck != -1 && specificCheck != position) { |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + String name = check_entry.getKey(); |
| 151 | + Check check = check_entry.getValue(); |
| 152 | + |
| 153 | + log.info("#{}. Processing [{}] at [{}]", position, name, new SimpleDateFormat( |
| 154 | + "yyyy-MM-dd HH:mm:ss.SSS").format(new Date())); |
| 155 | + |
| 156 | + sbReport.append("\n######################\n\n").append(name).append(":\n"); |
| 157 | + check.report(ri); |
| 158 | + sbReport.append(check.getReport()); |
| 159 | + } |
| 160 | + |
| 161 | + // save output to file |
| 162 | + if (fileName != null) { |
| 163 | + Context context = new Context(); |
| 164 | + context.setCurrentUser(ePersonService.find(context, this.getEpersonIdentifier())); |
| 165 | + |
| 166 | + InputStream inputStream = toInputStream(sbReport.toString(), StandardCharsets.UTF_8); |
| 167 | + handler.writeFilestream(context, fileName, inputStream, "export"); |
| 168 | + |
| 169 | + context.restoreAuthSystemState(); |
| 170 | + context.complete(); |
| 171 | + } |
| 172 | + |
| 173 | + // send email to email address from argument |
| 174 | + if (email != null) { |
| 175 | + try { |
| 176 | + Email e = Email.getEmail(I18nUtil.getEmailFilename(Locale.getDefault(), "healthcheck")); |
| 177 | + e.addRecipient(email); |
| 178 | + e.addArgument(sbReport.toString()); |
| 179 | + e.send(); |
| 180 | + } catch (IOException | MessagingException e) { |
| 181 | + log.error("Error sending email:", e); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + handler.logInfo(sbReport.toString()); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void printHelp() { |
| 190 | + handler.logInfo("\n\nINFORMATION\nThis process creates a health report of your DSpace.\n" + |
| 191 | + "You can choose from these available options:\n" + |
| 192 | + " -i, --info Show help information\n" + |
| 193 | + " -e, --email Send report to specified email address\n" + |
| 194 | + " -c, --check Perform only specific check by index (0-" + (getNumberOfChecks() - 1) + ")\n" + |
| 195 | + " -f, --for Specify the last N days to consider\n" + |
| 196 | + " -o, --output Specify a file to save the report\n\n" + |
| 197 | + "If you want to execute only one check using -c, use check index:\n" + checksNamesToString() + "\n" |
| 198 | + ); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Convert checks names to string. |
| 203 | + */ |
| 204 | + private String checksNamesToString() { |
| 205 | + StringBuilder names = new StringBuilder(); |
| 206 | + int pos = 0; |
| 207 | + for (String name : checks.keySet()) { |
| 208 | + names.append(String.format(" %d. %s\n", pos++, name)); |
| 209 | + } |
| 210 | + return names.toString(); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Get the number of checks. This is used for the `-c` option. |
| 215 | + */ |
| 216 | + public static int getNumberOfChecks() { |
| 217 | + return checks.size(); |
| 218 | + } |
| 219 | +} |
0 commit comments