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