AFS-Monitor

 view release on metacpan or  search on metacpan

src/Monitor.xs  view on Meta::CPAN

const char *const xs_version = "Monitor.xs (Major Version 0.2 $Rev: 609 $)";

extern char *error_message();
extern struct hostent *hostutil_GetHostByName();
extern char *hostutil_GetNameByINet();

/* error handling macros */

#define SETCODE(code) set_code(code)
#define FSSETCODE(code) {if (code == -1) set_code(errno); else set_code(code);}
#define BSETCODE(code, msg) bv_set_code(code, msg)
#define VSETCODE(code, msg) bv_set_code(code, msg)

static int32 raise_exception = 0;


static void
bv_set_code(code, msg)
   int32 code;
   const char *msg;
{
   SV *sv = perl_get_sv("AFS::CODE", TRUE);
   sv_setiv(sv, (IV) code);
   if (code == 0) {
      sv_setpv(sv, "");
   }
   else {
      if (raise_exception) {
         char buffer[1024];
         sprintf(buffer, "AFS exception: %s (%d)", msg, code);
         croak(buffer);
      }
      sv_setpv(sv, (char *)msg);
   }
   SvIOK_on(sv);
}

static void
set_code(code)
   int32 code;
{
   SV *sv = perl_get_sv("AFS::CODE", TRUE);
   sv_setiv(sv, (IV) code);
   if (code == 0) {
      sv_setpv(sv, "");
   }
   else {
      if (raise_exception) {
         char buffer[1024];
         sprintf(buffer, "AFS exception: %s (%d)", error_message(code), code);
         croak(buffer);
      }
      sv_setpv(sv, (char *)error_message(code));
   }
   SvIOK_on(sv);
}

/* end of error handling macros */


/* start of rxdebug helper functions */


/*
 * from src/rxdebug/rxdebug.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

static short
rxdebug_PortNumber(aport)
   register char *aport;
{
   register int tc;
   register short total;

   total = 0;
   while ((tc = (*aport++))) {
      if (tc < '0' || tc > '9')
         return -1; /* bad port number */
      total *= 10;
      total += tc - (int)'0';
   }
   return htons(total);
}


/*
 * from src/rxdebug/rxdebug.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

static short
rxdebug_PortName(char *aname)
{
   register struct servent *ts;
   ts = getservbyname(aname, (char *) NULL);
   if (!ts)
      return -1;
   return ts->s_port;   /* returns it in network byte order */
}


/*
 * replaces rx_PrintTheseStats() in original c code.
 * places stats in RXSTATS instead of printing them
 * from src/rx/rx.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

static void
myPrintTheseStats(HV *RXSTATS, struct rx_statistics *rxstats)
{
   HV *PACKETS;
   HV *TYPE;
   HV *TOTALRTT;
   HV *MINRTT;
   HV *MAXRTT;
   int i;
   int num_unused;

   hv_store(RXSTATS, "packetRequests", 14, newSViv(rxstats->packetRequests),
            0);

   hv_store(RXSTATS, "receivePktAllocFailures", 23,
            newSViv(rxstats->receivePktAllocFailures), 0);
   hv_store(RXSTATS, "receiveCbufPktAllocFailures", 27,
            newSViv(rxstats->receiveCbufPktAllocFailures), 0);
   hv_store(RXSTATS, "sendPktAllocFailures", 20,
            newSViv(rxstats->sendPktAllocFailures), 0);
   hv_store(RXSTATS, "sendCbufPktAllocFailures", 24,
            newSViv(rxstats->sendCbufPktAllocFailures), 0);
   hv_store(RXSTATS, "specialPktAllocFailures", 23,
            newSViv(rxstats->specialPktAllocFailures), 0);

   hv_store(RXSTATS, "socketGreedy", 12, newSViv(rxstats->socketGreedy), 0);
   hv_store(RXSTATS, "bogusPacketOnRead", 17,
            newSViv(rxstats->bogusPacketOnRead), 0);
   hv_store(RXSTATS, "bogusHost", 9, newSViv(rxstats->bogusHost), 0);
   hv_store(RXSTATS, "noPacketOnRead", 14, newSViv(rxstats->noPacketOnRead),
            0);
   hv_store(RXSTATS, "noPacketBuffersOnRead", 21,
            newSViv(rxstats->noPacketBuffersOnRead), 0);
   hv_store(RXSTATS, "selects", 7, newSViv(rxstats->selects), 0);
   hv_store(RXSTATS, "sendSelects", 11, newSViv(rxstats->sendSelects), 0);


   PACKETS = newHV();
   num_unused = 0;

   for (i = 0; i < RX_N_PACKET_TYPES; i++) {
      char *packet_type = rx_packetTypes[i];
      TYPE = newHV();
      hv_store(TYPE, "packetsRead", 11, newSViv(rxstats->packetsRead[i]), 0);

src/Monitor.xs  view on Meta::CPAN

   hv_store(RXSTATS, "dataPacketsRead", 15, newSViv(rxstats->dataPacketsRead),
            0);
   hv_store(RXSTATS, "ackPacketsRead", 14, newSViv(rxstats->ackPacketsRead),
            0);
   hv_store(RXSTATS, "dupPacketsRead", 14, newSViv(rxstats->dupPacketsRead),
            0);
   hv_store(RXSTATS, "spuriousPacketsRead", 19,
            newSViv(rxstats->spuriousPacketsRead), 0);
   hv_store(RXSTATS, "ignorePacketDally", 17,
            newSViv(rxstats->ignorePacketDally), 0);

   hv_store(RXSTATS, "pingPacketsSent", 15, newSViv(rxstats->pingPacketsSent),
            0);
   hv_store(RXSTATS, "abortPacketsSent", 16,
            newSViv(rxstats->abortPacketsSent), 0);
   hv_store(RXSTATS, "busyPacketsSent", 15, newSViv(rxstats->busyPacketsSent),
            0);

   hv_store(RXSTATS, "ackPacketsSent", 14, newSViv(rxstats->ackPacketsSent),
            0);
   hv_store(RXSTATS, "dataPacketsSent", 15, newSViv(rxstats->dataPacketsSent),
            0);
   hv_store(RXSTATS, "dataPacketsReSent", 17,
            newSViv(rxstats->dataPacketsReSent), 0);
   hv_store(RXSTATS, "dataPacketsPushed", 17,
            newSViv(rxstats->dataPacketsPushed), 0);
   hv_store(RXSTATS, "ignoreAckedPacket", 17,
            newSViv(rxstats->ignoreAckedPacket), 0);

   hv_store(RXSTATS, "netSendFailures", 15, newSViv(rxstats->netSendFailures),
            0);
   hv_store(RXSTATS, "fatalErrors", 11, newSViv(rxstats->fatalErrors), 0);

   hv_store(RXSTATS, "nServerConns", 12, newSViv(rxstats->nServerConns), 0);
   hv_store(RXSTATS, "nClientConns", 12, newSViv(rxstats->nClientConns), 0);
   hv_store(RXSTATS, "nPeerStructs", 12, newSViv(rxstats->nPeerStructs), 0);
   hv_store(RXSTATS, "nCallStructs", 12, newSViv(rxstats->nCallStructs), 0);
   hv_store(RXSTATS, "nFreeCallStructs", 16,
            newSViv(rxstats->nFreeCallStructs), 0);

   hv_store(RXSTATS, "nRttSamples", 11, newSViv(rxstats->nRttSamples), 0);

   TOTALRTT = newHV();
   hv_store(TOTALRTT, "sec", 3, newSViv(rxstats->totalRtt.sec), 0);
   hv_store(TOTALRTT, "usec", 4, newSViv(rxstats->totalRtt.usec), 0);
   hv_store(RXSTATS, "totalRtt", 8, newRV_inc((SV *) (TOTALRTT)), 0);
   MINRTT = newHV();
   hv_store(MINRTT, "sec", 3, newSViv(rxstats->minRtt.sec), 0);
   hv_store(MINRTT, "usec", 4, newSViv(rxstats->minRtt.usec), 0);
   hv_store(RXSTATS, "minRtt", 6, newRV_inc((SV *) (MINRTT)), 0);
   MAXRTT = newHV();
   hv_store(MAXRTT, "sec", 3, newSViv(rxstats->maxRtt.sec), 0);
   hv_store(MAXRTT, "usec", 4, newSViv(rxstats->maxRtt.usec), 0);
   hv_store(RXSTATS, "maxRtt", 6, newRV_inc((SV *) (MAXRTT)), 0);

#if !defined(AFS_PTHREAD_ENV) && !defined(AFS_USE_GETTIMEOFDAY)
   hv_store(RXSTATS, "clock_nUpdates", 14, newSViv(clock_nUpdates), 0);
#endif
}

/* end of rxdebug helper functions */





/* start of afsmonitor helper functions */

/*
 * from src/afsmonitor/afsmonitor.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

#define FS 1    /* for misc. use */
#define CM 2    /* for misc. use */
#define CFG_STR_LEN 80  /* max length of config file fields */
#define NUM_XSTAT_FS_AFS_PERFSTATS_LONGS 66 /* number of fields (longs)
                                             * in struct afs_PerfStats */
#define NUM_AFS_STATS_CMPERF_LONGS 40   /* number of longs in struct afs_stats_CMPerf
                                         * excluding up/down stats and fields we dont display */


/*
 * from src/afsmonitor/afsmonitor.h
 *
 */

#define HOST_NAME_LEN 80    /* length of server/cm names */
#define THRESH_VAR_NAME_LEN 80  /* THRESHOLD STRUCTURE DEFINITIONS */
#define THRESH_VAR_LEN 16
#define NUM_FS_STAT_ENTRIES 271 /* number of file server statistics
                                 * entries to display */
#define FS_STAT_STRING_LEN 14   /* max length of each string above */
#define NUM_CM_STAT_ENTRIES 571 /* number of cache manager statistics
                                 * entries to display */
#define CM_STAT_STRING_LEN 14   /* max length of each string above */

#define FS_NUM_DATA_CATEGORIES 9    /* # of fs categories */
#define CM_NUM_DATA_CATEGORIES 16   /* # of cm categories */


/*
 * from src/xstat/xstat_fs.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 *

/*
 * We have to pass a port to Rx to start up our callback listener
 * service, but 7001 is already taken up by the Cache Manager.  So,
 * we make up our own.
 */
#define XSTAT_FS_CBPORT 7101


/*
 * from src/afsmonitor/afsmon-output.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

static char *fsOpNames[] = {
   "FetchData",

src/Monitor.xs  view on Meta::CPAN

 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

void
my_afsmon_cmOutput(a_outfile, a_detOutput, xstat_cm_Results)
   char *a_outfile;             /* ptr to output file name */
   int a_detOutput;             /* detailed output ? */
   struct xstat_cm_ProbeResults xstat_cm_Results;
{

   char *printTime = 0;         /* ptr to time string */
   char *hostname = 0;          /* fileserner name */
   afs_int32 numLongs = 0;      /* longwords in result */
   afs_int32 *currLong = 0;     /* ptr to longwords in result */
   int i = 0;
   FILE *cm_outFD = 0;

   cm_outFD = fopen(a_outfile, "a");
   if (cm_outFD == (FILE *) NULL) {
      warn("failed to open output file %s", a_outfile);
      return;
   }

   /* get the probe time and strip the \n at the end */
   printTime = ctime((time_t *) & (xstat_cm_Results.probeTime));
   printTime[strlen(printTime) - 1] = '\0';
   hostname = xstat_cm_Results.connP->hostName;

   /* print "time hostname CM" prefix */
   fprintf(cm_outFD, "\n%s %s CM ", printTime, hostname);

   /* if probe failed print -1 and vanish */
   if (xstat_cm_Results.probeOK) {
      fprintf(cm_outFD, "-1\n");
      fclose(cm_outFD);
      return;
   }

   /* print out the probe information as  long words */
   numLongs = xstat_cm_Results.data.AFSCB_CollData_len;
   currLong = (afs_int32 *) (xstat_cm_Results.data.AFSCB_CollData_val);

   for (i = 0; i < numLongs; i++) {
      fprintf(cm_outFD, "%d ", *currLong++);
   }
   fprintf(cm_outFD, "\n\n");

   /* print detailed information */
   if (a_detOutput) {
      my_Print_cm_FullPerfInfo(xstat_cm_Results, cm_outFD);
      fflush(cm_outFD);
   }

   fclose(cm_outFD);

}   /* my_afsmon_cmOutput() */



/*
 * unchanged except for removing debugging print statements at beginning, and one
 * correction (replacing xstat_cm_Results with a_cmResults)
 *
 * from src/afsmonitor/afsmonitor.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

int
my_cm_Results_ltoa(a_cmData, a_cmResults)
   struct cm_Display_Data *a_cmData;    /* target buffer */
   struct xstat_cm_ProbeResults *a_cmResults;   /* ptr to xstat cm Results */
{
   struct afs_stats_CMFullPerf *fullP;  /* ptr to complete CM stats */
   afs_int32 *srcbuf;
   afs_int32 *tmpbuf;
   int i, j;
   int idx;
   afs_int32 numLongs;


   fullP = (struct afs_stats_CMFullPerf *)
      (a_cmResults->data.AFSCB_CollData_val);

   /* There are 4 parts to CM statistics
    * - Overall performance statistics (including up/down statistics)
    * - This CMs FS RPC operations info
    * - This CMs FS RPC errors info
    * - This CMs FS transfers info
    * - Authentication info
    * - [Un]Replicated access info
    */


