Add monitoring for COC file generation and

transmission.

Commit to save progress.

Commit to save progress.

Finished ListDirectory service with full 'mls'
command functionality.

Final commit for COC availability checks.

Implement further changes requested by Daniel.
Add notifications to critical statuses and automatic status clearing.

Change Mona resource from dev to prod.
This commit is contained in:
Infineon\Mitchem
2024-11-04 15:49:55 -07:00
parent 8716caf86c
commit 8dce7988c6
8 changed files with 590 additions and 87 deletions

View File

@ -21,6 +21,7 @@ Function FTP_Services(@Service, @Params)
History : (Date, Initials, Notes)
07/10/18 dmb Original programmer.
11/13/24 djm Add ListDirectory service.
***********************************************************************************************************************/
@ -31,9 +32,10 @@ $insert SERVICE_SETUP
$insert FTP_REQUESTS_EQUATES
$insert NOTIFICATION_EQUATES
Equ CRLF$ to \0D0A\
Equ Tab$ to Char(9)
Declare function FTP_Services, Memory_Services, Logging_Services, SRP_Path, SRP_Decode, RTI_CreateGUID
Declare function Environment_Services, Database_Services, Utility
Declare function Environment_Services, Database_Services, Utility, SRP_Run_Command
Declare subroutine FTP_Services, Memory_Services, Logging_Services, SRP_Run_Command, GetTempPath, GetTempFileName
Declare subroutine Database_Services, RList, Obj_Notes
@ -84,7 +86,7 @@ Options SERVERS = 'MESSA005', 'MESSA01EC'
// Sends the indicated local file or encoded data to the indicated host.
//----------------------------------------------------------------------------------------------------------------------
Service SendFile(Host=HOSTS, LocalFile, EncodedData, Username, Password, LocalDirectory, RemoteDirectory, RemoteFile, ScriptPath, DeleteScript=BOOLEAN, SSH=BOOLEAN)
LogData = ''
LogData<1> = LoggingDTM
LogData<2> = 'Start ' : Service
@ -92,7 +94,7 @@ Service SendFile(Host=HOSTS, LocalFile, EncodedData, Username, Password, LocalDi
LogData<4> = LocalFile
LogData<5> = RemoteDirectory
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
If (Host NE '') AND ((LocalFile NE '') OR (EncodedData NE '')) then
If LocalFile EQ '' then
// Data to send to the FTP server is coming in as Base64 string rather than a path to a local file. Decode the
@ -109,7 +111,7 @@ Service SendFile(Host=HOSTS, LocalFile, EncodedData, Username, Password, LocalDi
end else
Error_Services('Add', 'Host, LocalFile, or EncodedData argument was missing from the ' : Service : ' service.')
end
ServiceError = Error_Services('GetMessage')
LogData<1> = LoggingDTM
LogData<2> = 'Stop ' : Service
@ -119,7 +121,7 @@ Service SendFile(Host=HOSTS, LocalFile, EncodedData, Username, Password, LocalDi
LogData<6> = Error_Services('GetMessage')
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
Error_Services('Set', ServiceError)
end service
@ -141,7 +143,7 @@ end service
// Appends the indicated local file or encoded data to the indicated host.
//----------------------------------------------------------------------------------------------------------------------
Service AppendFile(Host=HOSTS, LocalFile, EncodedData, Username, Password, LocalDirectory, RemoteDirectory, RemoteFile, ScriptPath, DeleteScript=BOOLEAN, SSH=BOOLEAN)
LogData = ''
LogData<1> = LoggingDTM
LogData<2> = 'Start ' : Service
@ -149,7 +151,7 @@ Service AppendFile(Host=HOSTS, LocalFile, EncodedData, Username, Password, Local
LogData<4> = LocalFile
LogData<5> = RemoteDirectory
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
If (Host NE '') AND ((LocalFile NE '') OR (EncodedData NE '')) then
If LocalFile EQ '' then
// Data to send to the FTP server is coming in as Base64 string rather than a path to a local file. Decode the
@ -201,7 +203,7 @@ Service AppendFile(Host=HOSTS, LocalFile, EncodedData, Username, Password, Local
end else
Error_Services('Add', 'Host, LocalFile, or EncodedData argument was missing from the ' : Service : ' service.')
end
ServiceError = Error_Services('GetMessage')
LogData<1> = LoggingDTM
LogData<2> = 'Stop ' : Service
@ -211,7 +213,7 @@ Service AppendFile(Host=HOSTS, LocalFile, EncodedData, Username, Password, Local
LogData<6> = Error_Services('GetMessage')
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
Error_Services('Set', ServiceError)
end service
@ -232,7 +234,7 @@ end service
// Gets the indicated remote file from the indicated host.
//----------------------------------------------------------------------------------------------------------------------
Service GetFile(Host=HOSTS, RemoteFile, LocalFile, Username, Password, LocalDirectory, RemoteDirectory, ScriptPath, DeleteScript=BOOLEAN, SSH=BOOLEAN)
LogData = ''
LogData<1> = LoggingDTM
LogData<2> = 'Start ' : Service
@ -240,7 +242,7 @@ Service GetFile(Host=HOSTS, RemoteFile, LocalFile, Username, Password, LocalDire
LogData<4> = LocalFile
LogData<5> = RemoteDirectory
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
If (Host NE '') AND (RemoteFile NE '') then
ScriptPath = FTP_Services('CreateScript', ScriptPath, Host, LocalFile, Username, Password, LocalDirectory, RemoteDirectory, RemoteFile, 'get', SSH)
If Error_Services('NoError') then
@ -249,7 +251,7 @@ Service GetFile(Host=HOSTS, RemoteFile, LocalFile, Username, Password, LocalDire
end else
Error_Services('Add', 'Host or RemoteFile argument was missing from the ' : Service : ' service.')
end
ServiceError = Error_Services('GetMessage')
LogData<1> = LoggingDTM
LogData<2> = 'Stop ' : Service
@ -259,7 +261,7 @@ Service GetFile(Host=HOSTS, RemoteFile, LocalFile, Username, Password, LocalDire
LogData<6> = Error_Services('GetMessage')
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
Error_Services('Set', ServiceError)
end service
@ -278,7 +280,7 @@ end service
// Deletes the indicated remote file from the indicated host.
//----------------------------------------------------------------------------------------------------------------------
Service DeleteFile(Host=HOSTS, RemoteFile, Username, Password, RemoteDirectory, ScriptPath, DeleteScript=BOOLEAN, SSH=BOOLEAN)
LogData = ''
LogData<1> = LoggingDTM
LogData<2> = 'Start ' : Service
@ -286,7 +288,7 @@ Service DeleteFile(Host=HOSTS, RemoteFile, Username, Password, RemoteDirectory,
LogData<4> = RemoteFile
LogData<5> = RemoteDirectory
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
If (Host NE '') AND (RemoteFile NE '') then
ScriptPath = FTP_Services('CreateScript', ScriptPath, Host, '', Username, Password, '', RemoteDirectory, RemoteFile, 'delete', SSH)
If Error_Services('NoError') then
@ -295,7 +297,7 @@ Service DeleteFile(Host=HOSTS, RemoteFile, Username, Password, RemoteDirectory,
end else
Error_Services('Add', 'Host or RemoteFile argument was missing from the ' : Service : ' service.')
end
ServiceError = Error_Services('GetMessage')
LogData<1> = LoggingDTM
LogData<2> = 'Stop ' : Service
@ -305,7 +307,7 @@ Service DeleteFile(Host=HOSTS, RemoteFile, Username, Password, RemoteDirectory,
LogData<6> = Error_Services('GetMessage')
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
Error_Services('Set', ServiceError)
end service
@ -326,10 +328,10 @@ end service
// Runs the indicated FTP script.
//----------------------------------------------------------------------------------------------------------------------
Service RunScript(ScriptPath, Host, Username, Password, LocalFile, RemoteDirectory, DeleteScript, SSH)
If ScriptPath NE '' then
Output = 'VAR' ; // Use when running in production.
* Output = 'DOSOPEN' ; // Use only for debugging the console.
* Output = 'DOSOPEN' ; // Use only for debugging the console.
If SSH EQ True$ then
Command = 'psftp'
If Username NE '' then
@ -371,7 +373,7 @@ Service RunScript(ScriptPath, Host, Username, Password, LocalFile, RemoteDirecto
end else
Error_Services('Add', 'ScriptPath argument was missing from the ' : Service : ' service.')
end
end service
@ -392,10 +394,10 @@ end service
// Creates a script file based on the indicated arguments. This will be writen to the resulting script path.
//----------------------------------------------------------------------------------------------------------------------
Service CreateScript(ScriptPath, Host, LocalFile, Username, Password, LocalDirectory, RemoteDirectory, RemoteFile, Command=COMMANDS, SSH)
// First, create the script list of commands based on the indicated arguments.
Script = ''
If SSH EQ True$ then
// PSFTP requires credentials to be entered in the command line rather than in the script.
end else
@ -439,11 +441,17 @@ Service CreateScript(ScriptPath, Host, LocalFile, Username, Password, LocalDirec
end else
Script := command : ' ' : RemoteFile
end
Case Command _EQC 'list'
Script := 'ls '
If RemoteFile NE '' then
Script := ' ' : RemoteFile
end
End Case
Script := @FM
Script := 'quit'
Swap @FM with CRLF$ in Script
// Second, validate the script path. Use the specific directory and/or filename provided in the ScriptPath argument
// if possible. Otherwise use temp names.
ScriptDirectory = ''
@ -476,16 +484,16 @@ Service CreateScript(ScriptPath, Host, LocalFile, Username, Password, LocalDirec
end else
ScriptPath = SRP_Path('Combine', ScriptDirectory, ScriptFile)
end
// Finally, create the script file in the indicated ScriptPath.
Status() = 0
OSWrite Script to ScriptPath
If Status() then
Error_Services('Add', 'Error creating ' : ScriptPath : ' in the ' : Service : ' service. Description: ' : Field('Bad OS filename.,Access denied by operating system.,Disk or directory full.,File does not exist.,Unknown error.,Attempt to write to a read-only file.', ',', Status()))
end
Response = ScriptPath
end service
@ -497,9 +505,9 @@ end service
// Converts Base64 encoded data to plaint text and writes to a temporary file.
//----------------------------------------------------------------------------------------------------------------------
Service ConvertEncodedData(EncodedData)
LocalFile = ''
If EncodedData NE '' then
Data = SRP_Decode(EncodedData, 'BASE64')
If Data NE '' then
@ -518,9 +526,9 @@ Service ConvertEncodedData(EncodedData)
end else
Error_Services('Add', 'EncodedData argument was missing from the ' : Service : ' service.')
end
Response = LocalFile
end service
@ -530,17 +538,17 @@ end service
// Returns the path to a newly created temporary file.
//----------------------------------------------------------------------------------------------------------------------
Service GetTempFile()
TempFile = ''
TempDirectory = Str(\00\, 1024)
GetTempPath(Len(TempDirectory), TempDirectory)
Convert \00\ to '' in TempDirectory
TempFile = Str(\00\, 1024)
GetTempFileName(TempDirectory : \00\, \00\, 0, TempFile)
Response = TempFile
end service
@ -569,7 +577,7 @@ end service
// Posts the FTP request into the FTP_REQUESTS database table for future processing.
//----------------------------------------------------------------------------------------------------------------------
Service PostRequest(Command=COMMANDS, Host=HOSTS, Server=SERVERS, LocalFile, EncodedData, Username, Password, LocalDirectory, RemoteDirectory, RemoteFile, ScriptPath, DeleteScript=BOOLEAN, SSH=BOOLEAN, ProcessDirectory, NumberAttempts, DeleteLocalFile)
LogData = ''
LogData<1> = LoggingDTM
LogData<2> = 'Start ' : Service
@ -577,9 +585,9 @@ Service PostRequest(Command=COMMANDS, Host=HOSTS, Server=SERVERS, LocalFile, Enc
LogData<4> = Command : ' ' : LocalFile
LogData<5> = RemoteDirectory
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
FTPRequestID = ''
If (Command NE '') AND (Host NE '') AND (Server NE '') AND ((LocalFile NE '') OR (EncodedData NE '')) then
RequestDate = Date()
RequestTime = Time()
@ -632,7 +640,7 @@ Service PostRequest(Command=COMMANDS, Host=HOSTS, Server=SERVERS, LocalFile, Enc
end else
Error_Services('Add', 'Command, Host, Server, LocalFile, or EncodedData argument was missing from the ' : Service : ' service.')
end
ServiceError = Error_Services('GetMessage')
LogData<1> = LoggingDTM
LogData<2> = 'Stop ' : Service
@ -642,9 +650,9 @@ Service PostRequest(Command=COMMANDS, Host=HOSTS, Server=SERVERS, LocalFile, Enc
LogData<6> = Error_Services('GetMessage')
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
Error_Services('Set', ServiceError)
Response = FTPRequestID
end service
@ -656,12 +664,12 @@ end service
// Processes the FTP request specified by the FTPRequestID.
//----------------------------------------------------------------------------------------------------------------------
Service ProcessRequest(FTPRequestID)
If (FTPRequestID NE '') then
FTPRequestRow = Database_Services('ReadDataRow', 'FTP_REQUESTS', FTPRequestID)
If Error_Services('NoError') then
Server = FTPRequestRow<FTP_REQUESTS.SERVER$>
* If Server EQ 'MESSA01EC' THEN Debug
* If Server EQ 'MESSA01EC' THEN Debug
ThisServer = Environment_Services('GetServer')
If Server _EQC ThisServer then
// This is the designated server to process this request.
@ -672,7 +680,7 @@ Service ProcessRequest(FTPRequestID)
LogData<4> = FTPRequestID
LogData<5> = ''
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
RequestDate = FTPRequestRow<FTP_REQUESTS.REQUEST_DATE$>
RequestTime = FTPRequestRow<FTP_REQUESTS.REQUEST_TIME$>
Command = FTPRequestRow<FTP_REQUESTS.FTP_COMMAND$>
@ -699,7 +707,7 @@ Service ProcessRequest(FTPRequestID)
Case Command _EQC 'delete'
FTP_Services('DeleteFile', Host, RemoteFile, Username, Password, RemoteDirectory, ScriptPath, DeleteScript, SSH)
End Case
If Error_Services('NoError') then
Result = 'Success'
If (ProcessDirectory NE '') AND (LocalFile NE '') then
@ -727,7 +735,7 @@ Service ProcessRequest(FTPRequestID)
MessageParms = Recipients:@RM:SendFrom:@RM:Subject:@RM:Result:@RM:AttachWindow:@RM:AttachKey:@RM:SendToGroup
obj_Notes('Create',MessageParms)
end
// Update the FTP request with the latest attempt details.
AttemptCount = FTPRequestRow<FTP_REQUESTS.ATTEMPT_COUNT$>
AttemptCount += 1
@ -736,7 +744,7 @@ Service ProcessRequest(FTPRequestID)
FTPRequestRow<FTP_REQUESTS.ATTEMPT_TIME$, AttemptCount> = Time()
FTPRequestRow<FTP_REQUESTS.RESULT$, AttemptCount> = Result
Database_Services('WriteDataRow', 'FTP_REQUESTS', FTPRequestID, FTPRequestRow, True$, False$, True$)
LogData<1> = LoggingDTM
LogData<2> = 'Stop ' : Service
LogData<3> = ''
@ -750,7 +758,7 @@ Service ProcessRequest(FTPRequestID)
end else
Error_Services('Add', 'FTPRequestID argument was missing from the ' : Service : ' service.')
end
end service
@ -781,19 +789,110 @@ Service ProcessRequests()
ClearSelect 0
Unlock hSysLists, ServiceKeyID else Null
end
end service
//----------------------------------------------------------------------------------------------------------------------
// ListDirectory
//
// Host - FTP server name or IP. - [Required]
// Query - Specify wildcard query. - [Optional]
// Username - Username to authenticate against the FTP server. - [Optional]
// Password - Password to authenticate against the FTP server. - [Optional]
// RemoteDirectory - Required working directory of the remote computer. Used with the "cd' FTP command. - [Optional]
// ScriptPath - Local directory and/or file name to use for the script file. - [Optional]
// DeleteScript - Boolean flag to indicate if the script file should be deleted after being ran. - [Optional]
// SSH - Boolean flag to indicate if SSH (Secure FTP) is required. Default is False. - [Optional]
// OutputPath - Path to output file list to file. - [Optional]
//
// Gets the indicated remote file from the indicated host. Functions as 'mls' command.
//----------------------------------------------------------------------------------------------------------------------
Service ListDirectory(Host=HOSTS, Username, Password, RemoteDirectory, Query, ScriptPath, DeleteScript=BOOLEAN, SSH=BOOLEAN, OutputPath)
LogData = ''
LogData<1> = LoggingDTM
LogData<2> = 'Start ' : Service
LogData<3> = Host
LogData<4> = ''
LogData<5> = RemoteDirectory
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
If (Host NE '') AND (RemoteDirectory NE '') then
ScriptPath = FTP_Services('CreateScript', ScriptPath, Host, '', Username, Password, '', RemoteDirectory, Query, 'list', SSH)
If Error_Services('NoError') then
//FTP_Services('RunScript', ScriptPath, Host, Username, Password, '', RemoteDirectory, DeleteScript, SSH)
If SSH EQ True$ then
Command = 'psftp'
If Username NE '' then
If Host NE '' then Command := ' ' : Username : '@' : Host
end else
If Host NE '' then Command := ' ' : Host
end
If Password NE '' then
Command := ' -pw ' : Password
end
Command := ' -b ' : ScriptPath
end else
Command = 'ftp -i -n -s:' : ScriptPath
end
//Command = 'psftp ' : ScriptPath
DirectoryList = SRP_Run_Command(Command, 'VAR')
Gosub LsToMls
Response = DirectoryList
If OutputPath NE '' then
OutputDirectoryList = DirectoryList
Swap @FM with CRLF$ in OutputDirectoryList
OSWrite OutputDirectoryList to OutputPath
end
end
end else
Error_Services('Add', 'Host or RemoteFile argument was missing from the ' : Service : ' service.')
end
If DeleteScript EQ True$ then
OSDelete ScriptPath
end
ServiceError = Error_Services('GetMessage')
LogData<1> = LoggingDTM
LogData<2> = 'Stop ' : Service
LogData<3> = Host
LogData<4> = ''
LogData<5> = RemoteDirectory
LogData<6> = Error_Services('GetMessage')
Logging_Services('AppendLog', objLog, LogData, @RM, @FM)
Error_Services('Set', ServiceError)
end service
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------------------------------------------------------
// GoSubLabel
// LsToMls
//
// Parse output of ls command to remove directory paths and other unneccessary metadata. Functions as mls command.
//----------------------------------------------------------------------------------------------------------------------
GoSubLabel:
LsToMls:
OldDirectoryList = DirectoryList
DirectoryList = ''
// Skip the first period because it is related to username.
FileCount = Count(OldDirectoryList, '.') - 1
// Index starts at 5 because that CRLF$ Field is the first containing a file name.
FieldIndex = 5
For FileIndex = 1 to FileCount
FileName = Field(OldDirectoryList, CRLF$, FieldIndex)
SpaceCount = DCount(FileName, ' ')
FileName = Field(FileName, ' ', SpaceCount)
DirectoryList<-1> = FileName
FieldIndex += 1
Next FileIndex
return