added code and resource to write a file that nagios will read
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-checker-se-plugin@146943 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
8acd0735d1
commit
8d52170fd7
|
@ -2,10 +2,16 @@
|
|||
*
|
||||
*/
|
||||
package org.gcube.informationsystem.resource_checker;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
||||
import org.gcube.common.resources.gcore.GenericResource;
|
||||
|
@ -31,9 +37,10 @@ import org.slf4j.LoggerFactory;
|
|||
@SuppressWarnings("rawtypes")
|
||||
public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclaration>{
|
||||
|
||||
private static final int SECONDS2WAIT = 1; // 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: ";
|
||||
public static final String ROLE_TO_NOTIFY = "role";
|
||||
private static final String NAGIOS_PROPERTY_FILE = "resources_to_fetch.properties";
|
||||
private static final Logger logger = LoggerFactory.getLogger(ResourceCheckerPlugin.class);
|
||||
|
||||
public ResourceCheckerPlugin(ResourceCheckerPluginDeclaration pluginDeclaration){
|
||||
|
@ -50,22 +57,29 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
|
|||
|
||||
logger.info("Starting plugin with inputs: " + inputs);
|
||||
|
||||
Map<String, List<BasicFunctionalityBean>> missingResourcesPerContext = new HashMap<String, List<BasicFunctionalityBean>>();
|
||||
List<BasicFunctionalityBean> mandatoryFunctionalities = new BasicFunctionalitiesMandatoryReader().getMandatoryFunctionalities();
|
||||
List<String> contexts = RetrieveContextsList.getContexts();
|
||||
|
||||
logger.info("Contexts are " + contexts);
|
||||
logger.info("Mandatory functionalities are " + mandatoryFunctionalities);
|
||||
|
||||
Map<String, List<BasicFunctionalityBean>> missingResourcesPerContext = new HashMap<String, List<BasicFunctionalityBean>>(contexts.size());
|
||||
|
||||
String otherFailures = INITIAL_ERRORS_STATEMENT;
|
||||
DiscoveryClient client = ICFactory.client();
|
||||
|
||||
Map<String, String> resourceToIdentifierMap = new HashMap<String, String>(mandatoryFunctionalities.size());
|
||||
Set<String> missingResourcesKey = new HashSet<String>(mandatoryFunctionalities.size());
|
||||
|
||||
for (String context : contexts) {
|
||||
// switch context
|
||||
ScopeProvider.instance.set(context);
|
||||
for (BasicFunctionalityBean service : mandatoryFunctionalities) {
|
||||
try{
|
||||
|
||||
if(!isServicePresent(service, client)){
|
||||
String resourceId = null;
|
||||
String resourceKey = service.getName() + ":" + service.getCategory();
|
||||
if((resourceId = isServicePresent(service, client)) == null){
|
||||
|
||||
List<BasicFunctionalityBean> missingServices = null;
|
||||
if(missingResourcesPerContext.containsKey(context))
|
||||
|
@ -73,9 +87,14 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
|
|||
else
|
||||
missingServices = new ArrayList<BasicFunctionalityBean>();
|
||||
|
||||
// add among missing resources
|
||||
missingResourcesKey.add(resourceKey);
|
||||
|
||||
missingServices.add(service);
|
||||
missingResourcesPerContext.put(context, missingServices);
|
||||
|
||||
}else{
|
||||
resourceToIdentifierMap.put(resourceKey, resourceId);
|
||||
}
|
||||
|
||||
Thread.sleep(1000 * SECONDS2WAIT);
|
||||
|
@ -95,6 +114,13 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
|
|||
else
|
||||
otherFailures += "\n";
|
||||
|
||||
// write nagios report
|
||||
List<String> identifiers = new ArrayList<String>(mandatoryFunctionalities.size());
|
||||
for (String key : missingResourcesKey) {
|
||||
identifiers.add(resourceToIdentifierMap.get(key));
|
||||
}
|
||||
writeReport4Nagios(identifiers);
|
||||
|
||||
// if there are missing resources, notify administrators via mail
|
||||
if(!missingResourcesPerContext.isEmpty())
|
||||
SendNotification.sendMessage(missingResourcesPerContext, otherFailures, (String)inputs.get(ROLE_TO_NOTIFY));
|
||||
|
@ -106,18 +132,20 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
|
|||
* Check if a given resource is present or is missing.
|
||||
* @param service
|
||||
* @param client
|
||||
* @return
|
||||
* @return the identifier of the resource
|
||||
* @throws Exception
|
||||
*/
|
||||
private boolean isServicePresent(BasicFunctionalityBean service, DiscoveryClient client) throws Exception {
|
||||
private String isServicePresent(BasicFunctionalityBean service, DiscoveryClient client) throws Exception {
|
||||
|
||||
Class classFor = service.getType() == null? ServiceEndpoint.class : service.getType(); // default is service end point
|
||||
SimpleQuery q = ICFactory.queryFor(classFor);
|
||||
|
||||
List<ServiceEndpoint> result = null;
|
||||
if(classFor.equals(ServiceEndpoint.class)){
|
||||
|
||||
q.addCondition("$resource/Profile/Name/text() eq '"+ service.getName() +"'");
|
||||
q.addCondition("$resource/Profile/Category/text() eq '"+ service.getCategory() +"'");
|
||||
result = (List<ServiceEndpoint>)client.submit(q);
|
||||
|
||||
}else if(classFor.equals(GCoreEndpoint.class)){
|
||||
|
||||
|
@ -146,9 +174,8 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
|
|||
}else
|
||||
throw new Exception("Unable to check resource because its type is missing " + service);
|
||||
|
||||
List result = client.submit(q);
|
||||
|
||||
return result != null && result.size() > 0;
|
||||
return result != null && result.size() > 0 ? result.get(0).id() : null;
|
||||
}
|
||||
|
||||
/**{@inheritDoc}*/
|
||||
|
@ -157,4 +184,31 @@ public class ResourceCheckerPlugin extends Plugin<ResourceCheckerPluginDeclarati
|
|||
logger.debug("onStop() invoked");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a report for nagios
|
||||
* @throws IOException
|
||||
*/
|
||||
private void writeReport4Nagios(List<String> idsMissingResources) throws IOException{
|
||||
|
||||
// find location of nagios file and write the ids of the resources there
|
||||
Properties prop = new Properties();
|
||||
prop.load(getClass().getResourceAsStream(NAGIOS_PROPERTY_FILE));
|
||||
String location = prop.getProperty("location");
|
||||
|
||||
File f = new File(location);
|
||||
f.delete();
|
||||
f.createNewFile();
|
||||
|
||||
FileWriter writer = new FileWriter(f, false);
|
||||
|
||||
if (idsMissingResources == null || idsMissingResources.isEmpty()) {
|
||||
writer.write("none");
|
||||
}else{
|
||||
for (String idMissing : idsMissingResources) {
|
||||
writer.write(idMissing + "\n");
|
||||
}
|
||||
}
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
location=/home/gcube/scopes_data/scopes_status
|
Loading…
Reference in New Issue