   /* copy overall performance statistics */
   srcbuf = (afs_int32 *) & (fullP->perf);
   idx = 0;


   /* we skip the 19 entry, ProtServAddr, so the index must account for this */
   for (i = 0; i < NUM_AFS_STATS_CMPERF_LONGS + 1; i++) {
      if (i == 19) {
         srcbuf++;
         continue;  /* skip ProtServerAddr */
      }
      sprintf(a_cmData->data[idx], "%d", *srcbuf);
      idx++;
      srcbuf++;
   }

   /*printf("Ending index value = %d\n",idx-1); */

   /* server up/down statistics */
   /* copy file server up/down stats */
   srcbuf = (afs_int32 *) (fullP->perf.fs_UpDown);
   numLongs =
      2 * (sizeof(struct afs_stats_SrvUpDownInfo) / sizeof(afs_int32));
   for (i = 0; i < numLongs; i++) {
      sprintf(a_cmData->data[idx], "%d", *srcbuf);
      idx++;
      srcbuf++;
   }

src/Monitor.xs  view on Meta::CPAN

 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

int
my_xstat_cm_Cleanup(int a_releaseMem, int xstat_cm_numServers,
                    struct xstat_cm_ConnectionInfo *xstat_cm_ConnInfo,
                    char *buffer)
{
   int code = 0;                /*Return code */
   int conn_idx = 0;            /*Current connection index */
   struct xstat_cm_ConnectionInfo *curr_conn = 0;   /*Ptr to xstat_fs connection */

   /*
    * Take care of all Rx connections first.  Check to see that the
    * server count is a legal value.
    */
   if (xstat_cm_numServers <= 0) {
      sprintf(buffer,
              "Illegal number of servers (xstat_cm_numServers = %d)",
              xstat_cm_numServers);
      code = -1;
   }
   else {
      if (xstat_cm_ConnInfo != (struct xstat_cm_ConnectionInfo *) NULL) {
         /*
          * The xstat_fs connection structure array exists.  Go through
          * it and close up any Rx connections it holds.
          */
         curr_conn = xstat_cm_ConnInfo;
         for (conn_idx = 0; conn_idx < xstat_cm_numServers; conn_idx++) {
            if (curr_conn->rxconn != (struct rx_connection *) NULL) {
               rx_DestroyConnection(curr_conn->rxconn);
               curr_conn->rxconn = (struct rx_connection *) NULL;
            }
            curr_conn++;
         }  /*for each xstat_cm connection */
      } /*xstat_cm connection structure exists */
   }    /*Legal number of servers */

   /*
    * If asked to, release the space we've allocated.
    */
   if (a_releaseMem) {
      if (xstat_cm_ConnInfo != (struct xstat_cm_ConnectionInfo *) NULL)
         free(xstat_cm_ConnInfo);
   }

   /*
    * Return the news, whatever it is.
    */
   return (code);

}   /* my_xstat_cm_Cleanup() */



/* end of afsmonitor helper functions */



/* cmdebug helper functions */

/*
 * from src/venus/cmdebug.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

static int
IsLocked(register struct AFSDBLockDesc *alock)
{
    if (alock->waitStates || alock->exclLocked || alock->numWaiting
        || alock->readersReading)
        return 1;
    return 0;
}


/*
 * from src/venus/cmdebug.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

struct cell_cache {
   afs_int32 cellnum;
   char *cellname;
   struct cell_cache *next;
};


/*
 * from src/venus/cmdebug.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

#ifdef USE_GETCELLNAME
static char *
GetCellName(struct rx_connection *aconn, afs_int32 cellnum)
{
   static int no_getcellbynum;
   static struct cell_cache *cache;
   struct cell_cache *tcp;
   int code;
   char *cellname;
   serverList sl;

   if (no_getcellbynum)
      return NULL;

   for (tcp = cache; tcp; tcp = tcp->next)
      if (tcp->cellnum == cellnum)
         return tcp->cellname;

   cellname = NULL;
   sl.serverList_len = 0;
   sl.serverList_val = NULL;
   code = RXAFSCB_GetCellByNum(aconn, cellnum, &cellname, &sl);
   if (code) {
      if (code == RXGEN_OPCODE)
         no_getcellbynum = 1;
      return NULL;
   }

   if (sl.serverList_val)
      free(sl.serverList_val);
   tcp = malloc(sizeof(struct cell_cache));
   tcp->next = cache;
   tcp->cellnum = cellnum;
   tcp->cellname = cellname;
   cache = tcp;

   return cellname;
}
#endif


/*
 * from src/venus/cmdebug.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

void
my_PrintLock(register struct AFSDBLockDesc *alock, HV *LOCK)
{
   hv_store(LOCK, "waitStates", 10, newSViv(alock->waitStates), 0);
   hv_store(LOCK, "exclLocked", 10, newSViv(alock->exclLocked), 0);
   hv_store(LOCK, "pid_writer", 10, newSViv(alock->pid_writer), 0);
   hv_store(LOCK, "src_indicator", 13, newSViv(alock->src_indicator), 0);
   hv_store(LOCK, "readersReading", 14, newSViv(alock->readersReading), 0);
   hv_store(LOCK, "pid_last_reader", 15, newSViv(alock->pid_last_reader), 0);
   hv_store(LOCK, "numWaiting", 10, newSViv(alock->numWaiting), 0);
}


/*
 * from src/venus/cmdebug.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

int
my_PrintLocks(register struct rx_connection *aconn, int aint32,
              AV *LOCKS, char *buffer)
{
   register int i;
   struct AFSDBLock lock;
   afs_int32 code;
   HV *LOCK;
   HV *LOCKDESC;

   for (i = 0; i < 1000; i++) {
      code = RXAFSCB_GetLock(aconn, i, &lock);
      if (code) {
         if (code == 1)
            break;
         /* otherwise we have an unrecognized error */
         sprintf(buffer, "cmdebug: error checking locks: %s",
                 error_message(code));
         return code;
      }
      /* here we have the lock information, so display it, perhaps */
      if (aint32 || IsLocked(&lock.lock)) {
         LOCK = newHV();
         hv_store(LOCK, "name", 4, newSVpv(lock.name, 0), 0);
         LOCKDESC = newHV();
         my_PrintLock(&lock.lock, LOCKDESC);
         hv_store(LOCK, "lock", 4, newRV_inc((SV *) LOCKDESC), 0);
         av_store(LOCKS, i, newRV_inc((SV *) LOCK));
      }
   }
   return 0;
}

#ifdef OpenAFS_1_2
int
my_PrintCacheEntries(aconn, aint32, CACHE_ENTRIES, buffer)
   register struct rx_connection *aconn;
   int aint32;
   AV *CACHE_ENTRIES;
   char *buffer;
{
   register int i;
   register afs_int32 code;
   struct AFSDBCacheEntry centry;
   char *cellname;
   HV *NETFID;
   HV *CENTRY;
   HV *LOCK;

   for (i = 0; i < 10000; i++) {
      code = RXAFSCB_GetCE(aconn, i, &centry);
      if (code) {
         if (code == 1)
            break;
         sprintf(buffer, "cmdebug: failed to get cache entry %d (%s)", i,
                 error_message(code));
         return code;
      }

      CENTRY = newHV();

      hv_store(CENTRY, "addr", 4, newSViv(centry.addr), 0);

      if (centry.addr == 0) {
         /* PS output */
         NETFID = newHV();
         hv_store(NETFID, "Vnode", 5, newSViv(centry.netFid.Vnode), 0);
         hv_store(NETFID, "Volume", 6, newSViv(centry.netFid.Volume), 0);
         hv_store(NETFID, "Unique", 6, newSViv(centry.netFid.Unique), 0);
         hv_store(CENTRY, "netFid", 6, newRV_inc((SV *) NETFID), 0);
         av_store(CACHE_ENTRIES, i, newRV_inc((SV *) CENTRY));
         continue;
      }

      if (!aint32 && !IsLocked(&centry.lock))
         continue;

      hv_store(CENTRY, "cell", 4, newSViv(centry.cell), 0);
      NETFID = newHV();
      hv_store(NETFID, "Vnode", 5, newSViv(centry.netFid.Vnode), 0);
      hv_store(NETFID, "Volume", 6, newSViv(centry.netFid.Volume), 0);
      hv_store(NETFID, "Unique", 6, newSViv(centry.netFid.Unique), 0);
      hv_store(CENTRY, "netFid", 6, newRV_inc((SV *) NETFID), 0);

#ifdef USE_GETCELLNAME
      cellname = GetCellName(aconn, centry.cell);
      if (cellname)
         hv_store(CENTRY, "cellname", 8, newSVpv(cellname, 0), 0);
#endif

      if (IsLocked(&centry.lock)) {
         LOCK = newHV();
         my_PrintLock(&centry.lock, LOCK);
         hv_store(CENTRY, "lock", 4, newRV_inc((SV *) LOCK), 0);
      }

      hv_store(CENTRY, "Length", 6, newSViv(centry.Length), 0);
      hv_store(CENTRY, "DataVersion", 11, newSViv(centry.DataVersion), 0);
      hv_store(CENTRY, "refCount", 8, newSViv(centry.refCount), 0);
      hv_store(CENTRY, "callback", 8, newSViv(centry.callback), 0);
      hv_store(CENTRY, "cbExpires", 9, newSViv(centry.cbExpires), 0);
      hv_store(CENTRY, "opens", 5, newSViv(centry.opens), 0);
      hv_store(CENTRY, "writers", 7, newSViv(centry.writers), 0);

      /* now display states */
      hv_store(CENTRY, "mvstat", 6, newSViv(centry.mvstat), 0);

      hv_store(CENTRY, "states", 6, newSViv(centry.states), 0);
      av_store(CACHE_ENTRIES, i, newRV_inc((SV *) CENTRY));
   }
   return 0;
}
#endif		/* ifdef OpenAFS_1_2 */



#ifdef OpenAFS_1_4
/*
 * from src/venus/cmdebug.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

int
PrintCacheEntries32(struct rx_connection *aconn, int aint32,
                    AV *CACHE_ENTRIES, char *buffer)
{
    register int i;
    register afs_int32 code;
    struct AFSDBCacheEntry centry;
    char *cellname;
    HV *NETFID;
    HV *CENTRY;
    HV *LOCK;

    for (i = 0; i < 10000; i++) {
        code = RXAFSCB_GetCE(aconn, i, &centry);
        if (code) {
            if (code == 1)
                break;
            sprintf(buffer, "cmdebug: failed to get cache entry %d (%s)\n", i,
                   error_message(code));
            return code;
        }

        CENTRY = newHV();

        hv_store(CENTRY, "addr", 4, newSViv(centry.addr), 0);

        if (centry.addr == 0) {
            /* PS output */
            printf("Proc %4d sleeping at %08x, pri %3d\n",
                   centry.netFid.Vnode, centry.netFid.Volume,
                   centry.netFid.Unique - 25);
            continue;
        }

        if (((aint32 == 0) && !IsLocked(&centry.lock)) ||
            ((aint32 == 2) && (centry.refCount == 0)) ||
            ((aint32 == 4) && (centry.callback == 0)))
            continue;

        /* otherwise print this entry */
        hv_store(CENTRY, "cell", 4, newSViv(centry.cell), 0);
        NETFID = newHV();
        hv_store(NETFID, "Vnode", 5, newSViv(centry.netFid.Vnode), 0);
        hv_store(NETFID, "Volume", 6, newSViv(centry.netFid.Volume), 0);
        hv_store(NETFID, "Unique", 6, newSViv(centry.netFid.Unique), 0);
        hv_store(CENTRY, "netFid", 6, newRV_inc((SV *) NETFID), 0);

#ifdef USE_GETCELLNAME
        cellname = GetCellName(aconn, centry.cell);
        if (cellname)
            hv_store(CENTRY, "cellname", 8, newSVpv(cellname, 0), 0);
#endif

        if (IsLocked(&centry.lock)) {
            LOCK = newHV();
            my_PrintLock(&centry.lock, LOCK);
            hv_store(CENTRY, "lock", 4, newRV_inc((SV *) LOCK), 0);
        }
        hv_store(CENTRY, "Length",       6, newSViv(centry.Length), 0);
        hv_store(CENTRY, "DataVersion", 11, newSViv(centry.DataVersion), 0);
        hv_store(CENTRY, "refCount",     8, newSViv(centry.refCount), 0);
        hv_store(CENTRY, "callback",     8, newSViv(centry.callback), 0);
        hv_store(CENTRY, "cbExpires",    9, newSViv(centry.cbExpires), 0);
        hv_store(CENTRY, "opens",        5, newSViv(centry.opens), 0);
        hv_store(CENTRY, "writers",      7, newSViv(centry.writers), 0);


        /* now display states */
        hv_store(CENTRY, "mvstat", 6, newSViv(centry.mvstat), 0);

        hv_store(CENTRY, "states", 6, newSViv(centry.states), 0);
        av_store(CACHE_ENTRIES, i, newRV_inc((SV *) CENTRY));
    }
    return 0;
}


