]> git.itanic.dy.fi Git - rrdd/blob - main.c
utils.h: min() max() cleanup
[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                 { .val = 'v', .name = "verbose", },
30                 { .val = 'q', .name = "quiet", },
31                 { },
32         };
33         char short_options[] = "j:c:l:vq";
34
35         while (1) {
36                 c = getopt_long(argc, argv, short_options, long_options,
37                                 &option_index);
38
39                 if (c == -1)
40                         break;
41
42                 switch (c) {
43                 case 'j':
44                         opts->max_jobs = atoi(optarg);
45                         break;
46
47                 case 'c':
48                         opts->config_file = optarg;
49                         break;
50
51                 case 'l':
52                         open_log_file(optarg);
53                         break;
54
55                 case 'v':
56                         trace_level++;
57                         break;
58
59                 case 'q':
60                         trace_level--;
61                         break;
62
63                 case '?':
64                         return -1;
65                 }
66         }
67
68         while (optind < argc) {
69                 opts->config_file = argv[optind];
70                 optind++;
71         }
72
73         return 0;
74 }
75
76 int main(int argc, char *argv[])
77 {
78         struct user_options opts;
79         struct rrd_database *db, **db_list = NULL;
80         int sleeptime;
81         int ret = 0;
82
83         pr_info("%s Version %s starting\n", argv[0], RRDD_VERSION);
84
85         bzero(&opts, sizeof(opts));
86
87         if (read_args(argc, argv, &opts) < 0)
88                 return -1;
89
90         init_plugin_manager(argv[0]);
91         register_built_in_parsers();
92
93         if (!opts.config_file) {
94                 pr_err("No database config file given. Nothing to do\n");
95                 return 1;
96         }
97
98         db_list = populate_database(opts.config_file);
99
100         if (ret || !db_list)
101                 return 1;
102
103         if (rrdtool_create_missing_databases(db_list))
104                 return 1;
105
106         if (init_jobcontrol(opts.max_jobs))
107                 return -1;
108
109         while (1) {
110                 const char *db_name;
111                 char timestr[128];
112                 time_t t;
113
114                 /*
115                  * Update all databases parallel in one shot
116                  */
117                 while ((db = get_outdated_db(db_list)))
118                         rrdtool_update_data(db);
119
120                 sleeptime = get_next_update(db_list, &db_name);
121
122                 t = time(0) + sleeptime;
123                 strftime(timestr, sizeof(timestr), "%T", localtime(&t));
124                 pr_info("Next scheduled event \"%s\" at %s, in %d seconds\n",
125                         db_name, timestr, sleeptime);
126
127                 poll_job_requests(sleeptime);
128         }
129         return 0;
130 }