I have this program I've been working on that monitors the size of Linux mounts on servers. Here is the code:
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class LinuxMounts {
public static void main(String[] args) throws IOException {
String mailFrom = "java@email";
String mailTo = "<recipients@email>";
String mailSubject = "Testing from Java";
String mailBody = "You passed!";
String mailHost = "localhost";
printHeading();
for (FileStore store : FileSystems.getDefault().getFileStores()) {
String strPath = store.toString();
if (strPath.matches("^/(u01|backups).*$")){
long total = store.getTotalSpace() / 1024 / 1024 / 1024;
long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024 / 1024 / 1024;
long avail = store.getUsableSpace() / 1024 / 1024 / 1024;
float percentage = ((used * 100) / total);
System.out.format("%-50s %20d %20d %20d %20.2f %n", store, total, used, avail, percentage);
}
}
sendEmail(mailFrom, mailTo, mailSubject, mailBody, mailHost);
}
static void printHeading(){
String heading1 = "Filesystem";
String heading2 = "Total Size (MB)";
String heading3 = "Used Space (MB)";
String heading4 = "Available Space (MB)";
String heading5 = "Percentage Used (%)";
System.out.format("%-50s %20s %20s %20s %20s %n", heading1, heading2, heading3, heading4, heading5);
}
public static void sendEmail(String fromEmail, String toEmail, String subject, String body,String mailHost){
Properties properties = System.getProperties();
//Setup mail server
properties.setProperty("mail.smtp.host", mailHost);
Session session = Session.getDefaultInstance(properties);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
message.setSubject(subject);
message.setText(body);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
The program works. I would like to send the output of printHeading() and the mount values in the body of the email. Eventually, I would like to add the server name and some other details to the 'Subject' line.