/*
 * from src/venus/cmdebug.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

int
PrintCacheEntries64(struct rx_connection *aconn, int aint32,
                    AV *CACHE_ENTRIES, char *buffer)
{
    register int i;
    register afs_int32 code;
    struct AFSDBCacheEntry64 centry;
    char *cellname;
    char *data_name;
    HV *NETFID;
    HV *CENTRY;
    HV *LOCK;

    for (i = 0; i < 10000; i++) {
        code = RXAFSCB_GetCE64(aconn, i, &centry);
        if (code) {
            if (code == 1)
                break;
            sprintf(buffer, "cmdebug: failed to get cache entry %d (%s)\n", i,
                   error_message(code));
            return code;
        }

        CENTRY = newHV();

        hv_store(CENTRY, "addr", 4, newSViv(centry.addr), 0);

        if (centry.addr == 0) {
            /* PS output */
            NETFID = newHV();
            hv_store(NETFID, "Vnode", 5, newSViv(centry.netFid.Vnode), 0);
            hv_store(NETFID, "Volume", 6, newSViv(centry.netFid.Volume), 0);
            hv_store(NETFID, "Unique", 6, newSViv(centry.netFid.Unique-25), 0);
            hv_store(CENTRY, "netFid", 6, newRV_inc((SV *) NETFID), 0);
            av_store(CACHE_ENTRIES, i, newRV_inc((SV *) CENTRY));
            continue;
        }

        if (((aint32 == 0) && !IsLocked(&centry.lock)) ||
            ((aint32 == 2) && (centry.refCount == 0)) ||
            ((aint32 == 4) && (centry.callback == 0)))
            continue;

        /* otherwise print this entry */
        hv_store(CENTRY, "cell", 4, newSViv(centry.cell), 0);
        NETFID = newHV();
        hv_store(NETFID, "Vnode", 5, newSViv(centry.netFid.Vnode), 0);
        hv_store(NETFID, "Volume", 6, newSViv(centry.netFid.Volume), 0);
        hv_store(NETFID, "Unique", 6, newSViv(centry.netFid.Unique), 0);
        hv_store(CENTRY, "netFid", 6, newRV_inc((SV *) NETFID), 0);

#ifdef USE_GETCELLNAME
        cellname = GetCellName(aconn, centry.cell);
        if (cellname)
            hv_store(CENTRY, "cellname", 8, newSVpv(cellname, 0), 0);
#endif

        if (IsLocked(&centry.lock)) {
            LOCK = newHV();
            my_PrintLock(&centry.lock, LOCK);
            hv_store(CENTRY, "lock", 4, newRV_inc((SV *) LOCK), 0);
        }

        hv_store(CENTRY, "Length",       6, newSViv(centry.Length), 0);
        hv_store(CENTRY, "DataVersion", 11, newSViv(centry.DataVersion), 0);
        hv_store(CENTRY, "refCount",     8, newSViv(centry.refCount), 0);
        hv_store(CENTRY, "callback",     8, newSViv(centry.callback), 0);
        hv_store(CENTRY, "cbExpires",    9, newSViv(centry.cbExpires), 0);
        hv_store(CENTRY, "opens",        5, newSViv(centry.opens), 0);
        hv_store(CENTRY, "writers",      7, newSViv(centry.writers), 0);

        /* now display states */
        if (centry.mvstat == 0)
            data_name = "normal file";
        else if (centry.mvstat == 1)
            data_name = "mount point";
        else if (centry.mvstat == 2)
            data_name = "volume root";
        else if (centry.mvstat == 3)
            data_name = "directory";
        else if (centry.mvstat == 4)
            data_name = "symlink";
        else if (centry.mvstat == 5)
            data_name = "microsoft dfs link";
        else if (centry.mvstat == 6)
            data_name = "invalid link";
        else
            data_name = "bogus mvstat";
        hv_store(CENTRY, "mvstat", 6, newSVpv(data_name, strlen(data_name)), 0);

        data_name = "";
        if (centry.states & 1)
            sprintf(data_name, "%s, stat'd", data_name);
        if (centry.states & 2)
            sprintf(data_name, "%s, backup", data_name);
        if (centry.states & 4)
            sprintf(data_name, "%s, read-only", data_name);
        if (centry.states & 8)
            sprintf(data_name, "%s, mt pt valid", data_name);
        if (centry.states & 0x10)
            sprintf(data_name, "%s, pending core", data_name);
        if (centry.states & 0x40)
            sprintf(data_name, "%s, wait-for-store", data_name);
        if (centry.states & 0x80)
            sprintf(data_name, "%s, mapped", data_name);
        hv_store(CENTRY, "states", 6, newSVpv(data_name, strlen(data_name)), 0);

        av_store(CACHE_ENTRIES, i, newRV_inc((SV *) CENTRY));
    }
    return 0;
}


/*
 * from src/venus/cmdebug.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

int
my_PrintCacheEntries(struct rx_connection *aconn, int aint32,
                     AV *CACHE_ENTRIES, char *buffer)
{
    register afs_int32 code;
    struct AFSDBCacheEntry64 centry64;

    code = RXAFSCB_GetCE64(aconn, 0, &centry64);
    if (code != RXGEN_OPCODE)
        return PrintCacheEntries64(aconn, aint32, CACHE_ENTRIES, buffer);
    else
        return PrintCacheEntries32(aconn, aint32, CACHE_ENTRIES, buffer);
}
#endif		/* ifdef OpenAFS_1_4 */


/* end of cmdebug helper functions */



/* udebug helper functions */

/*
 * from src/ubik/ubik.h
 *
 */

#define MAXSKEW  10


/*
 * from src/ubik/udebug.c
 *     ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

static short
udebug_PortNumber(register char *aport)
{
       register int tc;
    register afs_int32 total;

    total = 0;
    while ((tc = *aport++)) {
        if (tc < '0' || tc > '9')
            return -1;          /* bad port number */
        total *= 10;
        total += tc - (int)'0';
    }
    return (total);
}


/*
 * from src/ubik/udebug.c
 *     ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

static short
udebug_PortName(char *aname)
{
    struct servent *ts;
    int len;

    ts = getservbyname(aname, NULL);

    if (ts)
        return ntohs(ts->s_port);       /* returns it in host byte order */

    len = strlen(aname);
    if (strncmp(aname, "vlserver", len) == 0) {
        return 7003;
    } else if (strncmp(aname, "ptserver", len) == 0) {
        return 7002;
    } else if (strncmp(aname, "kaserver", len) == 0) {
        return 7004;
    } else if (strncmp(aname, "buserver", len) == 0) {
        return 7021;
    }
    return (-1);
}


/* end of udebug helper functions */





/* scout helper functions */


/*
 * from src/fsprobe/fsprobe.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

#define FSPROBE_CBPORT  7101

extern int RXAFSCB_ExecuteRequest();

int
my_fsprobe_LWP(fsprobe_numServers, fsprobe_ConnInfo, fsprobe_Results,
               fsprobe_statsBytes, fsprobe_probeOKBytes,
               scout_debugfd, RETVAL, buffer)
   int fsprobe_numServers;
   struct fsprobe_ConnectionInfo *fsprobe_ConnInfo;
   struct fsprobe_ProbeResults *fsprobe_Results;
   int fsprobe_statsBytes;
   int fsprobe_probeOKBytes;
   FILE *scout_debugfd;
   AV *RETVAL;
   char *buffer;
{
   static char rn[] = "fsprobe_LWP";    /*Routine name */
   register afs_int32 code = 0; /*Results of calls */
   int conn_idx;                /*Connection index */
   struct fsprobe_ConnectionInfo *curr_conn;    /*Current connection */
   struct ProbeViceStatistics *curr_stats;  /*Current stats region */
   int *curr_probeOK;           /*Current probeOK field */
   int i;

   if (scout_debugfd) {
      fprintf(scout_debugfd, "[%s] Called\n", rn);
      fprintf(scout_debugfd,
              "[%s] Collecting data from %d connected servers\n", rn,
              fsprobe_numServers);
      fflush(scout_debugfd);
   }
   curr_conn = fsprobe_ConnInfo;
   curr_stats = fsprobe_Results->stats;
   curr_probeOK = fsprobe_Results->probeOK;
   fsprobe_Results->probeNum++;
   memset(fsprobe_Results->stats, 0, fsprobe_statsBytes);
   memset(fsprobe_Results->probeOK, 0, fsprobe_probeOKBytes);

   for (conn_idx = 0; conn_idx < fsprobe_numServers; conn_idx++) {
      /*
       * Grab the statistics for the current FileServer, if the
       * connection is valid.
       */
      if (scout_debugfd) {
         fprintf(scout_debugfd, "[%s] Contacting server %s\n", rn,
                 curr_conn->hostName);
         fflush(scout_debugfd);
      }
      if (curr_conn->rxconn != (struct rx_connection *) NULL) {
         if (scout_debugfd) {
            fprintf(scout_debugfd,
                    "[%s] Connection valid, calling RXAFS_GetStatistics\n",
                    rn);
            fflush(scout_debugfd);
         }
         *curr_probeOK = RXAFS_GetStatistics(curr_conn->rxconn, (struct ViceStatistics *)curr_stats);

      } /*Valid Rx connection */

      /*
       * Call the Volume Server too to get additional stats
       */
      if (scout_debugfd) {
         fprintf(scout_debugfd, "[%s] Contacting volume server %s\n", rn,
                 curr_conn->hostName);
         fflush(scout_debugfd);
      }
      if (curr_conn->rxVolconn != (struct rx_connection *) NULL) {
         char pname[10];
         struct diskPartition partition;

         if (scout_debugfd) {
            fprintf(scout_debugfd,
                    "[%s] Connection valid, calling RXAFS_GetStatistics\n",
                    rn);
            fflush(scout_debugfd);
         }
         for (i = 0; i < curr_conn->partCnt; i++) {
            if (curr_conn->partList.partFlags[i] & PARTVALID) {
               MapPartIdIntoName(curr_conn->partList.partId[i], pname);
               code =
                  AFSVolPartitionInfo(curr_conn->rxVolconn, pname,
                                      &partition);
               if (code) {
                  sprintf(buffer,
                          "Could not get information on server %s partition %s",
                          curr_conn->hostName, pname);
               }
               else {
                  curr_stats->Disk[i].BlocksAvailable = partition.free;
                  curr_stats->Disk[i].TotalBlocks = partition.minFree;
                  strcpy(curr_stats->Disk[i].Name, pname);
               }
            }
         }
      }

      /*
       * Advance the fsprobe connection pointer & stats pointer.
       */
      curr_conn++;
      curr_stats++;
      curr_probeOK++;

   }    /*For each fsprobe connection */

   return (code);
}   /* my_fsprobe_LWP */


/*
 * from src/fsprobe/fsprobe.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

int
my_XListPartitions(aconn, ptrPartList, cntp, scout_debugfd)
   struct rx_connection *aconn;
   struct partList *ptrPartList;
   afs_int32 *cntp;
   FILE *scout_debugfd;
{
   struct pIDs partIds;
   struct partEntries partEnts;
   register int i, j = 0, code;
   static int newvolserver = 0;

   static char rn[] = "my_XListPartitions";
   if (scout_debugfd) {
      fprintf(scout_debugfd, "[%s] Called\n", rn);
      fflush(scout_debugfd);
   }

   *cntp = 0;
   if (newvolserver == 1) {
      for (i = 0; i < 26; i++)
         partIds.partIds[i] = -1;
    tryold:
      code = AFSVolListPartitions(aconn, &partIds);
      if (!code) {
         for (i = 0; i < 26; i++) {
            if ((partIds.partIds[i]) != -1) {
               ptrPartList->partId[j] = partIds.partIds[i];
               ptrPartList->partFlags[j] = PARTVALID;
               j++;
            }
            else
               ptrPartList->partFlags[i] = 0;
         }
         *cntp = j;
      }
      goto out;
   }
   partEnts.partEntries_len = 0;
   partEnts.partEntries_val = (afs_int32 *) NULL;
   code = AFSVolXListPartitions(aconn, &partEnts);
   if (!newvolserver) {
      if (code == RXGEN_OPCODE) {
         newvolserver = 1;  /* Doesn't support new interface */
         goto tryold;
      }
      else if (!code) {
         newvolserver = 2;
      }
   }
   if (!code) {
      *cntp = partEnts.partEntries_len;
      if (*cntp > VOLMAXPARTS) {
         warn
            ("Warning: number of partitions on the server too high %d (process only %d)\n",
             *cntp, VOLMAXPARTS);
         *cntp = VOLMAXPARTS;
      }
      for (i = 0; i < *cntp; i++) {
         ptrPartList->partId[i] = partEnts.partEntries_val[i];
         ptrPartList->partFlags[i] = PARTVALID;
      }
      free(partEnts.partEntries_val);
   }
 out:
   if (code)
      warn("Could not fetch the list of partitions from the server\n");
   return code;
}


