]> git.itanic.dy.fi Git - rrdd/blob - main.c
debug: Add support for log files
[rrdd] / main.c
1 #include <sys/types.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <time.h>
5
6 #include "version.h"
7 #include "process.h"
8 #include "rrdtool.h"
9 #include "parser.h"
10 #include "debug.h"
11
12 #include "config.h"
13 #include "built_in_parsers.h"
14
15 #include "plugin_manager.h"
16
17 struct user_options {
18         int max_jobs;
19         char *config_file;
20 };
21
22 int read_args(int argc, char *argv[], struct user_options *opts)
23 {
24         int option_index = 0, c;
25         static struct option long_options[] = {
26                 { .val = 'j', .has_arg = 1, .name = "jobs", },
27                 { .val = 'c', .has_arg = 1, .name = "config", },
28                 { .val = 'l', .has_arg = 1, .name = "log-file", },
29                 { },
30         };
31         char short_options[] = "j:c:l:";
32
33         while (1) {
34                 c = getopt_long(argc, argv, short_options, long_options,
35                                 &option_index);
36
37                 if (c == -1)
38                         break;
39
40                 switch (c) {
41                 case 'j':
42                         opts->max_jobs = atoi(optarg);
43                         break;
44
45                 case 'c':
46                         opts->config_file = optarg;
47                         break;
48
49                 case 'l':
50                         open_log_file(optarg);
51                         break;
52
53                 case '?':
54                         return -1;
55                 }
56         }
57
58         while (optind < argc) {
59                 opts->config_file = argv[optind];
60                 optind++;
61         }
62
63         return 0;
64 }
65
66 int main(int argc, char *argv[])
67 {
68         struct user_options opts;
69         struct rrd_database *db, **db_list = NULL;
70         int sleeptime;
71         int ret = 0;
72
73         pr_info("%s Version %s starting\n", argv[0], RRDD_VERSION);
74
75         bzero(&opts, sizeof(opts));
76
77         if (read_args(argc, argv, &opts) < 0)
78                 return -1;
79
80         init_plugin_manager(argv[0]);
81         register_built_in_parsers();
82
83         if (!opts.config_file) {
84                 pr_err("No database config file given. Nothing to do\n");
85                 return 1;
86         }
87
88         db_list = populate_database(opts.config_file);
89
90         if (ret || !db_list)
91                 return 1;
92
93         if (rrdtool_create_missing_databases(db_list))
94                 return 1;
95
96         if (init_jobcontrol(opts.max_jobs))
97                 return -1;
98
99         while (1) {
100                 const char *db_name;
101                 char timestr[128];
102                 time_t t;
103
104                 pr_info("loop start\n");
105                 /*
106                  * Update all databases parallel in one shot
107                  */
108                 while ((db = get_outdated_db(db_list)))
109                         rrdtool_update_data(db);
110
111                 sleeptime = get_next_update(db_list, &db_name);
112
113                 t = time(0) + sleeptime;
114                 strftime(timestr, sizeof(timestr), "%T", localtime(&t));
115                 pr_info("Next scheduled event \"%s\" at %s, in %d seconds\n",
116                         db_name, timestr, sleeptime);
117
118                 poll_job_requests(sleeptime);
119         }
120         return 0;
121 }