Search in sources :

Example 1 with AlertDefinition

use of com.yahoo.dba.perf.myperf.common.AlertDefinition in project mysql_perf_analyzer by yahoo.

the class UdmController method processNewAlert.

private ModelAndView processNewAlert(HttpServletRequest req, HttpServletResponse resp) {
    boolean valid = true;
    String message = "OK";
    String cmd = req.getParameter("cmd");
    String name = req.getParameter("name");
    if (name != null)
        name = name.trim();
    String source = req.getParameter("source");
    String metricsName = req.getParameter("mname");
    String metricComparison = req.getParameter("mcomp");
    String metricValueType = req.getParameter("mval");
    String defaultThreshold = req.getParameter("mthreshold");
    String testdb = req.getParameter("testdb");
    String num = req.getParameter("num");
    AlertDefinition alertDef = new AlertDefinition(name);
    alertDef.setSource(source);
    if (AlertDefinition.SOURCE_METRICS.equals(source) || AlertDefinition.SOURCE_GLOBAL_STATUS.equals(source)) {
        alertDef.setMetricName(metricsName);
        alertDef.setMetricComparison(metricComparison);
        alertDef.setMetricValueType(metricValueType);
        try {
            alertDef.setDefaultThreshold(Float.parseFloat(defaultThreshold));
        } catch (Exception iex) {
        }
    } else {
        alertDef.setSqlText(req.getParameter("sqlText"));
        //alertDef.setSqlId(req.getParameter("sqlId"));
        int cnt = 0;
        try {
            cnt = Integer.parseInt(num);
        } catch (Exception ex) {
        }
        for (int i = 0; i < cnt; i++) {
            String pname = req.getParameter("pname_" + i);
            String pval = req.getParameter("pval_" + i);
            alertDef.addParam(pname, pval);
        }
    }
    //now validation
    try {
        alertDef.validate();
    } catch (Exception ex) {
        valid = false;
        message = "Validation error: " + ex.getMessage();
    }
    if (//now run test and store
    valid) {
        try {
            this.testAndValidateAlert(req, alertDef, testdb);
            //if reach here, add and store it.
            if ("publish_alert".equalsIgnoreCase(cmd)) {
                this.frameworkContext.getMetricsDef().getUdmManager().addAlertDefinition(alertDef);
            //this.frameworkContext.getMetricDb().addNewUDM(alertDef);
            }
        } catch (Exception ex) {
            logger.log(Level.WARNING, "Failed to test ALERT", ex);
            valid = false;
            message = "Failed on test: " + ex.getMessage();
        }
    }
    ModelAndView mv = new ModelAndView(this.jsonView);
    if (//we response with udm name
    valid)
        mv.addObject("json_result", "{\"resp\":{\"status\":0, \"alert\": \"" + alertDef.getName() + "\", \"message\":\"" + message + "\"}}");
    else
        mv = this.respondFailure(message, req);
    return mv;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) SQLException(java.sql.SQLException) AlertDefinition(com.yahoo.dba.perf.myperf.common.AlertDefinition)

Example 2 with AlertDefinition

use of com.yahoo.dba.perf.myperf.common.AlertDefinition in project mysql_perf_analyzer by yahoo.

the class UdmController method handleRequestImpl.