/*
 * from src/fsprobe/fsprobe.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

int
my_fsprobe_Cleanup(fsprobe_Results, fsprobe_ConnInfo, fsprobe_numServers,
                   scout_debugfd, buffer)
   struct fsprobe_ProbeResults *fsprobe_Results;
   struct fsprobe_ConnectionInfo *fsprobe_ConnInfo;
   int fsprobe_numServers;
   FILE *scout_debugfd;
   char *buffer;
{

   int code = 0;                /*Return code */
   int conn_idx;                /*Current connection index */
   struct fsprobe_ConnectionInfo *curr_conn;    /*Ptr to fsprobe connection */
   static char rn[] = "my_fsprobe_Cleanup";

   if (scout_debugfd) {
      fprintf(scout_debugfd, "[%s] Called\n", rn);
      fflush(scout_debugfd);
   }


   /*
    * Take care of all Rx connections first.  Check to see that the
    * server count is a legal value.
    */
   if (fsprobe_numServers <= 0) {
      sprintf(buffer,
              "[%s] Illegal number of servers to clean up (fsprobe_numServers = %d)",
              rn, fsprobe_numServers);
      code = -1;
   }
   else {
      if (fsprobe_ConnInfo != (struct fsprobe_ConnectionInfo *) NULL) {
         /*
          * The fsprobe connection structure array exists.  Go through it
          * and close up any Rx connections it holds.
          */
         curr_conn = fsprobe_ConnInfo;
         for (conn_idx = 0; conn_idx < fsprobe_numServers; conn_idx++) {
            if (curr_conn->rxconn != (struct rx_connection *) NULL) {
               rx_DestroyConnection(curr_conn->rxconn);
               curr_conn->rxconn = (struct rx_connection *) NULL;
            }
            if (curr_conn->rxVolconn != (struct rx_connection *) NULL) {
               rx_DestroyConnection(curr_conn->rxVolconn);
               curr_conn->rxVolconn = (struct rx_connection *) NULL;
            }
            curr_conn++;
         }  /*for each fsprobe connection */
      } /*fsprobe connection structure exists */
   }    /*Legal number of servers */

   /*
    * Now, release all the space we've allocated
    */
   if (fsprobe_ConnInfo != (struct fsprobe_ConnectionInfo *) NULL)
      free(fsprobe_ConnInfo);
   if (fsprobe_Results->stats != (struct ProbeViceStatistics *) NULL)
      free(fsprobe_Results->stats);
   if (fsprobe_Results->probeOK != (int *) NULL)
      free(fsprobe_Results->probeOK);

   /*
    * Return the news, whatever it is.
    */
   return (code);


}


/*
 * from src/fsprobe/fsprobe.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

int
my_fsprobe_Init(fsprobe_Results, fsprobe_ConnInfo, a_numServers,
                a_socketArray, RETVAL, scout_debugfd, buffer)
   struct fsprobe_ProbeResults *fsprobe_Results;    /*Latest probe results */
   struct fsprobe_ConnectionInfo **fsprobe_ConnInfo;    /*Ptr to connection array */
   int a_numServers;
   struct sockaddr_in *a_socketArray;
   AV *RETVAL;
   FILE *scout_debugfd;
   char *buffer;
{
   static char rn[] = "my_fsprobe_Init";
   struct fsprobe_ConnectionInfo *curr_conn;    /*Current connection */
   int fsprobe_statsBytes;      /*Num bytes in stats block */
   int fsprobe_probeOKBytes;    /*Num bytes in probeOK block */
   int conn_err = 0, code = 0;
   int curr_srv;
   char *hostNameFound;
   int PortToUse;
   struct rx_securityClass *secobj;
   struct rx_securityClass *CBsecobj;
   struct rx_service *rxsrv_afsserver;
   char buff2[256] = "";

   struct rx_call *rxcall;      /*Bogus param */
   AFSCBFids *Fids_Array;       /*Bogus param */
   AFSCBs *CallBack_Array;      /*Bogus param */
   struct interfaceAddr *interfaceAddr; /*Bogus param */


   if (scout_debugfd) {
      fprintf(scout_debugfd, "[%s] Called\n", rn);
      fflush(scout_debugfd);
   }
   if (a_numServers <= 0) {
      sprintf(buffer, "[%s] Illegal number of servers: %d", rn, a_numServers);
      return (-1);
   }
   if (a_socketArray == (struct sockaddr_in *) NULL) {
      sprintf(buffer, "[%s] Null server socket array argument", rn);
      return (-1);
   }

   memset(fsprobe_Results, 0, sizeof(struct fsprobe_ProbeResults));


   rxcall = (struct rx_call *) NULL;
   Fids_Array = (AFSCBFids *) NULL;
   CallBack_Array = (AFSCBs *) NULL;
   interfaceAddr = (struct interfaceAddr *) NULL;

   SRXAFSCB_CallBack(rxcall, Fids_Array, CallBack_Array);
   SRXAFSCB_InitCallBackState2(rxcall, interfaceAddr);
   SRXAFSCB_Probe(rxcall);



   *fsprobe_ConnInfo = (struct fsprobe_ConnectionInfo *)
      malloc(a_numServers * sizeof(struct fsprobe_ConnectionInfo));
   if (*fsprobe_ConnInfo == (struct fsprobe_ConnectionInfo *) NULL) {
      sprintf(buffer,
              "[%s] Can't allocate %d connection info structs (%d bytes)\n",
              rn, a_numServers,
              (a_numServers * sizeof(struct fsprobe_ConnectionInfo)));
      return (-1);  /*No cleanup needs to be done yet */
   }
   else if (scout_debugfd) {
      fprintf(scout_debugfd, "[%s] *fsprobe_ConnInfo allocated (%d bytes)\n",
              rn, a_numServers * sizeof(struct fsprobe_ConnectionInfo));
      fflush(scout_debugfd);
   }

   fsprobe_statsBytes = a_numServers * sizeof(struct ProbeViceStatistics);
   fsprobe_Results->stats = (struct ProbeViceStatistics *)
      malloc(fsprobe_statsBytes);
   if (fsprobe_Results->stats == (struct ProbeViceStatistics *) NULL) {
      /*Delete already-malloc'ed areas */
      my_fsprobe_Cleanup(fsprobe_Results, *fsprobe_ConnInfo, a_numServers,
                         scout_debugfd, buff2);
      sprintf(buffer,
              "[%s] Can't allocate %d statistics structs (%d bytes). %s", rn,
              a_numServers, fsprobe_statsBytes, buff2);
      return (-1);
   }
   else if (scout_debugfd) {
      fprintf(scout_debugfd,
              "[%s] fsprobe_Results->stats allocated (%d bytes)\n", rn,
              fsprobe_statsBytes);
      fflush(scout_debugfd);
   }

   fsprobe_probeOKBytes = a_numServers * sizeof(int);
   fsprobe_Results->probeOK = (int *)malloc(fsprobe_probeOKBytes);
   if (fsprobe_Results->probeOK == (int *) NULL) {
      /* Delete already-malloc'ed areas */
      my_fsprobe_Cleanup(fsprobe_Results, *fsprobe_ConnInfo, a_numServers,
                         scout_debugfd, buff2);
      sprintf(buffer,
              "[%s] Can't allocate %d probeOK array entries (%d bytes). %s",
              rn, a_numServers, fsprobe_probeOKBytes, buff2);
      return (-1);
   }
   else if (scout_debugfd) {
      fprintf(scout_debugfd,
              "[%s] fsprobe_Results->probeOK allocated (%d bytes)\n",
              rn, fsprobe_probeOKBytes);
      fflush(scout_debugfd);
   }
   fsprobe_Results->probeNum = 0;
   fsprobe_Results->probeTime = 0;
   memset(fsprobe_Results->stats, 0,
          (a_numServers * sizeof(struct ProbeViceStatistics)));

   if (scout_debugfd) {
      fprintf(scout_debugfd, "[%s] Initializing Rx\n", rn);
      fflush(scout_debugfd);
   }
   PortToUse = FSPROBE_CBPORT;
   do {
      code = rx_Init(htons(PortToUse));
      if (code) {
         if (code == RX_ADDRINUSE) {
            if (scout_debugfd) {
               fprintf(scout_debugfd,
                       "[%s] Callback port %d in use, advancing\n", rn,
                       PortToUse);
               fflush(scout_debugfd);
            }
            PortToUse++;
         }
         else {
            sprintf(buffer, "[%s] Fatal error in rx_Init()\n", rn);
            return (-1);
         }
      }
   } while (code);
   if (scout_debugfd) {
      fprintf(scout_debugfd, "[%s] Rx initialized on port %d\n", rn,
              PortToUse);
      fflush(scout_debugfd);
   }


   /*
    * Create a null Rx server security object, to be used by the
    * Callback listener.
    */
   CBsecobj = (struct rx_securityClass *)rxnull_NewServerSecurityObject();
   if (CBsecobj == (struct rx_securityClass *) NULL) {
      /*Delete already-malloc'ed areas */
      my_fsprobe_Cleanup(fsprobe_Results, *fsprobe_ConnInfo, a_numServers,
                         scout_debugfd, buff2);
      sprintf(buffer,
              "[%s] Can't create null security object for the callback listener. %s",
              rn, buff2);
      return (-1);
   }
   if (scout_debugfd)
      fprintf(scout_debugfd, "[%s] Callback server security object created\n",
              rn);


   /*
    * Create a null Rx client security object, to be used by the
    * probe LWP.
    */
   secobj = (struct rx_securityClass *)rxnull_NewClientSecurityObject();
   if (secobj == (struct rx_securityClass *) NULL) {
      /*Delete already-malloc'ed areas */
      my_fsprobe_Cleanup(fsprobe_Results, *fsprobe_ConnInfo, a_numServers,
                         scout_debugfd, buff2);
      sprintf(buffer,
              "[%s] Can't create client security object for probe LWP. %s",
              rn, buff2);
      return (-1);
   }
   if (scout_debugfd) {
      fprintf(scout_debugfd,
              "[%s] Probe LWP client security object created\n", rn);
      fflush(scout_debugfd);
   }


   curr_conn = *fsprobe_ConnInfo;
   conn_err = 0;
   for (curr_srv = 0; curr_srv < a_numServers; curr_srv++) {
      /*
       * Copy in the socket info for the current server, resolve its
       * printable name if possible.
       */
      if (scout_debugfd) {
         fprintf(scout_debugfd,
                 "[%s] Copying in the following socket info:\n", rn);
         fprintf(scout_debugfd, "[%s] IP addr 0x%lx, port %d\n", rn,
                 (a_socketArray + curr_srv)->sin_addr.s_addr,
                 (a_socketArray + curr_srv)->sin_port);
         fflush(scout_debugfd);
      }
      memcpy(&(curr_conn->skt), a_socketArray + curr_srv,
             sizeof(struct sockaddr_in));

      hostNameFound = hostutil_GetNameByINet(curr_conn->skt.sin_addr.s_addr);
      if (hostNameFound == (char *) NULL) {
         warn("Can't map Internet address %lu to a string name\n",
              curr_conn->skt.sin_addr.s_addr);
         curr_conn->hostName[0] = '\0';
      }
      else {
         strcpy(curr_conn->hostName, hostNameFound);
         if (scout_debugfd) {
            fprintf(scout_debugfd,
                    "[%s] Host name for server index %d is %s\n", rn,
                    curr_srv, curr_conn->hostName);
            fflush(scout_debugfd);
         }
      }

      /*
       * Make an Rx connection to the current server.
       */
      if (scout_debugfd) {
         fprintf(scout_debugfd,
                 "[%s] Connecting to srv idx %d, IP addr 0x%lx, port %d, service 1\n",
                 rn, curr_srv, curr_conn->skt.sin_addr.s_addr,
                 curr_conn->skt.sin_port);
         fflush(scout_debugfd);
      }

      curr_conn->rxconn = rx_NewConnection(curr_conn->skt.sin_addr.s_addr,  /*Server addr */
                                           curr_conn->skt.sin_port, /*Server port */
                                           1,   /*AFS service num */
                                           secobj,  /*Security object */
                                           0);  /*Number of above */
      if (curr_conn->rxconn == (struct rx_connection *) NULL) {
         sprintf(buffer,
                 "[%s] Can't create Rx connection to server %s (%lu)",
                 rn, curr_conn->hostName, curr_conn->skt.sin_addr.s_addr);
         conn_err = 1;
      }
      if (scout_debugfd) {
         fprintf(scout_debugfd, "[%s] New connection at 0x%lx\n",
                 rn, curr_conn->rxconn);
         fflush(scout_debugfd);
      }

      /*
       * Make an Rx connection to the current volume server.
       */
      if (scout_debugfd) {
         fprintf(scout_debugfd,
                 "[%s] Connecting to srv idx %d, IP addr 0x%lx, port %d, service 1\n",
                 rn, curr_srv, curr_conn->skt.sin_addr.s_addr, htons(7005));
         fflush(scout_debugfd);
      }
      curr_conn->rxVolconn = rx_NewConnection(curr_conn->skt.sin_addr.s_addr,   /*Server addr */
                                              htons(AFSCONF_VOLUMEPORT),    /*Volume Server port */
                                              VOLSERVICE_ID,    /*AFS service num */
                                              secobj,   /*Security object */
                                              0);   /*Number of above */
      if (curr_conn->rxVolconn == (struct rx_connection *) NULL) {
         sprintf(buffer,
                 "[%s] Can't create Rx connection to volume server %s (%lu)\n",
                 rn, curr_conn->hostName, curr_conn->skt.sin_addr.s_addr);
         conn_err = 1;
      }
      else {
         int i, cnt;

         memset(&curr_conn->partList, 0, sizeof(struct partList));
         curr_conn->partCnt = 0;
         i = my_XListPartitions(curr_conn->rxVolconn, &curr_conn->partList,
                                &cnt, scout_debugfd);
         if (!i) {
            curr_conn->partCnt = cnt;
         }
      }
      if (scout_debugfd) {
         fprintf(scout_debugfd, "[%s] New connection at 0x%lx\n",
                 rn, curr_conn->rxVolconn);
         fflush(scout_debugfd);
      }


      /*
       * Bump the current fsprobe connection to set up.
       */
      curr_conn++;
   }    /*for curr_srv */

   /*
    * Create the AFS callback service (listener).
    */
   if (scout_debugfd)
      fprintf(scout_debugfd, "[%s] Creating AFS callback listener\n", rn);
   rxsrv_afsserver = rx_NewService(0,   /*Use default port */
                                   1,   /*Service ID */
                                   "afs",   /*Service name */
                                   &CBsecobj,   /*Ptr to security object(s) */
                                   1,   /*Number of security objects */
                                   RXAFSCB_ExecuteRequest); /*Dispatcher */
   if (rxsrv_afsserver == (struct rx_service *) NULL) {
      /*Delete already-malloc'ed areas */
      my_fsprobe_Cleanup(fsprobe_Results, *fsprobe_ConnInfo, a_numServers,
                         scout_debugfd, buff2);
      sprintf(buffer, "[%s] Can't create callback Rx service/listener. %s",
              rn, buff2);
      return (-1);
   }
   if (scout_debugfd)
      fprintf(scout_debugfd, "[%s] Callback listener created\n", rn);

   /*
    * Start up the AFS callback service.
    */
   if (scout_debugfd)
      fprintf(scout_debugfd, "[%s] Starting up callback listener.\n", rn);
   rx_StartServer(0 /*Don't donate yourself to LWP pool */ );

   /* start probe */
   code = my_fsprobe_LWP(a_numServers, *fsprobe_ConnInfo, fsprobe_Results,
                         fsprobe_statsBytes, fsprobe_probeOKBytes,
                         scout_debugfd, RETVAL, buffer);
   if (code)
      return (code);

   if (conn_err)
      return (-2);
   else
      return (0);

}   /* my_fsprobe_Init() */


