XrdFrmMonitor.cc

Go to the documentation of this file.
00001 /******************************************************************************/
00002 /*                                                                            */
00003 /*                      X r d F r m M o n i t o r . c c                       */
00004 /*                                                                            */
00005 /* (c) 2010 by the Board of Trustees of the Leland Stanford, Jr., University  */
00006 /*                            All Rights Reserved                             */
00007 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00008 /*              DE-AC03-76-SFO0515 with the Department of Energy              */
00009 /******************************************************************************/
00010   
00011 //       $Id: XrdFrmMonitor.cc 35287 2010-09-14 21:19:35Z ganis $
00012 
00013 const char *XrdFrmMonitorCVSID = "$Id: XrdFrmMonitor.cc 35287 2010-09-14 21:19:35Z ganis $";
00014 
00015 #include <errno.h>
00016 #include <stdlib.h>
00017 #include <time.h>
00018 #include <unistd.h>
00019 #include <sys/socket.h>
00020 #include <sys/types.h>
00021 
00022 #include "XrdFrm/XrdFrmTrace.hh"
00023 #include "XrdFrm/XrdFrmMonitor.hh"
00024 #include "XrdNet/XrdNet.hh"
00025 #include "XrdNet/XrdNetDNS.hh"
00026 #include "XrdNet/XrdNetPeer.hh"
00027 #include "XrdSys/XrdSysError.hh"
00028 #include "XrdSys/XrdSysPlatform.hh"
00029 
00030 using namespace XrdFrm;
00031 
00032 /******************************************************************************/
00033 /*                     S t a t i c   A l l o c a t i o n                      */
00034 /******************************************************************************/
00035   
00036 char              *XrdFrmMonitor::Dest1      = 0;
00037 int                XrdFrmMonitor::monFD1     = -1;
00038 int                XrdFrmMonitor::monMode1   = 0;
00039 struct sockaddr    XrdFrmMonitor::InetAddr1;
00040 char              *XrdFrmMonitor::Dest2      = 0;
00041 int                XrdFrmMonitor::monFD2     = -1;
00042 int                XrdFrmMonitor::monMode2   = 0;
00043 struct sockaddr    XrdFrmMonitor::InetAddr2;
00044 kXR_int32          XrdFrmMonitor::startTime  = 0;
00045 int                XrdFrmMonitor::isEnabled  = 0;
00046 char               XrdFrmMonitor::monSTAGE   = 0;
00047 
00048 /******************************************************************************/
00049 /*                              D e f a u l t s                               */
00050 /******************************************************************************/
00051 
00052 void XrdFrmMonitor::Defaults(char *dest1, int mode1, char *dest2, int mode2)
00053 {
00054 
00055 // Make sure if we have a proper destinations and modes
00056 //
00057    if (dest1 && !mode1) {free(dest1); dest1 = 0; mode1 = 0;}
00058    if (dest2 && !mode2) {free(dest2); dest2 = 0; mode2 = 0;}
00059 
00060 // Propogate the destinations
00061 //
00062    if (!dest1)
00063       {mode1 = (dest1 = dest2) ? mode2 : 0;
00064        dest2 = 0; mode2 = 0;
00065       }
00066 
00067 // Set the default destinations (caller supplied strdup'd strings)
00068 //
00069    if (Dest1) free(Dest1);
00070    Dest1 = dest1; monMode1 = mode1;
00071    if (Dest2) free(Dest2);
00072    Dest2 = dest2; monMode2 = mode2;
00073 
00074 // Set overall monitor mode
00075 //
00076    monSTAGE  = ((mode1 | mode2) & XROOTD_MON_STAGE ? 1 : 0);
00077 
00078 // Do final check
00079 //
00080    isEnabled = (Dest1 == 0 && Dest2 == 0 ? 0 : 1);
00081 }
00082 
00083 /******************************************************************************/
00084 /*                                  I n i t                                   */
00085 /******************************************************************************/
00086   
00087 int XrdFrmMonitor::Init()
00088 {
00089    XrdNet     myNetwork(&Say, 0);
00090    XrdNetPeer monDest;
00091    char      *etext;
00092 
00093 // There is nothing to do unless we have been enabled via Defaults()
00094 //
00095    if (!isEnabled) return 1;
00096 
00097 // Get the address of the primary destination
00098 //
00099    if (!XrdNetDNS::Host2Dest(Dest1, InetAddr1, &etext))
00100       {Say.Emsg("Monitor", "setup monitor collector;", etext);
00101        return 0;
00102       }
00103 
00104 // Allocate a socket for the primary destination
00105 //
00106    if (!myNetwork.Relay(monDest, Dest1, XRDNET_SENDONLY)) return 0;
00107    monFD1 = monDest.fd;
00108 
00109 // Do the same for the secondary destination
00110 //
00111    if (Dest2)
00112       {if (!XrdNetDNS::Host2Dest(Dest2, InetAddr2, &etext))
00113           {Say.Emsg("Monitor", "setup monitor collector;", etext);
00114            return 0;
00115           }
00116        if (!myNetwork.Relay(monDest, Dest2, XRDNET_SENDONLY)) return 0;
00117        monFD2 = monDest.fd;
00118       }
00119 
00120 // All done
00121 //
00122    startTime = htonl(time(0));
00123    return 1;
00124 }
00125 
00126 /******************************************************************************/
00127 /*                                   M a p                                    */
00128 /******************************************************************************/
00129   
00130 kXR_unt32 XrdFrmMonitor::Map(const char code,
00131                                    const char *uname, const char *path)
00132 {
00133      static XrdSysMutex  seqMutex;
00134      static unsigned int monSeqID = 1;
00135      XrdXrootdMonMap     map;
00136      int                 size, montype;
00137      unsigned int        mySeqID;
00138 
00139 // Assign a unique ID for this entry
00140 //
00141    seqMutex.Lock();
00142    mySeqID = monSeqID++;
00143    seqMutex.UnLock();
00144 
00145 // Copy in the username and path
00146 //
00147    map.dictid = htonl(mySeqID);
00148    strcpy(map.info, uname);
00149    size = strlen(uname);
00150    if (path)
00151       {*(map.info+size) = '\n';
00152        strlcpy(map.info+size+1, path, sizeof(map.info)-size-1);
00153        size = size + strlen(path) + 1;
00154       }
00155 
00156 // Fill in the header
00157 //
00158    size = sizeof(XrdXrootdMonHeader)+sizeof(kXR_int32)+size;
00159    fillHeader(&map.hdr, code, size);
00160 
00161 // Route the packet to all destinations that need them
00162 //
00163         if (code == XROOTD_MON_MAPSTAG) montype = XROOTD_MON_STAGE;
00164    else                                 montype = XROOTD_MON_INFO;
00165    Send(montype, (void *)&map, size);
00166 
00167 // Return the dictionary id
00168 //
00169    return map.dictid;
00170 }
00171 
00172 /******************************************************************************/
00173 /*                       P r i v a t e   M e t h o d s                        */
00174 /******************************************************************************/
00175 /******************************************************************************/
00176 /*                            f i l l H e a d e r                             */
00177 /******************************************************************************/
00178   
00179 void XrdFrmMonitor::fillHeader(XrdXrootdMonHeader *hdr,
00180                                const char id, int size)
00181 {  static XrdSysMutex seqMutex;
00182    static int         seq = 0;
00183           int         myseq;
00184 
00185 // Generate a new sequence number
00186 //
00187    seqMutex.Lock();
00188    myseq = 0x00ff & (seq++);
00189    seqMutex.UnLock();
00190 
00191 // Fill in the header
00192 //
00193    hdr->code = static_cast<kXR_char>(id);
00194    hdr->pseq = static_cast<kXR_char>(myseq);
00195    hdr->plen = htons(static_cast<uint16_t>(size));
00196    hdr->stod = startTime;
00197 }
00198  
00199 /******************************************************************************/
00200 /*                                  S e n d                                   */
00201 /******************************************************************************/
00202   
00203 int XrdFrmMonitor::Send(int monMode, void *buff, int blen)
00204 {
00205     EPNAME("Send");
00206     static XrdSysMutex sendMutex;
00207     int rc1, rc2;
00208 
00209     sendMutex.Lock();
00210     if (monMode & monMode1 && monFD1 >= 0)
00211        {rc1  = (int)sendto(monFD1, buff, blen, 0,
00212                         (const struct sockaddr *)&InetAddr1, sizeof(sockaddr));
00213         DEBUG(blen <<" bytes sent to " <<Dest1 <<" rc=" <<(rc1 ? errno : 0));
00214        }
00215        else rc1 = 0;
00216     if (monMode & monMode2 && monFD2 >= 0)
00217        {rc2 = (int)sendto(monFD2, buff, blen, 0,
00218                         (const struct sockaddr *)&InetAddr2, sizeof(sockaddr));
00219         DEBUG(blen <<" bytes sent to " <<Dest2 <<" rc=" <<(rc2 ? errno : 0));
00220        }
00221        else rc2 = 0;
00222     sendMutex.UnLock();
00223 
00224     return (rc1 > rc2 ? rc1 : rc2);
00225 }

Generated on Tue Jul 5 14:46:35 2011 for ROOT_528-00b_version by  doxygen 1.5.1