]> git.itanic.dy.fi Git - rrdd/blob - main.c
main: Implement command line parsing
[rrdd] / main.c
1 #include <sys/types.h>
2 #include <unistd.h>
3 #include <getopt.h>
4
5 #include "process.h"
6 #include "rrdtool.h"
7 #include "parser.h"
8 #include "scheduler.h"
9 #include "debug.h"
10
11 #include "database.h"
12
13 struct user_options {
14         int max_jobs;
15 };
16
17 int read_args(int argc, char *argv[], struct user_options *opts)
18 {
19         int option_index = 0, c;
20         static struct option long_options[] = {
21                 { .val = 'j', .name = "jobs", .has_arg = 1, },
22         };
23         char short_options[] = "j:";
24
25         while (1) {
26                 c = getopt_long(argc, argv, short_options, long_options,
27                                 &option_index);
28
29                 if (c == -1)
30                         break;
31
32                 switch (c) {
33                 case 'j':
34                         opts->max_jobs = atoi(optarg);
35                         break;
36
37                 case '?':
38                         return -1;
39                 }
40         }
41         return 0;
42 }
43
44 int main(int argc, char *argv[])
45 {
46         struct user_options opts;
47         struct rrd_database *db;
48         int sleeptime;
49
50         bzero(&opts, sizeof(opts));
51
52         if (read_args(argc, argv, &opts) < 0)
53                 return -1;
54
55         rrdtool_create_missing_databases(all_rrds);
56
57         init_max_jobs(opts.max_jobs);
58
59         while (1) {
60                 pr_info("loop start\n");
61                 /*
62                  * Update all databases parallel in one shot
63                  */
64                 while ((db = get_outdated_db((struct rrd_database **)
65                                                &all_rrds)))
66                         rrdtool_update_data(db);
67
68                 sleeptime = get_next_update((struct rrd_database **)&all_rrds);
69
70                 pr_info("Time to sleep %d seconds\n", sleeptime);
71                 poll_job_requests(sleeptime);
72         }
73         return 0;
74 }