Monitor

The Ptyhon module is called at every monitor refresh. The whole processing code, executed both from the monitor and the Python module, consists on the following steps at each refresh time:

1) Execution of the SQL query to retrieve the variable value 2) Update of the Period Counters 3) Manual storage of the Period Counters into the Statistics Database 4) Period Change Detection: reset the Period Counters if period has changed 

The monitor Pre-Event scripting code is the following:

if VSMScriptFirstEvent: #import the t04Custom module NFR from t04Custom import KPI_Contratacion_Oracle reload(KPI_Contratacion_Oracle) import datetime import time import os #initialize the module KPI=KPI_Contratacion_Oracle.KPI_Contratacion_Oracle(locals(),globals()) lastRecollectedIndex=-1 

The monitor Pre-Health scripting code is the following:

now=datetime.datetime.now() index = KPI.getTimeIndex(now) # The first time the script is executed, we process all 5-min periods from LasRecollectionPointer to Now if lastRecollectedIndex==-1 : lastRecollectedIndex=0 # We get the index from the persistence file [lastRecollectedIndex,lastRecollectionDate]=KPI.getLastRecollection(now) # We initialized eveyrthing else KPI.initContador(lastRecollectionDate) while not KPI.upToDate(lastRecollectionDate,now,lastRecollectedIndex,index): SendMessageToConfigurator(SUCCESS,'INFO:\tComputing past values for index : %d'%(lastRecollectedIndex+1)) [lastRecollectedIndex,lastRecollectionDate,persistence]=KPI.recollection(lastRecollectedIndex,lastRecollectionDate) ContextInfo=KPI.ContextInfoContador() KPI.storeDataOrdenes(lastRecollectedIndex,lastRecollectionDate) KPI.saveData(lastRecollectedIndex,lastRecollectionDate) SendMessageToConfigurator(MINOR,'Last 5 minute period already processed (Last Recollection Index: %s)'%(lastRecollectedIndex,)) # We are up-to-date, we just ckeck the last 5-min period has been finished else: [lastRecollectedIndex,lastRecollectionDate]=KPI.getLastRecollection(now) while not KPI.upToDate(lastRecollectionDate,now,lastRecollectedIndex,index): #we collect and compute the data for the last index [lastRecollectedIndex,lastRecollectionDate,persistence]=KPI.recollection(lastRecollectedIndex,lastRecollectionDate) ContextInfo=KPI.ContextInfoContador() KPI.storeDataOrdenes(lastRecollectedIndex,lastRecollectionDate) KPI.saveData(lastRecollectedIndex,lastRecollectionDate) else: SendMessageToConfigurator(MINOR,'Last 5 minute period already processed (Last Recollection Index: %s)'%(lastRecollectedIndex,)) 

 

Python Module

The Ptyhon module is called at every monitor refresh. Within the Python module we define functions that are called from the monitor scripting.

 

