groovy脚本:类错误没有这样的属性

时间:2016-12-29 15:02:30

标签: groovy

我是Groovy脚本的新手,我正在开发一个自定义脚本。在调试/跟踪模式下测试我得到了这个错误:

2016-12-29 12:26:50,913 [pool-4467-thread-1] ERROR ENI-callMDC.invoke0(?) - >> setAdminGroup>无法设置CA ENI-AdminGroup。 原因:没有这样的属性:类的ucmdbcacheDumpFile:ENIcallMDC

这里是完整的脚本,其中显示了两个相关的功能:

*

*
import java.util.List;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.TimerTask;
import java.util.Timer;
import com.hp.opr.api.scripting.Event
import com.hp.opr.api.scripting.Severity
import com.hp.opr.api.scripting.LifecycleState
import com.hp.opr.api.scripting.UcmdbServiceProvider
import com.hp.ucmdb.api.UcmdbService
import com.hp.ucmdb.api.topology.QueryDefinition
import com.hp.ucmdb.api.topology.Topology
import com.hp.ucmdb.api.topology.TopologyQueryFactory
import com.hp.ucmdb.api.topology.TopologyQueryService
import com.hp.ucmdb.api.types.TopologyCI
import com.hp.ucmdb.api.types.UcmdbId
import com.hp.ucmdb.api.classmodel.ClassModelService
import com.hp.ucmdb.api.classmodel.ClassDefinition
import com.hp.ucmdb.api.classmodel.Attribute
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
// Libraries for HTTP GET call
import org.apache.http.HttpRequest;
import groovyx.net.http.HTTPBuilder;
//import static groovyx.net.http.Method.GET;
import groovyx.net.http.Method;
//import static groovyx.net.http.ContentType.TEXT;
import groovyx.net.http.ContentType;

class ENIcallMDC
{
    private Log s_log = LogFactory.getLog("ENI-callMDC")
    UcmdbServiceProvider ucmdbServiceProvider
    UcmdbService ucmdbServiceAccess = null
    public Map<String, Map<String,String>> dataSHM                           // Global HashMap for CIs
    //public Map<String,String> catHashMap                                   // Global HashMap for Category suffixes - Rimane locale solo nella init - non serve globale
    public Map<String, Map<String,String>> ucmdbCache
    public static final long REFRESH_INTERVAL = 3600;                        // in seconds
    public static final long EVT_PIPELINE_WAITING_TIMEOUT = 300;             // in seconds
    public static final int MAX_CHUNK_SIZE = 20;                             // in number of events
    public static final String rtsmTQL = "ENI-OPR-callMDC";                  // TQL for CIs which are mapped on events - attributes of interest are defined in TQL element layout

    public static final String ciTypeCaName       = "ENI-CItype";            // Event Custom Attribute for OStype/CIT
    public static final String ciTypeAttrName     = "discovered_os_name";    // Attribute name for Discovered OS Name            // in data struct
    public static final String adminGroupCaName   = "ENI-AdminGroup";        // Event Custom Attribute for Gruppo di gestione
    public static final String adminGroupAttrName = "tools_admin_group";     // Attribute name for Gruppo di Gestione            // in data struct
    public static final String locationCaName     = "ENI-Location";          // Event Custom Attribute for Location
    public static final String locationAttrName   = "calculated_location";   // Attribute name for Location                      // in data struct  
    public static final String tagsCaName         = "ENI-Tags";              // Event Custom Attribute for Tags  
    public static final String tagsAttrName       = "tags_string";           // Attribute name for Tags                          // in data struct        

    public static final String citAttrName = "root_class";                   // Attribute name for CIT
    public static final String CITgroup    = "CITgroup";                     // Attribute for CIT group                     // in data struct

    public boolean enableEvtHandler = false;                                 // Global var for enable EVT handling (once HashMaps are calculated)
    public boolean threadHasToStop = false
    public Thread myThread = null

