private void addMeasurementConfig(
      List<MeasurementConfigEntity> ents, AppdefEntityID id, String typeName) {
    MeasurementConfigEntity ent = new MeasurementConfigEntity();
    ConfigResponse response;
    byte[] config;

    try {
      response =
          configManager.getMergedConfigResponse(
              authzSubjectManager.getOverlordPojo(), ProductPlugin.TYPE_MEASUREMENT, id, true);

      // Only send the configuration to the agent if log or
      // config file tracking has been enabled.
      if (ConfigTrackPlugin.isEnabled(response, id.getType())
          || LogTrackPlugin.isEnabled(response, id.getType())) {

        config = response.encode();

        ent.setPluginName(id.getAppdefKey());
        ent.setPluginType(typeName);
        ent.setConfig(config);

        ents.add(ent);
      }

    } catch (Exception exc) {
      return; // Not a fatal condition
    }
  }
 public void processEvents(List<ResourcesCleanupZevent> events) {
   log.info("starting removeDeletedResources");
   final StopWatch watch = new StopWatch();
   final Collection<String> typeNames = new ArrayList<String>();
   for (final ResourcesCleanupZevent e : events) {
     if (e instanceof ResourceTypeCleanupZevent) {
       typeNames.addAll(((ResourceTypeCleanupZevent) e).getTypeNames());
     }
   }
   if (events != null && !events.isEmpty()) {
     try {
       Map<Integer, List<AppdefEntityID>> agentCache = buildAsyncDeleteAgentCache(events);
       removeDeletedResources(agentCache, typeNames);
       final AuthzSubject overlord = authzSubjectManager.getOverlordPojo();
       if (!typeNames.isEmpty()) {
         resourceManager.removeResourceTypes(typeNames);
       }
       removeOrphanedPlatforms(overlord);
       removeOrphanedServers(overlord);
       removeOrphanedServices(overlord);
       removeOrphanedAlertDefs();
       removeOrphanedResourceGroupMembers();
       removeOrphanedAudits();
       removeOrphanedEscalationStates(overlord);
       removeOrphanedResources(overlord);
     } catch (Exception e) {
       log.error("removeDeletedResources failed", e);
     }
   }
   log.info("completed removeDeletedResources " + watch);
 }
