OLD | NEW |
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
| 6 #include <base/logging.h> |
| 7 #include <base/string_util.h> |
6 #include <gflags/gflags.h> | 8 #include <gflags/gflags.h> |
| 9 #include <rootdev/rootdev.h> |
7 | 10 |
8 #include "metrics_daemon.h" | 11 #include "metrics_daemon.h" |
9 | 12 |
10 DEFINE_bool(daemon, true, "run as daemon (use -nodaemon for debugging)"); | 13 DEFINE_bool(daemon, true, "run as daemon (use -nodaemon for debugging)"); |
11 | 14 |
12 // Path to disk stats. This may be system dependent. | 15 // Return the path to the disk stats in the sysfs. |
13 const char kMetricsMainDiskStatsPath[] = "/sys/class/block/sda/stat"; | 16 static |
| 17 const std::string MetricsMainDiskStatsPath() { |
| 18 char dev_path_cstr[PATH_MAX]; |
| 19 std::string dev_prefix = "/dev/"; |
| 20 std::string dev_path; |
| 21 std::string dev_name; |
| 22 |
| 23 int ret = rootdev(dev_path_cstr, sizeof(dev_path_cstr), true, true); |
| 24 if (ret != 0) { |
| 25 LOG(WARNING) << "error " << ret << " determining root device"; |
| 26 return ""; |
| 27 } |
| 28 dev_path = dev_path_cstr; |
| 29 // Check that rootdev begins with "/dev/". |
| 30 if (!StartsWithASCII(dev_path, dev_prefix, false)) { |
| 31 LOG(WARNING) << "unexpected root device " << dev_path; |
| 32 return ""; |
| 33 } |
| 34 // Get the device name, e.g. "sda" from "/dev/sda". |
| 35 dev_name = dev_path.substr(dev_prefix.length()); |
| 36 return "/sys/class/block/" + dev_name + "/stat"; |
| 37 } |
14 | 38 |
15 int main(int argc, char** argv) { | 39 int main(int argc, char** argv) { |
16 google::ParseCommandLineFlags(&argc, &argv, true); | 40 google::ParseCommandLineFlags(&argc, &argv, true); |
17 MetricsLibrary metrics_lib; | 41 MetricsLibrary metrics_lib; |
18 metrics_lib.Init(); | 42 metrics_lib.Init(); |
19 MetricsDaemon daemon; | 43 MetricsDaemon daemon; |
20 daemon.Init(false, &metrics_lib, kMetricsMainDiskStatsPath); | 44 daemon.Init(false, &metrics_lib, MetricsMainDiskStatsPath()); |
21 daemon.Run(FLAGS_daemon); | 45 daemon.Run(FLAGS_daemon); |
22 } | 46 } |
OLD | NEW |