XRootD
XrdXrootdTpcMon.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d X r o o t d T p c M o n . c c */
4 /* */
5 /* (c) 2022 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 
34 #include "XrdSys/XrdSysError.hh"
37 
38 /******************************************************************************/
39 /* J s o n T e m p l a t e */
40 /******************************************************************************/
41 
42 namespace
43 {
44 const char *json_fmt = "{\"TPC\":\"%s\",\"Client\":\"%s\","
45 "\"Xeq\":{\"Beg\":\"%s\",\"End\":\"%s\",\"RC\":%d,\"Strm\":%u,\"Type\":\"%s\","
46  "\"IPv\":%c},"
47 "\"Src\":\"%s\",\"Dst\":\"%s\",\"Size\":%zu}";
48 
49 const char *hostport = "";
50 
51 XrdSysError eDest(0, "Ouc");
52 }
53 
54 /******************************************************************************/
55 /* C o n s t r u c t o r */
56 /******************************************************************************/
57 
59  XrdSysLogger *logP,
60  XrdXrootdGStream &gStrm)
61  : protocol(proto), gStream(gStrm)
62 {
63  const char *colon = ":";
64  char buff[512];
65 
66 // Initialize eror object
67 //
68  eDest.logger(logP);
69 
70 // Get our host:port
71 //
72  const char *host = getenv("XRDHOST"); if (!host) host = "localhost";
73  const char *port = getenv("XRDPORT"); if (!port) {colon = ""; port = "";}
74 
75  snprintf(buff, sizeof(buff), "%s%s%s", host, colon, port);
76  hostport = strdup(buff);
77 }
78 
79 /******************************************************************************/
80 /* Private: g e t U R L */
81 /******************************************************************************/
82 
83 const char *XrdXrootdTpcMon::getURL(const char *spec, const char *prot,
84  char *buff, int bsz)
85 {
86 // Handle the spec
87 //
88  if (*spec == '/')
89  {snprintf(buff, bsz, "%s://%s/%s", prot, hostport, spec);
90  spec = buff;
91  }
92  return spec;
93 }
94 
95 /******************************************************************************/
96 /* Private: g e t U T C */
97 /******************************************************************************/
98 
99 const char *XrdXrootdTpcMon::getUTC(struct timeval& tod,
100  char* utcBuff, int utcBLen)
101 {
102  struct tm utcDT;
103  char *bP;
104 
105 // Get the time in UTC
106 //
107  gmtime_r(&tod.tv_sec, &utcDT);
108 
109 // Format this ISO 8601 style
110 //
111  size_t n = strftime(utcBuff, utcBLen, "%FT%T", &utcDT);
112  bP = utcBuff + n; utcBLen -= n;
113  snprintf(bP, utcBLen, ".%03uZ", static_cast<unsigned int>(tod.tv_usec));
114 
115 // Return result
116 //
117  return utcBuff;
118 }
119 
120 /******************************************************************************/
121 /* R e p o r t */
122 /******************************************************************************/
123 
125 {
126  const char *srcURL, *dstURL;
127  char bt_buff[40], et_buff[40], sBuff[1024], dBuff[1024], buff[8192];
128 
129 // Get correct source and destination URLs
130 //
131  srcURL = getURL(info.srcURL, protocol, sBuff, sizeof(sBuff));
132  dstURL = getURL(info.dstURL, protocol, dBuff, sizeof(dBuff));
133 
134 // Format the line
135 //
136  int n = snprintf(buff, sizeof(buff), json_fmt, protocol, info.clID,
137  getUTC(info.begT, bt_buff, sizeof(bt_buff)),
138  getUTC(info.endT, et_buff, sizeof(et_buff)),
139  info.endRC, static_cast<unsigned int>(info.strm),
140  (info.opts & TpcInfo::isaPush ? "push" : "pull"),
141  (info.opts & TpcInfo::isIPv4 ? '4' : '6'),
142  srcURL, dstURL, info.fSize);
143 
144 // Check for truncation
145 //
146  if (n >= (int)sizeof(buff))
147  eDest.Emsg("TpcMon", protocol, "invalid json; line truncated!");
148 
149 // Send the message
150 //
151  if (!gStream.Insert(buff, n+1))
152  eDest.Emsg("TpcMon", protocol, "invalid json; gStream buffer rejected!");
153 }
static XrdSysError eDest(0,"crypto_")
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
XrdSysLogger * logger(XrdSysLogger *lp=0)
Definition: XrdSysError.hh:141
bool Insert(const char *data, int dlen)
void Report(TpcInfo &info)
XrdXrootdTpcMon(const char *proto, XrdSysLogger *logP, XrdXrootdGStream &gStrm)
static const int isaPush