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