Compile subroutine MONA_MONITORS(@Service, @Params)
#pragma precomp SRP_PreCompiler
/*****************************************************************************\
	This is a utility function. It is a container for auditing general OI metrics
	and then reporting them into MonA directly.
	
	History
	-------
	04/07/2021    JRO    Original Programmer
\*****************************************************************************/

Declare subroutine RList, Mona_Services, Error_Services
Declare function Database_Services, SRP_Datetime, Environment_Services, File_Services

$Insert SERVICE_SETUP
$Insert REVDOTNETEQUATES
$Insert LOGICAL
$Insert RLIST_EQUATES

GoToService else
	Error_Services('Add', Service : ' is not a valid service request within the ' : ServiceModule : ' services module.')
end

Return Response else ''


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Services
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Service RunMonitors()

    hSysLists   = Database_Services('GetTableHandle', 'SYSLISTS')
    Lock hSysLists, ServiceKeyID then
        
        Query      = 'SELECT SQL_BACKLOG'
        RList(Query, TARGET_ACTIVELIST$, '', '', '')
        QueueCount = @RecCount
        GoSub ClearCursors
        Mona_Services('PostAverageMetric', 'SQL_BACKLOG', 'SQL_BACKLOG', QueueCount)
        
        Query      = 'SELECT SQL_PENDING'
        RList(Query, TARGET_ACTIVELIST$, '', '', '')
        QueueCount = @RecCount
        GoSub ClearCursors
        Mona_Services('PostAverageMetric', 'SQL_PENDING', 'SQL_PENDING', QueueCount)
        
        Query      = 'SELECT PROC_QUEUE2'
        RList(Query, TARGET_ACTIVELIST$, '', '', '')
        QueueCount = @RecCount
        GoSub ClearCursors
        Mona_Services('PostAverageMetric', 'GRP_OPENINSIGHT_MES_OP_FE_SERVICE_MANAGER', 'PROC_QUEUE', QueueCount)
        
    	Query      = 'SELECT TRANSACTION_QUEUE'
        RList(Query, TARGET_ACTIVELIST$, '', '', '')
        QueueCount = @RecCount
        GoSub ClearCursors
        Mona_Services('PostAverageMetric', 'GRP_OPENINSIGHT_MES_OP_FE_SERVICE_MANAGER', 'TRANSACTION_QUEUE', QueueCount)
        
    	Query      = 'SELECT MONA_QUEUE'
        RList(Query, TARGET_ACTIVELIST$, '', '', '')
        QueueCount = @RecCount
        GoSub ClearCursors
        Mona_Services('PostAverageMetric', 'GRP_OPENINSIGHT_MES_OP_FE_SERVICE_MANAGER', 'MONA_QUEUE', QueueCount)        
        
        GoSub SAPHeartbeat
		GoSub MonitorMetrologyDirs
		
        Unlock hSysLists, ServiceKeyID else Null
    end
	    
end service

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

SAPHeartbeat:
	
	SAPHeartbeatDTM = Database_Services('ReadDataRow', 'APP_INFO', 'SAP_HEARTBEAT_DTM')
	CurrDTM = SRP_Datetime('Now')
	If SAPHeartbeatDTM NE '' then
		If SRP_Datetime('MinuteSpan', SAPHeartbeatDTM, CurrDTM) LT 5 then
			Mona_Services('PostStatus', 'GRP_OPENINSIGHT_MES_OP_FE_SAP', 'Heartbeat', 'OK')
		end else
			Mona_Services('PostStatus', 'GRP_OPENINSIGHT_MES_OP_FE_SAP', 'Heartbeat', 'CRITICAL')
		end
	end else
		Mona_Services('PostStatus', 'GRP_OPENINSIGHT_MES_OP_FE_SAP', 'Heartbeat', 'WARNING')
	end

return