/*
 * from src/scout/scout.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

int
my_FS_Handler(fsprobe_Results, numServers, fsprobe_ConnInfo, scout_debugfd,
              RETVAL, buffer)
   struct fsprobe_ProbeResults fsprobe_Results;
   int numServers;
   struct fsprobe_ConnectionInfo *fsprobe_ConnInfo;
   FILE *scout_debugfd;
   AV *RETVAL;
   char *buffer;
{
   static char rn[] = "my_FS_Handler";  /*Routine name */
   int code;                    /*Return code */
   struct ProbeViceStatistics *curr_stats;  /*Ptr to current stats */
   struct fsprobe_ConnectionInfo *curr_conn;
   ViceDisk *curr_diskstat;
   int curr_disk;
   int *curr_probeOK;           /*Ptr to current probeOK field */
   int i = 0, j = 0;
   HV *RESULTS;
   HV *STATS;
   AV *DISKS;
   HV *DISK;

   if (scout_debugfd) {
      fprintf(scout_debugfd, "[%s] Called\n", rn);
      fflush(scout_debugfd);
   }

   curr_stats = fsprobe_Results.stats;
   curr_probeOK = fsprobe_Results.probeOK;
   curr_conn = fsprobe_ConnInfo;

   for (i = 0; i < numServers; i++) {
      RESULTS = newHV();

      hv_store(RESULTS, "probeOK", 7, newSViv((*curr_probeOK) ? 0 : 1), 0);
      hv_store(RESULTS, "probeTime", 9, newSViv(fsprobe_Results.probeTime),
               0);
      hv_store(RESULTS, "hostName", 8, newSVpv(curr_conn->hostName, 0), 0);

      if (*curr_probeOK == 0) {
         STATS = newHV();
         hv_store(STATS, "CurrentConnections", 18,
                  newSViv(curr_stats->CurrentConnections), 0);
         hv_store(STATS, "TotalFetchs", 11, newSViv(curr_stats->TotalFetchs),
                  0);
         hv_store(STATS, "TotalStores", 11, newSViv(curr_stats->TotalStores),
                  0);
         hv_store(STATS, "WorkStations", 12,
                  newSViv(curr_stats->WorkStations), 0);

         hv_store(STATS, "CurrentMsgNumber", strlen("CurrentMsgNumber"),
                  newSViv(curr_stats->CurrentMsgNumber), 0);
         hv_store(STATS, "OldestMsgNumber", strlen("OldestMsgNumber"),
                  newSViv(curr_stats->OldestMsgNumber), 0);
         hv_store(STATS, "CurrentTime", strlen("CurrentTime"),
                  newSViv(curr_stats->CurrentTime), 0);
         hv_store(STATS, "BootTime", strlen("BootTime"),
                  newSViv(curr_stats->BootTime), 0);
         hv_store(STATS, "StartTime", strlen("StartTime"),
                  newSViv(curr_stats->StartTime), 0);
         hv_store(STATS, "TotalViceCalls", strlen("TotalViceCalls"),
                  newSViv(curr_stats->TotalViceCalls), 0);
         hv_store(STATS, "FetchDatas", strlen("FetchDatas"),
                  newSViv(curr_stats->FetchDatas), 0);
         hv_store(STATS, "FetchedBytes", strlen("FetchedBytes"),
                  newSViv(curr_stats->FetchedBytes), 0);
         hv_store(STATS, "FetchDataRate", strlen("FetchDataRate"),
                  newSViv(curr_stats->FetchDataRate), 0);
         hv_store(STATS, "StoreDatas", strlen("StoreDatas"),
                  newSViv(curr_stats->StoreDatas), 0);
         hv_store(STATS, "StoredBytes", strlen("StoredBytes"),
                  newSViv(curr_stats->StoredBytes), 0);
         hv_store(STATS, "StoreDataRate", strlen("StoreDataRate"),
                  newSViv(curr_stats->StoreDataRate), 0);
         hv_store(STATS, "TotalRPCBytesSent", strlen("TotalRPCBytesSent"),
                  newSViv(curr_stats->TotalRPCBytesSent), 0);
         hv_store(STATS, "TotalRPCBytesReceived",
                  strlen("TotalRPCBytesReceived"),
                  newSViv(curr_stats->TotalRPCBytesReceived), 0);
         hv_store(STATS, "TotalRPCPacketsSent", strlen("TotalRPCPacketsSent"),
                  newSViv(curr_stats->TotalRPCPacketsSent), 0);
         hv_store(STATS, "TotalRPCPacketsReceived",
                  strlen("TotalRPCPacketsReceived"),
                  newSViv(curr_stats->TotalRPCPacketsReceived), 0);
         hv_store(STATS, "TotalRPCPacketsLost", strlen("TotalRPCPacketsLost"),

src/Monitor.xs  view on Meta::CPAN

                  newSViv(curr_stats->EtherNetGoodReads), 0);
         hv_store(STATS, "EtherNetTotalBytesWritten",
                  strlen("EtherNetTotalBytesWritten"),
                  newSViv(curr_stats->EtherNetTotalBytesWritten), 0);
         hv_store(STATS, "EtherNetTotalBytesRead",
                  strlen("EtherNetTotalBytesRead"),
                  newSViv(curr_stats->EtherNetTotalBytesRead), 0);
         hv_store(STATS, "ProcessSize", strlen("ProcessSize"),
                  newSViv(curr_stats->ProcessSize), 0);
         hv_store(STATS, "WorkStations", strlen("WorkStations"),
                  newSViv(curr_stats->WorkStations), 0);
         hv_store(STATS, "ActiveWorkStations", strlen("ActiveWorkStations"),
                  newSViv(curr_stats->ActiveWorkStations), 0);
         hv_store(STATS, "Spare1", strlen("Spare1"),
                  newSViv(curr_stats->Spare1), 0);
         hv_store(STATS, "Spare2", strlen("Spare2"),
                  newSViv(curr_stats->Spare2), 0);
         hv_store(STATS, "Spare3", strlen("Spare3"),
                  newSViv(curr_stats->Spare3), 0);
         hv_store(STATS, "Spare4", strlen("Spare4"),
                  newSViv(curr_stats->Spare4), 0);
         hv_store(STATS, "Spare5", strlen("Spare5"),
                  newSViv(curr_stats->Spare5), 0);
         hv_store(STATS, "Spare6", strlen("Spare6"),
                  newSViv(curr_stats->Spare6), 0);
         hv_store(STATS, "Spare7", strlen("Spare7"),
                  newSViv(curr_stats->Spare7), 0);
         hv_store(STATS, "Spare8", strlen("Spare8"),
                  newSViv(curr_stats->Spare8), 0);

         DISKS = newAV();

         curr_diskstat = (ViceDisk *) curr_stats->Disk;
         j = 0;
         for (curr_disk = 0; curr_disk < VOLMAXPARTS; curr_disk++) {
            if (strncmp("/vice", curr_diskstat->Name, 5) == 0) {
               DISK = newHV();
               hv_store(DISK, "Name", 4, newSVpv(&curr_diskstat->Name[6], 0),
                        0);
               hv_store(DISK, "TotalBlocks", 10,
                        newSViv(curr_diskstat->TotalBlocks), 0);
               hv_store(DISK, "BlocksAvailable", 15,
                        newSViv(curr_diskstat->BlocksAvailable), 0);
               av_store(DISKS, j, newRV_inc((SV *) DISK));
               curr_diskstat++;
               j++;
            }
         }

         hv_store(STATS, "Disk", 4, newRV_inc((SV *) DISKS), 0);
         hv_store(RESULTS, "stats", 5, newRV_inc((SV *) STATS), 0);
      }
      av_store(RETVAL, i, newRV_inc((SV *) RESULTS));
      curr_stats++;
      curr_probeOK++;
      curr_conn++;
   }

   code =
      my_fsprobe_Cleanup(&fsprobe_Results, fsprobe_ConnInfo, numServers,
                         scout_debugfd, buffer);

   return code;
}

/* end of scout helper functions */



/* xstat_fs_test helper functions */

/*
 * from src/xstat/xstat_fs_test.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

void
my_PrintOpTiming(int a_opIdx, struct fs_stats_opTimingData *a_opTimeP, HV *DATA)
{
   HV *OPTIMING = newHV();

   hv_store(OPTIMING, "sumTime", 7,
            newSVnv(a_opTimeP->sumTime.tv_sec +
                    a_opTimeP->sumTime.tv_usec / 1000000.0), 0);
   hv_store(OPTIMING, "sqrTime", 7,
            newSVnv(a_opTimeP->sqrTime.tv_sec +
                    a_opTimeP->sqrTime.tv_usec / 1000000.0), 0);
   hv_store(OPTIMING, "minTime", 7,
            newSVnv(a_opTimeP->minTime.tv_sec +
                    a_opTimeP->minTime.tv_usec / 1000000.0), 0);
   hv_store(OPTIMING, "maxTime", 7,
            newSVnv(a_opTimeP->maxTime.tv_sec +
                    a_opTimeP->maxTime.tv_usec / 1000000.0), 0);
   hv_store(OPTIMING, "numSuccesses", 12, newSViv(a_opTimeP->numSuccesses),
            0);
   hv_store(OPTIMING, "numOps", 6, newSViv(a_opTimeP->numOps), 0);

   hv_store(DATA, fsOpNames[a_opIdx], strlen(fsOpNames[a_opIdx]),
            newRV_inc((SV *) OPTIMING), 0);
}


/*
 * from src/xstat/xstat_fs_test.c
 *    ("$Header: /afs/slac/g/scs/slur/Repository/AFSDebug/Debug/src/Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $");
 */

void
my_PrintXferTiming(int a_opIdx, struct fs_stats_xferData *a_xferP, HV *DATA)
{
   HV *XFERTIMING = newHV();
   AV *COUNT = newAV();
   int i;

   hv_store(XFERTIMING, "sumTime", 7,
            newSVnv(a_xferP->sumTime.tv_sec +
                    a_xferP->sumTime.tv_usec / 1000000.0), 0);
   hv_store(XFERTIMING, "sqrTime", 7,
            newSVnv(a_xferP->sqrTime.tv_sec +
                    a_xferP->sqrTime.tv_usec / 1000000.0), 0);
   hv_store(XFERTIMING, "minTime", 7,

src/Monitor.xs  view on Meta::CPAN

        FSSktArray[currFS].sin_family = htons(AF_INET); /*Internet family */
#endif
	FSSktArray[currFS].sin_port   = htons(7000);	/*FileServer port*/
	he = hostutil_GetHostByName((char *) SvPV(*av_fetch(host_array, currFS, 0), PL_na));
	if (he == (struct hostent *) NULL) {
	    sprintf(buffer,
		    "Can't get host info for '%s'",
		    (char *) SvPV(*av_fetch(host_array, currFS, 0), PL_na));
	    BSETCODE(-1, buffer);
            XSRETURN_UNDEF;
	}
	memcpy(&(FSSktArray[currFS].sin_addr.s_addr), he->h_addr, 4);

    } /*Get socket info for each File Server*/

    collIDP = (afs_int32 *) malloc(numCollIDs * sizeof(afs_int32));
    currCollIDP = collIDP;
    for (currCollIDIdx = 0; currCollIDIdx < numCollIDs; currCollIDIdx++) {
	*currCollIDP = (afs_int32) SvIV(*av_fetch(coll_array, currCollIDIdx, 0));
	currCollIDP++;
    }

    code = my_xstat_fs_Init(my_xstat_FS_Handler, numFSs, FSSktArray,
                            numCollIDs, collIDP, buffer, RETVAL);
    if(code) {
       BSETCODE(code, buffer);
       XSRETURN_UNDEF;
    }

    ST(0) = sv_2mortal(newRV_inc((SV*)RETVAL));
    SETCODE(0);
    XSRETURN(1);

  }