# initializations __version__='1.0' __DEBUG__=False # initializations # importing modules import sys import re import datetime import os,sys import shutil #ODBC import pyodbc as pyodbcinit import pyodbc #serialize and Persistence import cPickle # importing modules from Thinkserver import InternalPython #import the specific module statisticsDBAccess from t04Custom import statisticsDBAccess #querys to be executed query2 = """ SELECT count(*) FROM %(Library_iSeries)s.%(Log_File)s WHERE DATE='%(timeInfDate)s' """ query = """ SELECT count(*) FROM %(Library_iSeries)s.%(Log_File)s WHERE FPRESMOV >= TO_DATE('%(timeInfDate)s','DD-MM-YYYY HH24:MI') AND FPRESMOV < TO_DATE('%(timeSupDate)s','DD-MM-YYYY HH24:MI') AND CACEPMOV = '01' """ def loadconfig(field,filepath): #Reads the configuration file with format FIELD=VALUE f = open(filepath, 'r') for line in f: fileField=line.split("=")[0] if fileField==field: value=line.split("=")[1].strip() f.close() return value def incDict(self,dict,key1,key2,addition): # if __DEBUG__: # self.SendMessageToConfigurator(self.MINOR, 'DEBUG:\tKey1: %s Key2: %s Addition: %r' % (key1,key2,addition,)) if key2 in dict[key1]: dict[key1][key2] = dict[key1][key2] + addition else: dict[key1][key2] = addition return dict def incDictArray(self,dict,key1,indice,addition): if key1 in dict: if len(dict[key1][indice])<>0: dict[key1][indice] = dict[key1][indice] + addition else: dict[key1][indice] = np.append(dict[key1][indice],int(addition)) else: dict[key1] = {} for i in range(12*24): dict[key1][i]=np.empty(0,int) dict[key1][indice] = np.append(dict[key1][indice],int(addition)) return dict class KPI_Contratacion_Oracle: def __init__(self,varLocals, varGlobals): self.SendMessageToConfigurator = varLocals['SendMessageToConfigurator'] self.StoreCustomValues = varLocals['StoreCustomValues'] self.StoreSummaryCustomValue = varLocals['StoreSummaryCustomValue'] self.SetObjectHealth = varLocals['SetObjectHealth'] self.AC_REAL_TIME = -1 self.AC_HOUR = varLocals['AC_HOUR'] self.AC_DAY = varLocals['AC_DAY'] self.AC_WEEK = varLocals['AC_WEEK'] self.AC_MONTH = varLocals['AC_MONTH'] self.AC_YEAR = varLocals['AC_YEAR'] self.accumulations = (self.AC_REAL_TIME, self.AC_HOUR, self.AC_DAY,self.AC_WEEK,self.AC_MONTH) self.accumNames = {self.AC_REAL_TIME:"Ultimos 5 min", self.AC_HOUR:"Hora", self.AC_DAY:"Dia",self.AC_WEEK:"Semana",self.AC_MONTH:"Mes"} self.SUCCESS = varLocals['SUCCESS'] self.ERROR = varLocals['ERROR'] self.WARNING = varLocals['WARNING'] self.MINOR = varLocals['MINOR'] self.CHART_EVENT= varLocals['CHART_EVENT'] self.CHART_GROUP= varLocals['CHART_GROUP'] self.VSMThinkServerDirectory = varLocals['VSMThinkServerDirectory'] self.VSMMonitorName = varLocals['VSMMonitorName'] self.VSMTimer = varLocals['VSMTimer'] #Configuration File self.CONFIG_FILENAME = os.path.join(self.VSMThinkServerDirectory,'PythonLib','Lib','t04Custom','config','KPIconfig.ini') #filename to persitent the Ordenes self.PERSISTENCE_FILENAME_LASTVALUE = os.path.join(self.VSMThinkServerDirectory,'Persistence','t04Custom',self.VSMMonitorName,'lastValue') self.PERSISTENCE_FILENAME_CONTADOR = os.path.join(self.VSMThinkServerDirectory,'Persistence','t04Custom',self.VSMMonitorName,'contador') self.PERSISTENCE_FILENAME_MEANCONTADOR = os.path.join(self.VSMThinkServerDirectory,'Persistence','t04Custom',self.VSMMonitorName,'meanContador') self.PERSISTENCE_FILENAME_LASTACCUM = os.path.join(self.VSMThinkServerDirectory,'Persistence','t04Custom',self.VSMMonitorName,'lastAccum') #filename to persitent the lastRecollectionIndex self.PERSISTENCE_FILENAME_INDEX = os.path.join(self.VSMThinkServerDirectory,'Persistence','t04Custom',self.VSMMonitorName,'lastRecollectionIndex') try: DSNName=varLocals['DSNName'] DSNUser=varLocals['DSNUser'] DSNPassword=varLocals['DSNPassword'] self.Library_iSeries=varLocals['Library_iSeries'] self.Log_File=varLocals['Log_File'] self.Host = varLocals['Host'] except Exception,e: self.SendMessageToConfigurator(self.ERROR, "Ckeck out the configuration file %s\n\nException: %s"%(self.CONFIG_FILENAME,e)) self._connectionStringiSeries = "DSN=%(DSNName)s;Uid=%(DSNUser)s;Pwd=%(DSNPassword)s;" % \ {'DSNName':DSNName,'DSNUser':DSNUser,'DSNPassword':DSNPassword} #initializacion self.refreshTime=self.VSMTimer/60 self.contador={} self.meanContador={} self._connection=None self.SendMessageToConfigurator(self.SUCCESS,'Module %(moduleName)s successfully initialized. (Version: %(version)s)'%{'version':__version__,'moduleName':__name__} ) def _getConnection(self): if (self._connection==None): createConnection=True else: try: dummyQuery =""" select 1 from sysibm.sysdummy1""" # SQLServer: select @@version # Oracle: select 1 from dual # iSeries: select 1 from sysibm/sysdummy cursor=self._connection.cursor() cursor.execute(dummyQuery) test=cursor.fetchall() createConnection=False except Exception,e: self.SendMessageToConfigurator(self.WARNING,'Problem with the current ODBC connection, a new database connection will be created.\n\nException: %s'%e) createConnection=True self._connection=None if createConnection: if __DEBUG__: self.SendMessageToConfigurator(self.SUCCESS, "DEBUG:\tCreating a new ODBC connection with connect string: %s."%self._connectionStringiSeries ) try: self._connection = pyodbc.connect(self._connectionStringiSeries,autocommit=True) # create a connection except Exception,e: self.SendMessageToConfigurator(self.ERROR,'ERROR:\tProblem while creating the ODBC connection.\n\nException: %s'%e) createConnection=True return self._connection def _getConnectioniSeries(self): if (self._connection==None): if __DEBUG__: self.SendMessageToConfigurator(self.SUCCESS, "DEBUG:\tCreating connection with connect string %s."%self._connectionStringiSeries ) self._connection = pyodbc.connect(self._connectionStringiSeries,autocommit=True) # create a connection return self._connection #init the structure Responses time from the persistence file def initContador(self,lastRecollectionDatetime): if __DEBUG__: self.SendMessageToConfigurator(self.SUCCESS, 'Trying to get Ordenes initialized from persistent file' ) self.contador={} self.meanContador={} self.lastValue=9999999999 for acc in self.accumulations: self.contador[acc] ={} self.meanContador[acc]={} self.lastAccum={} try: f = open(self.PERSISTENCE_FILENAME_LASTVALUE) unpickler = cPickle.Unpickler(f) self.lastValue = unpickler.load() if __DEBUG__: self.SendMessageToConfigurator(self.SUCCESS, 'DEBUG:\tContador initialized from persistent file') f.close() del unpickler f = open(self.PERSISTENCE_FILENAME_CONTADOR) unpickler = cPickle.Unpickler(f) self.contador = unpickler.load() if __DEBUG__: self.SendMessageToConfigurator(self.SUCCESS, 'DEBUG:\tContador initialized from persistent file') f.close() del unpickler f = open(self.PERSISTENCE_FILENAME_MEANCONTADOR) unpickler = cPickle.Unpickler(f) self.meanContador = unpickler.load() if __DEBUG__: self.SendMessageToConfigurator(self.SUCCESS, 'DEBUG:\tmeanContador initialized from persistent file') f.close() del unpickler f = open(self.PERSISTENCE_FILENAME_LASTACCUM) unpickler = cPickle.Unpickler(f) self.lastAccum = unpickler.load() if __DEBUG__: self.SendMessageToConfigurator(self.SUCCESS, 'DEBUG:\tlastAccum initialized from persistent file') f.close() del unpickler except IOError: #if file does not exist or can not be loaded, put a default value in loaded variable self.SendMessageToConfigurator(self.WARNING, 'ERROR:\tContador initialization failed : using default empty structure') for acc in self.accumulations: #self.lastAccum[acc] = lastRecollectionDatetime punteroInicial=datetime.datetime.now()-datetime.timedelta(days=0) Last Modified On: October 22, 2018