]> git.itanic.dy.fi Git - rrdd/blob - main.c
Update .gitignore
[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
50         while (optind < argc) {
51                 opts->config_file = argv[optind];
52                 optind++;
53         }
54
55         return 0;
56 }
57
58 int main(int argc, char *argv[])
59 {
60         struct user_options opts;
61         struct rrd_database *db, **db_list = NULL;
62         int sleeptime;
63         int ret = 0;
64
65         bzero(&opts, sizeof(opts));
66
67         if (read_args(argc, argv, &opts) < 0)
68                 return -1;
69
70         register_built_in_parsers();
71
72         if (!opts.config_file) {
73                 pr_err("No database config file given. Nothing to do\n");
74                 return 1;
75         }
76
77         db_list = populate_database(opts.config_file);
78
79         if (ret || !db_list)
80                 return 1;
81
82         if (rrdtool_create_missing_databases(db_list))
83                 return 1;
84
85         if (init_jobcontrol(opts.max_jobs))
86                 return -1;
87
88         while (1) {
89                 const char *db_name;
90                 char timestr[128];
91                 time_t t;
92
93                 pr_info("loop start\n");
94                 /*
95                  * Update all databases parallel in one shot
96                  */
97                 while ((db = get_outdated_db(db_list)))
98                         rrdtool_update_data(db);
99
100                 sleeptime = get_next_update(db_list, &db_name);
101
102                 t = time(0) + sleeptime;
103                 strftime(timestr, sizeof(timestr), "%T", localtime(&t));
104                 pr_info("Next scheduled event \"%s\" at %s, in %d seconds\n",
105                         db_name, timestr, sleeptime);
106
107                 poll_job_requests(sleeptime);
108         }
109         return 0;
110 }