MonitorMetrologyDirs:
	
	CurrDTM = SRP_Datetime('Now')
	//Tencor
	TencorInstanceName = 'MET08DDUPSFS6420'
	TencorDir = Environment_Services('GetApplicationRootPath') : '\Metrology\':TencorInstanceName:'\Source\' : TencorInstanceName
	TencorFileCount = File_Services('GetDirFileCount', TencorDir)
	TencorOldestFileDTM = File_Services('GetDirOldestFileDate', TencorDir)
	If TencorOldestFileDTM EQ '' then
		TencorOldestFileAgeMinute = 0
	end else
		TencorOldestFileAgeMinute = SRP_Datetime('MinuteSpan', TencorOldestFileDTM, CurrDTM)		
	end
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_COUNT', TencorInstanceName, TencorFileCount)
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_AGE', TencorInstanceName, TencorOldestFileAgeMinute)
	
	//HGCV
	HGCVInstanceName = 'MET08RESIHGCV'
	HGCVDir = Environment_Services('GetApplicationRootPath') : '\Metrology\':HGCVInstanceName:'\Source\' : HGCVInstanceName
	HGCVFileCount = File_Services('GetDirFileCount', HGCVDir)
	HGCVOldestFileDTM = File_Services('GetDirOldestFileDate', HGCVDir)
	If HGCVOldestFileDTM EQ '' then
		HGCVOldestFileAgeMinute = 0
	end else
		HGCVOldestFileAgeMinute = SRP_Datetime('MinuteSpan', HGCVOldestFileDTM, CurrDTM)		
	end
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_COUNT', HGCVInstanceName, HGCVFileCount)
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_AGE', HGCVInstanceName, HGCVOldestFileAgeMinute)
	
	//CDE
	CDEInstanceName = 'MET08RESIMAPCDE'
	CDEDir = Environment_Services('GetApplicationRootPath') : '\Metrology\':CDEInstanceName:'\Source\' : CDEInstanceName
	CDEFileCount = File_Services('GetDirFileCount', CDEDir)
	CDEOldestFileDTM = File_Services('GetDirOldestFileDate', CDEDir)
	If CDEOldestFileDTM EQ '' then
		CDEOldestFileAgeMinute = 0
	end else
		CDEOldestFileAgeMinute = SRP_Datetime('MinuteSpan', CDEOldestFileDTM, CurrDTM)		
	end
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_COUNT', CDEInstanceName, CDEFileCount)
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_AGE', CDEInstanceName, CDEOldestFileAgeMinute)
	
	//BioRad
	BioRadInstanceName = 'MET08THFTIRQS408M'
	BioRadDir = Environment_Services('GetApplicationRootPath') : '\Metrology\':BioRadInstanceName:'\Source\' : BioRadInstanceName
	BioRadFileCount = File_Services('GetDirFileCount', BioRadDir)
	BioRadOldestFileDTM = File_Services('GetDirOldestFileDate', BioRadDir)
	If BioRadOldestFileDTM EQ '' then
		BioRadOldestFileAgeMinute = 0
	end else
		BioRadOldestFileAgeMinute = SRP_Datetime('MinuteSpan', BioRadOldestFileDTM, CurrDTM)		
	end
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_COUNT', BioRadInstanceName, BioRadFileCount)
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_AGE', BioRadInstanceName, BioRadOldestFileAgeMinute)
	
	//Stratus
	StratusInstanceName = 'MET08THFTIRSTRATUS'
	StratusDir = Environment_Services('GetApplicationRootPath') : '\Metrology\':StratusInstanceName:'\Source\' : StratusInstanceName
	StratusFileCount = File_Services('GetDirFileCount', StratusDir)
	StratusOldestFileDTM = File_Services('GetDirOldestFileDate', StratusDir)
	If StratusOldestFileDTM EQ '' then
		StratusOldestFileAgeMinute = 0
	end else
		StratusOldestFileAgeMinute = SRP_Datetime('MinuteSpan', StratusOldestFileDTM, CurrDTM)		
	end
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_COUNT', StratusInstanceName, StratusFileCount)
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_AGE', StratusInstanceName, StratusOldestFileAgeMinute)
	
	//SP1
	SP1InstanceName = 'MET08DDUPSP1TBI'
	SP1Dir = Environment_Services('GetApplicationRootPath') : '\Metrology\':SP1InstanceName:'\Source\' : SP1InstanceName
	SP1FileCount = File_Services('GetDirFileCount', SP1Dir)
	SP1OldestFileDTM = File_Services('GetDirOldestFileDate', SP1Dir)
	If SP1OldestFileDTM EQ '' then
		SP1OldestFileAgeMinute = 0
	end else
		SP1OldestFileAgeMinute = SRP_Datetime('MinuteSpan', SP1OldestFileDTM, CurrDTM)		
	end
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_COUNT', SP1InstanceName, SP1FileCount)
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_AGE', SP1InstanceName, SP1OldestFileAgeMinute)
	
	//SPV
	SPVInstanceName = 'MET08RESISRP2100'
	SPVDir = Environment_Services('GetApplicationRootPath') : '\Metrology\':SPVInstanceName:'\Source\' : SPVInstanceName
	SPVFileCount = File_Services('GetDirFileCount', SPVDir)
	SPVOldestFileDTM = File_Services('GetDirOldestFileDate', SPVDir)
	If SPVOldestFileDTM EQ '' then
		SPVOldestFileAgeMinute = 0
	end else
		SPVOldestFileAgeMinute = SRP_Datetime('MinuteSpan', SPVOldestFileDTM, CurrDTM)		
	end
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_COUNT', SPVInstanceName, SPVFileCount)
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_AGE', SPVInstanceName, SPVOldestFileAgeMinute)
	
	//SRP
	SRPInstanceName = 'MET08ANLYSDIFAAST230'
	SRPDir = Environment_Services('GetApplicationRootPath') : '\Metrology\':SRPInstanceName:'\Source\' : SRPInstanceName
	SRPFileCount = File_Services('GetDirFileCount', SRPDir)
	SRPOldestFileDTM = File_Services('GetDirOldestFileDate', SRPDir)
	If SRPOldestFileDTM EQ '' then
		SRPOldestFileAgeMinute = 0
	end else
		SRPOldestFileAgeMinute = SRP_Datetime('MinuteSpan', SRPOldestFileDTM, CurrDTM)		
	end
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_COUNT', SRPInstanceName, SRPFileCount)
	Mona_Services('PostAverageMetric', 'EXPORT_TO_OI_FILE_AGE', SRPInstanceName, SRPOldestFileAgeMinute)

return


ClearCursors:
	
	For counter = 0 to 8
		ClearSelect counter
	Next counter
	
return