Exemplo n.º 3
0
  @RequestMapping(value = "/login", method = RequestMethod.GET)
  public ModelAndView login(
      HttpServletRequest request, HttpServletResponse response, HttpSession session) {
    final boolean debug = log.isDebugEnabled();

    ModelAndView result = new ModelAndView();

    // ...first check for an authentication object, if one exists we are already logged in...
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (authentication != null
        && !(authentication instanceof AnonymousAuthenticationToken)
        && authentication.isAuthenticated()) {
      try {
        if (debug) log.debug("User has already been authenticated.  Redirecting to dashboard.");

        // Redirect to Dashboard.action for struts2 support
        response.sendRedirect("Dashboard.action");

        return result;
      } catch (IOException e) {
        log.warn(
            "Could not perform the redirect for an authenticated user, displaying login page instead");
      }
    }

    // ...we're dealing with an unauthenticated user, we're going to show the login form...
    AuthzSubject guestUser = authzSubjectManager.getSubjectById(AuthzConstants.guestId);

    // ...before we return, check for an error message...
    boolean loginError = request.getParameter("authfailed") != null;

    if (loginError) {
      if (session != null) {
        AuthenticationException ex =
            (AuthenticationException)
                session.getAttribute(
                    AbstractAuthenticationProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY);

        if (ex != null) {
          result.addObject("errorMessage", RequestUtils.message(request, ex.getMessage()));
        }
      }
    }

    result.addObject("guestUsername", (guestUser != null) ? guestUser.getName() : "guest");
    result.addObject("guestEnabled", (guestUser != null && guestUser.getActive()));

    // ...set a response header so we can identify the login page explicitly...
    response.setHeader("hq-requires-auth", "1");

    return result;
  }
  /**
   * Register an Agent with the server. This method calls out to verify it can talk to the agent,
   * and then adds it to the database. With all the connection info used to make further
   * connections.
   */
  private RegisterAgent_result cmdRegisterAgent(LatherContext ctx, RegisterAgent_args args)
      throws LatherRemoteException {
    try {
      checkUserCanManageAgent(ctx, args.getUser(), args.getPword(), "register");
    } catch (PermissionException exc) {
      return new RegisterAgent_result("Permission denied");
    }
    Collection<Integer> ids = new ArrayList<Integer>();

    RegisterAgent_result result = registerAgent(args, ids);

    /**
     * Reschedule all metrics on a platform when it is started for the first time. This allows the
     * schedule to be updated immediately on either agent updates, or if the user removes the agent
     * data directory.
     */
    if (!ids.isEmpty()) {
      try {
        List<ResourceRefreshZevent> zevents = new ArrayList<ResourceRefreshZevent>();
        ResourceRefreshZevent zevent;
        AuthzSubject overlord = authzSubjectManager.getOverlordPojo();
        for (Integer id : ids) {

          Platform platform = platformManager.findPlatformById(id);

          zevent = new ResourceRefreshZevent(overlord, platform.getEntityId());
          zevents.add(zevent);

          Collection<Server> servers = platform.getServers();
          for (Server server : servers) {

            zevent = new ResourceRefreshZevent(overlord, server.getEntityId());
            zevents.add(zevent);

            Collection<Service> services = server.getServices();
            for (Service service : services) {

              zevent = new ResourceRefreshZevent(overlord, service.getEntityId());
              zevents.add(zevent);
            }
          }
        }

        zeventManager.enqueueEventsAfterCommit(zevents);

      } catch (Exception e) {
        // Not fatal, the metrics will eventually be rescheduled...
        log.error("Unable to refresh agent schedule", e);
      }
    }
    return result;
  }
  /** Get config information about all the entities which an agent is servicing. */
  private MeasurementGetConfigs_result cmdMeasurementGetConfigs(MeasurementGetConfigs_args args)
      throws LatherRemoteException {
    ResourceTree tree;
    AuthzSubject overlord = authzSubjectManager.getOverlordPojo();
    try {
      tree = agentManager.getEntitiesForAgent(overlord, args.getAgentToken());
    } catch (PermissionException exc) {
      throw new SystemException("Overlord unable to get resource " + "tree list");
    } catch (AgentNotFoundException exc) {
      throw new SystemException("Validated an agent which could " + "not be found");
    }
    ArrayList<MeasurementConfigEntity> ents = new ArrayList<MeasurementConfigEntity>();
    for (Iterator<PlatformNode> p = tree.getPlatformIterator(); p.hasNext(); ) {
      PlatformNode pNode = p.next();
      addMeasurementConfig(ents, pNode.getPlatform());
      try {
        AppdefEntityValue aeval =
            new AppdefEntityValue(pNode.getPlatform().getEntityId(), overlord);
        List<AppdefResourceValue> services = aeval.getAssociatedServices(PageControl.PAGE_ALL);
        for (int i = 0; i < services.size(); i++) {
          ServiceValue val = (ServiceValue) services.get(i);

          AppdefEntityID id = val.getEntityId();

          addMeasurementConfig(ents, id, val.getServiceType().getName());
        }
      } catch (Exception e) {
        // Shouldn't happen
        log.error("Encountered exception looking up platform " + "services: " + e.getMessage(), e);
      }

      for (Iterator<ServerNode> s = pNode.getServerIterator(); s.hasNext(); ) {
        ServerNode sNode = s.next();

        addMeasurementConfig(ents, sNode.getServer());

        for (Iterator<ServiceNode> v = sNode.getServiceIterator(); v.hasNext(); ) {
          ServiceNode vNode = v.next();

          addMeasurementConfig(ents, vNode.getService());
        }
      }
    }

    MeasurementConfigList cList = new MeasurementConfigList();
    cList.setEntities(ents.toArray(new MeasurementConfigEntity[ents.size()]));
    MeasurementGetConfigs_result res = new MeasurementGetConfigs_result();
    res.setConfigs(cList);
    return res;
  }
 private void checkUserCanManageAgent(
     LatherContext ctx, String user, String pword, String operation) throws PermissionException {
   try {
     authManager.authenticate(user, pword);
     AuthzSubject subject =
         authzSubjectManager.findSubjectByAuth(user, HQConstants.ApplicationName);
     permissionManager.checkCreatePlatformPermission(subject);
   } catch (SecurityException exc) {
     log.warn(
         "Security exception when '"
             + user
             + "' tried to "
             + operation
             + " an Agent @ "
             + ctx.getCallerIP(),
         exc);
     throw new PermissionException();
   } catch (PermissionException exc) {
     log.warn(
         "Permission denied when '"
             + user
             + "' tried to "
             + operation
             + " an Agent @ "
             + ctx.getCallerIP());
     throw new PermissionException();
   } catch (ApplicationException exc) {
     log.warn(
         "Application exception when '"
             + user
             + "' tried to "
             + operation
             + " an Agent @ "
             + ctx.getCallerIP(),
         exc);
     throw new PermissionException();
   } catch (SystemException exc) {
     log.warn(
         "System exception when '"
             + user
             + "' tried to "
             + operation
             + " an Agent @ "
             + ctx.getCallerIP(),
         exc);
     throw new PermissionException();
   }
 }
  /** @see org.hyperic.hq.zevents.ZeventListener#processEvents(java.util.List) */
  public void processEvents(List<TransferAgentBundleZevent> events) {
    AuthzSubject overlord = authzSubjectManager.getOverlordPojo();

    for (TransferAgentBundleZevent zevent : events) {

      try {
        agentManager.transferAgentBundle(overlord, zevent.getAgent(), zevent.getAgentBundleFile());
      } catch (Exception e) {
        _log.warn(
            "Failed to transfer agent bundle "
                + zevent.getAgentBundleFile()
                + " to agent "
                + zevent.getAgent().getID(),
            e);
      }
    }
  }
  /** Receive status information about a previous control action */
  private NullLatherValue cmdControlSendCommandResult(ControlSendCommandResult_args args) {
    controlManager.sendCommandResult(
        args.getId(), args.getResult(), args.getStartTime(), args.getEndTime(), args.getMessage());

    // Get live measurements on the resource
    String name = args.getName();
    if (name != null) {
      log.info("Getting live measurements for " + name);
      AppdefEntityID id = new AppdefEntityID(name);
      try {
        measurementManager.getLiveMeasurementValues(authzSubjectManager.getOverlordPojo(), id);
      } catch (Exception e) {
        log.error("Unable to fetch live measurements: " + e, e);
      }
    } else {
      log.error("No plugin name found, not fetching live measurements");
    }

    return NullLatherValue.INSTANCE;
  }
  @SuppressWarnings("unchecked")
  private void removeDeletedResources(
      Map<Integer, List<AppdefEntityID>> agentCache, Collection<String> typeNames)
      throws ApplicationException, VetoException {
    final boolean debug = log.isDebugEnabled();
    final StopWatch watch = new StopWatch();
    final AuthzSubject subject = authzSubjectManager.findSubjectById(AuthzConstants.overlordId);
    if (debug) watch.markTimeBegin("unscheduleMeasurementsForAsyncDelete");
    unscheduleMeasurementsForAsyncDelete(agentCache);
    if (debug) watch.markTimeEnd("unscheduleMeasurementsForAsyncDelete");

    // Look through services, servers, platforms, applications, and groups
    if (debug) watch.markTimeBegin("removeApplications");
    Collection<Application> applications = applicationManager.findDeletedApplications();
    removeApplications(subject, applications);
    if (debug) watch.markTimeEnd("removeApplications");

    if (debug) watch.markTimeBegin("removeResourceGroups");
    Collection<ResourceGroup> groups = resourceGroupManager.findDeletedGroups();
    removeResourceGroups(subject, groups);
    if (debug) watch.markTimeEnd("removeResourceGroups");

    typeNames = (typeNames == null) ? Collections.EMPTY_LIST : typeNames;
    if (debug) watch.markTimeBegin("removeGroupsCompatibleWith");
    for (String name : typeNames) {
      resourceGroupManager.removeGroupsCompatibleWith(name);
    }
    if (debug) watch.markTimeEnd("removeGroupsCompatibleWith");

    Collection<Service> services = serviceManager.findDeletedServices();
    removeServices(subject, services);

    Collection<Server> servers = serverManager.findDeletedServers();
    removeServers(subject, servers);

    if (debug) watch.markTimeBegin("removePlatforms");
    Collection<Platform> platforms = platformManager.findDeletedPlatforms();
    removePlatforms(subject, platforms);
    if (debug) watch.markTimeEnd("removePlatforms");
    if (debug) log.debug("removeDeletedResources: " + watch);
  }
  /**
   * Disable measurements and unschedule from the agent in bulk with the agent cache info because
   * the resources have been de-referenced from the agent
   *
   * @param agentCache {@link Map} of {@link Integer} of agentIds to {@link List} of {@link
   *     AppdefEntityID}s
   */
  private void unscheduleMeasurementsForAsyncDelete(Map<Integer, List<AppdefEntityID>> agentCache) {
    if (agentCache == null) {
      return;
    }

    try {
      AuthzSubject subject = authzSubjectManager.findSubjectById(AuthzConstants.overlordId);

      for (Integer agentId : agentCache.keySet()) {

        Agent agent = agentManager.getAgent(agentId);
        List<AppdefEntityID> resources = agentCache.get(agentId);

        measurementManager.disableMeasurementsForDeletion(
            subject,
            agent,
            (AppdefEntityID[]) resources.toArray(new AppdefEntityID[resources.size()]));
      }
    } catch (Exception e) {
      log.error("Error unscheduling measurements during async delete", e);
    }
  }
