ADSM-L

[no subject]

2015-10-04 17:35:05
/*
**+
** Source Name:
**      logExpire.c
** Abstract:
**      API (3.1.07) interface to ADSM - used to expire Informix Log
**      which aren't normally expired by the ADSM
** Environment:
**      hp-ux 10.20 / AnsiC
** Public names:
**      int main
**      Synopsis:
**              logExpire <nodeName> <password> <filespace> <Y/N>
**         where:
**              nodeName:  node that owns the Informix filespace
**              password:  node password
**              filespace: filespace where informix is backed-up
**              Y/N:       whether to expire (Y) or not (N)
** Notes:
**      1) The program expires log files after the 'days' recorded in
**         the Management Class of the object (Retain extra versions).
**         This leads to a retention time which is doubled. But that's
**         ok for me!
**      2) It doesn't look for still ACTIVE logs ... it simply expires
**         them after the period declared in the MC.
**      3) The program can expires maximum <Max Transaction No> objects
**         in a single run.
** Author:
**      (MT) Mauro M. Tinelli
**      1.00    Original for STMicroelectronics                 - MT 24/ 8/1999
** Copyrights:
**      This program is provided "AS IS" WITHOUT WARRANTY OF ANY KIND,
**      EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO, THE
**      IMPLIED WARRANTY OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
**      PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF
**      THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU
**      ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
**-
*/

#include        <stdio.h>
#include        <time.h>

#include        <dsmapitd.h>
#include        <dsmrc.h>

#define NO      0
#define YES     1


typedef dsInt16_t  i16;
typedef dsUint16_t u16;
typedef dsUint32_t u32;

enum _e_ERRORS { Success=0, ERR_CantLogon, ERR_WrongParmsNo,
                 ERR_NoObjectFound, ERR_NoExpiring,ERR_WrongApiVer,
                 ERR_NoTransPerm, ERR_TxnAbort
               };
typedef enum _e_ERRORS Errors;

typedef struct S_qObjLog
{
    struct S_qObjLog *  Next;
    dsmObjName          objName;
    char                mcName[DSM_MAX_MC_NAME_LENGTH + 1];
    u32                 copyGroup;
    dsmDate             insDate;
    int                 isExpired;
} qObjLog;

qObjLog *qObjFirst = (qObjLog *)NULL;
qObjLog *qObjLast  = (qObjLog *)NULL;




/*      is the running Api Version correct?
 */
int _apiVersionCheck( void )
{
    dsmApiVersion       av;

    dsmQueryApiVersion( &av );
    return av.version == DSM_API_VERSION
        && av.release == DSM_API_RELEASE
        && av.level   == DSM_API_LEVEL;
}


/*      login into adsm server
 */
i16 _apiLogon( u32 *pHandle, char *pnName, char *pnPwd )
{
    dsmApiVersion       av = {DSM_API_VERSION,DSM_API_RELEASE,DSM_API_LEVEL};
    return dsmInit(
        pHandle,
        &av,
        pnName,(char *)NULL,pnPwd,"Mauro/API",(char *)NULL,(char *)NULL
        );
}


/*      I'm only interested in the Max Number of
 *      Object Transactions permitted
 */
u16 _apiGetMaxTxn( u32 Handle )
{
    ApiSessInfo api = {ApiSessInfoVersion};
    return (dsmQuerySessInfo(Handle, &api)==DSM_RC_OK ? api.maxObjPerTxn : 0);
}


/*      I'm getting the "expiration period" of the management class.
 *      The data received are stored in "static" structure so they act
 *      as a cache buffer to avoid further request to the ADSM-Server.
 *      return 0 for error, "NDAYS" for "expiration period"
 */
int _getExpireDays( u32 Handle,char *mcName )
{
    static int  rxv = 0;
    static char mcn[DSM_MAX_MC_NAME_LENGTH + 1] = "";

    if (strcmp(mcn,mcName) != 0)
    {
        qryRespMCDetailData     rmc = {qryRespMCDetailDataVersion};
        qryMCData               qmc = {qryMCDataVersion,(char *)NULL,YES};
        DataBlk                 blk = 
{DataBlkVersion,sizeof(qryRespMCDetailData),
                                        0,(char *)&rmc
                                      };
        qmc.mcName = mcName;
        if (dsmBeginQuery(Handle, qtMC,(dsmQueryBuff *)&qmc) == 0)
        {
            while (dsmGetNextQObj(Handle, &blk) == DSM_RC_MORE_DATA)
                if (strcmp(rmc.mcName,mcName) == 0)
                {
                    rxv = rmc.backupDet.retXtraVers;
                    strcpy(mcn,mcName);
                }
            dsmEndQuery(Handle);
        }
    }
    return rxv;
}


void _printInformixObj( qObjLog *pObj )
{
    dsmObjName  *pn = &pObj->objName;
    dsmDate     *pd = &pObj->insDate;
    printf("%s%s\n",pn->hl,pn->ll);
    printf(
        "Insertion Date:%2d.%02d.%4d %2d:%02d:%02d ",
        pd->day,pd->month,pd->year, pd->hour,pd->minute,pd->second
        );
    printf("Expired:%s\n",(pObj->isExpired ? "YES" : "NO"));
}

/*      is the file/log expired?
 */
