Skip to content

Commit

Permalink
Create health check for size of the log files xwikisas#35 - draft
Browse files Browse the repository at this point in the history
* Created a check for verifying the size of the logs folder
  • Loading branch information
ChiuchiuSorin committed Dec 7, 2023
1 parent 1d53e87 commit c8b8a8e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This 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 2.1 of
* the License, or (at your option) any later version.
*
* This software 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 this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package com.xwiki.admintools.internal.health.checks.performance;

import java.io.File;
import java.text.DecimalFormat;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.xwiki.component.annotation.Component;

import com.xwiki.admintools.ServerIdentifier;
import com.xwiki.admintools.health.HealthCheck;
import com.xwiki.admintools.health.HealthCheckResult;
import com.xwiki.admintools.internal.data.identifiers.CurrentServer;

@Component
@Named(LogsSizeCheck.HINT)
@Singleton
public class LogsSizeCheck implements HealthCheck
{
/**
* Component identifier.
*/
public static final String HINT = "logSize";

@Inject
private CurrentServer currentServer;

@Override
public HealthCheckResult check()
{
ServerIdentifier server = currentServer.getCurrentServer();

long logsSize = getFolderSize(server.getLogsFolderPath());

String[] units = new String[] { "B", "KB", "MB", "GB" };
int unitIndex = (int) (Math.log10(logsSize) / 3);
double unitValue = 1 << (unitIndex * 10);

String readableSize = new DecimalFormat("#,##0.#").format(logsSize / unitValue) + " " + units[unitIndex];

// NEED TO SET SOME METRICS. WHAT LOG SIZE IS TOO BIG?
if (logsSize / (1024 * 1024) > 500) {
return new HealthCheckResult("adminTools.dashboard.healthcheck.performance.cpu.info", "info");
} else {
return new HealthCheckResult("adminTools.dashboard.healthcheck.performance.cpu.warn",
"adminTools.dashboard.healthcheck.performance.cpu.recommendation", "error", "cpuSpecifications");
}
}

public static long getFolderSize(String folderPath)
{
File folder = new File(folderPath);
long length = 0;
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
length += file.length();
} else {
length += getFolderSize(file.getAbsolutePath());
}
}
}
return length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ com.xwiki.admintools.internal.health.checks.security.ConfigurationEncodingHealth
com.xwiki.admintools.internal.health.checks.security.FileEncodingHealthCheck
com.xwiki.admintools.internal.health.checks.security.LangEncodingHealthCheck
com.xwiki.admintools.internal.health.checks.performance.CPUHealthCheck
com.xwiki.admintools.internal.health.checks.performance.LogsSizeCheck
com.xwiki.admintools.internal.health.checks.performance.PhysicalMemoryHealthCheck
com.xwiki.admintools.internal.health.checks.performance.PhysicalSpaceHealthCheck
com.xwiki.admintools.internal.health.checks.memory.CacheMemoryHealthCheck
Expand Down

0 comments on commit c8b8a8e

Please sign in to comment.