    public static final String ucmdbCacheDumpFile    = "F:\\HPBSM\\Temp\\ENI-HandleIncomingEventsAR-ExtUcmdbCache.dump";

    // MDC parameters are in callMDC method


    def init()
    {
     .....      
    }

    def destroy()
    {
     ....
    }

    def synchronized initUcmdbService() {
     ....
    }

    def synchronized getMap () {
        .........
    }


    def process(List<Event> events) {
      .....
    }
    def setEniCItype(Event evt,String relCiID) {
        .....
    }
    def setAdminGroup(Event evt,String relCiID) {
        try {
            String myEvtTitle = evt.getTitle();
            // DISCRIMINAZIONE TIPO FILE SYSTEM - codice preso da ENI-HandleIncomingEventsAR.groovy a riga #1183
            if (myEvtTitle.contains("File System ") ) {
                s_log.debug("Process >         >> setAdminGroup > RelCI is Node > FS event > checking FS name");
                createUcmdbCache() 
                String myFsName;
                Pattern omlAllPattern = Pattern.compile("^File\\s+System\\s+\\<([^:]+):?\\>\\s+on\\s+server\\s+"); // from OML win|lnx
                Matcher omlAllMathcer = omlAllPattern.matcher(myEvtTitle);
                s_log.trace("Process >         >> setAdminGroup > RelCI is Node > FS event > checking FS name > checking OML win|lnx regex");
                if (omlAllMathcer.find()) {
                    myFsName=omlAllMathcer.group(1);
                    s_log.trace("Process >         >> setAdminGroup > RelCI is Node > FS event > checking FS name > checking OML win|lnx regex > matched FS: " + myFsName);
                } else {
                    s_log.trace("Process >         >> setAdminGroup > RelCI is Node > FS event > checking FS name > checking OML win|lnx regex > NOT matched");
                    s_log.trace("Process >         >> setAdminGroup > RelCI is Node > FS event > checking FS name > checking SIS lnx regex");
                    Pattern sisLnxPattern = Pattern.compile("^File\\s+System\\s+\\'Disk\\/File System\\/\\[.+\\((.+)\\)\\s*\\]\\/percent\\s+full\\'\\s+on\\s+server\\s+"); // from SIS lnx
                    Matcher sisLnxMathcer = sisLnxPattern.matcher(myEvtTitle);
                    if (sisLnxMathcer.find()) {
                        myFsName=sisLnxMathcer.group(1);
                        s_log.trace("Process >         >> setAdminGroup > RelCI is Node > FS event > checking FS name > checking SIS lnx regex > matched FS: " + myFsName);
                    } else {
                        s_log.trace("Process >         >> setAdminGroup > RelCI is Node > FS event > checking FS name > checking SIS lnx regex > NOT matched");
                        s_log.trace("Process >         >> setAdminGroup > RelCI is Node > FS event > checking FS name > checking SIS win regex");
                        Pattern sisWinPattern = Pattern.compile("^File\\s+System\\s+\\'Disk\\/File System\\/\\[([^\\(]+):?\\]\\/percent\\s+full\\'\\s+on\\s+server\\s+"); // from SIS win
                        Matcher sisWinMathcer = sisWinPattern.matcher(myEvtTitle);
                        if (sisWinMathcer.find()) {
                            myFsName=sisWinMathcer.group(1);
                            s_log.trace("Process >         >> setAdminGroup > RelCI is Node > FS event > checking FS name > checking SIS win regex > matched FS: " + myFsName);
                        } else {
                            s_log.trace("Process >         >> setAdminGroup > RelCI is Node > FS event > checking FS name > checking SIS win regex > NOT matched");
                        }
                    }
                }
                if ( (myFsName != null)  ) {
                    try {
                        // se myFsName non è nullo allora l'admin group viene settato come il valore admin group presente nella hashmap ucmdbCache
                        String adminGrp = ucmdbCache.get(relCiID).get(myFsName)
                        // se l'admin group appena settato è nullo (quindi il file system non è stato trovato nella cache ucmdbCache)
                        if(adminGrp==null){
                            // l'admin group viene settato col valore del padre >> comportamento di ENI-callMDC originale (v 1.1.1)
                            adminGrp = dataSHM.get(relCiID).get(adminGroupAttrName); 
                        }
                        evt.addCustomAttribute (adminGroupCaName,adminGrp);
                        return; // nothing else to do
                    } catch (Exception e) {
                        s_log.error("        >> setAdminGroup > Error: " + e.getMessage());
                    }
                } else {
                // if I was not able to extract FS name I don\'t return: fall back to node
                }
            }
            if ( dataSHM.containsKey(relCiID) ) {   
                if (dataSHM.get(relCiID).get(CITgroup) == "business_application") {    // Not have to calculate if CIT is BA
                    s_log.debug("        >> setAdminGroup > RelatedCI is BusinessApplication > nothing to do");
                    return;
                }
                String adminGrp = dataSHM.get(relCiID).get(adminGroupAttrName);
                evt.addCustomAttribute (adminGroupCaName,adminGrp);
                s_log.debug("        >> setAdminGroup > Added CA " + adminGroupCaName + " = " + adminGrp);                
            } else {
                s_log.debug("        >> setAdminGroup > RelatedCI " + relCiID + " is not present in cache > returning");
                return;
            }            
        } catch (Exception e) {
            s_log.error("        >> setAdminGroup > Failed to set CA " + adminGroupCaName + ". Reason: " + e.getMessage());
        }
    }
    def setTags(Event evt,String relCiID) {
        ......
    }
    def setLocation(Event evt, String relNodeID) {
        ...
    }

