/**
  * Destroys a virtual system in hyper-v
  *
  * @param vmDispatch TODO
  * @return the error code of
  */
 public int destroyVirtualSystem(final IJIDispatch vmDispatch) throws Exception {
   JIVariant tmp = vmDispatch.get("Path_");
   IJIDispatch dispatchTemp =
       (IJIDispatch)
           JIObjectFactory.narrowObject(
               tmp.getObjectAsComObject().queryInterface(IJIDispatch.IID));
   String virtualSystemPath = dispatchTemp.get("Path").getObjectAsString2();
   JIVariant[] results =
       dispatch.callMethodA(
           "DestroyVirtualSystem",
           new Object[] {
             new JIString(virtualSystemPath), JIVariant.EMPTY_BYREF(), JIVariant.EMPTY_BYREF()
           });
   int error = results[0].getObjectAsInt();
   if (results.length > 1) {
     if (error != 0) {
       if (error == 4096) {
         logger.debug("Destroying virtual system...");
         String jobPath = results[2].getObjectAsVariant().getObjectAsString2();
         HyperVUtils.monitorJob(jobPath, service.getObjectDispatcher());
       } else {
         String message = "The virtual system could no te destroyed " + virtualSystemPath;
         logger.error(message);
         throw new JIException(error, message);
       }
     }
   }
   return error;
 }
  public LinkedList<IJIDispatch> getDiskDrives() throws JIException {
    System.gc();

    // get all local disks
    JIVariant results[] =
        service_dispatch.callMethodA(
            "ExecQuery",
            new Object[] {
              new JIString("select * from Win32_LogicalDisk where DriveType = 3"),
              JIVariant.OPTIONAL_PARAM(),
              JIVariant.OPTIONAL_PARAM(),
              JIVariant.OPTIONAL_PARAM()
            });
    IJIDispatch wbemObjectSet_dispatch =
        (IJIDispatch) JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
    JIVariant variant = wbemObjectSet_dispatch.get("_NewEnum");
    IJIComObject object2 = variant.getObjectAsComObject();

    IJIEnumVariant enumVARIANT =
        (IJIEnumVariant) JIObjectFactory.narrowObject(object2.queryInterface(IJIEnumVariant.IID));

    LinkedList<IJIDispatch> drives = new LinkedList<IJIDispatch>();

    int count = wbemObjectSet_dispatch.get("Count").getObjectAsInt();
    for (int i = 0; i < count; i++) {
      Object[] values = enumVARIANT.next(1);
      JIArray array = (JIArray) values[0];
      JIVariant[] variants = (JIVariant[]) array.getArrayInstance();
      for (JIVariant item : variants) {
        drives.add((IJIDispatch) JIObjectFactory.narrowObject(item.getObjectAsComObject()));
      }
    }
    return drives;
  }
  /* FIXME: This code sometimes fails with an "org.jinterop.dcom.common.JIException: An internal error occurred. [0x8001FFFF]"
   * on Windows 2008 */
  public int getCPUUsage() throws JIException {
    System.gc();

    JIVariant results[] =
        service_dispatch.callMethodA(
            "Get",
            new Object[] {
              new JIString("Win32_PerfRawData_PerfOS_Processor.Name='_Total'"),
              new Integer(0),
              JIVariant.OPTIONAL_PARAM()
            });

    IJIDispatch wbemObject_dispatch =
        (IJIDispatch) JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
    long ppt =
        Long.parseLong(
            wbemObject_dispatch.get("PercentProcessorTime").getObjectAsString().getString());
    long tss =
        Long.parseLong(
            wbemObject_dispatch.get("TimeStamp_Sys100NS").getObjectAsString().getString());

    if (this.percentprocessortime == -1 && this.timestamp == -1) {
      this.percentprocessortime = ppt;
      this.timestamp = tss;
      return -1;
    }

    double load =
        (1 - ((double) (this.percentprocessortime - ppt) / (double) (this.timestamp - tss))) * 100;
    this.percentprocessortime = ppt;
    this.timestamp = tss;

    return (int) Math.round(load);
  }
  /**
   * Removes resources to an existing virtual computer system
   *
   * @param vmDispatch A reference to the computer system instance to which the resource is to be
   *     added.
   * @param resourceAllocationDispatch the resource allocation setting data reference to be removed.
   * @throws JIException
   */
  public void removeVirtualSystemResources(
      final IJIDispatch vmDispatch, final IJIDispatch resourceAllocationDispatch)
      throws JIException {
    // Getting the dispatcher of the VM Path
    IJIDispatch vmPathDispatcher =
        (IJIDispatch)
            JIObjectFactory.narrowObject(
                vmDispatch.get("Path_").getObjectAsComObject().queryInterface(IJIDispatch.IID));

    // Getting the virtual machine path
    String vmPath = vmPathDispatcher.get("Path").getObjectAsString2();

    // Getting the dispatcher of the resource path
    IJIDispatch resourcePathDispatcher =
        (IJIDispatch)
            JIObjectFactory.narrowObject(
                resourceAllocationDispatch
                    .get("Path_")
                    .getObjectAsComObject()
                    .queryInterface(IJIDispatch.IID));

    // Getting the virtual machine path
    String resourcePath = resourcePathDispatcher.get("Path").getObjectAsString2();

    JIVariant[] tmp =
        dispatch.callMethodA(
            "RemoveVirtualSystemResources",
            new Object[] {
              new JIString(vmPath),
              new JIArray(new JIString[] {new JIString(resourcePath)}),
              JIVariant.EMPTY_BYREF(),
              JIVariant.EMPTY_BYREF()
            });

    int result = tmp[0].getObjectAsInt();

    String name = resourceAllocationDispatch.get("ElementName").getObjectAsString2();

    if (result == 0) {
      logger.debug(name + " removed from " + vmPath);
    } else {
      if (result == 4096) {
        logger.debug("Removing resources...");
        String jobPath = tmp[2].getObjectAsVariant().getObjectAsString2();
        HyperVUtils.monitorJob(jobPath, service.getObjectDispatcher());
      } else {
        logger.error(name + " deleting to " + vmPath + " failed with error code " + result);
        throw new IllegalStateException(
            "Cannot remove resource "
                + name
                + " to "
                + vmDispatch.get("ElementName").getObjectAsString2());
      }
    }
  }
  public long getFreeMemorySpace() throws JIException {
    System.gc();

    JIVariant results[] =
        service_dispatch.callMethodA(
            "ExecQuery",
            new Object[] {
              new JIString("select * from Win32_PerfRawData_PerfOS_Memory"),
              JIVariant.OPTIONAL_PARAM(),
              JIVariant.OPTIONAL_PARAM(),
              JIVariant.OPTIONAL_PARAM()
            });
    IJIDispatch wbemObjectSet_dispatch =
        (IJIDispatch) JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
    JIVariant variant = wbemObjectSet_dispatch.get("_NewEnum");
    IJIComObject object2 = variant.getObjectAsComObject();

    IJIEnumVariant enumVARIANT =
        (IJIEnumVariant) JIObjectFactory.narrowObject(object2.queryInterface(IJIEnumVariant.IID));
    if (wbemObjectSet_dispatch.get("Count").getObjectAsInt() != 1)
      return -1; // there should be 1 hint

    JIArray array = (JIArray) enumVARIANT.next(1)[0];
    JIVariant item = ((JIVariant[]) array.getArrayInstance())[0];
    IJIDispatch wbemObject_dispatch =
        (IJIDispatch) JIObjectFactory.narrowObject(item.getObjectAsComObject());
    return Long.parseLong(
        wbemObject_dispatch.get("AvailableKBytes").getObjectAsString().getString());
  }
  /**
   * Destroys a virtual system in hyper-v
   *
   * @param vmDispatch A reference to the virtual computer system instance to be destroyed.
   */
  public void destroyVirtualSystem2(final IJIDispatch vmDispatch) throws Exception {
    if (this.destroyVirtualSystem == null) {
      for (final SWbemMethod m : super.getMethods()) {
        if (m.getName().equals("DestroyVirtualSystem")) {
          this.destroyVirtualSystem = m;
        }
      }
    }

    JIVariant tmp = vmDispatch.get("Path_");
    IJIDispatch dispatchTemp =
        (IJIDispatch)
            JIObjectFactory.narrowObject(
                tmp.getObjectAsComObject().queryInterface(IJIDispatch.IID));
    String virtualSystemPath = dispatchTemp.get("Path").getObjectAsString2();

    SWbemObject inParams = this.destroyVirtualSystem.getInParameters();

    inParams
        .getObjectDispatcher()
        .put("ComputerSystem", new JIVariant(new JIString(virtualSystemPath)));

    Object[] methodParams =
        new Object[] {
          new JIString("DestroyVirtualSystem"),
          new JIVariant(inParams.getObjectDispatcher()),
          new Integer(0),
          JIVariant.NULL(),
        };

    // Execute the method.
    super.objectDispatcher.callMethodA("ExecMethod_", methodParams);
    // JIVariant[] tmp =
    // dispatch.callMethodA("DefineVirtualSystem", new Object[] {
    // new JIString(globalSettingDataText), JIVariant.OPTIONAL_PARAM(),
    // JIVariant.OPTIONAL_PARAM(),});
    // int defRes = tmp[0].getObjectAsInt();
  }
  public LinkedList<IJIDispatch> getServices() throws JIException {
    System.gc();

    // get all services which should start automatically but are not running
    JIVariant results[] =
        service_dispatch.callMethodA(
            "ExecQuery",
            new Object[] {
              new JIString(
                  "select * from Win32_Service where StartMode = 'Auto' and Started = FALSE"),
              JIVariant.OPTIONAL_PARAM(),
              JIVariant.OPTIONAL_PARAM(),
              JIVariant.OPTIONAL_PARAM()
            });

    IJIDispatch wbemObjectSet_dispatch =
        (IJIDispatch) JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
    JIVariant variant = wbemObjectSet_dispatch.get("_NewEnum");
    IJIComObject object2 = variant.getObjectAsComObject();
    IJIEnumVariant enumVARIANT =
        (IJIEnumVariant) JIObjectFactory.narrowObject(object2.queryInterface(IJIEnumVariant.IID));

    LinkedList<IJIDispatch> services = new LinkedList<IJIDispatch>();

    int count = wbemObjectSet_dispatch.get("Count").getObjectAsInt();
    for (int i = 0; i < count; i++) {
      Object[] values = enumVARIANT.next(1);
      JIArray array = (JIArray) values[0];
      JIVariant[] variants = (JIVariant[]) array.getArrayInstance();
      for (JIVariant item : variants) {
        IJIDispatch wbemObject_dispatch =
            (IJIDispatch) JIObjectFactory.narrowObject(item.getObjectAsComObject());
        services.add(wbemObject_dispatch);
      }
    }
    return services;
  }
  public WindowsHealth(
      String address, String domain, String user, String passwd, int timeout, boolean verbose)
      throws JIException, UnknownHostException {
    if (verbose) System.out.print("Creating session... ");
    session = JISession.createSession(domain, user, passwd);
    session.useSessionSecurity(true);
    session.setGlobalSocketTimeout(timeout);

    if (verbose) {
      System.out.println("OK");
      System.out.print("Connecting to COMServer... ");
    }

    JIProgId progid = JIProgId.valueOf("WbemScripting.SWbemLocator");
    comStub = new JIComServer(progid, address, session);
    IJIComObject unknown = comStub.createInstance();
    comObject =
        (IJIComObject)
            unknown.queryInterface("76A6415B-CB41-11d1-8B02-00600806D9B6"); // ISWbemLocator

    if (verbose) {
      System.out.println("OK");
      System.out.print("Connecting to targethost... ");
    }

    dispatch =
        (IJIDispatch) JIObjectFactory.narrowObject(comObject.queryInterface(IJIDispatch.IID));
    service =
        dispatch
            .callMethodA(
                "ConnectServer",
                new Object[] {
                  new JIString(address),
                  JIVariant.OPTIONAL_PARAM(),
                  JIVariant.OPTIONAL_PARAM(),
                  JIVariant.OPTIONAL_PARAM(),
                  JIVariant.OPTIONAL_PARAM(),
                  JIVariant.OPTIONAL_PARAM(),
                  new Integer(0),
                  JIVariant.OPTIONAL_PARAM()
                })[0];

    service_dispatch = (IJIDispatch) JIObjectFactory.narrowObject(service.getObjectAsComObject());
    if (verbose) System.out.println("OK");

    percentprocessortime = -1;
    timestamp = -1;
  }
  /**
   * Modifies the settings for an existing virtual computer system.
   *
   * @param vmDispatch A reference to the virtual computer system to be modified.
   * @param systemSettingData describes the modified setting values for the virtual computer system.
   * @throws JIException
   */
  public void modifyVirtualSystem(final IJIDispatch vmDispatch, final IJIDispatch systemSettingData)
      throws JIException {
    // Getting the dispatcher of the VM Path
    IJIDispatch vmPathDispatcher =
        (IJIDispatch)
            JIObjectFactory.narrowObject(
                vmDispatch.get("Path_").getObjectAsComObject().queryInterface(IJIDispatch.IID));

    // Getting the virtual machine path
    String vmPath = vmPathDispatcher.get("Path").getObjectAsString2();

    // Getting the dispatcher of the resource path

    String resourceText =
        systemSettingData.callMethodA("GetText_", new Object[] {new Integer(1)})[0]
            .getObjectAsString2();

    JIVariant[] tmp =
        dispatch.callMethodA(
            "ModifyVirtualSystem",
            new Object[] {
              new JIString(vmPath),
              new JIArray(new JIString[] {new JIString(resourceText)}),
              JIVariant.EMPTY_BYREF(),
              JIVariant.EMPTY_BYREF()
            });

    int result = tmp[0].getObjectAsInt();

    String name = systemSettingData.get("ElementName").getObjectAsString2();
    if (result == 0) {
      logger.debug("Setting data properly modified" + vmPath);
    } else {
      if (result == 4096) {
        logger.debug("Modifying setting data...");
        String jobPath = tmp[1].getObjectAsVariant().getObjectAsString2();
        HyperVUtils.monitorJob(jobPath, service.getObjectDispatcher());
      } else {
        logger.error(name + " addition to " + vmPath + " failed with error code " + result);
        throw new IllegalStateException(
            "Setting data cannot be modified to"
                + vmDispatch.get("ElementName").getObjectAsString2());
      }
    }
  }
