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 
174  std::set<std::string> const * info = pSLC->statisticsInfo();
175  if (info && info->size() && (m_userdn != "unknown") && (
176  (info->find("dn") == info->end()) ||
177  (info->find("nodn") != info->end()))
178  )
179  {
180  m_userdn = "not reported";
181  }
182 
184  fillUDP(pSLC->siteName(), usedFallback, results);
185 
186  for (const struct addrinfo *address = addresses; address != nullptr; address = address->ai_next) {
187  int sock = socket(address->ai_family, address->ai_socktype, address->ai_protocol);
188  if (sock < 0) {
189  continue;
190  }
191  if (sendto(sock, results.c_str(), results.size(), 0, address->ai_addr, address->ai_addrlen) >= 0) {
192  break;
193  }
194  }
195 
196  m_counter++;
197 }
198 
199 void
201  char tmpName[HOST_NAME_MAX];
202  if (gethostname(tmpName, HOST_NAME_MAX) != 0) {
203  // Sigh, no way to log errors from here.
204  m_clienthost = "unknown";
205  } else {
206  m_clienthost = tmpName;
207  }
208  size_t dot_pos = m_clienthost.find(".");
209  if (dot_pos == std::string::npos) {
210  m_clientdomain = "unknown";
211  } else {
212  m_clientdomain = m_clienthost.substr(dot_pos+1, m_clienthost.size()-dot_pos-1);
213  m_clienthost = m_clienthost.substr(0, dot_pos);
214  }
215 }
216 
217 void
218 StatisticsSenderService::fillUDP(const std::string& siteName, bool usedFallback, std::string &udpinfo) {
219  std::ostringstream os;
220 
221  // Header - same for all IO accesses
222  os << "{";
223  if (!siteName.empty()) {
224  os << "\"site_name\":\"" << siteName << "\", ";
225  }
226  if (usedFallback) {
227  os << "\"fallback\": true, ";
228  }
229  os << "\"user_dn\":\"" << m_userdn << "\", ";
230  os << "\"client_host\":\"" << m_clienthost << "\", ";
231  os << "\"client_domain\":\"" << m_clientdomain << "\", ";
232  os << "\"server_host\":\"" << m_serverhost << "\", ";
233  os << "\"server_domain\":\"" << m_serverdomain << "\", ";
234  os << "\"unique_id\":\"" << m_guid << "-" << m_counter << "\", ";
235  os << "\"file_lfn\":\"" << m_filelfn << "\", ";
236  // Dashboard devs requested that we send out no app_info if a job ID
237  // is not present in the environment.
238  const char * jobId = getJobID();
239  if (jobId) {
240  os << "\"app_info\":\"" << jobId << "\", ";
241  }
242 
243  if (m_size >= 0) {
244  os << "\"file_size\":" << m_size << ", ";
245  }
246 
247  m_filestats.fillUDP(os);
248 
249  os << "}";
250  udpinfo = os.str();
251 }
252 
253 /*
254  * Pull the X509 user subject from the environment.
255  * Based on initial code from the Frontier client:
256  * http://cdcvs.fnal.gov/cgi-bin/public-cvs/cvsweb-public.cgi/~checkout~/frontier/client/frontier.c?rev=1.57&content-type=text/plain
257  * This was further extended by walking up the returned chain similar to the Globus function
258  * globus_gsi_cert_utils-6.6/library/globus_gsi_cert_utils.c:globus_gsi_cert_utils_get_eec
259  * globus_gsi_credential-3.5/library/globus_gsi_credential.c:globus_gsi_cred_read_proxy_bio
260  */
261 
262 /*
263  * Given a stack of x509 proxies, take a guess at the EEC.
264  * Assumes the proxies are in reverse sorted order and looks for the first
265  * proxy which is not a substring of the prior proxy.
266  * THIS DOES NOT VERIFY THE RESULTS, and is a best-effort GUESS.
267  * Again, DO NOT REUSE THIS CODE THINKING IT VERIFIES THE CHAIN!
268  */
269 static X509 * findEEC(STACK_OF(X509) * certstack) {
270  int depth = sk_X509_num(certstack);
271  if (depth == 0) {
272  return nullptr;
273  }
274  int idx = depth-1;
275  char *priorsubject = nullptr;
276  char *subject = nullptr;
277  X509 *x509cert = sk_X509_value(certstack, idx);
278  for (; x509cert && idx>0; idx--) {
279  subject = X509_NAME_oneline(X509_get_subject_name(x509cert),0,0);
280  if (subject && priorsubject && (strncmp(subject, priorsubject, strlen(subject)) != 0)) {
281  break;
282  }
283  x509cert = sk_X509_value(certstack, idx);
284  if (subject) {
285  OPENSSL_free(subject);
286  subject = nullptr;
287  }
288  }
289  if (subject) {
290  OPENSSL_free(subject);
291  subject = nullptr;
292  }
293  return x509cert;
294 }
295 
296 static bool
298  BIO *biof = nullptr;
299  STACK_OF(X509) *certs = nullptr;
300  char *subject = nullptr;
301  unsigned char *data = nullptr;
302  char *header = nullptr;
303  char *name = nullptr;
304  long len = 0U;
305 
306  if((biof = BIO_new_file(filename.c_str(), "r"))) {
307 
308  certs = sk_X509_new_null();
309  bool encountered_error = false;
310  while ((!encountered_error) && (!BIO_eof(biof)) && PEM_read_bio(biof, &name, &header, &data, &len)) {
311  if (strcmp(name, PEM_STRING_X509) == 0 || strcmp(name, PEM_STRING_X509_OLD) == 0) {
312  X509 * tmp_cert = nullptr;
313  // See WARNINGS section in http://www.openssl.org/docs/crypto/d2i_X509.html
314  // Without this cmsRun crashes on a mac with a valid grid proxy.
315  const unsigned char *p;
316  p=data;
317  tmp_cert = d2i_X509(&tmp_cert, &p, len);
318  if (tmp_cert) {
319  sk_X509_push(certs, tmp_cert);
320  } else {
321  encountered_error = true;
322  }
323  } // Note we ignore any proxy key in the file.
324  if (data) { OPENSSL_free(data); data = nullptr;}
325  if (header) { OPENSSL_free(header); header = nullptr;}
326  if (name) { OPENSSL_free(name); name = nullptr;}
327  }
328  X509 *x509cert = nullptr;
329  if (!encountered_error && sk_X509_num(certs)) {
330  x509cert = findEEC(certs);
331  }
332  if (x509cert) {
333  subject = X509_NAME_oneline(X509_get_subject_name(x509cert),0,0);
334  }
335  // Note we do not free x509cert directly, as it's still owned by the certs stack.
336  if (certs) {
337  sk_X509_pop_free(certs, X509_free);
338  x509cert = nullptr;
339  }
340  BIO_free(biof);
341  if (subject) {
342  result = subject;
343  OPENSSL_free(subject);
344  return true;
345  }
346  }
347  return false;
348 }
349 
350 bool
352  char *filename = getenv("X509_USER_PROXY");
353  if (filename && getX509SubjectFromFile(filename, result)) {
354  return true;
355  }
356  std::stringstream ss;
357  ss << "/tmp/x509up_u" << geteuid();
358  return getX509SubjectFromFile(ss.str(), result);
359 }
void setCurrentServer(const std::string &servername)
int i
Definition: DBlmapReader.cc:9
static const TGPicture * info(bool iBackgroundIsBlack)
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.
virtual std::set< std::string > const * statisticsInfo() const =0
#define UPDATE_AND_OUTPUT_STATISTIC(x)