Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 16 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 17 | #include <base/at_exit.h> |
Luigi Semenzato | 254d22e | 2014-04-21 14:33:32 -0700 | [diff] [blame] | 18 | #include <base/command_line.h> |
Luigi Semenzato | 0f132bb | 2011-02-28 11:17:43 -0800 | [diff] [blame] | 19 | #include <base/logging.h> |
Ben Chan | 2e6543d | 2014-02-05 23:26:25 -0800 | [diff] [blame] | 20 | #include <base/strings/string_util.h> |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 21 | #include <brillo/flag_helper.h> |
| 22 | #include <brillo/syslog_logging.h> |
Bertrand SIMONNET | ebbe35c | 2015-09-08 10:13:35 -0700 | [diff] [blame] | 23 | #include <rootdev.h> |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 24 | |
Bertrand SIMONNET | bd3505e | 2015-08-04 14:04:51 -0700 | [diff] [blame] | 25 | #include "constants.h" |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 26 | #include "metrics_collector.h" |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 27 | |
Luigi Semenzato | fb3a821 | 2013-05-07 16:55:00 -0700 | [diff] [blame] | 28 | |
Bertrand SIMONNET | ebbe35c | 2015-09-08 10:13:35 -0700 | [diff] [blame] | 29 | // Returns the path to the disk stats in the sysfs. Returns the null string if |
| 30 | // it cannot find the disk stats file. |
| 31 | static |
| 32 | const std::string MetricsMainDiskStatsPath() { |
| 33 | char dev_path_cstr[PATH_MAX]; |
| 34 | std::string dev_prefix = "/dev/block/"; |
| 35 | std::string dev_path; |
| 36 | |
| 37 | int ret = rootdev(dev_path_cstr, sizeof(dev_path_cstr), true, true); |
| 38 | if (ret != 0) { |
| 39 | LOG(WARNING) << "error " << ret << " determining root device"; |
| 40 | return ""; |
| 41 | } |
| 42 | dev_path = dev_path_cstr; |
| 43 | // Check that rootdev begins with "/dev/block/". |
Alex Vakulenko | ea05ff9 | 2016-01-20 07:53:57 -0800 | [diff] [blame] | 44 | if (!base::StartsWith(dev_path, dev_prefix, |
| 45 | base::CompareCase::INSENSITIVE_ASCII)) { |
Bertrand SIMONNET | ebbe35c | 2015-09-08 10:13:35 -0700 | [diff] [blame] | 46 | LOG(WARNING) << "unexpected root device " << dev_path; |
| 47 | return ""; |
| 48 | } |
| 49 | return "/sys/class/block/" + dev_path.substr(dev_prefix.length()) + "/stat"; |
| 50 | } |
| 51 | |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 52 | int main(int argc, char** argv) { |
Bertrand SIMONNET | 0a1119f | 2015-11-06 13:25:41 -0800 | [diff] [blame] | 53 | DEFINE_bool(foreground, false, "Don't daemonize"); |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 54 | |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 55 | DEFINE_string(private_directory, metrics::kMetricsCollectorDirectory, |
| 56 | "Path to the private directory used by metrics_collector " |
| 57 | "(testing only)"); |
| 58 | DEFINE_string(shared_directory, metrics::kSharedMetricsDirectory, |
| 59 | "Path to the shared metrics directory, used by " |
| 60 | "metrics_collector, metricsd and all metrics clients " |
| 61 | "(testing only)"); |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 62 | |
Bertrand SIMONNET | 0a1119f | 2015-11-06 13:25:41 -0800 | [diff] [blame] | 63 | DEFINE_bool(logtostderr, false, "Log to standard error"); |
| 64 | DEFINE_bool(logtosyslog, false, "Log to syslog"); |
| 65 | |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 66 | brillo::FlagHelper::Init(argc, argv, "Chromium OS Metrics Daemon"); |
Luigi Semenzato | 254d22e | 2014-04-21 14:33:32 -0700 | [diff] [blame] | 67 | |
Bertrand SIMONNET | 0a1119f | 2015-11-06 13:25:41 -0800 | [diff] [blame] | 68 | int logging_location = (FLAGS_foreground ? brillo::kLogToStderr |
| 69 | : brillo::kLogToSyslog); |
| 70 | if (FLAGS_logtosyslog) |
| 71 | logging_location = brillo::kLogToSyslog; |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 72 | |
Bertrand SIMONNET | 0a1119f | 2015-11-06 13:25:41 -0800 | [diff] [blame] | 73 | if (FLAGS_logtostderr) |
| 74 | logging_location = brillo::kLogToStderr; |
| 75 | |
| 76 | // Also log to stderr when not running as daemon. |
| 77 | brillo::InitLog(logging_location | brillo::kLogHeader); |
| 78 | |
| 79 | if (FLAGS_logtostderr && FLAGS_logtosyslog) { |
| 80 | LOG(ERROR) << "only one of --logtosyslog and --logtostderr can be set"; |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | if (!FLAGS_foreground && daemon(0, 0) != 0) { |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 85 | return errno; |
| 86 | } |
| 87 | |
Darin Petkov | 11b8eb3 | 2010-05-18 11:00:59 -0700 | [diff] [blame] | 88 | MetricsLibrary metrics_lib; |
Bertrand SIMONNET | a5b40d0 | 2015-10-02 16:40:51 -0700 | [diff] [blame] | 89 | metrics_lib.InitWithNoCaching(); |
Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 90 | MetricsCollector daemon; |
| 91 | daemon.Init(false, |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 92 | &metrics_lib, |
Bertrand SIMONNET | ebbe35c | 2015-09-08 10:13:35 -0700 | [diff] [blame] | 93 | MetricsMainDiskStatsPath(), |
Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 94 | base::FilePath(FLAGS_private_directory), |
| 95 | base::FilePath(FLAGS_shared_directory)); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 96 | |
Steve Fung | e86591e | 2014-12-01 13:38:21 -0800 | [diff] [blame] | 97 | daemon.Run(); |
Darin Petkov | 65b0146 | 2010-04-14 13:32:20 -0700 | [diff] [blame] | 98 | } |