void
afs_do_scout(args)
    HV* args = (HV*) SvRV($arg);
  PREINIT:
  PPCODE:
  {
    static char rn[] = "afs_do_scout";
    SV *value;
    I32 keylen = 0;
    char *key;
    int num_args = 0;
    char buffer[256] = "";
    struct fsprobe_ProbeResults fsprobe_Results;
    struct fsprobe_ConnectionInfo *fsprobe_ConnInfo; /*Ptr to connection array*/
    char buff2[256] = "";

    char basename[64] = "";
    int numservers = 0;
    char fullsrvname[128] = "";
    struct sockaddr_in *FSSktArray;
    struct sockaddr_in *curr_skt;
    struct hostent *he;
    int i, code;
    int sktbytes;
    FILE *scout_debugfd = (FILE *) NULL;
    char *debug_filename = (char *) NULL;

    AV *host_array = (AV *) NULL;

    AV *RETVAL = newAV();

    /* parse arguments */
    num_args = hv_iterinit(args);
    while (num_args--) {

      value = hv_iternextsv(args, &key, &keylen);

      switch (*key) {

        case 'b':
          if(strncmp(key, "basename", keylen) == 0 && keylen <= 8) {
            sprintf(basename, "%s", SvPV(value, PL_na));
          } else goto unrecognized;
        break;

        case 'd':
          if(strncmp(key, "debug", keylen) == 0 && keylen <= 5) {
            debug_filename = (char *) SvPV(value, PL_na);
          } else goto unrecognized;
        break;

        case 's':
          if(strncmp(key, "servers", keylen) == 0 && keylen <= 7) {
            if (SvROK(value))
              host_array = (AV*) SvRV(value);
            else {
              host_array = av_make(1, &value);
              sv_2mortal((SV *) host_array);
            }
            numservers = av_len(host_array) + 1;
          } else goto unrecognized;
        break;

        default:
          unrecognized:
          sprintf(buffer, "Unrecognized flag: %s", key);
          BSETCODE(-1, buffer);
          XSRETURN_UNDEF;
        break;
      } /* end switch */
    } /* end while */
    /* done parsing arguments */

    if(numservers == 0) {
      sprintf(buffer, "Missing required parameter 'server'");
      BSETCODE(-1, buffer);
      XSRETURN_UNDEF;
    }

    if(debug_filename) {
      scout_debugfd = fopen(debug_filename, "w");
      if(scout_debugfd == (FILE *) NULL) {
        sprintf(buffer, "Can't open debugging file '%s'!", debug_filename);
        BSETCODE(-1, buffer);
        XSRETURN_UNDEF;
      }
      fprintf(scout_debugfd, "[%s] Writing to Scout debugging file '%s'\n",
              rn, debug_filename);
      fflush(scout_debugfd);
    }

    /* execute_scout */

    sktbytes = numservers * sizeof(struct sockaddr_in);
    FSSktArray = (struct sockaddr_in *) malloc(sktbytes);
    if (FSSktArray == (struct sockaddr_in *) NULL) {
      sprintf(buffer,
              "Can't malloc() %d sockaddrs (%d bytes) for the given servers",
              numservers, sktbytes);
      BSETCODE(-1, buffer);
      if (scout_debugfd != (FILE *) NULL) {
        fprintf(scout_debugfd, "[%s] Closing debugging file\n", rn);
        fclose(scout_debugfd);
      }
      XSRETURN_UNDEF;
    }
    memset(FSSktArray, 0, sktbytes);

    curr_skt = FSSktArray;
    for(i=0; i<numservers; i++) {
       if(*basename == '\0')
         sprintf(fullsrvname, "%s", (char *) SvPV(*av_fetch(host_array, i, 0), PL_na));
       else
         sprintf(fullsrvname, "%s.%s", (char *) SvPV(*av_fetch(host_array, i, 0), PL_na), basename);
       he = hostutil_GetHostByName(fullsrvname);
       if(he == (struct hostent *) NULL) {
         sprintf(buffer, "Can't get host info for '%s'", fullsrvname);
         BSETCODE(-1, buffer);
         if (scout_debugfd != (FILE *) NULL) {
           fprintf(scout_debugfd, "[%s] Closing debugging file\n", rn);
           fclose(scout_debugfd);
         }
         XSRETURN_UNDEF;
       }
       memcpy(&(curr_skt->sin_addr.s_addr), he->h_addr, 4);
#if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
        curr_skt->sin_family = AF_INET;         /*Internet family */
#else
        curr_skt->sin_family = htons(AF_INET);  /*Internet family */
#endif
       curr_skt->sin_port   = htons(7000);       /* FileServer port */
       curr_skt++;
    }
    code = my_fsprobe_Init(&fsprobe_Results, &fsprobe_ConnInfo, numservers,
                           FSSktArray, RETVAL, scout_debugfd, buffer);
    if(code) {
      if(buffer == "") {
        sprintf(buffer, "Error returned by fsprobe_Init: %d", code);
      }
      BSETCODE(code, buffer);
      if (scout_debugfd != (FILE *) NULL) {
        fprintf(scout_debugfd, "[%s] Closing debugging file\n", rn);
        fclose(scout_debugfd);
      }
      XSRETURN_UNDEF;
    }
    code = my_FS_Handler(fsprobe_Results, numservers, fsprobe_ConnInfo,
                         scout_debugfd, RETVAL, buff2);
    if (code) {
      sprintf(buffer, "[%s] Handler routine returned error code %d. %s", rn, code, buff2);
      BSETCODE(code, buffer);
      if (scout_debugfd != (FILE *) NULL) {
        fprintf(scout_debugfd, "[%s] Closing debugging file\n", rn);
        fclose(scout_debugfd);
      }
      XSRETURN_UNDEF;
    }

    if (scout_debugfd != (FILE *) NULL) {
       fprintf(scout_debugfd, "[%s] Closing debugging file\n", rn);
       fclose(scout_debugfd);
    }

    ST(0) = sv_2mortal(newRV_inc((SV*)RETVAL));
    SETCODE(0);
    XSRETURN(1);
  }

void
afs_do_udebug(args)
    HV* args = (HV*) SvRV($arg);
  PREINIT:
  PPCODE:
  {

    SV *value;
    I32 keylen = 0;
    char *key;
    int num_args = 0;
    char buffer[256] = "";

    char *hostName = (char *) NULL;
    char *portName = (char *) NULL;
    afs_int32 hostAddr;
    struct in_addr inhostAddr;
    register afs_int32 i, j, code;
    short port;
    int int32p = 0;
    struct hostent *th;
    struct rx_connection *tconn;
    struct rx_securityClass *sc;
    struct ubik_debug udebug;
    struct ubik_sdebug usdebug;
    int oldServer = 0;   /* are we talking to a pre 3.5 server? */
    afs_int32 isClone = 0;

    HV *RETVAL = newHV();
    AV *ADDRESSES;
    HV *LOCALVERSION;
    HV *SYNCVERSION;
    HV *SYNCTID;
    AV *SERVERS;
    HV *USDEBUG;
    AV *ALTADDR;
    HV *REMOTEVERSION;

    /* parse arguments */
    num_args = hv_iterinit(args);
    while (num_args--) {

      value = hv_iternextsv(args, &key, &keylen);

      switch (*key) {

        case 'l':
          if(strncmp(key, "long", keylen) == 0 && keylen <= 4) {
            int32p = (int) SvIV(value);
          } else goto unrecognized;
        break;

        case 'p':
          if(strncmp(key, "port", keylen) == 0 && keylen <= 4) {
            portName = (char *) SvPV(value, PL_na);
          } else goto unrecognized;
        break;

        case 's':
          if(strncmp(key, "server", keylen) == 0 && keylen <= 6) {
            hostName = (char *) SvPV(value, PL_na);
          } else goto unrecognized;
        break;

        default:
          unrecognized:
          sprintf(buffer, "Unrecognized flag: %s", key);
          BSETCODE(-1, buffer);
          XSRETURN_UNDEF;
        break;
      } /* end switch */
    } /* end while */
    /* done parsing arguments */


    /* lookup host */
    if (hostName) {
       th = hostutil_GetHostByName(hostName);
       if (!th) {
          sprintf(buffer, "udebug: host %s not found in host table", hostName);
          BSETCODE(1, buffer);
          XSRETURN_UNDEF;
       }
       memcpy(&hostAddr, th->h_addr, sizeof(afs_int32));
    }
    else hostAddr = htonl(0x7f000001);  /* IP localhost */


    if (!portName)
       port = htons(3000);             /* default */
    else {
       port = udebug_PortNumber(portName);
       if (port < 0)
          port = udebug_PortName(portName);
       if (port < 0) {
          sprintf(buffer, "udebug: can't resolve port name %s", portName);
          BSETCODE(1, buffer);
          XSRETURN_UNDEF;
       }
       port = htons(port);
    }

    rx_Init(0);
    sc = rxnull_NewClientSecurityObject();
    tconn = rx_NewConnection(hostAddr, port, VOTE_SERVICE_ID, sc, 0);

    /* now do the main call */
#ifdef USE_VOTEXDEBUG
    code = VOTE_XDebug(tconn, &udebug, &isClone);
    if (code) code = VOTE_Debug(tconn, &udebug);
#else
    code = VOTE_Debug(tconn, &udebug);
#endif
    if (code == RXGEN_OPCODE)
    {  ubik_debug * ptr = &udebug;
       oldServer = 1;             /* talking to a pre 3.5 server */
       memset(&udebug, 0, sizeof(udebug));
       code = VOTE_DebugOld(tconn, (struct ubik_debug_old *) ptr);
    }

    if (code) {
       sprintf(buffer, "return code %d from VOTE_Debug", code);
       BSETCODE(code, buffer);
       XSRETURN_UNDEF;
    }

    /* now print the main info */
    inhostAddr.s_addr = hostAddr;
    if ( !oldServer )
    {
       ADDRESSES = newAV();
       for ( j=0; udebug.interfaceAddr[j] && ( j<UBIK_MAX_INTERFACE_ADDR ); j++) {
          av_store(ADDRESSES, j, newSVpv(afs_inet_ntoa(htonl(udebug.interfaceAddr[j])), 0));
       }
       hv_store(RETVAL, "interfaceAddr", 13, newRV_inc((SV*)ADDRESSES), 0);
    }

    hv_store(RETVAL, "host", 4, newSVpv(inet_ntoa(inhostAddr), 0), 0);
    hv_store(RETVAL, "now", 3, newSViv(udebug.now), 0);

    /* UBIK skips the voting if 1 server - so we fudge it here */
    if ( udebug.amSyncSite && (udebug.nServers == 1) ) {
       udebug.lastYesHost  = hostAddr;
       udebug.lastYesTime  = udebug.now;
       udebug.lastYesState = 1;
       udebug.lastYesClaim = udebug.now;
       udebug.syncVersion.epoch   = udebug.localVersion.epoch;
       udebug.syncVersion.counter = udebug.localVersion.counter;
    }

    /* sockaddr is always in net-order */
    if ( udebug.lastYesHost != 0xffffffff ) {
       inhostAddr.s_addr = htonl(udebug.lastYesHost);
       hv_store(RETVAL, "lastYesHost", 11, newSVpv(inet_ntoa(inhostAddr), 0), 0);
       hv_store(RETVAL, "lastYesTime", 11, newSViv(udebug.lastYesTime), 0);
       hv_store(RETVAL, "lastYesState", 12, newSViv(udebug.lastYesState), 0);
       hv_store(RETVAL, "lastYesClaim", 12, newSViv(udebug.lastYesClaim), 0);
    }

    LOCALVERSION = newHV();
    hv_store(LOCALVERSION, "epoch", 5, newSViv(udebug.localVersion.epoch), 0);
    hv_store(LOCALVERSION, "counter", 7, newSViv(udebug.localVersion.counter), 0);
    hv_store(RETVAL, "localVersion", 12, newRV_inc((SV*)LOCALVERSION), 0);

    hv_store(RETVAL, "amSyncSite", 10, newSViv(udebug.amSyncSite), 0);

    hv_store(RETVAL, "epochTime", 9, newSViv(udebug.epochTime), 0);

    if (udebug.amSyncSite) {
       hv_store(RETVAL, "syncSiteUntil", 13, newSViv(udebug.syncSiteUntil), 0);
       hv_store(RETVAL, "nServers", 8, newSViv(udebug.nServers), 0);
       hv_store(RETVAL, "recoveryState", 13, newSViv(udebug.recoveryState), 0);
       if (udebug.activeWrite) {
          hv_store(RETVAL, "tidCounter", 10, newSViv(udebug.tidCounter), 0);
       }
    }
    else {
       hv_store(RETVAL, "isClone", 7, newSViv(isClone), 0);

       inhostAddr.s_addr = htonl(udebug.lowestHost);
       hv_store(RETVAL, "lowestHost", 10, newSVpv(inet_ntoa(inhostAddr), 0), 0);
       hv_store(RETVAL, "lowestTime", 10, newSViv(udebug.lowestTime), 0);

       inhostAddr.s_addr = htonl(udebug.syncHost);
       hv_store(RETVAL, "syncHost", 8, newSVpv(inet_ntoa(inhostAddr), 0), 0);
       hv_store(RETVAL, "syncTime", 8, newSViv(udebug.syncTime), 0);
    }

    SYNCVERSION = newHV();
    hv_store(SYNCVERSION, "epoch", 5, newSViv(udebug.syncVersion.epoch), 0);
    hv_store(SYNCVERSION, "counter", 7, newSViv(udebug.syncVersion.counter), 0);
    hv_store(RETVAL, "syncVersion", 11, newRV_inc((SV*)SYNCVERSION), 0);

    hv_store(RETVAL, "lockedPages", 11, newSViv(udebug.lockedPages), 0);
    hv_store(RETVAL, "writeLockedPages", 16, newSViv(udebug.writeLockedPages), 0);

    hv_store(RETVAL, "anyReadLocks", 12, newSViv(udebug.anyReadLocks), 0);
    hv_store(RETVAL, "anyWriteLocks", 13, newSViv(udebug.anyWriteLocks), 0);

    hv_store(RETVAL, "currentTrans", 12, newSViv(udebug.currentTrans), 0);
    if (udebug.currentTrans) {
       hv_store(RETVAL, "writeTrans", 10, newSViv(udebug.writeTrans), 0);
       SYNCTID = newHV();
       hv_store(SYNCTID, "epoch", 5, newSViv(udebug.syncTid.epoch), 0);
       hv_store(SYNCTID, "counter", 7, newSViv(udebug.syncTid.counter), 0);
       hv_store(RETVAL, "syncTid", 7, newRV_inc((SV*)SYNCTID), 0);
    }

    if (int32p || udebug.amSyncSite) {
       /* now do the subcalls */
       SERVERS = newAV();

       for ( i=0; ; i++ ) {
#ifdef USE_VOTEXDEBUG
          isClone = 0;
          code = VOTE_XSDebug(tconn, i, &usdebug, &isClone);
          if (code < 0) {
             if ( oldServer ) {                      /* pre 3.5 server */
                ubik_sdebug * ptr = &usdebug;
                memset(&usdebug, 0, sizeof(usdebug));
                code = VOTE_SDebugOld(tconn, i, (struct ubik_sdebug_old *) ptr);
             }
             else
                code = VOTE_SDebug(tconn, i, &usdebug);
          }
#else
          if ( oldServer ) {                      /* pre 3.5 server */
             ubik_sdebug * ptr = &usdebug;
             memset(&usdebug, 0, sizeof(usdebug));
             code = VOTE_SDebugOld(tconn, i, (struct ubik_sdebug_old *) ptr);
          }
          else
             code = VOTE_SDebug(tconn, i, &usdebug);
#endif

          if (code > 0)
                break;          /* done */
          if (code < 0) {
             warn("error code %d from VOTE_SDebug\n", code);
             break;
          }

          /* otherwise print the structure */
          USDEBUG = newHV();
          inhostAddr.s_addr = htonl(usdebug.addr);

          hv_store(USDEBUG, "addr", 4, newSVpv(afs_inet_ntoa(htonl(usdebug.addr)), 0), 0);

          ALTADDR = newAV();
          for ( j=0;((usdebug.altAddr[j]) && (j<UBIK_MAX_INTERFACE_ADDR-1)); j++) {
             av_store(ALTADDR, j, newSVpv(afs_inet_ntoa(htonl(usdebug.altAddr[j])), 0));
          }
          if (j) hv_store(USDEBUG, "altAddr", 7, newRV_inc((SV*)ALTADDR), 0);

          REMOTEVERSION = newHV();
          hv_store(REMOTEVERSION, "epoch", 5, newSViv(usdebug.remoteVersion.epoch), 0);
          hv_store(REMOTEVERSION, "counter", 7, newSViv(usdebug.remoteVersion.counter), 0);
          hv_store(USDEBUG, "remoteVersion", 13, newRV_inc((SV*)REMOTEVERSION), 0);

          hv_store(USDEBUG, "isClone", 7, newSViv(isClone), 0);

          hv_store(USDEBUG, "lastVoteTime", 12, newSViv(usdebug.lastVoteTime), 0);

          hv_store(USDEBUG, "lastBeaconSent", 14, newSViv(usdebug.lastBeaconSent), 0);
          hv_store(USDEBUG, "lastVote", 8, newSViv(usdebug.lastVote), 0);

          hv_store(USDEBUG, "currentDB", 9, newSViv(usdebug.currentDB), 0);
          hv_store(USDEBUG, "up", 2, newSViv(usdebug.up), 0);
          hv_store(USDEBUG, "beaconSinceDown", 15, newSViv(usdebug.beaconSinceDown), 0);

          av_store(SERVERS, i, newRV_inc((SV*)USDEBUG));
       }
       hv_store(RETVAL, "servers", 7, newRV_inc((SV*)SERVERS), 0);
    }

    /* return RETVAL */
    ST(0) = sv_2mortal(newRV_inc((SV*)RETVAL));
    SETCODE(0);
    XSRETURN(1);

  }

