| Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [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 |  */ | 
 | 16 |  | 
| Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 17 | #include <base/command_line.h> | 
 | 18 | #include <base/files/file_path.h> | 
 | 19 | #include <base/logging.h> | 
| Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 20 | #include <base/time/time.h> | 
 | 21 | #include <brillo/flag_helper.h> | 
 | 22 | #include <brillo/syslog_logging.h> | 
 | 23 |  | 
 | 24 | #include "constants.h" | 
| Bertrand SIMONNET | b6c77af | 2015-12-08 17:46:00 -0800 | [diff] [blame] | 25 | #include "uploader/metricsd_service_runner.h" | 
| Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 26 | #include "uploader/upload_service.h" | 
 | 27 |  | 
| Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 28 | int main(int argc, char** argv) { | 
 | 29 |   DEFINE_bool(foreground, false, "Don't daemonize"); | 
 | 30 |  | 
 | 31 |   // Upload the metrics once and exit. (used for testing) | 
| Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 32 |   DEFINE_bool(uploader_test, false, "run the uploader once and exit"); | 
| Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 33 |  | 
 | 34 |   // Upload Service flags. | 
| Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 35 |   DEFINE_int32(upload_interval_secs, 1800, | 
| Bertrand SIMONNET | 0586504 | 2015-12-15 12:33:01 -0800 | [diff] [blame] | 36 |                "Interval at which metricsd uploads the metrics."); | 
 | 37 |   DEFINE_int32(disk_persistence_interval_secs, 300, | 
 | 38 |                "Interval at which metricsd saves the aggregated metrics to " | 
 | 39 |                "disk to avoid losing them if metricsd stops in between " | 
 | 40 |                "two uploads."); | 
| Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 41 |   DEFINE_string(server, metrics::kMetricsServer, | 
| Bertrand SIMONNET | 0586504 | 2015-12-15 12:33:01 -0800 | [diff] [blame] | 42 |                 "Server to upload the metrics to."); | 
| Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 43 |   DEFINE_string(private_directory, metrics::kMetricsdDirectory, | 
 | 44 |                 "Path to the private directory used by metricsd " | 
 | 45 |                 "(testing only)"); | 
 | 46 |   DEFINE_string(shared_directory, metrics::kSharedMetricsDirectory, | 
 | 47 |                 "Path to the shared metrics directory, used by " | 
 | 48 |                 "metrics_collector, metricsd and all metrics clients " | 
 | 49 |                 "(testing only)"); | 
| Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 50 |  | 
 | 51 |   DEFINE_bool(logtostderr, false, "Log to standard error"); | 
 | 52 |   DEFINE_bool(logtosyslog, false, "Log to syslog"); | 
 | 53 |  | 
 | 54 |   brillo::FlagHelper::Init(argc, argv, "Brillo metrics daemon."); | 
 | 55 |  | 
| Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 56 |   int logging_location = | 
 | 57 |       (FLAGS_foreground ? brillo::kLogToStderr : brillo::kLogToSyslog); | 
| Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 58 |   if (FLAGS_logtosyslog) | 
 | 59 |     logging_location = brillo::kLogToSyslog; | 
 | 60 |  | 
 | 61 |   if (FLAGS_logtostderr) | 
 | 62 |     logging_location = brillo::kLogToStderr; | 
 | 63 |  | 
 | 64 |   // Also log to stderr when not running as daemon. | 
 | 65 |   brillo::InitLog(logging_location | brillo::kLogHeader); | 
 | 66 |  | 
 | 67 |   if (FLAGS_logtostderr && FLAGS_logtosyslog) { | 
 | 68 |     LOG(ERROR) << "only one of --logtosyslog and --logtostderr can be set"; | 
 | 69 |     return 1; | 
 | 70 |   } | 
 | 71 |  | 
 | 72 |   if (!FLAGS_foreground && daemon(0, 0) != 0) { | 
 | 73 |     return errno; | 
 | 74 |   } | 
 | 75 |  | 
| Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 76 |   UploadService upload_service( | 
| Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 77 |       FLAGS_server, base::TimeDelta::FromSeconds(FLAGS_upload_interval_secs), | 
| Bertrand SIMONNET | 0586504 | 2015-12-15 12:33:01 -0800 | [diff] [blame] | 78 |       base::TimeDelta::FromSeconds(FLAGS_disk_persistence_interval_secs), | 
| Bertrand SIMONNET | 9d3a4ae | 2015-11-25 13:49:12 -0800 | [diff] [blame] | 79 |       base::FilePath(FLAGS_private_directory), | 
| Bertrand SIMONNET | b6c77af | 2015-12-08 17:46:00 -0800 | [diff] [blame] | 80 |       base::FilePath(FLAGS_shared_directory)); | 
| Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 81 |  | 
| Bertrand SIMONNET | b6c77af | 2015-12-08 17:46:00 -0800 | [diff] [blame] | 82 |   return upload_service.Run(); | 
| Bertrand SIMONNET | 608e428 | 2015-11-12 17:52:17 -0800 | [diff] [blame] | 83 | } |