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