Writing Out An Indirect File for Targeted Compacting

Category
Dim s As NotesSession Dim db As NotesDatabase Dim dc As NotesDocumentCollection Dim localdoc As NotesDocument Dim stream As NotesStream Dim filename As String Dim catdb As NotesDatabase Dim catalog As Boolean Dim catview As NotesView Dim catdoc As NotesDocument Dim agentprodoc Dim dbsize As Double Dim dbpercentused As Double Dim dbservers As String Dim dbserveritem As NotesItem Dim runagent As String Dim compactTitle() As String Dim compactfilepath() As String Dim compactdbsize() As Double Dim compactdbpercentused() As Double Dim savings As Double Dim arraycounter As Integer Dim totalcounter As Double %REM NEXT RELEASE -- split into multiple ind files for running parallel compact tasks -- insert openlog for error trapping THIS AGENT IS ONLY SUPPORTED ON ND6.X AND UP REQUIRES RESTRICTED RIGHTS BECAUSE IT WILL BE WRITING TO THE FILE SYSTEM REQUIRES THE CATALOG BEING RUN DAILY --> CHECK WHEN IT IS OVER DON'T EVEN TRY TO DO THIS WITHOUT THE CATALOG UPDATED. YOU CAN GET THE DBSIZE FROM A DBDIRECTORY LIST, BUT NOT THE PERCENTAGE USED. FOR THAT YOU HAVE TO OPEN UP THE DATABASE, AND WITH THOUSANDS OF DATABASES, THE AGENT WILL LIKELY TIMEOUT (OR FAIL ON SOME BECAUSE OF AUTHENTICATION DENIALS) FUNCTIONS ARE DECLARED AS Boolean THEN VERIFIED WITH 0 (NO) OR -1(YES) AS THAT IS THE TECHNIQUE UNTIL ND7 THIS AGENT SCOURS THE CATALOG TO CREATE A LIST FOR NSFS TO COMPACT. THE LIST IS CHECKED BY A PROFILE DOCUMENT THAT READS IN THE MINIMUM SIZE AS WELL AS THE PERCENT OF USED SPACE. THE RESULTING LIST IS WRITTEN OUT AS AN INDIRECT FILE TO BE REFERENCED BY A COMPACT PROGRAM DOCUMENT INITIALIZE --> MAKE SURE IT IS SUPPOSED TO RUN ON CURRENT SERVER --> Setup the notesstream file to write and read in profile doc settings GETCATALOG --> If the catalog exists and can be opened, fine, if not then exit out. BUILDLIST ---> Rip through every document in the catalog and compare it to the profile doc values to populate arrays of the chosen RECORDNEWLIST ---> Store the results of the arrays into RTF for a report. It has to RTF, because if the sort returns thousands of entries, text fields will choke at the 32k limit. WRITEOUTCOMPACTREPORT ---> If the profile document has approved the creation of a file report, then it's done here. Works on Win/*nix %END REM Sub Initialize filename = "compactreport.ind" Set s = New NotesSession Set db = s.CurrentDatabase Set agentprodoc = s.CurrentDatabase.GetProfileDocument("AGENTPROPERTIES") Set stream = s.CreateStream dbsize =agentprodoc.dbsize(0) dbpercentused = agentprodoc.dbpercentused(0) 'Check the dbservers to see if this agent is on the right server to run Set dbserveritem = agentprodoc.GetFirstItem( "dbservers" ) Forall v In dbserveritem.Values If v = db.Server Then runagent = "YES" Exit Forall Else runagent = "NO" End If End Forall If runagent = "NO" Then Exit Sub End If If Not stream.Open(filename, "ASCII") Then Exit Sub End If If stream.IsReadOnly Then Print filename,, "File is read-only" 'NEED ANOTHER ERROR TRAP Exit Sub End If If getcatalog =0 Then Exit Sub End If Call buildlist If recordnewlist =0 Then Exit Sub End If If agentprodoc.runcompact(0) = "YES" Then Call writeoutcompactreport Else Print "it is not yes" End If Print "RAN OK" End Sub Function writeoutcompactreport As Boolean Call stream.Truncate Forall x In compactfilepath Call stream.WriteText(x) Call stream.WriteText(Chr(13) & Chr(10)) End Forall Call stream.Close writeoutcompactreport = True Print "wrote text" End Function Function getcatalog As Boolean Set catdb = s.GetDatabase(db.Server, "catalog.nsf") If Not db.IsOpen Then Print "Catalog fails" getcatalog = False Exit Function End If getcatalog = True End Function Function buildlist 'get the view Dim catcollection As NotesDocumentCollection Set catcollection = catdb.AllDocuments Set catdoc = catcollection.GetFirstDocument arraycounter = 0 Redim compactTitle(arraycounter) Redim compactfilepath(arraycounter) Redim compactdbsize(arraycounter) Redim compactdbpercentused(arraycounter) While Not (catdoc Is Nothing) If (catdoc.dbsize(0) >= dbsize) And (catdoc.dbpercentused(0) <= dbpercentused) Then 'build arrary for report compactTitle(arraycounter) = catdoc.Title(0) & ": " & catdoc.pathname(0) compactdbsize(arraycounter)=catdoc.dbsize(0) compactdbpercentused(arraycounter)=catdoc.dbpercentused(0) 'need pathname for the indirect file, not for the notes report compactfilepath(arraycounter)=catdoc.pathname(0) arraycounter=arraycounter +1 Redim Preserve compactTitle(arraycounter) Redim Preserve compactfilepath(arraycounter) Redim Preserve compactdbsize(arraycounter) Redim Preserve compactdbpercentused(arraycounter) Else 'not End If Set catdoc = catcollection.GetNextDocument( catdoc ) totalcounter = totalcounter + 1 Wend End Function Function recordnewlist As Boolean 'all the forall loopers are not predimmed, they'll be destroyed upon function exit 'arrays are too big to simply stuffed into text fields, they have to be looped into rtf Set localdoc = New NotesDocument (db ) Dim rtitemlisting As NotesRichTextItem Dim rtitemdbsize As NotesRichTextItem Dim rtitempercentused As NotesRichTextItem Set rtitemlisting = New NotesRichTextItem ( localdoc, "listing" ) Set rtitemdbsize = New NotesRichTextItem ( localdoc, "dbsize" ) Set rtitempercentused = New NotesRichTextItem ( localdoc, "dbpercentused" ) Forall x In compactTitle Call rtitemlisting.AppendText(x) Call rtitemlisting.AddNewline(1) End Forall Forall y In compactdbsize Call rtitemdbsize.AppendText(y) Call rtitemdbsize.AddNewline(1) End Forall Forall z In compactdbpercentused Call rtitempercentused.AppendText(z) Call rtitempercentused.AddNewline(1) End Forall localdoc.form="fcr" localdoc.totalcount = totalcounter localdoc.minsize = dbsize localdoc.maxpercent = dbpercentused localdoc.foundcount = arraycounter localdoc.server=db.Server Call localdoc.Save( True, True ) recordnewlist = True End Function