Example #10
0
 public final int getStatus() throws JIException {
   final JIVariant variant = dispatch.get("Status"); // i18n lib
   return variant.getObjectAsInt();
 }
  /**
   * Add resources to an existing virtual computer system
   *
   * @param vmDispatch A reference to the computer system instance to which the resource is to be
   *     added.
   * @param newResourceAllocationDispatch the new resource allocation setting data reference to be
   *     added.
   * @return the resource allocation setting data path of the added resource
   * @deprecated
   * @throws JIException
   */
  @Deprecated
  public String addVirtualSystemResources2(
      final IJIDispatch vmDispatch, final IJIDispatch newResourceAllocationDispatch)
      throws JIException {
    if (this.addVirtualSystemResources == null) {
      for (final SWbemMethod m : super.getMethods()) {
        if (m.getName().equals("AddVirtualSystemResources")) {
          this.addVirtualSystemResources = m;
        }
      }
    }

    // Getting the dispatcher of the VM Path
    IJIDispatch vmPathDispatcher =
        (IJIDispatch)
            JIObjectFactory.narrowObject(
                vmDispatch.get("Path_").getObjectAsComObject().queryInterface(IJIDispatch.IID));

    // Getting the virtual machine path
    String vmPath = vmPathDispatcher.get("Path").getObjectAsString2();

    // Getting the dispatcher of the resource path

    String resourceText =
        newResourceAllocationDispatch.callMethodA("GetText_", new Object[] {new Integer(1)})[0]
            .getObjectAsString2();

    SWbemObject inParams = this.addVirtualSystemResources.getInParameters();

    inParams.getObjectDispatcher().put("TargetSystem", new JIVariant(new JIString(vmPath)));

    inParams
        .getObjectDispatcher()
        .put(
            "ResourceSettingData",
            new JIVariant(new JIArray(new JIString[] {new JIString(resourceText)})));

    Object[] methodParams =
        new Object[] {
          new JIString("AddVirtualSystemResources"),
          new JIVariant(inParams.getObjectDispatcher()),
          new Integer(0),
          JIVariant.NULL(),
        };

    // Execute the method.
    JIVariant[] results = super.objectDispatcher.callMethodA("ExecMethod_", methodParams);

    // Get the out parameters.
    JIVariant outParamsVar = results[0];
    IJIComObject co = outParamsVar.getObjectAsComObject();
    IJIDispatch outParamsDisp = (IJIDispatch) JIObjectFactory.narrowObject(co);

    // Get the out parameter virtualSystemResources and convert it into an
    // array of JIVariants.
    JIVariant newResourcesVars = outParamsDisp.get("NewResources");
    JIArray newResourcesVarsJIArr = newResourcesVars.getObjectAsArray();
    JIVariant[] newResourcesVarsJIVarArr = (JIVariant[]) newResourcesVarsJIArr.getArrayInstance();

    String newResourceCoString = newResourcesVarsJIVarArr[0].getObjectAsString2();

    return newResourceCoString;
  }
