Home | History | Annotate | Download | only in metricsd
      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  */
     16 
     17 #include <base/at_exit.h>
     18 #include <base/command_line.h>
     19 #include <base/logging.h>
     20 #include <base/strings/string_util.h>
     21 #include <brillo/flag_helper.h>
     22 #include <brillo/syslog_logging.h>
     23 #include <rootdev.h>
     24 
     25 #include "constants.h"
     26 #include "metrics_collector.h"
     27 
     28 
     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/".
     44   if (!base::StartsWith(dev_path, dev_prefix,
     45                         base::CompareCase::INSENSITIVE_ASCII)) {
     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 
     52 int main(int argc, char** argv) {
     53   DEFINE_bool(foreground, false, "Don't daemonize");
     54 
     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)");
     62 
     63   DEFINE_bool(logtostderr, false, "Log to standard error");
     64   DEFINE_bool(logtosyslog, false, "Log to syslog");
     65 
     66   brillo::FlagHelper::Init(argc, argv, "Chromium OS Metrics Daemon");
     67 
     68   int logging_location = (FLAGS_foreground ? brillo::kLogToStderr
     69                           : brillo::kLogToSyslog);
     70   if (FLAGS_logtosyslog)
     71     logging_location = brillo::kLogToSyslog;
     72 
     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) {
     85     return errno;
     86   }
     87 
     88   MetricsLibrary metrics_lib;
     89   metrics_lib.InitWithNoCaching();
     90   MetricsCollector daemon;
     91   daemon.Init(false,
     92               &metrics_lib,
     93               MetricsMainDiskStatsPath(),
     94               base::FilePath(FLAGS_private_directory),
     95               base::FilePath(FLAGS_shared_directory));
     96 
     97   daemon.Run();
     98 }
     99