CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
StatisticsSenderService.cc
Go to the documentation of this file.
1 
8 
9 #include <string>
10 
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <string.h>
14 
15 #include <openssl/x509.h>
16 #include <openssl/pem.h>
17 
18 #define UPDATE_STATISTIC(x) \
19  m_ ## x = x;
20 
21 #define UPDATE_AND_OUTPUT_STATISTIC(x) \
22  os << "\"" #x "\":" << (x-m_ ## x) << ", "; \
23  UPDATE_STATISTIC(x)
24 
25 // Simple hack to define HOST_NAME_MAX on Mac.
26 // Allows arrays to be statically allocated
27 #ifndef HOST_NAME_MAX
28 #define HOST_NAME_MAX 128
29 #endif
30 
31 #define JOB_UNIQUE_ID_ENV "CRAB_UNIQUE_JOB_ID"
32 #define JOB_UNIQUE_ID_ENV_V2 "DashboardJobId"
33 
34 using namespace edm::storage;
35 
37  m_read_single_operations(0),
38  m_read_single_bytes(0),
39  m_read_single_square(0),
40  m_read_vector_operations(0),
41  m_read_vector_bytes(0),
42  m_read_vector_square(0),
43  m_read_vector_count_sum(0),
44  m_read_vector_count_square(0),
45  m_read_bytes_at_close(0),
46  m_start_time(time(NULL))
47 {}
48 
49 void
52  ssize_t read_single_operations = 0;
53  ssize_t read_single_bytes = 0;
54  ssize_t read_single_square = 0;
55  ssize_t read_vector_operations = 0;
56  ssize_t read_vector_bytes = 0;
57  ssize_t read_vector_square = 0;
58  ssize_t read_vector_count_sum = 0;
59  ssize_t read_vector_count_square = 0;
60  for (StorageAccount::StorageStats::const_iterator i = stats.begin (); i != stats.end(); ++i) {
61  if (i->first == "tstoragefile") {
62  continue;
63  }
64  for (StorageAccount::OperationStats::const_iterator j = i->second->begin(); j != i->second->end(); ++j) {
65  if (j->first == "readv") {
66  read_vector_operations += j->second.attempts;
67  read_vector_bytes += j->second.amount;
68  read_vector_count_square += j->second.vector_square;
69  read_vector_square += j->second.amount_square;
70  read_vector_count_sum += j->second.vector_count;
71  } else if (j->first == "read") {
72  read_single_operations += j->second.attempts;
73  read_single_bytes += j->second.amount;
74  read_single_square += j->second.amount_square;
75  }
76  }
77  }
78  int64_t single_op_count = read_single_operations - m_read_single_operations;
79  if (single_op_count > 0) {
80  double single_sum = read_single_bytes-m_read_single_bytes;
81  double single_average = single_sum/static_cast<double>(single_op_count);
82  os << "\"read_single_sigma\":" << sqrt((static_cast<double>(read_single_square-m_read_single_square) - single_average*single_average*single_op_count)/static_cast<double>(single_op_count)) << ", ";
83  os << "\"read_single_average\":" << single_average << ", ";
84  }
85  m_read_single_square = read_single_square;
86  int64_t vector_op_count = read_vector_operations - m_read_vector_operations;
87  if (vector_op_count > 0) {
88  double vector_average = static_cast<double>(read_vector_bytes-m_read_vector_bytes)/static_cast<double>(vector_op_count);
89  os << "\"read_vector_average\":" << vector_average << ", ";
90  os << "\"read_vector_sigma\":" << sqrt((static_cast<double>(read_vector_square-m_read_vector_square) - vector_average*vector_average*vector_op_count)/static_cast<double>(vector_op_count)) << ", ";
91  double vector_count_average = static_cast<double>(read_vector_count_sum-m_read_vector_count_sum)/static_cast<double>(vector_op_count);
92  os << "\"read_vector_count_average\":" << vector_count_average << ", ";
93  os << "\"read_vector_count_sigma\":" << sqrt((static_cast<double>(read_vector_count_square-m_read_vector_count_square) - vector_count_average*vector_count_average*vector_op_count)/static_cast<double>(vector_op_count)) << ", ";
94  }
95  m_read_vector_square = read_vector_square;
96  m_read_vector_count_square = read_vector_count_square;
97  m_read_vector_count_sum = read_vector_count_sum;
98 
99  os << "\"read_bytes\":" << (read_vector_bytes + read_single_bytes - m_read_vector_bytes - m_read_single_bytes) << ", ";
100  os << "\"read_bytes_at_close\":" << (read_vector_bytes + read_single_bytes - m_read_vector_bytes - m_read_single_bytes) << ", ";
101 
102  // See top of file for macros; not complex, just avoiding copy/paste
103  UPDATE_AND_OUTPUT_STATISTIC(read_single_operations)
104  UPDATE_AND_OUTPUT_STATISTIC(read_single_bytes)
105  UPDATE_AND_OUTPUT_STATISTIC(read_vector_operations)
106  UPDATE_AND_OUTPUT_STATISTIC(read_vector_bytes)
107 
108  os << "\"start_time\":" << m_start_time << ", ";
109  m_start_time = time(NULL);
110  // NOTE: last entry doesn't have the trailing comma.
111  os << "\"end_time\":" << m_start_time;
112 }
113 
115  m_clienthost("unknown"),
116  m_clientdomain("unknown"),
117  m_serverhost("unknown"),
118  m_serverdomain("unknown"),
119  m_filelfn("unknown"),
120  m_filestats(),
121  m_guid(Guid().toString()),
122  m_counter(0),
123  m_size(-1),
124  m_userdn("unknown")
125 {
128  if (!getX509Subject(m_userdn)) {
129  m_userdn = "unknown";
130  }
131 }
132 
133 const char *
135  const char * id = getenv(JOB_UNIQUE_ID_ENV);
136  // Dashboard developers requested that we migrate to this environment variable.
137  return id ? id : getenv(JOB_UNIQUE_ID_ENV_V2);
138 }
139 
140 void
142  size_t dot_pos = servername.find(".");
143  if (dot_pos == std::string::npos) {
144  m_serverhost = servername.substr(0, servername.find(":"));
145  m_serverdomain = "unknown";
146  } else {
147  m_serverhost = servername.substr(0, dot_pos);
148  m_serverdomain = servername.substr(dot_pos+1, servername.find(":")-dot_pos-1);
149  if (m_serverdomain.empty()) {
150  m_serverdomain = "unknown";
151  }
152  }
153 }
154 
155 void
157  m_size = size;
158 }
159 
160 void
162  m_filelfn = lfn;
163 
165  if (!pSLC.isAvailable()) {
166  return;
167  }
168 
169  const struct addrinfo * addresses = pSLC->statisticsDestination();
170  if (!addresses) {
171  return;
172  }
173 
175  fillUDP(pSLC->siteName(), usedFallback, results);
176 
177  for (const struct addrinfo *address = addresses; address != nullptr; address = address->ai_next) {
178  int sock = socket(address->ai_family, address->ai_socktype, address->ai_protocol);
179  if (sock < 0) {
180  continue;
181  }
182  if (sendto(sock, results.c_str(), results.size(), 0, address->ai_addr, address->ai_addrlen) >= 0) {
183  break;
184  }
185  }
186 
187  m_counter++;
188 }
189 
190 void
192  char tmpName[HOST_NAME_MAX];
193  if (gethostname(tmpName, HOST_NAME_MAX) != 0) {
194  // Sigh, no way to log errors from here.
195  m_clienthost = "unknown";
196  } else {
197  m_clienthost = tmpName;
198  }
199  size_t dot_pos = m_clienthost.find(".");
200  if (dot_pos == std::string::npos) {
201  m_clientdomain = "unknown";
202  } else {
203  m_clientdomain = m_clienthost.substr(dot_pos+1, m_clienthost.size()-dot_pos-1);
204  m_clienthost = m_clienthost.substr(0, dot_pos);
205  }
206 }
207 
208 void
209 StatisticsSenderService::fillUDP(const std::string& siteName, bool usedFallback, std::string &udpinfo) {
210  std::ostringstream os;
211 
212  // Header - same for all IO accesses
213  os << "{";
214  if (!siteName.empty()) {
215  os << "\"site_name\":\"" << siteName << "\", ";
216  }
217  if (usedFallback) {
218  os << "\"fallback\": true, ";
219  }
220  os << "\"user_dn\":\"" << m_userdn << "\", ";
221  os << "\"client_host\":\"" << m_clienthost << "\", ";
222  os << "\"client_domain\":\"" << m_clientdomain << "\", ";
223  os << "\"server_host\":\"" << m_serverhost << "\", ";
224  os << "\"server_domain\":\"" << m_serverdomain << "\", ";
225  os << "\"unique_id\":\"" << m_guid << "-" << m_counter << "\", ";
226  os << "\"file_lfn\":\"" << m_filelfn << "\", ";
227  // Dashboard devs requested that we send out no app_info if a job ID
228  // is not present in the environment.
229  const char * jobId = getJobID();
230  if (jobId) {
231  os << "\"app_info\":\"" << jobId << "\", ";
232  }
233 
234  if (m_size >= 0) {
235  os << "\"file_size\":" << m_size << ", ";
236  }
237 
238  m_filestats.fillUDP(os);
239 
240  os << "}";
241  udpinfo = os.str();
242 }
243 
244 /*
245  * Pull the X509 user subject from the environment.
246  * Based on initial code from the Frontier client:
247  * http://cdcvs.fnal.gov/cgi-bin/public-cvs/cvsweb-public.cgi/~checkout~/frontier/client/frontier.c?rev=1.57&content-type=text/plain
248  * This was further extended by walking up the returned chain similar to the Globus function
249  * globus_gsi_cert_utils-6.6/library/globus_gsi_cert_utils.c:globus_gsi_cert_utils_get_eec
250  * globus_gsi_credential-3.5/library/globus_gsi_credential.c:globus_gsi_cred_read_proxy_bio
251  */
252 
253 /*
254  * Given a stack of x509 proxies, take a guess at the EEC.
255  * Assumes the proxies are in reverse sorted order and looks for the first
256  * proxy which is not a substring of the prior proxy.
257  * THIS DOES NOT VERIFY THE RESULTS, and is a best-effort GUESS.
258  * Again, DO NOT REUSE THIS CODE THINKING IT VERIFIES THE CHAIN!
259  */
260 static X509 * findEEC(STACK_OF(X509) * certstack) {
261  int depth = sk_X509_num(certstack);
262  if (depth == 0) {
263  return nullptr;
264  }
265  int idx = depth-1;
266  char *priorsubject = nullptr;
267  char *subject = nullptr;
268  X509 *x509cert = sk_X509_value(certstack, idx);
269  for (; x509cert && idx>0; idx--) {
270  subject = X509_NAME_oneline(X509_get_subject_name(x509cert),0,0);
271  if (subject && priorsubject && (strncmp(subject, priorsubject, strlen(subject)) != 0)) {
272  break;
273  }
274  x509cert = sk_X509_value(certstack, idx);
275  if (subject) {
276  OPENSSL_free(subject);
277  subject = nullptr;
278  }
279  }
280  if (subject) {
281  OPENSSL_free(subject);
282  subject = nullptr;
283  }
284  return x509cert;
285 }
286 
287 static bool
289  BIO *biof = nullptr;
290  STACK_OF(X509) *certs = nullptr;
291  char *subject = nullptr;
292  unsigned char *data = nullptr;
293  char *header = nullptr;
294  char *name = nullptr;
295  long len = 0U;
296 
297  if((biof = BIO_new_file(filename.c_str(), "r"))) {
298 
299  certs = sk_X509_new_null();
300  bool encountered_error = false;
301  while ((!encountered_error) && (!BIO_eof(biof)) && PEM_read_bio(biof, &name, &header, &data, &len)) {
302  if (strcmp(name, PEM_STRING_X509) == 0 || strcmp(name, PEM_STRING_X509_OLD) == 0) {
303  X509 * tmp_cert = nullptr;
304  // See WARNINGS section in http://www.openssl.org/docs/crypto/d2i_X509.html
305  // Without this cmsRun crashes on a mac with a valid grid proxy.
306  const unsigned char *p;
307  p=data;
308  tmp_cert = d2i_X509(&tmp_cert, &p, len);
309  if (tmp_cert) {
310  sk_X509_push(certs, tmp_cert);
311  } else {
312  encountered_error = true;
313  }
314  } // Note we ignore any proxy key in the file.
315  if (data) { OPENSSL_free(data); data = nullptr;}
316  if (header) { OPENSSL_free(header); header = nullptr;}
317  if (name) { OPENSSL_free(name); name = nullptr;}
318  }
319  X509 *x509cert = nullptr;
320  if (!encountered_error && sk_X509_num(certs)) {
321  x509cert = findEEC(certs);
322  }
323  if (x509cert) {
324  subject = X509_NAME_oneline(X509_get_subject_name(x509cert),0,0);
325  }
326  // Note we do not free x509cert directly, as it's still owned by the certs stack.
327  if (certs) {
328  sk_X509_pop_free(certs, X509_free);
329  x509cert = nullptr;
330  }
331  BIO_free(biof);
332  if (subject) {
333  result = subject;
334  OPENSSL_free(subject);
335  return true;
336  }
337  }
338  return false;
339 }
340 
341 bool
343  char *filename = getenv("X509_USER_PROXY");
344  if (filename && getX509SubjectFromFile(filename, result)) {
345  return true;
346  }
347  std::stringstream ss;
348  ss << "/tmp/x509up_u" << geteuid();
349  return getX509SubjectFromFile(ss.str(), result);
350 }
void setCurrentServer(const std::string &servername)
int i
Definition: DBlmapReader.cc:9
static const StorageStats & summary(void)
void filePreCloseEvent(std::string const &lfn, bool usedFallback)
#define NULL
Definition: scimark2.h:8
std::map< std::string, boost::shared_ptr< OperationStats > > StorageStats
static X509 * findEEC(STACK_OF(X509)*certstack)
Definition: Guid.h:23
#define JOB_UNIQUE_ID_ENV_V2
T sqrt(T t)
Definition: SSEVec.h:48
#define JOB_UNIQUE_ID_ENV
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
tuple result
Definition: query.py:137
bool isAvailable() const
Definition: Service.h:46
int j
Definition: DBlmapReader.cc:9
void fillUDP(const std::string &, bool, std::string &)
void watchPreCloseFile(PreCloseFile::slot_type const &iSlot)
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
virtual std::string const & siteName(void) const =0
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
#define HOST_NAME_MAX
tuple filename
Definition: lut2db_cfg.py:20
StatisticsSenderService(edm::ParameterSet const &pset, edm::ActivityRegistry &ar)
virtual struct addrinfo const * statisticsDestination() const =0
static bool getX509SubjectFromFile(const std::string &filename, std::string &result)
tuple size
Write out results.
#define UPDATE_AND_OUTPUT_STATISTIC(x)