Example #12
0
 public final int getPID() throws JIException {
   final JIVariant variant = dispatch.get("ProcessID"); // i18n lib
   return variant.getObjectAsInt();
 }
  /**
   * Add resources to an existing virtual computer system
   *
   * @param vmDispatch A reference to the computer system instance to which the resource is to be
   *     added.
   * @param newResourceAllocationDispatch the new resource allocation setting data reference to be
   *     added.
   * @return the resource allocation setting data path of the added resource
   * @throws JIException
   */
  public String addVirtualSystemResources(
      final IJIDispatch vmDispatch, final IJIDispatch newResourceAllocationDispatch)
      throws JIException {
    // Getting the dispatcher of the VM Path
    IJIDispatch vmPathDispatcher =
        (IJIDispatch)
            JIObjectFactory.narrowObject(
                vmDispatch.get("Path_").getObjectAsComObject().queryInterface(IJIDispatch.IID));

    // Getting the virtual machine path
    String vmPath = vmPathDispatcher.get("Path").getObjectAsString2();

    // Getting the dispatcher of the resource path

    String resourceText =
        newResourceAllocationDispatch.callMethodA("GetText_", new Object[] {new Integer(1)})[0]
            .getObjectAsString2();

    JIVariant[] tmp =
        dispatch.callMethodA(
            "AddVirtualSystemResources",
            new Object[] {
              new JIString(vmPath),
              new JIArray(new JIString[] {new JIString(resourceText)}),
              JIVariant.EMPTY_BYREF(),
              JIVariant.EMPTY_BYREF()
            });

    int result = tmp[0].getObjectAsInt();

    JIVariant resultVariant = tmp[2];

    JIVariant variant2 = resultVariant.getObjectAsVariant();

    JIArray newResourcesArr = variant2.getObjectAsArray();

    if (newResourcesArr == null) {
      throw new JIException(32768, "The resource could not be added");
    }

    JIVariant[] newResourcesVarArr = (JIVariant[]) newResourcesArr.getArrayInstance();

    String newResourcePath = newResourcesVarArr[0].getObjectAsString2();

    String name = newResourceAllocationDispatch.get("ElementName").getObjectAsString2();
    if (result == 0) {
      logger.debug(name + " added to " + vmPath);
    } else {
      if (result == 4096) {
        logger.debug("Addind resources...");
        String jobPath = tmp[1].getObjectAsVariant().getObjectAsString2();
        HyperVUtils.monitorJob(jobPath, service.getObjectDispatcher());
      } else {
        logger.error(name + " addition to " + vmPath + " failed with error code " + result);
        throw new IllegalStateException(
            "Cannot add resource "
                + name
                + " to "
                + vmDispatch.get("ElementName").getObjectAsString2());
      }
    }

    return newResourcePath;
  }