int _expireInformixObj( u32 Handle )
{
    int         ndays,nobj = 0;
    qObjLog     *p;
    time_t      now = time((time_t)NULL);

    for (p=qObjFirst; p; p=p->Next)
        if ((ndays = _getExpireDays(Handle, p->mcName)) > 0)
        {
            time_t      etm = now - (60*60*24)*ndays;
            dsmDate     *pd = &p->insDate;
            time_t      itm;
            struct tm   tm;
            tm.tm_sec   = pd->second;
            tm.tm_min   = pd->minute;
            tm.tm_hour  = pd->hour;
            tm.tm_mday  = pd->day;
            tm.tm_mon   = pd->month - 1;
            tm.tm_year  = pd->year - 1900;
            tm.tm_wday  = 0;
            tm.tm_yday  = 0;
            tm.tm_isdst = 0;
            itm         = mktime(&tm);
            if (itm < etm)
            {
                p->isExpired = YES;
                nobj++;
            }
        }
    return nobj;
}


/*      Ok, now tell ADSM which files/logs should be considered
 *      Expired.
 */
int _updateAdsmDB( u32 Handle,int nMaxObj )
{
    qObjLog     *p;
    i16         rsn;

    dsmBeginTxn(Handle);
    for (p=qObjFirst; p; p=p->Next)
        if (p->isExpired && nMaxObj>0)
        {
            delBack     dbo     = {delBackVersion};
            dbo.objNameP        = &p->objName;
            dbo.copyGroup       = p->copyGroup;
            if (dsmDeleteObj(Handle, dtBackup,&dbo) == 0)
                --nMaxObj;
            else
                p->isExpired = NO;
            _printInformixObj(p);
        }
    dsmEndTxn(Handle, DSM_VOTE_COMMIT,&rsn);
    return (rsn==DSM_RC_OK ? DSM_RC_OK : ERR_TxnAbort);
}



/*      getting the ACTIVE objects with the relevant info
 */
int _getInformixObjDetail( u32 Handle, char *pFName )
{
    dsmObjName          obj = {"","*","/*",DSM_OBJ_ANY_TYPE};
    qryRespBackupData   rbd = {qryRespBackupDataVersion};
    qryBackupData       qbd = 
{qryBackupDataVersion,&obj,"",DSM_ACTIVE,{0,0,0,0,0,0}};
    DataBlk             blk = {DataBlkVersion,sizeof(qryRespBackupData),0,(char 
*)&rbd};

    strcpy(obj.fs,pFName);
    if (dsmBeginQuery(Handle, qtBackup,(dsmQueryBuff *)&qbd) == DSM_RC_OK)
    {
        qObjLog *p;

        while (dsmGetNextQObj(Handle, &blk) == DSM_RC_MORE_DATA)
            if (p = (qObjLog *)malloc(sizeof(qObjLog)))
            {
                                        /* save relevant info           */

                memcpy(&p->objName,&rbd.objName,sizeof(dsmObjName));
                memcpy(&p->insDate,&rbd.insDate,sizeof(dsmDate));
                strcpy(p->mcName,rbd.mcName);
                p->copyGroup    = rbd.copyGroup;
                p->Next         = (qObjLog *)NULL;

                                        /* Add to the queue             */
                if (qObjFirst)
                    qObjLast->Next = p;
                else
                    qObjFirst      = p;
                qObjLast        = p;
            }
        dsmEndQuery(Handle);
    }
    return (qObjFirst ? DSM_RC_OK : ERR_NoObjectFound);
}


/*      Error Handling
 */
int _errorCheck( Errors Error )
{
    int rv;

    switch (Error)
    {
    case ERR_WrongApiVer:
        fprintf(stderr,
            "This exec was built with Api Version %d.%d.%d NOT the current 
one\n",
            DSM_API_VERSION,DSM_API_RELEASE,DSM_API_LEVEL
            );
        rv = 2;
        break;

    case ERR_WrongParmsNo:
        fprintf(stderr,"Expecting: NodeName, Password, FileSpace, Y/N\n");
        rv = 2;
        break;

    case ERR_CantLogon:
        fprintf(stderr,"Sorry - Can't login!\n");
        rv = 2;
        break;

    case ERR_NoExpiring:
        fprintf(stderr,"No expiration's necessary\n");
        rv = 2;
        break;

    case ERR_NoObjectFound:
        fprintf(stderr,"No Object has been found or an error occurred!\n");
        rv = 2;
        break;

    case ERR_NoTransPerm:
        fprintf(stderr,"No Transactions are Permitted!\n");
        rv = 2;
        break;

    Success:
        rv = 0;
    }
    return rv;
}

int main(int ac, char *av[])
{
    i16         err;
    u16         tmax;
    u32         sesno;
    Errors      rv = Success;

    if (_apiVersionCheck())
        if (ac == 5)
            if (_apiLogon(&sesno, av[1],av[2]) == DSM_RC_OK)
            {
                if (tmax = _apiGetMaxTxn(sesno))
                    if (_getInformixObjDetail(sesno, av[3]) == DSM_RC_OK)
                        if (_expireInformixObj(sesno)>0 && *av[4]=='Y')
                        {
                            _updateAdsmDB(sesno,tmax);
                        }
                        else rv = ERR_NoExpiring;
                    else rv = ERR_NoObjectFound;
                else rv = ERR_NoTransPerm;
                dsmTerminate(sesno);
            }
            else rv = ERR_CantLogon;
        else rv = ERR_WrongParmsNo;
    else rv = ERR_WrongApiVer;

    return _errorCheck( rv );
}

--openmail-part-38647736-00000001--
=========================================================================
<Prev in Thread] Current Thread [Next in Thread>
  • [no subject], Unknown <=