AFS-Monitor

 view release on metacpan or  search on metacpan

src/Monitor.xs  view on Meta::CPAN

/***********************************************************************
 *
 * AFS.xs - AFS extensions for Perl
 *
 * RCS-Id: @(#)$Id: Monitor.xs,v 1.2 2006/07/05 22:25:10 alfw Exp $
 *
 * Copyright (c) 2003, International Business Machines Corporation and others.
 *
 * This software has been released under the terms of the IBM Public
 * License.  For details, see the IBM-LICENSE file in the LICENCES
 * directory or online at http://www.openafs.org/dl/license10.html
 *
 * Contributors
 *         2004-2006: Elizabeth Cassell <e_a_c@mailsnare.net>
 *                    Alf Wachsmann <alfw@slac.stanford.edu>
 *
 * The code for the original library were mainly taken from the AFS
 * source distribution, which comes with this message:
 *
 *    Copyright (C) 1989-1994 Transarc Corporation - All rights reserved
 *    P_R_P_Q_# (C) COPYRIGHT IBM CORPORATION 1987, 1988, 1989
 *
 ***********************************************************************/

#include "EXTERN.h"

#ifdef __sgi    /* needed to get a clean compile */
#include <setjmp.h>
#endif

#include <stdarg.h>

#include "perl.h"
#include "XSUB.h"
#include "ppport.h"

#include <afs/afsint.h>

#include <afs/kautils.h>

#include <afs/xstat_fs.h>
#include <afs/xstat_cm.h>
#include "afsmon-labels.h"  /* labels for afsmonitor variables */

#include <afs/fsprobe.h>

/* from volser/volser_prototypes.h */
extern void MapPartIdIntoName(afs_int32 partId, char *partName);


#if defined(AFS_3_4) || defined(AFS_3_5)
#else
#define int32 afs_int32
#define uint32 afs_uint32
#endif

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;
{

src/Monitor.xs  view on Meta::CPAN

   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);
      hv_store(TYPE, "packetsSent", 11, newSViv(rxstats->packetsSent[i]), 0);
      if (packet_type == "unused") {
         /* rename "unused" types */
         /* can't have several entries in a hash with same name */
         char packet_type_unused[7];
         sprintf(packet_type_unused, "unused%d", num_unused);
         packet_type = packet_type_unused;
         num_unused++;
      }
      hv_store(PACKETS, packet_type, strlen(packet_type),
               newRV_inc((SV *) (TYPE)), 0);

src/Monitor.xs  view on Meta::CPAN


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

/* structure of each threshold item */
struct Threshold {
   char itemName[THRESH_VAR_NAME_LEN];  /* field name */
   int index;                   /* positional index */
   char threshVal[THRESH_VAR_LEN];  /* user provided threshold value */
   char handler[256];           /* user provided ovf handler */
};


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

/* structures to store info of hosts to be monitored */
struct afsmon_hostEntry {
   char hostName[HOST_NAME_LEN];    /* fs or cm host name */
   int numThresh;               /* number of thresholds for this host */
   struct Threshold *thresh;    /* ptr to threshold entries */
   struct afsmon_hostEntry *next;
};


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

/* structures to store statistics in a format convenient to dump to the
screen */
/* for file servers */
struct fs_Display_Data {
   char hostName[HOST_NAME_LEN];
   int probeOK;                 /* 0 => probe failed */
   char data[NUM_FS_STAT_ENTRIES][FS_STAT_STRING_LEN];
   short threshOvf[NUM_FS_STAT_ENTRIES];    /* overflow flags */
   int ovfCount;                /* overflow count */
};
/* for cache managers */
struct cm_Display_Data {
   char hostName[HOST_NAME_LEN];
   int probeOK;                 /* 0 => probe failed */
   char data[NUM_CM_STAT_ENTRIES][CM_STAT_STRING_LEN];
   short threshOvf[NUM_CM_STAT_ENTRIES];    /* overflow flags */
   int ovfCount;                /* overflow count */
};



