minor fixes

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-checker-se-plugin@146949 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-04-19 09:25:51 +00:00
parent 8d52170fd7
commit e2536eb4c6
5 changed files with 54 additions and 19 deletions

View File

@ -22,5 +22,10 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

View File

@ -5,6 +5,7 @@ package org.gcube.informationsystem.resource_checker;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -13,11 +14,15 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.gcube.common.resources.gcore.GCoreEndpoint; import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.resources.gcore.GenericResource; import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.HostingNode; import org.gcube.common.resources.gcore.HostingNode;
import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.Software; import org.gcube.common.resources.gcore.Software;
import org.gcube.common.resources.gcore.utils.XPathHelper;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.resource_checker.utils.BasicFunctionalitiesMandatoryReader; import org.gcube.informationsystem.resource_checker.utils.BasicFunctionalitiesMandatoryReader;
import org.gcube.informationsystem.resource_checker.utils.BasicFunctionalityBean; import org.gcube.informationsystem.resource_checker.utils.BasicFunctionalityBean;
@ -29,6 +34,8 @@ import org.gcube.resources.discovery.icclient.ICFactory;
import org.gcube.vremanagement.executor.plugin.Plugin; import org.gcube.vremanagement.executor.plugin.Plugin;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
/** /**
* The resource-checker-se-plugin implementation class. * The resource-checker-se-plugin implementation class.
@ -40,7 +47,7 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
private static final int SECONDS2WAIT = 2; // seconds to wait among IS requests private static final int SECONDS2WAIT = 2; // seconds to wait among IS requests
private static final String INITIAL_ERRORS_STATEMENT = "Other errors: "; private static final String INITIAL_ERRORS_STATEMENT = "Other errors: ";
public static final String ROLE_TO_NOTIFY = "role"; public static final String ROLE_TO_NOTIFY = "role";
private static final String NAGIOS_PROPERTY_FILE = "resources_to_fetch.properties"; private static final String NAGIOS_PROPERTY_FILE = "/META-INF/plugin_resources/nagios-report-location.properties";
private static final Logger logger = LoggerFactory.getLogger(ResourceCheckerPlugin.class); private static final Logger logger = LoggerFactory.getLogger(ResourceCheckerPlugin.class);
public ResourceCheckerPlugin(ResourceCheckerPluginDeclaration pluginDeclaration){ public ResourceCheckerPlugin(ResourceCheckerPluginDeclaration pluginDeclaration){
@ -67,6 +74,7 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
String otherFailures = INITIAL_ERRORS_STATEMENT; String otherFailures = INITIAL_ERRORS_STATEMENT;
DiscoveryClient client = ICFactory.client(); DiscoveryClient client = ICFactory.client();
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Map<String, String> resourceToIdentifierMap = new HashMap<String, String>(mandatoryFunctionalities.size()); Map<String, String> resourceToIdentifierMap = new HashMap<String, String>(mandatoryFunctionalities.size());
Set<String> missingResourcesKey = new HashSet<String>(mandatoryFunctionalities.size()); Set<String> missingResourcesKey = new HashSet<String>(mandatoryFunctionalities.size());
@ -79,7 +87,7 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
String resourceId = null; String resourceId = null;
String resourceKey = service.getName() + ":" + service.getCategory(); String resourceKey = service.getName() + ":" + service.getCategory();
if((resourceId = isServicePresent(service, client)) == null){ if((resourceId = isServicePresent(service, client, docBuilder)) == null){
List<BasicFunctionalityBean> missingServices = null; List<BasicFunctionalityBean> missingServices = null;
if(missingResourcesPerContext.containsKey(context)) if(missingResourcesPerContext.containsKey(context))
@ -110,7 +118,7 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
} }
if(otherFailures.equals(INITIAL_ERRORS_STATEMENT)) if(otherFailures.equals(INITIAL_ERRORS_STATEMENT))
otherFailures += "none\n"; otherFailures += "none.\n";
else else
otherFailures += "\n"; otherFailures += "\n";
@ -122,7 +130,7 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
writeReport4Nagios(identifiers); writeReport4Nagios(identifiers);
// if there are missing resources, notify administrators via mail // if there are missing resources, notify administrators via mail
if(!missingResourcesPerContext.isEmpty()) if(!missingResourcesPerContext.isEmpty() || !otherFailures.equals(INITIAL_ERRORS_STATEMENT + "none.\n"))
SendNotification.sendMessage(missingResourcesPerContext, otherFailures, (String)inputs.get(ROLE_TO_NOTIFY)); SendNotification.sendMessage(missingResourcesPerContext, otherFailures, (String)inputs.get(ROLE_TO_NOTIFY));
logger.info("Plugin's execution ended. Map of not available services per scope is the following: \n" + missingResourcesPerContext); logger.info("Plugin's execution ended. Map of not available services per scope is the following: \n" + missingResourcesPerContext);
@ -132,20 +140,21 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
* Check if a given resource is present or is missing. * Check if a given resource is present or is missing.
* @param service * @param service
* @param client * @param client
* @param docBuilder
* @return the identifier of the resource * @return the identifier of the resource
* @throws Exception * @throws Exception
*/ */
private String isServicePresent(BasicFunctionalityBean service, DiscoveryClient client) throws Exception { private String isServicePresent(BasicFunctionalityBean service, DiscoveryClient client, DocumentBuilder docBuilder) throws Exception {
Class classFor = service.getType() == null? ServiceEndpoint.class : service.getType(); // default is service end point Class classFor = service.getType() == null? ServiceEndpoint.class : service.getType(); // default is service end point
SimpleQuery q = ICFactory.queryFor(classFor); SimpleQuery q = ICFactory.queryFor(classFor);
List<ServiceEndpoint> result = null; List result = null;
if(classFor.equals(ServiceEndpoint.class)){ if(classFor.equals(ServiceEndpoint.class)){
q.addCondition("$resource/Profile/Name/text() eq '"+ service.getName() +"'"); q.addCondition("$resource/Profile/Name/text() eq '"+ service.getName() +"'");
q.addCondition("$resource/Profile/Category/text() eq '"+ service.getCategory() +"'"); q.addCondition("$resource/Profile/Category/text() eq '"+ service.getCategory() +"'");
result = (List<ServiceEndpoint>)client.submit(q); result = client.submit(q);
}else if(classFor.equals(GCoreEndpoint.class)){ }else if(classFor.equals(GCoreEndpoint.class)){
@ -175,7 +184,23 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
throw new Exception("Unable to check resource because its type is missing " + service); throw new Exception("Unable to check resource because its type is missing " + service);
return result != null && result.size() > 0 ? result.get(0).id() : null; if(result == null || result.isEmpty())
return null;
else{
String elem = (String) result.get(0);
Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement();
XPathHelper helper = new XPathHelper(node);
List<String> currValue = null;
currValue = helper.evaluate("/Resource/ID/text()");
if (currValue != null && currValue.size() > 0) {
logger.debug("Id is " + currValue.get(0));
return currValue.get(0);
}
else throw new Exception("ID property is missing!");
}
} }
/**{@inheritDoc}*/ /**{@inheritDoc}*/
@ -189,14 +214,16 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
* Write a report for nagios * Write a report for nagios
* @throws IOException * @throws IOException
*/ */
private void writeReport4Nagios(List<String> idsMissingResources) throws IOException{ private void writeReport4Nagios(List<String> idsMissingResources) throws Exception{
// find location of nagios file and write the ids of the resources there
Properties prop = new Properties(); Properties prop = new Properties();
prop.load(getClass().getResourceAsStream(NAGIOS_PROPERTY_FILE)); prop.load(getClass().getResourceAsStream(NAGIOS_PROPERTY_FILE));
String location = prop.getProperty("location"); String location = prop.getProperty("location");
logger.trace("File location for nagios is " + location);
File f = new File(location); File f = new File(location);
f.getParentFile().mkdirs();
f.delete(); f.delete();
f.createNewFile(); f.createNewFile();

View File

@ -134,10 +134,10 @@ public class SendNotification extends PluginStateNotification {
logger.warn("Something failed when trying to notify the user", e); logger.warn("Something failed when trying to notify the user", e);
} }
}else }else
logger.error("No notification is going to be sent, because social service is missing or recipient is not specified"); logger.error("No notification is going to be sent because social service is missing or recipient is not specified");
break; break;
default: default:
logger.info("No notification is going to be sent, because the status of the plugin execution is " + pluginStateEvolution.getPluginState().name()); logger.info("No notification is going to be sent because the status of the plugin execution is " + pluginStateEvolution.getPluginState().name());
} }
} }
@ -197,13 +197,16 @@ public class SendNotification extends PluginStateNotification {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("Dear "); sb.append("Dear ");
sb.append(role).append(",").append("\n"); sb.append(role).append(",").append("\n");
sb.append("this is the report of the last running of the " + ResourceCheckerPluginDeclaration.NAME.toUpperCase() + " (" + dataStr + ").\nThe following missing resources have been found:"); sb.append("this is the report of the last running of the " + ResourceCheckerPluginDeclaration.NAME.toUpperCase() + " (" + dataStr + ").\nThe following missing resources have been found:\n");
Iterator<Entry<String, List<BasicFunctionalityBean>>> iterator = missingResourcesPerContext.entrySet().iterator(); Iterator<Entry<String, List<BasicFunctionalityBean>>> iterator = missingResourcesPerContext.entrySet().iterator();
if(!iterator.hasNext())
sb.append("none.");
while (iterator.hasNext()) { while (iterator.hasNext()) {
Map.Entry<java.lang.String, java.util.List<org.gcube.informationsystem.resource_checker.utils.BasicFunctionalityBean>> entry = (Map.Entry<java.lang.String, java.util.List<org.gcube.informationsystem.resource_checker.utils.BasicFunctionalityBean>>) iterator Map.Entry<java.lang.String, java.util.List<org.gcube.informationsystem.resource_checker.utils.BasicFunctionalityBean>> entry = (Map.Entry<java.lang.String, java.util.List<org.gcube.informationsystem.resource_checker.utils.BasicFunctionalityBean>>) iterator
.next(); .next();
sb.append("\n");
List<BasicFunctionalityBean> list = entry.getValue(); List<BasicFunctionalityBean> list = entry.getValue();
for (BasicFunctionalityBean basicFunctionalityBean : list) { for (BasicFunctionalityBean basicFunctionalityBean : list) {
sb.append("- resource's name "); sb.append("- resource's name ");
@ -213,13 +216,13 @@ public class SendNotification extends PluginStateNotification {
sb.append(" (in context " + entry.getKey() + ")"); sb.append(" (in context " + entry.getKey() + ")");
} }
if(iterator.hasNext()) if(iterator.hasNext())
sb.append(";"); sb.append(";\n");
else else
sb.append("."); sb.append(".\n");
} }
sb.append("\n\n"); sb.append("\n\n");
sb.append(otherFailures + ".\n"); sb.append(otherFailures + "\n");
sb.append("Best,\n" + ResourceCheckerPluginDeclaration.NAME); sb.append("Best,\n" + ResourceCheckerPluginDeclaration.NAME);
String message = sb.toString(); String message = sb.toString();

View File

@ -1 +1 @@
location=/home/gcube/scopes_data/scopes_status location=/home/gcube/missing_resources/identifiers

View File

@ -1 +1 @@
org.gcube.common.resource_checker.ResourceCheckerPluginDeclaration org.gcube.informationsystem.resource_checker.ResourceCheckerPluginDeclaration