    def callMDC(String evtID) {
        ....
    }
    def synchronized createUcmdbCache () {
        try {
            Map<String, Map<String,String>> localHM = new HashMap()
            String line
            // leggo dal ucmdb cache file
            FileReader fr = new FileReader(ucmdbcacheDumpFile)
            BufferedReader textReader = new BufferedReader(fr)
            // creazione della hashmap annidata
            while((line = textReader.readLine()) != null) {
                String[] parts = line.split("~")
                // parts[0]=relatedCI
                // parts[1]=File System Type
                // parts[2]=Admin group
                if (localHM.containsKey(parts[0])) {
                    // tempHM = mappa più interna che contiene FileSystemTipe,AdminGroup
                    Map<String,String> tempHM = localHM.get(parts[0])
                    tempHM.put(parts[1], parts[2])
                    localHM.put(parts[0],tempHM)
                }
                else {
                    Map<String,String> tempHM = new HashMap()
                    tempHM.put(parts[1], parts[2])
                    // aggiunta alla hashmap più esterna di RelatedCI,(tempHM) dove tempHM=FileSystemTipe,AdminGroup
                    localHM.put(parts[0],tempHM)
                }
            }
            textReader.close()
            // set variabile globale ucmdbCache con il valore della hashmap localHM
            ucmdbCache = Collections.synchronizedMap(localHM)
        } catch (IOException e) {
        }
    }
}
*

*

我已将ucmdbCacheDumpFile定义为脚本之上的公共静态最终字符串:

*`public static final String ucmdbCacheDumpFile    = "F:\\HPBSM\\Temp\\ENI-HandleIncomingEventsAR-ExtUcmdbCache.dump";`*

我认为错误与此定义有关。你有什么建议吗?我需要以不同的方式设置吗?

谢谢,

的Fab

1 个答案:

答案 0 :(得分:0)

我想通知我已经解决了我的错误,改为:

String adminGrp = ucmdbCache.get(relCiID).get(myFsName) 

String adminGrp = ucmdbCache.get(myNodeNameHint).get(myFsName) 

因为 relCIID 值不存在于我的hashmap和设置中以及我的新字符串:

String myNodeNameHint = evt.getRelatedCiHint();

此致 FAB