2010-04-14 22:32:20 +02:00
|
|
|
// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-07-10 01:34:29 +02:00
|
|
|
#include "metrics/metrics_library.h"
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2014-05-14 00:16:24 +02:00
|
|
|
#include <base/logging.h>
|
|
|
|
#include <base/strings/stringprintf.h>
|
2010-04-14 22:32:20 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/file.h>
|
2010-08-27 03:35:06 +02:00
|
|
|
#include <sys/stat.h>
|
2010-04-16 01:40:23 +02:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2014-07-10 01:34:29 +02:00
|
|
|
#include "components/metrics/chromeos/metric_sample.h"
|
|
|
|
#include "components/metrics/chromeos/serialization_utils.h"
|
2013-02-14 21:15:35 +01:00
|
|
|
|
2011-07-22 23:59:51 +02:00
|
|
|
#include "policy/device_policy.h"
|
2011-02-24 21:48:30 +01:00
|
|
|
|
2013-04-01 08:27:39 +02:00
|
|
|
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-08-27 03:35:06 +02:00
|
|
|
static const char kAutotestPath[] = "/var/log/metrics/autotest-events";
|
2014-05-19 17:30:42 +02:00
|
|
|
static const char kUMAEventsPath[] = "/var/run/metrics/uma-events";
|
2010-08-27 03:35:06 +02:00
|
|
|
static const char kConsentFile[] = "/home/chronos/Consent To Send Stats";
|
2013-03-13 18:53:55 +01:00
|
|
|
static const char kCrosEventHistogramName[] = "Platform.CrOSEvent";
|
|
|
|
static const int kCrosEventHistogramMax = 100;
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2013-04-01 08:27:39 +02:00
|
|
|
/* Add new cros events here.
|
|
|
|
*
|
|
|
|
* The index of the event is sent in the message, so please do not
|
|
|
|
* reorder the names.
|
|
|
|
*/
|
|
|
|
static const char *kCrosEventNames[] = {
|
|
|
|
"ModemManagerCommandSendFailure", // 0
|
|
|
|
"HwWatchdogReboot", // 1
|
|
|
|
"Cras.NoCodecsFoundAtBoot", // 2
|
2013-07-19 23:09:50 +02:00
|
|
|
"Chaps.DatabaseCorrupted", // 3
|
|
|
|
"Chaps.DatabaseRepairFailure", // 4
|
|
|
|
"Chaps.DatabaseCreateFailure", // 5
|
2013-07-26 22:37:20 +02:00
|
|
|
"Attestation.OriginSpecificExhausted", // 6
|
2013-11-11 23:24:44 +01:00
|
|
|
"SpringPowerSupply.Original.High", // 7
|
|
|
|
"SpringPowerSupply.Other.High", // 8
|
2013-11-14 01:28:43 +01:00
|
|
|
"SpringPowerSupply.Original.Low", // 9
|
|
|
|
"SpringPowerSupply.ChargerIdle", // 10
|
2014-02-08 01:51:15 +01:00
|
|
|
"TPM.NonZeroDictionaryAttackCounter", // 11
|
2013-04-01 08:27:39 +02:00
|
|
|
};
|
|
|
|
|
2010-08-27 03:35:06 +02:00
|
|
|
time_t MetricsLibrary::cached_enabled_time_ = 0;
|
|
|
|
bool MetricsLibrary::cached_enabled_ = false;
|
|
|
|
|
2014-05-14 00:16:24 +02:00
|
|
|
MetricsLibrary::MetricsLibrary() : consent_file_(kConsentFile) {}
|
2014-03-10 05:39:08 +01:00
|
|
|
MetricsLibrary::~MetricsLibrary() {}
|
|
|
|
|
2010-10-02 00:38:42 +02:00
|
|
|
// We take buffer and buffer_size as parameters in order to simplify testing
|
|
|
|
// of various alignments of the |device_name| with |buffer_size|.
|
|
|
|
bool MetricsLibrary::IsDeviceMounted(const char* device_name,
|
|
|
|
const char* mounts_file,
|
|
|
|
char* buffer,
|
|
|
|
int buffer_size,
|
|
|
|
bool* result) {
|
|
|
|
if (buffer == NULL || buffer_size < 1)
|
|
|
|
return false;
|
|
|
|
int mounts_fd = open(mounts_file, O_RDONLY);
|
|
|
|
if (mounts_fd < 0)
|
|
|
|
return false;
|
|
|
|
// match_offset describes:
|
|
|
|
// -1 -- not beginning of line
|
|
|
|
// 0..strlen(device_name)-1 -- this offset in device_name is next to match
|
|
|
|
// strlen(device_name) -- matched full name, just need a space.
|
|
|
|
int match_offset = 0;
|
|
|
|
bool match = false;
|
|
|
|
while (!match) {
|
|
|
|
int read_size = read(mounts_fd, buffer, buffer_size);
|
|
|
|
if (read_size <= 0) {
|
|
|
|
if (errno == -EINTR)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < read_size; ++i) {
|
|
|
|
if (buffer[i] == '\n') {
|
|
|
|
match_offset = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (match_offset < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (device_name[match_offset] == '\0') {
|
|
|
|
if (buffer[i] == ' ') {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
match_offset = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer[i] == device_name[match_offset]) {
|
|
|
|
++match_offset;
|
|
|
|
} else {
|
|
|
|
match_offset = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(mounts_fd);
|
|
|
|
*result = match;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MetricsLibrary::IsGuestMode() {
|
|
|
|
char buffer[256];
|
|
|
|
bool result = false;
|
|
|
|
if (!IsDeviceMounted("guestfs",
|
|
|
|
"/proc/mounts",
|
|
|
|
buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
&result)) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-05-26 21:22:22 +02:00
|
|
|
return result && (access("/var/run/state/logged-in", F_OK) == 0);
|
2010-10-02 00:38:42 +02:00
|
|
|
}
|
|
|
|
|
2010-08-27 03:35:06 +02:00
|
|
|
bool MetricsLibrary::AreMetricsEnabled() {
|
2011-08-02 16:10:49 +02:00
|
|
|
static struct stat stat_buffer;
|
2010-08-27 03:35:06 +02:00
|
|
|
time_t this_check_time = time(NULL);
|
|
|
|
if (this_check_time != cached_enabled_time_) {
|
|
|
|
cached_enabled_time_ = this_check_time;
|
2011-08-02 16:10:49 +02:00
|
|
|
|
|
|
|
if (!policy_provider_.get())
|
|
|
|
policy_provider_.reset(new policy::PolicyProvider());
|
2013-02-04 17:58:14 +01:00
|
|
|
policy_provider_->Reload();
|
2011-08-02 16:10:49 +02:00
|
|
|
// We initialize with the default value which is false and will be preserved
|
|
|
|
// if the policy is not set.
|
|
|
|
bool enabled = false;
|
|
|
|
bool has_policy = false;
|
|
|
|
if (policy_provider_->device_policy_is_loaded()) {
|
|
|
|
has_policy =
|
|
|
|
policy_provider_->GetDevicePolicy().GetMetricsEnabled(&enabled);
|
|
|
|
}
|
|
|
|
// If policy couldn't be loaded or the metrics policy is not set we should
|
|
|
|
// still respect the consent file if it is present for migration purposes.
|
|
|
|
// TODO(pastarmovj)
|
|
|
|
if (!has_policy) {
|
2014-05-14 00:16:24 +02:00
|
|
|
enabled = stat(consent_file_.c_str(), &stat_buffer) >= 0;
|
2011-08-02 16:10:49 +02:00
|
|
|
}
|
|
|
|
|
2011-07-22 23:59:51 +02:00
|
|
|
if (enabled && !IsGuestMode())
|
2010-10-02 00:38:42 +02:00
|
|
|
cached_enabled_ = true;
|
|
|
|
else
|
|
|
|
cached_enabled_ = false;
|
2010-08-27 03:35:06 +02:00
|
|
|
}
|
|
|
|
return cached_enabled_;
|
|
|
|
}
|
2010-05-18 20:00:59 +02:00
|
|
|
|
2010-05-12 22:05:45 +02:00
|
|
|
void MetricsLibrary::Init() {
|
2010-05-18 20:00:59 +02:00
|
|
|
uma_events_file_ = kUMAEventsPath;
|
2010-05-12 22:05:45 +02:00
|
|
|
}
|
|
|
|
|
2014-05-14 00:16:24 +02:00
|
|
|
bool MetricsLibrary::SendToAutotest(const std::string& name, int value) {
|
2010-06-05 00:01:19 +02:00
|
|
|
FILE* autotest_file = fopen(kAutotestPath, "a+");
|
2010-04-16 01:40:23 +02:00
|
|
|
if (autotest_file == NULL) {
|
2014-05-14 00:16:24 +02:00
|
|
|
PLOG(ERROR) << kAutotestPath << ": fopen";
|
2010-04-16 01:40:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(autotest_file, "%s=%d\n", name.c_str(), value);
|
|
|
|
fclose(autotest_file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-14 00:16:24 +02:00
|
|
|
bool MetricsLibrary::SendToUMA(const std::string& name,
|
|
|
|
int sample,
|
|
|
|
int min,
|
|
|
|
int max,
|
|
|
|
int nbuckets) {
|
2014-07-10 01:34:29 +02:00
|
|
|
return metrics::SerializationUtils::WriteMetricToFile(
|
|
|
|
*metrics::MetricSample::HistogramSample(name, sample, min, max, nbuckets)
|
|
|
|
.get(),
|
|
|
|
kUMAEventsPath);
|
2010-04-14 22:32:20 +02:00
|
|
|
}
|
2010-04-22 00:45:10 +02:00
|
|
|
|
2010-05-13 00:26:16 +02:00
|
|
|
bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
|
|
|
|
int max) {
|
2014-07-10 01:34:29 +02:00
|
|
|
return metrics::SerializationUtils::WriteMetricToFile(
|
|
|
|
*metrics::MetricSample::LinearHistogramSample(name, sample, max).get(),
|
|
|
|
kUMAEventsPath);
|
2011-01-06 19:51:47 +01:00
|
|
|
}
|
|
|
|
|
2013-03-19 23:02:42 +01:00
|
|
|
bool MetricsLibrary::SendSparseToUMA(const std::string& name, int sample) {
|
2014-07-10 01:34:29 +02:00
|
|
|
return metrics::SerializationUtils::WriteMetricToFile(
|
|
|
|
*metrics::MetricSample::SparseHistogramSample(name, sample).get(),
|
|
|
|
kUMAEventsPath);
|
2013-03-19 23:02:42 +01:00
|
|
|
}
|
|
|
|
|
2011-01-06 19:51:47 +01:00
|
|
|
bool MetricsLibrary::SendUserActionToUMA(const std::string& action) {
|
2014-07-10 01:34:29 +02:00
|
|
|
return metrics::SerializationUtils::WriteMetricToFile(
|
|
|
|
*metrics::MetricSample::UserActionSample(action).get(), kUMAEventsPath);
|
2010-04-22 00:45:10 +02:00
|
|
|
}
|
2011-01-22 15:15:56 +01:00
|
|
|
|
|
|
|
bool MetricsLibrary::SendCrashToUMA(const char *crash_kind) {
|
2014-07-10 01:34:29 +02:00
|
|
|
return metrics::SerializationUtils::WriteMetricToFile(
|
|
|
|
*metrics::MetricSample::CrashSample(crash_kind).get(), kUMAEventsPath);
|
2011-01-22 15:15:56 +01:00
|
|
|
}
|
2011-07-22 23:59:51 +02:00
|
|
|
|
|
|
|
void MetricsLibrary::SetPolicyProvider(policy::PolicyProvider* provider) {
|
|
|
|
policy_provider_.reset(provider);
|
|
|
|
}
|
2013-03-13 18:53:55 +01:00
|
|
|
|
|
|
|
bool MetricsLibrary::SendCrosEventToUMA(const std::string& event) {
|
2013-04-01 08:27:39 +02:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(kCrosEventNames); i++) {
|
|
|
|
if (strcmp(event.c_str(), kCrosEventNames[i]) == 0) {
|
|
|
|
return SendEnumToUMA(kCrosEventHistogramName, i, kCrosEventHistogramMax);
|
|
|
|
}
|
2013-03-13 18:53:55 +01:00
|
|
|
}
|
2013-04-01 08:27:39 +02:00
|
|
|
return false;
|
2013-03-13 18:53:55 +01:00
|
|
|
}
|