ZPoolMonitor.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package hudson.plugins.ec2.ebs;
  2. import hudson.model.PeriodicWork;
  3. import hudson.model.Hudson;
  4. import hudson.model.AdministrativeMonitor;
  5. import hudson.util.TimeUnit2;
  6. import hudson.Extension;
  7. import org.jvnet.solaris.libzfs.LibZFS;
  8. import org.jvnet.solaris.libzfs.ZFSFileSystem;
  9. import org.jvnet.solaris.libzfs.ZFSPool;
  10. import java.net.URL;
  11. import java.io.IOException;
  12. /**
  13. * Once an hour, check if the main zpool is that hosts $HUDSON_HOME has still enough free space.
  14. *
  15. * @author Kohsuke Kawaguchi
  16. */
  17. @Extension
  18. public class ZPoolMonitor extends PeriodicWork {
  19. @Override
  20. public long getRecurrencePeriod() {
  21. return TimeUnit2.HOURS.toMillis(1);
  22. }
  23. @Override
  24. protected void doRun() {
  25. ZFSFileSystem fs=null;
  26. try {
  27. if(isInsideEC2())
  28. fs = new LibZFS().getFileSystemByMountPoint(Hudson.getInstance().getRootDir());
  29. } catch (LinkageError e) {
  30. // probably not running on OpenSolaris
  31. }
  32. if(fs==null) {
  33. cancel();
  34. return;
  35. }
  36. ZFSPool pool = fs.getPool();
  37. long a = pool.getAvailableSize();
  38. long t = pool.getSize();
  39. // if the disk is 90% filled up and the available space is less than 1GB,
  40. // notify the user
  41. ZPoolExpandNotice zen = AdministrativeMonitor.all().get(ZPoolExpandNotice.class);
  42. zen.activated = t/a>10 && a<1000L*1000*1000;
  43. }
  44. private static Boolean isInsideEC2;
  45. /**
  46. * Returns true if this JVM runs inside EC2.
  47. */
  48. public static synchronized boolean isInsideEC2() {
  49. if(isInsideEC2==null) {
  50. try {
  51. new URL("http://169.254.169.254/latest").openStream().close();
  52. isInsideEC2 = true;
  53. } catch (IOException e) {
  54. isInsideEC2 = false;
  55. }
  56. }
  57. return isInsideEC2;
  58. }
  59. }