@Override
protected ModelAndView handleRequestImpl(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    String message = "OK";
    String cmd = req.getParameter("cmd");
    boolean isValid = true;
    UserDefinedMetrics udm = null;
    logger.info("receive cmd " + cmd + ", url " + req.getQueryString());
    //it a form request
    if ((cmd == null || cmd.isEmpty()) && !WebAppUtil.hasValidSession(req)) {
        return new ModelAndView(new RedirectView(nosessView));
    }
    //first session check
    boolean isSessionValid = WebAppUtil.hasValidSession(req);
    if (!isSessionValid)
        return this.respondFailure("session timeout. Please logout and re-login.", req);
    AppUser appUser = AppUser.class.cast(req.getSession().getAttribute(AppUser.SESSION_ATTRIBUTE));
    ModelAndView mv = null;
    //By default, just display the page
    if (//or any invalid command
    cmd == null || cmd.isEmpty())
        message = null;
    if ("test".equals(cmd) || "publish".equals(cmd)) {
        if (appUser.isAdminUser())
            return this.processNewUDM(req, resp);
        else
            return this.respondFailure("Non admin user is not allowed to add UDM", req);
    } else if ("test_alert".equals(cmd) || "publish_alert".equals(cmd)) {
        if (appUser.isAdminUser())
            return this.processNewAlert(req, resp);
        else
            return this.respondFailure("Non admin user is not allowed to add UDM", req);
    } else if (//display
    "udm_detail".equalsIgnoreCase(cmd)) {
        String name = req.getParameter("name");
        udm = this.frameworkContext.getMetricsDef().getUdmManager().getUDMByName(name);
        if (udm != null) {
            mv = new ModelAndView(this.jsonView);
            String res = "{\"resp\":{\"status\": 0, \"message\":\"OK\", \"udm\": " + udm.toJSON(true) + "}}";
            //logger.info("UDM detail: "+res);
            mv.addObject("json_result", res);
            return mv;
        } else
            return this.respondFailure("UDM " + name + " not found", req);
    } else if (//display
    "alert_detail".equalsIgnoreCase(cmd)) {
        String name = req.getParameter("name");
        AlertDefinition alert = this.frameworkContext.getMetricsDef().getUdmManager().getAlertByName(name);
        if (alert != null) {
            mv = new ModelAndView(this.jsonView);
            String res = "{\"resp\":{\"status\": 0, \"message\":\"OK\", \"alert\": " + alert.toJSON(true) + "}}";
            //logger.info("UDM detail: "+res);
            mv.addObject("json_result", res);
            return mv;
        } else
            return this.respondFailure("ALERT " + name + " not found", req);
    } else if (//display
    "udmdb_detail".equalsIgnoreCase(cmd))
        return processUDMDBDetail(req, resp);
    else if (//display
    "udmdb_update".equalsIgnoreCase(cmd))
        return processUDMDBUpdate(req, resp, appUser);
    else if (//display
    "alertdb_update".equalsIgnoreCase(cmd))
        return this.processAlertDBUpdate(req, resp, appUser);
    mv = new ModelAndView(this.formView);
    //udm list
    List<String> udms = new ArrayList<String>();
    for (Map.Entry<String, UserDefinedMetrics> e : this.frameworkContext.getMetricsDef().getUdmManager().getUdms().entrySet()) {
        udms.add(e.getKey());
    }
    mv.addObject("udms", udms);
    //alerts
    List<String> alerts = new ArrayList<String>();
    for (Map.Entry<String, AlertDefinition> e : this.frameworkContext.getMetricsDef().getUdmManager().getAlerts().entrySet()) {
        alerts.add(e.getKey());
    }
    mv.addObject("alerts", alerts);
    //predefined
    Map<String, String> predefined = new TreeMap<String, String>();
    String[] predefinedGroups = this.frameworkContext.getMetricsDef().getGroupNames();
    for (String gname : predefinedGroups) {
        MetricsGroup g = this.frameworkContext.getMetricsDef().getGroupByName(gname);
        List<MetricsGroup> subGroups = g.getSubGroups();
        if (subGroups != null && subGroups.size() > 0) {
            for (MetricsGroup subG : subGroups) predefined.put(gname + "." + subG.getGroupName(), subG.isAuto() ? "y" : "n");
        } else
            predefined.put(gname, g.isAuto() ? "y" : "n");
    }
    mv.addObject("predefined", predefined);
    if (WebAppUtil.hasValidSession(req)) {
        mv.addObject("mydbs", this.frameworkContext.getDbInfoManager().listDbsByUserInfo(WebAppUtil.findUserFromRequest(req), retrieveAppUser(req).isRestrictedUser()));
        mv.addObject("mydbSize", this.frameworkContext.getDbInfoManager().getMyDatabases(WebAppUtil.findUserFromRequest(req), retrieveAppUser(req).isRestrictedUser()).size());
    } else {
        mv.addObject("mydbs", this.frameworkContext.getDbInfoManager().getClusters().keySet());
        mv.addObject("mydbSize", 0);
    }
    mv.addObject("dbMap", this.frameworkContext.getDbInfoManager().getClusters());
    mv.addObject("help_key", "udm");
    if (!isValid && message != null)
        mv.addObject("message", message);
    else if (message != null)
        mv.addObject("okmessage", message);
    if (cmd != null)
        mv.addObject("cmd", cmd);
    mv.addObject("u", appUser);
    return mv;
}
Also used : UserDefinedMetrics(com.yahoo.dba.perf.myperf.common.UserDefinedMetrics) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) AppUser(com.yahoo.dba.perf.myperf.common.AppUser) TreeMap(java.util.TreeMap) MetricsGroup(com.yahoo.dba.perf.myperf.common.MetricsGroup) RedirectView(org.springframework.web.servlet.view.RedirectView) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) AlertDefinition(com.yahoo.dba.perf.myperf.common.AlertDefinition)

Aggregations

AlertDefinition (com.yahoo.dba.perf.myperf.common.AlertDefinition)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 AppUser (com.yahoo.dba.perf.myperf.common.AppUser)1 MetricsGroup (com.yahoo.dba.perf.myperf.common.MetricsGroup)1 UserDefinedMetrics (com.yahoo.dba.perf.myperf.common.UserDefinedMetrics)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 RedirectView (org.springframework.web.servlet.view.RedirectView)1