XRootD
XrdQStats.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d Q S t a t s . c c */
4 /* */
5 /* (c) 2018 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 <iostream>
32 #include <cstdio>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/uio.h>
36 
37 #include "XrdApps/XrdMpxXml.hh"
38 #include "XrdCl/XrdClBuffer.hh"
39 #include "XrdCl/XrdClFileSystem.hh"
40 #include "XrdCl/XrdClURL.hh"
42 
43 
44 /******************************************************************************/
45 /* F a t a l */
46 /******************************************************************************/
47 
48 void Fatal(const XrdCl::XRootDStatus &Status)
49 {
50  std::string eText;
51 
52 // If this is an xrootd error then get the xrootd generated error
53 //
54  eText = (Status.code == XrdCl::errErrorResponse ?
55  eText = Status.GetErrorMessage() : Status.ToStr());
56 
57 // Blither and exit
58 //
59  std::cerr <<"xrdqstats: Unable to obtain statistic - " <<eText <<std::endl;
60  exit(3);
61 }
62 
63 /******************************************************************************/
64 /* U s a g e */
65 /******************************************************************************/
66 
67 void Usage(int rc)
68 {
69  std::cerr <<"\nUsage: xrdqstats [opts] <host>[:<port>]\n"
70  "\nopts: -f {cgi|flat|xml} -h -i <sec> -n <cnt> -s what -z\n"
71  "\n-f specify display format (default is wordy text format)."
72  "\n-i number of seconds to wait before between redisplays, default 10."
73  "\n-n number of redisplays; if -s > 0 and -n unspecified goes forever."
74  "\n-z does not display items with a zero value (wordy text only).\n"
75  "\nwhat: one or more of the following letters to select statistics:"
76  "\na - All (default) b - Buffer usage d - Device polling"
77  "\ni - Identification c - Connections p - Protocols"
78  "\ns - Scheduling u - Usage data z - Synchronized info"
79  <<std::endl;
80  exit(rc);
81 }
82 
83 /******************************************************************************/
84 /* m a i n */
85 /******************************************************************************/
86 
87 int main(int argc, char *argv[])
88 {
89  extern char *optarg;
90  extern int optind, opterr, optopt;
92  XrdMpxXml *xP = 0;
93  const char *Stats = "bldpsu", *pgm = "xrdqstats: ";
94  const char *valOpts = "df:i:n:s:z";
95  int WTime = 0, Count = 0;
96  char obuff[65536];
97  char *sP, c;
98  bool Debug = false, nozed = false;
99 
100 // Process the options
101 //
102  opterr = 0;
103  if (argc > 1 && '-' == *argv[1])
104  while ((c = getopt(argc,argv,valOpts)) && ((unsigned char)c != 0xff))
105  { switch(c)
106  {
107  case 'd': Debug = true;
108  break;
109  case 'f': if (!strcmp(optarg, "cgi" )) fType = XrdMpxXml::fmtCGI;
110  else if (!strcmp(optarg, "flat")) fType = XrdMpxXml::fmtFlat;
111  else if (!strcmp(optarg, "xml" )) fType = XrdMpxXml::fmtXML;
112  else {std::cerr <<pgm <<"Invalid format - " <<optarg <<std::endl;
113  Usage(1);
114  }
115  break;
116  case 'h': Usage(0);
117  break;
118  case 'i': if ((WTime = atoi(optarg)) <= 0)
119  {std::cerr <<pgm <<"Invalid interval - " <<optarg <<std::endl;
120  Usage(1);
121  }
122  break;
123  case 'n': if ((Count = atoi(optarg)) <= 0)
124  {std::cerr <<pgm <<"Invalid count - " <<optarg <<std::endl;
125  Usage(1);
126  }
127  break;
128  case 's': sP = optarg;
129  while(*sP)
130  {if (!index("abcdipsuz", *sP))
131  {std::cerr <<pgm<<"Invalid statistic letter - "<<*sP<<std::endl;
132  Usage(1);
133  } else if (*sP == 'c') *sP = 'l';
134  sP++;
135  }
136  Stats = optarg;
137  break;
138  case 'z': nozed = true;
139  break;
140  default: std::cerr <<pgm <<'-' <<char(optopt);
141  if (c == ':') std::cerr <<" value not specified." <<std::endl;
142  else std::cerr <<" option is invalid" <<std::endl;
143  Usage(1);
144  break;
145  }
146  }
147 
148 // Make sure host has been specified
149 //
150  if (optind >= argc)
151  {std::cerr <<pgm <<"Host has not been specified." <<std::endl; Usage(1);}
152 
153 // Construct the URL to get to the server
154 //
155  std::string sURL("root://");
156  sURL += argv[optind];
157  XrdCl::URL fsURL(sURL);
158  if (!fsURL.IsValid())
159  {std::cerr <<pgm <<"Invalid host specification - " <<argv[optind] <<std::endl;
160  Usage(1);
161  }
162 
163 // Establish what we wiil be asking for
164 //
165  XrdCl::Buffer wantStats;
166  wantStats.FromString(std::string(Stats));
167 
168 // Establish count and interval
169 //
170  if (!WTime && Count) WTime = 10;
171  else if (WTime && !Count) Count = -1;
172  else if (!WTime && !Count) Count = 1;
173 
174 // Establish format
175 //
176  if (fType != XrdMpxXml::fmtXML) xP = new XrdMpxXml(fType, nozed, Debug);
177 
178 // Create the file system to query the stats
179 //
180  XrdCl::FileSystem theFS(fsURL);
181 
182 // Perform statistics gathering and display
183 //
184  XrdCl::Buffer *theStats;
185  XrdCl::XRootDStatus xStatus;
186  while(Count--)
187  {xStatus = theFS.Query(XrdCl::QueryCode::Stats, wantStats, theStats);
188  if (!xStatus.IsOK()) Fatal(xStatus);
189  if (!xP) std::cout <<theStats->GetBuffer() <<std::endl;
190  else {int rc, wLen = xP->Format(0, theStats->GetBuffer(), obuff);
191  char *bP = obuff;
192  while(wLen > 0)
193  {do {rc = write(STDOUT_FILENO, bP, wLen);}
194  while(rc < 0 && errno == EINTR);
195  wLen -= rc; bP += rc;
196  }
197  }
198  delete theStats;
199  if (WTime) sleep(WTime);
200  if (Count) std::cout <<"\n";
201  }
202 
203 // All done
204 //
205  return 0;
206 }
int optopt
int optind
ssize_t write(int fildes, const void *buf, size_t nbyte)
int main(int argc, char *argv[])
Definition: XrdQStats.cc:87
void Usage(int rc)
Definition: XrdQStats.cc:67
void Fatal(const XrdCl::XRootDStatus &Status)
Definition: XrdQStats.cc:48
Binary blob representation.
Definition: XrdClBuffer.hh:34
void FromString(const std::string str)
Fill the buffer from a string.
Definition: XrdClBuffer.hh:205
const char * GetBuffer(uint32_t offset=0) const
Get the message buffer.
Definition: XrdClBuffer.hh:72
Send file/filesystem queries to an XRootD cluster.
URL representation.
Definition: XrdClURL.hh:31
bool IsValid() const
Is the url valid.
Definition: XrdClURL.cc:445
const std::string & GetErrorMessage() const
Get error message.
std::string ToStr() const
Convert to string.
int Format(const char *Host, char *ibuff, char *obuff)
Definition: XrdMpxXml.cc:242
const uint16_t errErrorResponse
Definition: XrdClStatus.hh:105
XrdPosixStats Stats
Definition: XrdPosixFile.cc:64
XrdSfsFileSystem * theFS
Definition: XrdSsiFile.cc:54
@ Stats
Query server stats.
uint16_t code
Error type, or additional hints on what to do.
Definition: XrdClStatus.hh:147
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:124