void
afs_do_cmdebug(args)
    HV* args = (HV*) SvRV($arg);
  PREINIT:
  PPCODE:
  {
    SV *value;
    I32 keylen = 0;
    char *key;
    int num_args = 0;
    int code = 0;
    int aint32 = 0;
    struct rx_connection *conn;
    register char *hostName = "";
    register struct hostent *thp;
    struct rx_securityClass *secobj;
    afs_int32 addr = 0;
    afs_int32 port = 7001;
    char buffer[256] = "";

    AV *LOCKS = newAV();           /* return */
    AV *CACHE_ENTRIES = newAV();   /* values */

    /* parse arguments */
    num_args = hv_iterinit(args);
    while (num_args--) {
      value = hv_iternextsv(args, &key, &keylen);

      switch (*key) {

        case 'l':
          if(strncmp(key, "long", keylen) == 0 && keylen <= 4) {
            aint32 = (int) SvIV(value);
          } else goto unrecognized;
        break;

        case 'p':
          if(strncmp(key, "port", keylen) == 0 && keylen <= 4) {
            port = (int) SvIV(value);
          } else goto unrecognized;
        break;

        case 's':
          if(strncmp(key, "servers", keylen) == 0 && keylen <= 7) {
            hostName = (char *) SvPV(value, PL_na);
          } else goto unrecognized;
        break;

        default:
          unrecognized:
          sprintf(buffer, "Unrecognized flag: %s", key);
          BSETCODE(-1, buffer);
          XSRETURN_UNDEF;
        break;
      } /* end switch */
    } /* end while */
    /* done parsing arguments */

    rx_Init(0);

    thp = hostutil_GetHostByName(hostName);
    if (!thp) {

src/Monitor.xs  view on Meta::CPAN

      /* Fill in the socket information for each CM  */

      curr_skt = CMSktArray;
      curr_CM = CMnameList;  /* CM name list header */
      while (curr_CM) {
        strncpy(fullhostname,curr_CM->hostName,sizeof(fullhostname));
        he = GetHostByName(fullhostname);
        if (he == (struct hostent *) NULL) {
          sprintf(buffer,"Cannot get host info for %s", fullhostname);
          BSETCODE(-1, buffer);
          XSRETURN_UNDEF;
        }
        strncpy(curr_CM->hostName,he->h_name,HOST_NAME_LEN); /* complete name*/
        memcpy(&(curr_skt->sin_addr.s_addr), he->h_addr, 4);
        curr_skt->sin_family = htons(AF_INET);    /*Internet family*/
        curr_skt->sin_port   = htons(7001);  /*Cache Manager port */
#ifdef STRUCT_SOCKADDR_HAS_SA_LEN
        curr_skt->sin_len = sizeof(struct sockaddr_in);
#endif

        /* get the next dude */
        curr_skt++;
        curr_CM = curr_CM->next;
      }

      /* initialize collection IDs. We need only one entry since we collect
         all the information from xstat */

      numCollIDs = 1;
      collIDP = (afs_int32 *) malloc (sizeof (afs_int32));
      if (collIDP == (afs_int32 *) NULL) {
        sprintf(buffer,"failed to allocate a measely afs_int32 word. Argh!");
        BSETCODE(-1, buffer);
        XSRETURN_UNDEF;
      }
      *collIDP = 2;     /* USE A macro for this */

      code = my_xstat_cm_Init(my_afsmon_CM_Handler, numCM, CMSktArray, numCollIDs,
                              collIDP, buff2, output_filename, detailed, CACHEMAN,
                              CMnameList, cm_showFlags, cm_showDefault);

      if (code) {
        sprintf(buffer,"my_xstat_cm_Init() returned error. %s", buff2);
        BSETCODE(130, buffer);
        XSRETURN_UNDEF;
      }

    }  /* end of process fileserver entries */

  /* end from afsmon_execute() */

    SETCODE(0);

    EXTEND(SP, 2);
    PUSHs(sv_2mortal(newRV_inc((SV*)FILESERV)));
    PUSHs(sv_2mortal(newRV_inc((SV*)CACHEMAN)));
  }


