private Number valueToNum(Object o) { if (o instanceof Number) { return (Number) o; } else if (o instanceof String) { String val = (String)o; return Util.parseStringNumber(val, Double.NaN); } else { return Double.NaN; } }
@SuppressWarnings("unchecked") public Map<KeyType, String> getCollectMapping() { Map<KeyType, String> rawMap = (Map<KeyType, String>) getPd().getCollectMapping(); Map<KeyType, String> retValues = new HashMap<KeyType, String>(rawMap.size()); for(Map.Entry<KeyType, String> e: rawMap.entrySet()) { String value = jrds.Util.parseTemplate(e.getValue(), this); KeyType key = e.getKey(); if(key instanceof String) key = (KeyType) jrds.Util.parseTemplate((String) key, this); retValues.put(key, value); } return retValues; }
protected void buildQuery(OutputStream out, XmlProvider xmlstarter) { Document ribclQ = makeDocument(xmlstarter); Map<String, String> properties = new HashMap<String, String>(); properties.put(OutputKeys.INDENT, "no"); properties.put(OutputKeys.OMIT_XML_DECLARATION, "yes"); properties.put(OutputKeys.ENCODING, encoding); try { Util.serialize(ribclQ, out, null, properties); } catch (TransformerException | IOException e) { throw new RuntimeException("Unable to serialize in memory", e); } }
public Graph getGraph() { Class<Graph> gclass = gd.getGraphClass(); Graph g; Map<String, GenericBean> beansList; try { g = gclass.getConstructor(GraphNode.class).newInstance(this); beansList = ArgFactory.getBeanPropertiesMap(gclass, Graph.class); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { Util.log(this, logger, Level.ERROR, e, "Failed to build a graph instance %s", gclass.getCanonicalName()); return null; } // Resolve the beans for(Map.Entry<String, String> e: beans.entrySet()) { String name = Util.parseTemplate(e.getKey(), probe); String textValue = Util.parseTemplate(e.getValue(), probe); GenericBean bean = beansList.get(name); if(bean == null) { logger.error(String.format("Unknown bean for %s: %s", gd.getName(), name)); continue; } logger.trace(Util.delayedFormatString("Found attribute %s with value %s", name, textValue)); bean.set(g, textValue); } return g; }
private Map<String, String> makeProperties(JrdsElement n, Object... o) { if(n == null) return Collections.emptyMap(); JrdsElement propElem = n.getElementbyName("properties"); if(propElem == null) return Collections.emptyMap(); Map<String, String> props = new HashMap<String, String>(); for(JrdsElement propNode: propElem.getChildElementsByName("entry")) { String key = propNode.getAttribute("key"); if(key != null) { String value = propNode.getTextContent(); value = Util.parseTemplate(value, o); logger.trace(Util.delayedFormatString("Adding propertie %s=%s", key, value)); props.put(key, value); } } logger.debug(Util.delayedFormatString("Properties map: %s", props)); return props; }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { DocumentBuilder dbuilder; try { dbuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); JrdsDocument hostDom = new JrdsDocument(dbuilder.newDocument()); hostDom.doRootElement("div"); for(DiscoverAgent da: getHostsList().getDiscoverAgent()) { logger.debug(jrds.Util.delayedFormatString("Adding discover agent %s", da)); da.doHtmlDiscoverFields(hostDom); } resp.setContentType(CONTENT_TYPE); Map<String, String> prop = new HashMap<String, String>(1); prop.put(OutputKeys.OMIT_XML_DECLARATION, "no"); prop.put(OutputKeys.INDENT, "yes"); prop.put("{http://xml.apache.org/xslt}indent-amount", "4"); Util.serialize(hostDom, resp.getOutputStream(), null, prop); } catch (ParserConfigurationException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Parser configuration error"); } catch (TransformerException e) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Transformer exception error"); } }
public void setArchivesSetMap(Map<String, ArchivesSet> archivessetmap) { logger.debug(Util.delayedFormatString("will look for archives in %s", archivessetmap)); this.archivessetmap = archivessetmap; }
/** * @param end the end to set */ public void setEnd(Date end) { // We normalize the last update time, it can't be used directly this.end = Util.endDate(node.getProbe(), end); }
private String parseTemplate(String template) { Object[] arguments = { "${graphdesc.name}", "${host}", "${index}", "${url}", "${probename}", "${index.signature}", "${url.signature}" }; return jrds.Util.parseOldTemplate(template, arguments, probe, gd); }
public Properties readProperties(ServletContext ctxt) { Properties p = new Properties(); InputStream propStream = ctxt.getResourceAsStream("/WEB-INF/jrds.properties"); if(propStream != null) { try { p.load(propStream); } catch (IOException ex) { logger.warn("Invalid properties stream " + propStream + ": " + ex); } } Enumeration<String> params = ctxt.getInitParameterNames(); for(String attr: jrds.Util.iterate(params)) { String value = ctxt.getInitParameter(attr); if(value != null) p.setProperty(attr, value); } String localPropFile = ctxt.getInitParameter("propertiesFile"); if(localPropFile != null) try { p.load(new FileReader(localPropFile)); } catch (IOException e) { } return p; }
/** * Used to normalize the end date to the last update time but only if it's * close to it * * @param p the probe to check against * @param endDate the desired end date * @return the normalized end date */ public static Date endDate(Probe<?, ?> p, Date endDate) { // Date normalized = endDate; // We normalize the last update time, it can't be used directly long step = p.getStep(); Date lastUpdate = p.getLastUpdate(); // We dont want to graph past the last normalized update time // but only if we are within a step interval if(Math.abs(endDate.getTime() - lastUpdate.getTime()) <= (step * 1000L)) return normalize(lastUpdate, step); // Else rrd4j will manage the normalization itself return endDate; }
public void addTree(String tab, List<?> tree) { trees.put(tab, tree); logger.trace(jrds.Util.delayedFormatString("Adding tree %s to tab %s", tree, tab)); }
public void register(StarterNode node) throws InvocationTargetException { try { Class<?>[] constArgsType = new Class[args.size()]; Object[] constArgsVal = new Object[args.size()]; int index = 0; for(Object arg: args) { constArgsType[index] = arg.getClass(); constArgsVal[index] = arg; index++; } Connection<?> cnx = type.getConstructor(constArgsType).newInstance(constArgsVal); for(Map.Entry<String, String> e: beansValue.entrySet()) { String textValue = Util.parseTemplate(e.getValue(), cnx); ArgFactory.beanSetter(cnx, e.getKey(), textValue); cnx.log(Level.TRACE, "Setting bean '%s' to value '%s' for %s", e.getKey(), textValue, node); } if(name != null && !name.trim().isEmpty()) cnx.setName(name.trim()); node.registerStarter(cnx); logger.debug(Util.delayedFormatString("Connexion registred: %s for %s", cnx, node)); } catch (InvocationTargetException ex) { String message = ex.getCause() != null ? ex.getCause().getMessage(): ex.getMessage(); throw new InvocationTargetException(ex.getCause(), "Error during connection creation of type " + type.getName() + " for " + node + ": " + message); } catch (Exception ex) { throw new InvocationTargetException(ex, "Error during connection creation of type " + type.getName() + " for " + node); } }
protected long getEndSec() { return Util.endDate(node.getProbe(), end).getTime() / 1000; }
private final String parseTemplate(String template) { Object[] arguments = { "${host}", "${index}", "${url}", "${port}", "${index.signature}", "${url.signature}" }; return jrds.Util.parseOldTemplate(template, arguments, this); }
static public void findDescs(PropertiesManager pm) { try { for(URL u: jrds.Util.iterate(pm.extensionClassLoader.getResources("desc"))) { pm.libspath.add(u.toURI()); } } catch (IOException | URISyntaxException e) { throw new RuntimeException(e); } } }
static public Period getPeriod(Probe<?, ?> p, long endSec) { Date end = org.rrd4j.core.Util.getDate(endSec); Calendar calBegin = Calendar.getInstance(); calBegin.setTime(end); calBegin.add(Calendar.MONTH, -1); Date begin = calBegin.getTime(); end = jrds.Util.normalize(end, p.getStep()); Period pr = null; try { pr = new Period(Long.toString(begin.getTime()), Long.toString(end.getTime())); } catch (ParseException e) { } return pr; }
/** * @param indexKey the indexKey to set */ public void setIndex(String indexKey) { this.indexKey = Util.parseStringNumber(indexKey, -1); }
public Boolean configure() { if(cmd == null) return false; String cmdargs = getPd().getSpecific("arguments"); if(cmdargs != null && !cmdargs.trim().isEmpty()) { cmd = cmd + " " + Util.parseTemplate(cmdargs, this); } return true; }
prop.put(OutputKeys.DOCTYPE_PUBLIC, "-//jrds//DTD Host//EN"); prop.put(OutputKeys.DOCTYPE_SYSTEM, "urn:jrds:host"); Util.serialize(hostDom, response.getOutputStream(), null, prop); } catch (IOException | ParserConfigurationException | TransformerException e) { logger.error(e);