/*
 * 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 $");
 */

/* wouldn't compile without this, but it wasn't in original afsmonitor.c */
#if !defined(__USE_GNU) && !defined(__APPLE_CC__)
char *
strcasestr(char *s1, char *s2)
{
   char *ptr;
   int len1, len2;

   len1 = strlen(s1);
   len2 = strlen(s2);

   if (len1 < len2)
      return ((char *)NULL);

   ptr = s1;

   while (len1 >= len2 && len1 > 0) {
      if ((strncasecmp(ptr, s2, len2)) == 0)
         return (ptr);
      ptr++;
      len1--;
   }
   return ((char *)NULL);
}
#endif           /* __USE_GNU */


/*
 * 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 $");
 */

struct hostent *
GetHostByName(char *name)
{
   struct hostent *he;
#ifdef AFS_SUN5_ENV
   char ip_addr[32];
#endif

   he = gethostbyname(name);
#ifdef AFS_SUN5_ENV
   /* On solaris the above does not resolve hostnames to full names */
   if (he != NULL) {
      memcpy(ip_addr, he->h_addr, he->h_length);
      he = gethostbyaddr(ip_addr, he->h_length, he->h_addrtype);
   }
#endif
   return (he);
}


/*
 * 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 $");
 */

/*
 * Constructs a string to pass back to Perl for easy execution of the threshold handler.
 * DOES NOT execute the threshold handler.
 *

src/Monitor.xs  view on Meta::CPAN

    struct hostent *he = 0;     /* host entry */
    afs_int32 *collIDP = 0;     /* ptr to collection ID */
    int numCollIDs = 0;         /* number of collection IDs */
    /* end of from afsmon_execute() */

    AV* FILESERV = newAV(); /* File Servers */
    AV* CACHEMAN = newAV(); /* Cache Managers */

    struct afsmon_hostEntry *FSnameList=0;
    struct afsmon_hostEntry *CMnameList=0;

    /* initialize showFlags for processing "show" directives in config file */
    for(i=0; i<NUM_FS_STAT_ENTRIES; i++)
      fs_showFlags[i] = 0;
    for(i=0; i<NUM_CM_STAT_ENTRIES; i++)
      cm_showFlags[i] = 0;

    /* parse arguments */
    num_args = hv_iterinit(args);
    /* fprintf(STDERR, "[afsmonitor] Parsing args now: %d\n", num_args); */
    while (num_args--) {
      value = hv_iternextsv(args, &key, &keylen);

      /* fprintf(STDERR, "got flag %s, size %d. %d remaining.\n", key, keylen, num_args); */

      switch (*key) {
        case 'c':
          if(keylen < 2) goto unrecognized;
          switch(key[1]) {
            case 'o':
              if(strncmp(key, "config", keylen) == 0 && keylen <= 6) {
                /* fprintf(STDERR, "flag %s recognized as config; value is %s\n",
                   key, (char *) SvPV(value, PL_na)); */
                config_filename = (char *) SvPV(value, PL_na);
              } else goto unrecognized;
            break;

            case 'm':
              if(keylen < 3) goto unrecognized;
              switch(key[2]) {
                case 'h':
                 if(strncmp(key, "cmhosts", keylen) == 0 && keylen <= 7) {
                   /* fprintf(STDERR, "flag %s recognized as cmhosts\n", key); */
                  if (SvROK(value))
                    host_array = (AV*) SvRV(value);
                  else {
                    host_array = av_make(1, &value);
                    sv_2mortal((SV *) host_array);
                  }
                   num = av_len(host_array);
                   /* fprintf(STDERR, "it has %d elements.\n", num+1); */
                   for(i=0; i<=num; i++) {
                     host = (char *) SvPV(*av_fetch(host_array, i, 0), PL_na);
                     sprintf(buffer,"cm %s",host);
                     code = my_parse_hostEntry(buffer, &numFS, &numCM, &lastHostType,
                                            &last_hostEntry, &FSnameList, &CMnameList, buff2);
                     /* fprintf(STDERR, "got host: %s\n", host); */
                     if (code) {
                       sprintf(buffer, "Could not parse cache manager %s. %s", host, buff2);
                       BSETCODE(180, buffer);
                       /* 180 is the exit code for this error in the original afsmonitor.c */
                       XSRETURN_UNDEF;
                     }
                   }
                 } else goto unrecognized;
                 break;
                case 's':
                 if(strncmp(key, "cmshow", keylen) == 0 && keylen <= 6) {
                   /* fprintf(STDERR, "flag %s recognized as cmshow\n", key); */
                   show_array = (AV*) SvRV(value);
                   num = av_len(show_array);
                   for (i=0; i<=num; i++) {
                     sprintf(buffer, "show cm %s", SvPV(*av_fetch(show_array, i, 0), PL_na));
                     code = my_parse_showEntry(buffer, &fs_showDefault, &cm_showDefault,
                                               fs_showFlags, cm_showFlags, buff2);
                     if(code) {
                       sprintf(buffer, "Error parsing cmshow flag. %s", buff2);
                       BSETCODE(-1, buffer);
                       XSRETURN_UNDEF;
                     }
                   }
                 } else goto unrecognized;
                 break;
                case 't':
                 if(strncmp(key, "cmthresh", keylen) == 0 && keylen <= 8) {
                   /* fprintf(STDERR, "flag %s recognized as cmthresh\n", key); */
                   cmthresh_array = (AV*) SvRV(value);
                 } else goto unrecognized;
                 break;
                default:
                 goto unrecognized;
              }
            break;

            default:
              goto unrecognized;
          }
          break;

        case 'd':
          if(strncmp(key, "detailed", keylen) == 0 && keylen <= 8) {
            /* fprintf(STDERR, "flag %s recognized as detailed; value is %d\n",
                       key, (int) SvIV(value)); */
            detailed = (int) SvIV(value);
          } else goto unrecognized;
        break;

        case 'f':
          if(keylen < 3 || key[1] != 's') goto unrecognized;
          switch(key[2]) {
            case 'h':
              if(strncmp(key, "fshosts", keylen) == 0 && keylen <= 7) {
                /* fprintf(STDERR, "flag %s recognized as fshosts\n", key); */
                if (SvROK(value))
                  host_array = (AV*) SvRV(value);
                else {
                  host_array = av_make(1, &value);
                  sv_2mortal((SV *) host_array);
                }
                num = av_len(host_array);
                /* fprintf(STDERR, "it has %d elements.\n", num+1); */

src/Monitor.xs  view on Meta::CPAN

                num = av_len(show_array);
                for (i=0; i<=num; i++) {
                  sprintf(buffer, "show fs %s", SvPV(*av_fetch(show_array, i, 0), PL_na));
                  code = my_parse_showEntry(buffer, &fs_showDefault, &cm_showDefault,
                                            fs_showFlags, cm_showFlags, buff2);
                  if(code) {
                    sprintf(buffer, "Error parsing fsshow flag. %s", buff2);
                    BSETCODE(-1, buffer);
                    XSRETURN_UNDEF;
                  }
                }
              } else goto unrecognized;
              break;
            case 't':
              if(strncmp(key, "fsthresh", keylen) == 0 && keylen <= 8) {
                /* fprintf(STDERR, "flag %s recognized as fsthresh\n", key); */
                fsthresh_array = (AV*) SvRV(value);
              } else goto unrecognized;
              break;
            default:
              goto unrecognized;
          }
        break;

        case 'o':
          if(strncmp(key, "output", keylen) == 0 && keylen <= 6) {
            /* fprintf(STDERR, "flag %s recognized as output; value is %s\n",
                       key, (char *) SvPV(value, PL_na)); */
            output_filename = (char *) SvPV(value, PL_na);
          } else goto unrecognized;
        break;

        default:
          unrecognized:
          /* fprintf(STDERR,
                     "flag not recognized. (key: %s) (value: %s)\n",
                     key, (char *) SvPV(value, PL_na)); */
          sprintf(buffer, "Unrecognized flag: %s", key);
          BSETCODE(-1, buffer);
          XSRETURN_UNDEF;
      } /* end switch */
    } /* end while */
    /* done parsing arguments */

    /* Open output file, if provided. */
    if (output_filename) {
      outputFD = fopen(output_filename,"a");
      if (outputFD == (FILE *) NULL) {
        sprintf(buffer, "Failed to open output file %s", output_filename);
        BSETCODE(160, buffer);
        XSRETURN_UNDEF;
      }
      fclose (outputFD);
    }

    /* cannot use 'detailed' without 'output' */
    if (detailed) {
      if (!output_filename) {
        sprintf(buffer, "detailed switch can be used only with output switch");
        BSETCODE(165, buffer);
        /* 165 is the exit code for this error in the original afsmonitor.c */
        XSRETURN_UNDEF;
      }
    }

    /* The config option is mutually exclusive with the fshosts,cmhosts options */
    if (config_filename) {
      if (numFS || numCM) {
        sprintf(buffer,"Cannot use config option with fshosts or cmhosts");
        BSETCODE(170, buffer);
        /* 170 is the exit code for this error in the original afsmonitor.c */
        XSRETURN_UNDEF;
      }
    }
    else {
      if (!numFS && !numCM) {
        sprintf(buffer,"Must specify either config or (fshosts and/or cmhosts) options");
        BSETCODE(175, buffer);
        /* 175 is the exit code for this error in the original afsmonitor.c */
        XSRETURN_UNDEF;
      }
    }

    if (fsthresh_array) {
      if(!numFS) {
        sprintf(buffer, "Cannot use fsthresh option without specifying fshosts");
        BSETCODE(-1, buffer);
        XSRETURN_UNDEF;
      }
      num = av_len(fsthresh_array);
      for (i=0; i<=num; i++) {
        thresh_host = 0;
        thresh_handler = "";
        thresh_entry = (HV*) SvRV(*av_fetch(fsthresh_array, i, 0));
        hv_iterinit(thresh_entry);
        while((value = hv_iternextsv(thresh_entry, &key, &keylen))) {
          if(strcmp(key, "host")==0) {
            thresh_host = (char *)SvPV(value, PL_na);
            he = GetHostByName(thresh_host);
            if(he == (struct hostent *) NULL) {
              sprintf(buffer,
                      "Couldn't parse fsthresh flag; unable to resolve hostname %s\n",
                      thresh_host);
              BSETCODE(-1, buffer);
              XSRETURN_UNDEF;
            }
            thresh_host = he->h_name;
          }
          else if(strcmp(key, "handler")==0) {
            thresh_handler = (char *) SvPV(value, PL_na);
          }
          else {
            thresh_name = key;
            thresh_value = (char *) SvPV(value, PL_na);
          }
        }
        sprintf(buffer, "thresh fs %s %s %s",
                thresh_name, thresh_value, thresh_handler);
        if(!thresh_host) {
          code = my_parse_threshEntry(buffer, &global_fsThreshCount,
                      &global_cmThreshCount, (struct afsmon_hostEntry *) NULL, 0, buff2);
          if (code) {
            sprintf(buffer, "Couldn't parse fsthresh entry. %s", buff2);
            BSETCODE(code, buffer);
            XSRETURN_UNDEF;
          }
        }
        else {
          temp_host = FSnameList;
          found = 0;
          for (j = 0; j < numFS; j++) {
            if(strcmp(thresh_host, temp_host->hostName) == 0) {
              found = 1;
              break;
            }
            temp_host = temp_host->next;
          }
          if(found) {
            code = my_parse_threshEntry(buffer, &global_fsThreshCount,



( run in 0.898 second using v1.01-cache-2.11-cpan-39bf76dae61 )