void
afs_do_rxdebug(args)
    HV* args = (HV*) SvRV($arg);
  PREINIT:
  PPCODE:
  {
    int size;
    I32 keylen;
    char *key;
    HE* entry;
    SV* value;
    HV* RETVAL = newHV(); /* return value */
    HV* TSTATS;
    HV* RXSTATS;
    AV* CONNECTIONS;
    HV* TCONN;
    AV* CALLSTATE;
    AV* CALLMODE;
    AV* CALLFLAGS;
    AV* CALLOTHER;
    AV* CALLNUMBER;
    AV* PEERS;
    HV* TPEER;
    HV* BYTESSENT;
    HV* BYTESRECEIVED;
    HV* TIMEOUT;
    int index;

    register int i;
    int s;
    int j;
    struct sockaddr_in taddr;
    afs_int32 host;
    struct in_addr hostAddr;
    short port;
    struct hostent *th;
    register afs_int32 code;
    int nodally=0;
    int allconns=0;
    int rxstats=0;
    int onlyClient=0;
    int onlyServer=0;
    afs_int32 onlyHost = -1;
    short onlyPort = -1;
    int onlyAuth = 999;
    int flag;
    int dallyCounter;
    int withSecStats;
    int withAllConn;
    int withRxStats;
    int withWaiters;
    int withIdleThreads;
    int withPeers;
    struct rx_debugStats tstats;
    char *portName = (char *) NULL;
    char *hostName = (char *) NULL;
    struct rx_debugConn tconn;
    short noConns=0;
    short showPeers=0;
    short showLong=0;
    int version_flag=0;
    afs_int32 length=64;
    char version[64];
    char buffer[240]; /* for error messages */

    afs_uint32 supportedDebugValues = 0;
    afs_uint32 supportedStatValues = 0;
    afs_uint32 supportedConnValues = 0;
    afs_uint32 supportedPeerValues = 0;
    afs_int32 nextconn = 0;
    afs_int32 nextpeer = 0;

  size = hv_iterinit(args);
  /* fprintf(STDERR, "Parsing args now: %d\n", size); */
  while (size--) {
    char *flag;
    entry = hv_iternext(args);
    key = hv_iterkey(entry, &keylen);
    value = hv_iterval(args, entry);
    flag = key;
    /* fprintf(STDERR, "size = %d, format: got flag %s\n", size, key); */

    switch (*flag) {
    case 'a':
        if (memcmp( flag, "allconnections", 14) == 0 ) {
            allconns = (int) SvIV(value);
        }
        break;

    case 'l':
        if (memcmp( flag, "long", 4) == 0 ) {
            showLong = (int) SvIV(value);
        }
        break;

    case 'n':
        if (memcmp( flag, "nodally", 7) == 0 ) {
            nodally = (int) SvIV(value);
        } else if (memcmp( flag, "noconns", 7) == 0 ) {
            noConns = (int) SvIV(value);
        }
        break;

    case 'o':
        if (memcmp( flag, "onlyserver", 10) == 0 ) {
            onlyServer = (int) SvIV(value);
        } else if (memcmp( flag, "onlyclient", 10) == 0 ) {
            onlyClient = (int) SvIV(value);
        } else if (memcmp( flag, "onlyhost", 8) == 0 ) {
            char *name = (char *) SvPV(value, PL_na);
        struct hostent *th;
        th = hostutil_GetHostByName(name);
        if (!th) {
            sprintf(buffer, "rxdebug: host %s not found in host table", name);
            BSETCODE(-1, buffer);
            XSRETURN_UNDEF;
        }
        memcpy(&onlyHost, th->h_addr, sizeof(afs_int32));

        } else if (memcmp( flag, "onlyauth", 8) == 0 ) {
            char *name = (char *) SvPV(value, PL_na);
            if (strcmp (name, "clear") == 0) onlyAuth = 0;
            else if (strcmp (name, "auth") == 0) onlyAuth = 1;
            else if (strcmp (name, "crypt") == 0) onlyAuth = 2;
            else if ((strcmp (name, "null") == 0) ||
                     (strcmp (name, "none") == 0) ||
                     (strncmp (name, "noauth", 6) == 0) ||
                     (strncmp (name, "unauth", 6) == 0)) onlyAuth = -1;
            else {
              sprintf (buffer, "Unknown authentication level: %s", name);
              BSETCODE(-1, buffer);
              XSRETURN_UNDEF;
            }

        } else if (memcmp( flag, "onlyport", 8) == 0 ) {
            char *name = (char *) SvPV(value, PL_na);
            if ((onlyPort = rxdebug_PortNumber(name)) == -1)
              onlyPort = rxdebug_PortName(name);
            if (onlyPort == -1) {
              sprintf(buffer, "rxdebug: can't resolve port name %s", name);
              VSETCODE(-1, buffer);
              XSRETURN_UNDEF;
            }
        }

        break;

    case 'p':
        if (memcmp( flag, "port", 4) == 0 ) {
            portName = (char *) SvPV(value, PL_na);
        } else if (memcmp( flag, "peers", 5) == 0 ) {
           showPeers  = (int) SvIV(value);
        }
        break;

    case 'r':
        if (memcmp( flag, "rxstats", 7) == 0 ) {
            rxstats = (int) SvIV(value);
        }
        break;

    case 's':
        if (memcmp( flag, "servers", 7) == 0 ) {
            hostName = (char *) SvPV(value, PL_na);
        }
        break;

    case 'v':
        if (memcmp( flag, "version", 7) == 0 ) {
            version_flag = (int) SvIV(value);
        }
        break;

    default:
        break;
    } /* switch */
  } /* while */
  /*  fprintf(STDERR, "Done parsing args\n\n"); */

    /* lookup host */
    if (hostName) {
      th = hostutil_GetHostByName(hostName);
      if (!th) {
        sprintf(buffer, "rxdebug: host %s not found in host table", hostName);
        VSETCODE(-1, buffer);
        XSRETURN_UNDEF;
      }
      memcpy(&host, th->h_addr, sizeof(afs_int32));
    }
    else host = htonl(0x7f000001);    /* IP localhost */

    if (!portName)
      port = htons(7000);        /* default is fileserver */
    else {
      if ((port = rxdebug_PortNumber(portName)) == -1)
        port = rxdebug_PortName(portName);
      if (port == -1) {
        sprintf(buffer, "rxdebug: can't resolve port name %s", portName);
        VSETCODE(-1, buffer);
        XSRETURN_UNDEF;
      }
    }

    dallyCounter = 0;
    hostAddr.s_addr = host;
    /* add address and port to RETVAL hash */
    hv_store(RETVAL, "address", 7, newSVpv(inet_ntoa(hostAddr), 0), 0);
    hv_store(RETVAL, "port", 4, newSViv(ntohs(port)), 0);
    s = socket(AF_INET, SOCK_DGRAM, 0);
    taddr.sin_family = AF_INET;
    taddr.sin_port = 0;
    taddr.sin_addr.s_addr = 0;
#ifdef STRUCT_SOCKADDR_HAS_SA_LEN
    taddr.sin_len = sizeof(struct sockaddr_in);
#endif
    code = bind(s, (struct sockaddr *) &taddr, sizeof(struct sockaddr_in));
    FSSETCODE(code);
    if (code) {
      perror("bind");
      XSRETURN_UNDEF;
    }

    if (version_flag) /* add version to RETVAL and finish */
    {
       code = rx_GetServerVersion(s, host, port, length, version);
       if (code < 0)
       {
          sprintf(buffer, "get version call failed with code %d, errno %d",code,errno);
          BSETCODE(code, buffer);
          XSRETURN_UNDEF;
       }
       hv_store(RETVAL, "version", 7, newSVpv(version, 0), 0);
       goto done;
    }

    code = rx_GetServerDebug(s, host, port, &tstats, &supportedDebugValues);

    if (code < 0) {
      sprintf(buffer, "getstats call failed with code %d", code);
      BSETCODE(code, buffer);
      XSRETURN_UNDEF;
    }

    withSecStats = (supportedDebugValues & RX_SERVER_DEBUG_SEC_STATS);
    withAllConn = (supportedDebugValues & RX_SERVER_DEBUG_ALL_CONN);
    withRxStats = (supportedDebugValues & RX_SERVER_DEBUG_RX_STATS);
    withWaiters = (supportedDebugValues & RX_SERVER_DEBUG_WAITER_CNT);
    withIdleThreads = (supportedDebugValues & RX_SERVER_DEBUG_IDLE_THREADS);
    withPeers = (supportedDebugValues & RX_SERVER_DEBUG_ALL_PEER);

    TSTATS = newHV();
    hv_store(TSTATS, "nFreePackets", 12, newSViv(tstats.nFreePackets), 0);
    hv_store(TSTATS, "packetReclaims", 14, newSViv(tstats.packetReclaims), 0);
    hv_store(TSTATS, "callsExecuted", 13, newSViv(tstats.callsExecuted), 0);
    hv_store(TSTATS, "usedFDs", 7, newSViv(tstats.usedFDs), 0);
    hv_store(TSTATS, "waitingForPackets", 17, newSViv(tstats.waitingForPackets), 0);
    hv_store(TSTATS, "version", 7, newSViv(tstats.version), 0);
    if (withWaiters)
       hv_store(TSTATS, "nWaiting", 8, newSViv(tstats.nWaiting), 0);
    if ( withIdleThreads )
       hv_store(TSTATS, "idleThreads", 11, newSViv(tstats.idleThreads), 0);
    hv_store(RETVAL, "tstats", 6, newRV_inc((SV*)(TSTATS)), 0);

    /* get rxstats if requested, and supported by the server */
    /* hash containing stats added at key 'rxstats' in RETVAL */
    if (rxstats)
    {
      if (!withRxStats)
      {
        noRxStats:
        withRxStats = 0;
        warn("WARNING: Server doesn't support retrieval of Rx statistics\n");
      }
      else {
        struct rx_statistics rxstats;

        /* should gracefully handle the case where rx_statistics grows */
        code = rx_GetServerStats(s, host, port, &rxstats, &supportedStatValues);
        if (code < 0) {
          sprintf(buffer, "rxstats call failed with code %d", code);
          VSETCODE(code, buffer);
          XSRETURN_UNDEF;
        }
        if (code != sizeof(rxstats)) {
          if ((((struct rx_debugIn *)(&rxstats))->type == RX_DEBUGI_BADTYPE))
            goto noRxStats;
          warn("WARNING: returned Rx statistics of unexpected size (got %d)\n", code);
          /* handle other versions?... */
        }

        RXSTATS = newHV();

        myPrintTheseStats(RXSTATS, &rxstats);

        hv_store(RETVAL, "rxstats", 7, newRV_inc((SV*)(RXSTATS)), 0);

      }
    }

    /* get connections unless -noconns flag was set */
    /* array of connections added at key 'connections' in RETVAL hash */
    if (!noConns) {
      if (allconns) {
        if (!withAllConn) {
          warn("WARNING: Server doesn't support retrieval of all connections,\n");
          warn("         getting only interesting instead.\n");
          }
      }

      CONNECTIONS = newAV();
      index = 0;
      for ( i = 0; ; i++) {
        code = rx_GetServerConnections(s, host, port, &nextconn, allconns,
                      supportedDebugValues, &tconn,
                      &supportedConnValues);
        if (code < 0) {
          warn("getconn call failed with code %d\n", code);
          break;
        }
        if (tconn.cid == 0xffffffff) {
          break;
        }

        /* see if we're in nodally mode and all calls are dallying */
        if (nodally) {
          flag = 0;
          for (j = 0; j < RX_MAXCALLS; j++) {
             if (tconn.callState[j] != RX_STATE_NOTINIT &&
                 tconn.callState[j] != RX_STATE_DALLY) {
               flag = 1;
               break;
             }
          }
          if (flag == 0) {
            /* this call looks too ordinary, bump skipped count and go
             * around again */
            dallyCounter++;
            continue;
          }
        }
        if ((onlyHost != -1) && (onlyHost != tconn.host)) continue;
        if ((onlyPort != -1) && (onlyPort != tconn.port)) continue;
        if (onlyServer && (tconn.type != RX_SERVER_CONNECTION)) continue;
        if (onlyClient && (tconn.type != RX_CLIENT_CONNECTION)) continue;
        if (onlyAuth != 999) {

src/Monitor.xs  view on Meta::CPAN


        if (withSecStats) {
          HV* SECSTATS = newHV();
          hv_store(SECSTATS, "type", 4,
                   newSViv(tconn.secStats.type), 0);
          hv_store(SECSTATS, "level", 5,
                   newSViv(tconn.secStats.level), 0);
          hv_store(SECSTATS, "flags", 5,
                   newSViv(tconn.secStats.flags), 0);
          hv_store(SECSTATS, "expires", 7,
                   newSViv(tconn.secStats.expires), 0);
          hv_store(SECSTATS, "packetsReceived", 15,
                   newSViv(tconn.secStats.packetsReceived), 0);
          hv_store(SECSTATS, "packetsSent", 11,
                   newSViv(tconn.secStats.packetsSent), 0);
          hv_store(SECSTATS, "bytesReceived", 13,
                   newSViv(tconn.secStats.bytesReceived), 0);
          hv_store(SECSTATS, "bytesSent", 9,
                   newSViv(tconn.secStats.bytesSent), 0);
          hv_store(TCONN, "secStats", 8, newRV_inc((SV*)(SECSTATS)), 0);
        }

        CALLSTATE = newAV();
        av_fill(CALLSTATE, RX_MAXCALLS-1);
        CALLMODE = newAV();
        av_fill(CALLMODE, RX_MAXCALLS-1);
        CALLFLAGS = newAV();
        av_fill(CALLFLAGS, RX_MAXCALLS-1);
        CALLOTHER = newAV();
        av_fill(CALLOTHER, RX_MAXCALLS-1);
        CALLNUMBER = newAV();
        av_fill(CALLNUMBER, RX_MAXCALLS-1);

        for (j = 0; j < RX_MAXCALLS; j++) {
          av_store(CALLSTATE, j, newSViv(tconn.callState[j]));
          av_store(CALLMODE, j, newSViv(tconn.callMode[j]));
          av_store(CALLFLAGS, j, newSViv(tconn.callFlags[j]));
          av_store(CALLOTHER, j, newSViv(tconn.callOther[j]));
          av_store(CALLNUMBER, j, newSViv(tconn.callNumber[j]));
        }

        hv_store(TCONN, "callState", 9, newRV_inc((SV*)(CALLSTATE)), 0);
        hv_store(TCONN, "callMode", 8, newRV_inc((SV*)(CALLMODE)), 0);
        hv_store(TCONN, "callFlags", 9, newRV_inc((SV*)(CALLFLAGS)), 0);
        hv_store(TCONN, "callOther", 9, newRV_inc((SV*)(CALLOTHER)), 0);
        hv_store(TCONN, "callNumber", 10, newRV_inc((SV*)(CALLNUMBER)), 0);

        av_store(CONNECTIONS, index, newRV_inc((SV*)(TCONN)));
        index++;
      } /* end of for loop */
      if (nodally) hv_store(RETVAL, "dallyCounter", 12, newSViv(dallyCounter), 0);
      hv_store(RETVAL, "connections", 11, newRV_inc((SV*)(CONNECTIONS)), 0);
    } /* end of if (!noConns) */

    /* get peers if requested */
    /* array of peers added at key 'peers' in RETVAL hash */
    if (showPeers && withPeers) {
      PEERS = newAV();
      index = 0;
      for (i = 0; ; i++) {
        struct rx_debugPeer tpeer;
        code = rx_GetServerPeers(s, host, port, &nextpeer, allconns,
                                 &tpeer, &supportedPeerValues);
        if (code < 0) {
          warn("getpeer call failed with code %d\n", code);
          break;
        }
        if (tpeer.host == 0xffffffff) {
          break;
        }

        if ((onlyHost != -1) && (onlyHost != tpeer.host)) continue;
        if ((onlyPort != -1) && (onlyPort != tpeer.port)) continue;

        TPEER = newHV();

        hostAddr.s_addr = tpeer.host;
        hv_store(TPEER, "host", 4, newSVpv(inet_ntoa(hostAddr), 0), 0);
        hv_store(TPEER, "port", 4, newSViv(ntohs(tpeer.port)), 0);

        hv_store(TPEER, "ifMTU", 5, newSViv(tpeer.ifMTU), 0);
        hv_store(TPEER, "natMTU", 6, newSViv(tpeer.natMTU), 0);
        hv_store(TPEER, "maxMTU", 6, newSViv(tpeer.maxMTU), 0);
        hv_store(TPEER, "nSent", 5, newSViv(tpeer.nSent), 0);
        hv_store(TPEER, "reSends", 7, newSViv(tpeer.reSends), 0);

        BYTESSENT = newHV();
        hv_store(BYTESSENT, "high", 4, newSViv(tpeer.bytesSent.high), 0);
        hv_store(BYTESSENT, "low", 3, newSViv(tpeer.bytesSent.low), 0);
        hv_store(TPEER, "bytesSent", 9, newRV_inc((SV*)(BYTESSENT)), 0);

        BYTESRECEIVED = newHV();
        hv_store(BYTESRECEIVED, "high", 4, newSViv(tpeer.bytesReceived.high), 0);
        hv_store(BYTESRECEIVED, "low", 3, newSViv(tpeer.bytesReceived.low), 0);
        hv_store(TPEER, "bytesReceived", 13, newRV_inc((SV*)(BYTESRECEIVED)), 0);

        hv_store(TPEER, "rtt", 3, newSViv(tpeer.rtt), 0);
        hv_store(TPEER, "rtt_dev", 7, newSViv(tpeer.rtt_dev), 0);

        TIMEOUT = newHV();
        hv_store(TIMEOUT, "sec", 3, newSViv(tpeer.timeout.sec), 0);
        hv_store(TIMEOUT, "usec", 4, newSViv(tpeer.timeout.usec), 0);
        hv_store(TPEER, "timeout", 7, newRV_inc((SV*)(TIMEOUT)), 0);

        if (showLong) {
          hv_store(TPEER, "inPacketSkew", 12,
                   newSViv(tpeer.inPacketSkew), 0);
          hv_store(TPEER, "outPacketSkew", 13,
                   newSViv(tpeer.outPacketSkew), 0);
          hv_store(TPEER, "cwind", 5,
                   newSViv(tpeer.cwind), 0);
          hv_store(TPEER, "MTU", 3,
                   newSViv(tpeer.MTU), 0);
          hv_store(TPEER, "nDgramPackets", 13,
                   newSViv(tpeer.nDgramPackets), 0);
          hv_store(TPEER, "ifDgramPackets", 14,
                   newSViv(tpeer.ifDgramPackets), 0);
          hv_store(TPEER, "maxDgramPackets", 15,
                   newSViv(tpeer.maxDgramPackets), 0);
        }



( run in 1.123 second using v1.01-cache-2.11-cpan-9581c071862 )