Exemplo n.º 11
0
  /**
   * Registers the agent according to the provided RegisterAgent_args under a new transaction
   *
   * @param args - the RegisterAgent_args
   * @param ids - if the is an existing agent, this collection will contain the ids of the platforms
   *     monitored by this agent after this method execution.
   * @return - RegisterAgent_result
   */
  @Transactional(propagation = Propagation.REQUIRES_NEW)
  private RegisterAgent_result registerAgent(RegisterAgent_args args, Collection<Integer> ids) {
    String agentIP = args.getAgentIP();
    int port = args.getAgentPort();
    String version = args.getVersion();
    boolean isNewTransportAgent = args.isNewTransportAgent();
    boolean unidirectional = args.isUnidirectional();
    boolean acceptValidation = args.isAcceptCertificates();
    String errRes =
        testAgentConn(
            agentIP,
            port,
            args.getAuthToken(),
            isNewTransportAgent,
            unidirectional,
            acceptValidation);

    if (errRes != null) {
      return new RegisterAgent_result(errRes);
    }

    boolean isOldAgentToken = true;
    String agentToken = args.getAgentToken();

    if (agentToken == null) {
      // Generate a unique agent token
      agentToken = SecurityUtil.generateRandomToken();

      while (!agentManager.isAgentTokenUnique(agentToken)) {
        agentToken = SecurityUtil.generateRandomToken();
      }

      isOldAgentToken = false;
    }

    // Check the to see if the agent already exists.
    // Lookup the agent by agent token (if it exists). Otherwise, use the
    // agent IP and port.
    try {
      Agent origAgent;

      if (isOldAgentToken) {
        origAgent = agentManager.getAgent(agentToken);
      } else {
        origAgent = agentManager.getAgent(agentIP, port);
      }

      try {
        ids.addAll(
            platformManager.getPlatformPksByAgentToken(
                authzSubjectManager.getOverlordPojo(), origAgent.getAgentToken()));
      } catch (Exception e) {
        // No platforms found, no a big deal
      }

      log.info(
          "Found preexisting agent during agent registration. "
              + "Updating agent information for "
              + agentIP
              + ":"
              + port
              + "; new transport="
              + isNewTransportAgent
              + "; unidirectional="
              + unidirectional);

      if (isOldAgentToken) {
        if (isNewTransportAgent) {
          agentManager.updateNewTransportAgent(
              agentToken, agentIP, port, args.getAuthToken(), version, unidirectional);
        } else {
          agentManager.updateLegacyAgent(agentToken, agentIP, port, args.getAuthToken(), version);
        }
      } else {
        if (isNewTransportAgent) {
          agentManager.updateNewTransportAgent(
              agentIP, port, args.getAuthToken(), agentToken, version, unidirectional);
        } else {
          agentManager.updateLegacyAgent(agentIP, port, args.getAuthToken(), agentToken, version);
        }
      }
    } catch (AgentNotFoundException exc) {
      log.info(
          "Registering agent at "
              + agentIP
              + ":"
              + port
              + ""
              + "; new transport="
              + isNewTransportAgent
              + "; unidirectional="
              + unidirectional);
      try {
        if (isNewTransportAgent) {
          agentManager.createNewTransportAgent(
              agentIP, new Integer(port), args.getAuthToken(), agentToken, version, unidirectional);
        } else {
          agentManager.createLegacyAgent(
              agentIP, new Integer(port), args.getAuthToken(), agentToken, version);
        }

      } catch (AgentCreateException oexc) {
        log.error("Error creating agent", oexc);
        return new RegisterAgent_result("Error creating agent: " + oexc.getMessage());
      } catch (SystemException oexc) {
        log.error("Error creating agent", oexc);
        return new RegisterAgent_result("Error creating agent:  " + "Internal system error");
      }
    } catch (SystemException exc) {
      log.error("Error updating agent", exc);
      return new RegisterAgent_result("Error updating agent:  " + "Internal system error");
    }

    RegisterAgent_result result = new RegisterAgent_result("token:" + agentToken);
    return result;
  }