Example #14
0
 public final InputTextStream getStderr() throws JIException {
   final JIVariant variant = dispatch.get("StdErr"); // i18n lib
   final IJIComObject comObject = variant.getObjectAsComObject();
   return new InputTextStream((IJIDispatch) JIObjectFactory.narrowObject(comObject));
 }
Example #15
0
 @SuppressWarnings("unused")
 public final void terminate() throws JIException {
   dispatch.callMethodA("Terminate"); // i18n lib
 }
  public static void main(String[] args) {
    // default values
    String host = "";
    String domain = "";
    String user = "";
    String passwd = "";
    int timeout = 5000;

    int n_measures = 1;
    int delay = 1000;

    boolean cpu = false;
    int cpu_warning = 85;
    int cpu_critical = 95;

    boolean memory = false;
    int mem_warning = 85;
    int mem_critical = 95;

    boolean disk = false;
    int disk_warning = 85;
    int disk_critical = 95;

    boolean service = false;
    String exclude = "";
    int serv_critical = 1;

    int verbose = 1;
    Level logging = Level.OFF;

    // create a new parser and add all possible options
    CmdLineParser parser = new CmdLineParser();
    Option host_op = parser.addStringOption('t', "targethost");
    Option domain_op = parser.addStringOption('d', "domain");
    Option user_op = parser.addStringOption('u', "user");
    Option passwd_op = parser.addStringOption('p', "password");
    Option timeout_op = parser.addIntegerOption("timeout");
    Option n_measures_op = parser.addIntegerOption('n', "number_of_measures");
    Option delay_op = parser.addIntegerOption("delay");
    Option cpu_op = parser.addBooleanOption("cpu");
    Option cpu_warning_op = parser.addIntegerOption("cpu_warning");
    Option cpu_critical_op = parser.addIntegerOption("cpu_critical");
    Option memory_op = parser.addBooleanOption("memory");
    Option mem_warning_op = parser.addIntegerOption("mem_warning");
    Option mem_critical_op = parser.addIntegerOption("mem_critical");
    Option disk_op = parser.addBooleanOption("disk");
    Option disk_warning_op = parser.addIntegerOption("disk_warning");
    Option disk_critical_op = parser.addIntegerOption("disk_critical");
    Option service_op = parser.addBooleanOption("services");
    Option exclude_op = parser.addStringOption('x', "exclude");
    Option help_op = parser.addBooleanOption('h', "help");
    Option verbose_op = parser.addBooleanOption('v', "verbose");
    Option version_op = parser.addBooleanOption('V', "version");

    try {
      // parse the arguments
      parser.parse(args);
    } catch (Exception e) {
      fail(e.getMessage());
    }

    // -h or --help option was given just print helpmessage and exit
    if ((Boolean) parser.getOptionValue(help_op, false)) {
      System.out.println(helpmessage);
      System.exit(0);
    }
    // -V or --version option was given just print version information and exit
    if ((Boolean) parser.getOptionValue(version_op, false)) {
      System.out.println(version);
      System.exit(0);
    }

    // check if a settingsfile was given and check if it exists
    if (args.length == 0) fail("Please provide a settingsfile");
    String settingsFile = args[0];
    if (!new File(settingsFile).exists()) fail("Settingsfile '" + settingsFile + "' not found");
    if (parser.getRemainingArgs().length != 1) fail("Syntax error");

    //
    Properties properties = new Properties();
    try {
      properties.load(new FileInputStream(settingsFile));
    } catch (IOException e) {
      fail(e.getMessage());
    }
    try {
      // get all values

      host = (String) getValue(parser, properties, host_op, host);
      domain = (String) getValue(parser, properties, domain_op, domain);
      user = (String) getValue(parser, properties, user_op, user);
      passwd = (String) getValue(parser, properties, passwd_op, passwd);

      timeout = (Integer) getValue(parser, properties, timeout_op, timeout);
      n_measures = (Integer) getValue(parser, properties, n_measures_op, n_measures);
      delay = (Integer) getValue(parser, properties, delay_op, delay);

      cpu = (Boolean) getValue(parser, properties, cpu_op, cpu);
      cpu_warning =
          getPercentage((Integer) getValue(parser, properties, cpu_warning_op, cpu_warning));
      cpu_critical =
          getPercentage((Integer) getValue(parser, properties, cpu_critical_op, cpu_critical));

      memory = (Boolean) getValue(parser, properties, memory_op, memory);
      mem_warning =
          getPercentage((Integer) getValue(parser, properties, mem_warning_op, mem_warning));
      mem_critical =
          getPercentage((Integer) getValue(parser, properties, mem_critical_op, mem_critical));

      disk = (Boolean) getValue(parser, properties, disk_op, disk);
      disk_warning =
          getPercentage((Integer) getValue(parser, properties, disk_warning_op, disk_warning));
      disk_critical =
          getPercentage((Integer) getValue(parser, properties, disk_critical_op, disk_critical));

      service = (Boolean) getValue(parser, properties, service_op, service);
      exclude = (String) getValue(parser, properties, exclude_op, service);

      verbose += parser.getOptionValues(verbose_op).size();

    } catch (NumberFormatException e) {
      fail(e.getMessage());
    } catch (IllegalOptionValueException e) {
      fail(e.getMessage());
    } catch (IllegalArgumentException e) {
      fail(e.getMessage());
    }

    // check if all necessary values were given
    if (host.isEmpty() || domain.isEmpty() || user.isEmpty() || passwd.isEmpty()) {
      String message = "Following values are missing: ";
      if (host.isEmpty()) message += "targethost, ";
      if (domain.isEmpty()) message += "domain, ";
      if (user.isEmpty()) message += "user, ";
      if (passwd.isEmpty()) message += "password, ";
      message = message.substring(0, message.length() - 2);
      fail(message);
    }

    // if the password is a asterisk ask the user for the password
    if (passwd.equals("*")) {
      Console cons = System.console();
      if (cons == null) fail("must use a console");
      System.out.print("Password: "******"getting password failed");
      passwd = "";
      for (char z : password) passwd += z;
      java.util.Arrays.fill(password, ' ');
    }

    // all warnings and criticals are added to this lists
    LinkedList<String> warnings = new LinkedList<String>();
    LinkedList<String> criticals = new LinkedList<String>();

    try {
      try {
        // disable console logging
        JISystem.setInBuiltLogHandler(false);
        Handler inbuildloghandler = JISystem.getLogger().getHandlers()[0];
        JISystem.getLogger().removeHandler(inbuildloghandler);
        inbuildloghandler.close();
      } catch (IOException e) {
      } catch (SecurityException e) {
      }

      JISystem.getLogger().setLevel(logging);
      if (logging != Level.OFF) {
        // enable file logging
        Random r = new Random();
        // create a random string with a length of 12 - 13 characters
        String token = Long.toString(Math.abs(r.nextLong()), 36);
        try {
          String tmpdir = System.getProperty("java.io.tmpdir");
          // on windows java.io.tmpdir ends with a slash on unix not
          if (!tmpdir.endsWith(File.separator)) tmpdir = tmpdir + File.separator;
          FileHandler logFile = new FileHandler(tmpdir + "j-Interop-" + token + ".log");
          logFile.setFormatter(new SimpleFormatter());
          JISystem.getLogger().addHandler(logFile);
        } catch (FileNotFoundException e) {
          System.out.println("ERROR: Failed to open log file: " + e.getMessage());
        } catch (SecurityException e) {
          fail(e, verbose);
        } catch (IOException e) {
          fail(e, verbose);
        }
      }

      JISystem.setAutoRegisteration(true);

      WindowsHealth monitor = new WindowsHealth(host, domain, user, passwd, timeout, verbose > 2);

      boolean print;

      if (cpu) monitor.getCPUUsage(); // first measure gives no result

      for (int i = 0; i < n_measures; i++) {

        try {
          Thread.sleep(delay);
        } catch (InterruptedException e) {
          break;
        }

        if (verbose > 0 && i != 0) {
          System.out.println();
        }

        if (verbose > 1 && n_measures > 1) System.out.println(" - Measure " + (i + 1) + " -");

        // cpu load measure
        if (cpu) {
          print = false;
          int percent_cpu = monitor.getCPUUsage();
          if (percent_cpu >= cpu_critical) {
            criticals.add("CPU (" + percent_cpu + "%)");
            print = true;
            if (verbose > 0) System.out.print("CRITICAL: ");
          } else if (percent_cpu >= cpu_warning) {
            warnings.add("CPU (" + percent_cpu + "%)");
            print = true;
            if (verbose > 0) System.out.print("WARNING: ");
          }
          if ((print && verbose > 0) || verbose > 1)
            System.out.println("CPU usage: " + percent_cpu + " %");
        }

        // memory space measure
        if (memory) {
          print = false;
          long mem_size = monitor.getTotalMemorySize();
          long mem_free = monitor.getFreeMemorySpace();
          long mem_used = mem_size - mem_free;
          double percent_mem = (double) mem_used / (double) mem_size * 100;
          if (percent_mem >= mem_critical) {
            criticals.add("Memory (" + round(percent_mem, 1) + "%)");
            print = true;
            if (verbose > 0) System.out.print("CRITICAL: ");
          } else if (percent_mem >= mem_warning) {
            warnings.add("Memory (" + round(percent_mem, 1) + "%)");
            print = true;
            if (verbose > 0) System.out.print("WARNING: ");
          }
          if ((print && verbose > 0) || verbose > 1)
            System.out.println(
                "Memory: " + round(percent_mem, 2) + " % used (" + mem_used + " KB)");
        }

        // disk space measure
        if (disk) {
          LinkedList<IJIDispatch> drives = monitor.getDiskDrives();

          for (IJIDispatch drive : drives) {
            print = false;

            String name = drive.get("Name").getObjectAsString2();
            double disk_free =
                Long.parseLong(drive.get("FreeSpace").getObjectAsString().getString());
            double disk_size = Long.parseLong(drive.get("Size").getObjectAsString().getString());
            double disk_used = disk_size - disk_free;
            double percent_disk = 0;

            if (disk_size != 0) percent_disk = disk_used / disk_size * 100;
            else {
              if (verbose > 1) System.out.println(name);
              continue;
            }

            if (percent_disk >= disk_critical) {
              criticals.add(name + " (" + round(percent_disk, 1) + "%)");
              print = true;
              if (verbose > 0) System.out.print("CRITICAL: ");
            } else if (percent_disk >= disk_warning) {
              warnings.add(name + " (" + round(percent_disk, 1) + "%)");
              print = true;
              if (verbose > 0) System.out.print("WARNING: ");
            }

            if ((print && verbose > 0) || verbose > 1)
              System.out.println(
                  name
                      + " "
                      + round(percent_disk, 3)
                      + " % used ("
                      + getSizeRepresentation(disk_used, 3)
                      + ")");
          }
        }

        // find services
        if (service) {

          LinkedList<IJIDispatch> services = monitor.getServices();
          LinkedList<IJIDispatch> services_final = new LinkedList<IJIDispatch>();

          Scanner scanner = new Scanner(exclude);
          scanner.useDelimiter("\\s*,\\s*");
          LinkedList<String> exclusions = new LinkedList<String>();
          while (scanner.hasNext()) exclusions.add(scanner.next());

          for (IJIDispatch service_dispatch : services) {
            String name = service_dispatch.get("DisplayName").getObjectAsString2();
            if (!exclusions.contains(name)) services_final.add(service_dispatch);
          }

          int size = services_final.size();
          String name;

          String serv = "services (";
          for (IJIDispatch service_dispatch : services_final) {
            name = service_dispatch.get("DisplayName").getObjectAsString2();
            serv += name + ";";
          }
          serv += ")";

          if (size >= serv_critical) {
            criticals.add(serv);
          } else if (verbose == 1) continue;

          if (verbose >= 1) {
            if (size >= serv_critical) System.out.print("CRITICAL: ");
            if (verbose == 1) {
              System.out.print(size + " service(s) (");
              for (IJIDispatch service_dispatch : services_final) {
                name = service_dispatch.get("DisplayName").getObjectAsString2();
                System.out.print(name + ";");
              }
              System.out.println(") are/is not running");
            } else {
              System.out.print(size + " problem(s) with services");
              if (services_final.size() == 0) System.out.println(".");
              else System.out.println(":");
              for (IJIDispatch service_dispatch : services_final) {
                name = service_dispatch.get("DisplayName").getObjectAsString2();
                System.out.println(" service '" + name + "' is not running");
              }
            }
          }
        }
      }

      // output a summary
      if (verbose < 1) {
        if (warnings.size() > 0) {
          System.out.print("WARNINGS:");
          for (String w : warnings) {
            System.out.print(" " + w + ";");
          }
        }
        if (criticals.size() > 0) {
          System.out.print(" CRITICALS:");
          for (String c : criticals) {
            System.out.print(" " + c + ";");
          }
        }
        if (warnings.size() == 0 && criticals.size() == 0) System.out.print("ALL OK");
        System.out.println();
      } else {
        if (warnings.size() == 0 && criticals.size() == 0) System.out.println("ALL OK");
        else {
          System.out.println();
          System.out.print("" + warnings.size() + " warnings and ");
          System.out.println("" + criticals.size() + " criticals.");
        }
      }

    } catch (UnknownHostException e) {
      fail("Unknown host: " + host, e, verbose);
    } catch (JIAutomationException e) {
      JIExcepInfo f = e.getExcepInfo();
      fail(f.getExcepDesc() + "0x" + Integer.toHexString(f.getErrorCode()) + " ]", e, verbose);
    } catch (JIException e) {
      if (e.getCause().getClass().equals(SocketTimeoutException.class))
        fail("Timeout error", e, verbose);
      else fail(e, verbose);
    }

    // if there are one or more criticals exit with exit status 2
    if (criticals.size() != 0) System.exit(2);
    // if there are one or more warnings exit with exit status 1
    if (warnings.size() != 0) System.exit(1);
    // otherwise exit with exit status 0
    System.exit(0);
  }
Example #17
0
 public final int getExitCode() throws JIException {
   final JIVariant variant = dispatch.get("ExitCode"); // i18n lib
   return variant.getObjectAsInt();
 }