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